Perl using Date::Parse module unable to print date in different format












1















I want to accept a user date on the command line in format



dd/mm/yyyy



then print the date out to the user in



yyyy/mm/dd



I am trying to use the Date::Parse module to parse into a date to be reprinted.



The Date:Parse docs show that I should be able to get $day, $month and $year from user input.



use Date::Parse;

$time = str2time($date);

($ss,$mm,$hh,$day,$month,$year,$zone) = strptime($date);


This is my current code:



use strict;
use Date::Parse;

print "Enter a date in dd/mm/yyy format: ";
my $user_date = <STDIN>;
my @date = strptime($user_date);

# ( $day, $month, $year ) = strptime($user_date);
# my $user_day = ( ($day) = strptime($user_date) );
print "%Y/%m/%d", @date;


However the print fails and it appears from output that entered 10 of 10 is 9 in output.



Output



Enter a date in dd/mm/yyy format: 16/10/1952
%Y/%m/%d1952916s


What should I do?










share|improve this question


















  • 1





    print "%Y/%m/%d", @date; - print does not interpolate sprintf-style replacements. You want print sprintf ..., or just printf

    – Corion
    Jan 2 at 11:31






  • 1





    @Corion: And those aren't sprintf-style placeholders anyway :-)

    – Dave Cross
    Jan 3 at 8:27
















1















I want to accept a user date on the command line in format



dd/mm/yyyy



then print the date out to the user in



yyyy/mm/dd



I am trying to use the Date::Parse module to parse into a date to be reprinted.



The Date:Parse docs show that I should be able to get $day, $month and $year from user input.



use Date::Parse;

$time = str2time($date);

($ss,$mm,$hh,$day,$month,$year,$zone) = strptime($date);


This is my current code:



use strict;
use Date::Parse;

print "Enter a date in dd/mm/yyy format: ";
my $user_date = <STDIN>;
my @date = strptime($user_date);

# ( $day, $month, $year ) = strptime($user_date);
# my $user_day = ( ($day) = strptime($user_date) );
print "%Y/%m/%d", @date;


However the print fails and it appears from output that entered 10 of 10 is 9 in output.



Output



Enter a date in dd/mm/yyy format: 16/10/1952
%Y/%m/%d1952916s


What should I do?










share|improve this question


















  • 1





    print "%Y/%m/%d", @date; - print does not interpolate sprintf-style replacements. You want print sprintf ..., or just printf

    – Corion
    Jan 2 at 11:31






  • 1





    @Corion: And those aren't sprintf-style placeholders anyway :-)

    – Dave Cross
    Jan 3 at 8:27














1












1








1








I want to accept a user date on the command line in format



dd/mm/yyyy



then print the date out to the user in



yyyy/mm/dd



I am trying to use the Date::Parse module to parse into a date to be reprinted.



The Date:Parse docs show that I should be able to get $day, $month and $year from user input.



use Date::Parse;

$time = str2time($date);

($ss,$mm,$hh,$day,$month,$year,$zone) = strptime($date);


This is my current code:



use strict;
use Date::Parse;

print "Enter a date in dd/mm/yyy format: ";
my $user_date = <STDIN>;
my @date = strptime($user_date);

# ( $day, $month, $year ) = strptime($user_date);
# my $user_day = ( ($day) = strptime($user_date) );
print "%Y/%m/%d", @date;


However the print fails and it appears from output that entered 10 of 10 is 9 in output.



Output



Enter a date in dd/mm/yyy format: 16/10/1952
%Y/%m/%d1952916s


What should I do?










share|improve this question














I want to accept a user date on the command line in format



dd/mm/yyyy



then print the date out to the user in



yyyy/mm/dd



I am trying to use the Date::Parse module to parse into a date to be reprinted.



The Date:Parse docs show that I should be able to get $day, $month and $year from user input.



use Date::Parse;

$time = str2time($date);

($ss,$mm,$hh,$day,$month,$year,$zone) = strptime($date);


This is my current code:



use strict;
use Date::Parse;

print "Enter a date in dd/mm/yyy format: ";
my $user_date = <STDIN>;
my @date = strptime($user_date);

# ( $day, $month, $year ) = strptime($user_date);
# my $user_day = ( ($day) = strptime($user_date) );
print "%Y/%m/%d", @date;


However the print fails and it appears from output that entered 10 of 10 is 9 in output.



Output



Enter a date in dd/mm/yyy format: 16/10/1952
%Y/%m/%d1952916s


What should I do?







perl






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jan 2 at 11:22









saythsayth

2,78683675




2,78683675








  • 1





    print "%Y/%m/%d", @date; - print does not interpolate sprintf-style replacements. You want print sprintf ..., or just printf

    – Corion
    Jan 2 at 11:31






  • 1





    @Corion: And those aren't sprintf-style placeholders anyway :-)

    – Dave Cross
    Jan 3 at 8:27














  • 1





    print "%Y/%m/%d", @date; - print does not interpolate sprintf-style replacements. You want print sprintf ..., or just printf

    – Corion
    Jan 2 at 11:31






  • 1





    @Corion: And those aren't sprintf-style placeholders anyway :-)

    – Dave Cross
    Jan 3 at 8:27








1




1





print "%Y/%m/%d", @date; - print does not interpolate sprintf-style replacements. You want print sprintf ..., or just printf

– Corion
Jan 2 at 11:31





print "%Y/%m/%d", @date; - print does not interpolate sprintf-style replacements. You want print sprintf ..., or just printf

– Corion
Jan 2 at 11:31




1




1





@Corion: And those aren't sprintf-style placeholders anyway :-)

– Dave Cross
Jan 3 at 8:27





@Corion: And those aren't sprintf-style placeholders anyway :-)

– Dave Cross
Jan 3 at 8:27












1 Answer
1






active

oldest

votes


















6














The documentation for Date::Parse isn't clear, but it looks like you get the values back in the format that localtime() would expect. The year, for example, seems to be the year minus 1900. This means that the month number will be 0 to 11 rather than 1 to 12.



Date::Parse hasn't been updated for over five years. I'd suggest that it should best be avoided these days. There are much better options to choose from. These include Time::Piece that has been included as a standard part of the Perl distribution since version 5.10.0. You can use its strptime() (string parse time) method to parse your string and its strftime() (string format time) method to format the date object as you like.



#!/usr/bin/perl

use strict;
use warnings;
use feature 'say';

use Time::Piece;

print "Enter a date in dd/mm/yyy format: ";
chomp(my $user_date = <STDIN>);

my $tp = Time::Piece->strptime($user_date, '%d/%m/%Y');

say $tp->strftime('%Y/%m/%d');


Update:



Also, it's really not clear what this line is supposed to do:



print "%Y/%m/%d", @date;


I think you were thinking of using the strftime() method from POSIX.pm.



print strftime "%Y/%m/%d", @date;


But with use warnings this generates warnings because of all the undefined values in @data (that's a rather bizarre design decision in that module and, in my opinion, another reason to avoid it). You can fix that by replacing:



my @date      = strptime($user_date);


With:



my @date      = map { $_ // 0 } strptime($user_date);





share|improve this answer

























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54005429%2fperl-using-dateparse-module-unable-to-print-stdin-date-in-different-format%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    6














    The documentation for Date::Parse isn't clear, but it looks like you get the values back in the format that localtime() would expect. The year, for example, seems to be the year minus 1900. This means that the month number will be 0 to 11 rather than 1 to 12.



    Date::Parse hasn't been updated for over five years. I'd suggest that it should best be avoided these days. There are much better options to choose from. These include Time::Piece that has been included as a standard part of the Perl distribution since version 5.10.0. You can use its strptime() (string parse time) method to parse your string and its strftime() (string format time) method to format the date object as you like.



    #!/usr/bin/perl

    use strict;
    use warnings;
    use feature 'say';

    use Time::Piece;

    print "Enter a date in dd/mm/yyy format: ";
    chomp(my $user_date = <STDIN>);

    my $tp = Time::Piece->strptime($user_date, '%d/%m/%Y');

    say $tp->strftime('%Y/%m/%d');


    Update:



    Also, it's really not clear what this line is supposed to do:



    print "%Y/%m/%d", @date;


    I think you were thinking of using the strftime() method from POSIX.pm.



    print strftime "%Y/%m/%d", @date;


    But with use warnings this generates warnings because of all the undefined values in @data (that's a rather bizarre design decision in that module and, in my opinion, another reason to avoid it). You can fix that by replacing:



    my @date      = strptime($user_date);


    With:



    my @date      = map { $_ // 0 } strptime($user_date);





    share|improve this answer






























      6














      The documentation for Date::Parse isn't clear, but it looks like you get the values back in the format that localtime() would expect. The year, for example, seems to be the year minus 1900. This means that the month number will be 0 to 11 rather than 1 to 12.



      Date::Parse hasn't been updated for over five years. I'd suggest that it should best be avoided these days. There are much better options to choose from. These include Time::Piece that has been included as a standard part of the Perl distribution since version 5.10.0. You can use its strptime() (string parse time) method to parse your string and its strftime() (string format time) method to format the date object as you like.



      #!/usr/bin/perl

      use strict;
      use warnings;
      use feature 'say';

      use Time::Piece;

      print "Enter a date in dd/mm/yyy format: ";
      chomp(my $user_date = <STDIN>);

      my $tp = Time::Piece->strptime($user_date, '%d/%m/%Y');

      say $tp->strftime('%Y/%m/%d');


      Update:



      Also, it's really not clear what this line is supposed to do:



      print "%Y/%m/%d", @date;


      I think you were thinking of using the strftime() method from POSIX.pm.



      print strftime "%Y/%m/%d", @date;


      But with use warnings this generates warnings because of all the undefined values in @data (that's a rather bizarre design decision in that module and, in my opinion, another reason to avoid it). You can fix that by replacing:



      my @date      = strptime($user_date);


      With:



      my @date      = map { $_ // 0 } strptime($user_date);





      share|improve this answer




























        6












        6








        6







        The documentation for Date::Parse isn't clear, but it looks like you get the values back in the format that localtime() would expect. The year, for example, seems to be the year minus 1900. This means that the month number will be 0 to 11 rather than 1 to 12.



        Date::Parse hasn't been updated for over five years. I'd suggest that it should best be avoided these days. There are much better options to choose from. These include Time::Piece that has been included as a standard part of the Perl distribution since version 5.10.0. You can use its strptime() (string parse time) method to parse your string and its strftime() (string format time) method to format the date object as you like.



        #!/usr/bin/perl

        use strict;
        use warnings;
        use feature 'say';

        use Time::Piece;

        print "Enter a date in dd/mm/yyy format: ";
        chomp(my $user_date = <STDIN>);

        my $tp = Time::Piece->strptime($user_date, '%d/%m/%Y');

        say $tp->strftime('%Y/%m/%d');


        Update:



        Also, it's really not clear what this line is supposed to do:



        print "%Y/%m/%d", @date;


        I think you were thinking of using the strftime() method from POSIX.pm.



        print strftime "%Y/%m/%d", @date;


        But with use warnings this generates warnings because of all the undefined values in @data (that's a rather bizarre design decision in that module and, in my opinion, another reason to avoid it). You can fix that by replacing:



        my @date      = strptime($user_date);


        With:



        my @date      = map { $_ // 0 } strptime($user_date);





        share|improve this answer















        The documentation for Date::Parse isn't clear, but it looks like you get the values back in the format that localtime() would expect. The year, for example, seems to be the year minus 1900. This means that the month number will be 0 to 11 rather than 1 to 12.



        Date::Parse hasn't been updated for over five years. I'd suggest that it should best be avoided these days. There are much better options to choose from. These include Time::Piece that has been included as a standard part of the Perl distribution since version 5.10.0. You can use its strptime() (string parse time) method to parse your string and its strftime() (string format time) method to format the date object as you like.



        #!/usr/bin/perl

        use strict;
        use warnings;
        use feature 'say';

        use Time::Piece;

        print "Enter a date in dd/mm/yyy format: ";
        chomp(my $user_date = <STDIN>);

        my $tp = Time::Piece->strptime($user_date, '%d/%m/%Y');

        say $tp->strftime('%Y/%m/%d');


        Update:



        Also, it's really not clear what this line is supposed to do:



        print "%Y/%m/%d", @date;


        I think you were thinking of using the strftime() method from POSIX.pm.



        print strftime "%Y/%m/%d", @date;


        But with use warnings this generates warnings because of all the undefined values in @data (that's a rather bizarre design decision in that module and, in my opinion, another reason to avoid it). You can fix that by replacing:



        my @date      = strptime($user_date);


        With:



        my @date      = map { $_ // 0 } strptime($user_date);






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Jan 2 at 11:58

























        answered Jan 2 at 11:40









        Dave CrossDave Cross

        48.2k34079




        48.2k34079
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54005429%2fperl-using-dateparse-module-unable-to-print-stdin-date-in-different-format%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Mossoró

            Error while reading .h5 file using the rhdf5 package in R

            Pushsharp Apns notification error: 'InvalidToken'