Issue with Time::Local

Discussion in 'Perl Misc' started by SSS Develop, Oct 21, 2011.

  1. SSS Develop

    SSS Develop Guest

    Hello,

    When i run the following script, I get error:


    Day '31' out of range 1..28 at exp.pl line 18



    -----------
    use strict;
    use warnings;
    use Time::Local;

    my $date_string = '2011-01-31 17:30:55';
    my ($date, $time) = split /\s+/, $string;

    my ($y, $m, $d) = split /-/, $date;
    my ($hr, $min, $sec) = split /:/, $time;

    print timelocal($sec,$min,$hr,$d,$m,$y);


    --------------
    what could be issue?
     
    SSS Develop, Oct 21, 2011
    #1
    1. Advertising

  2. SSS Develop

    hymie! Guest

    In our last episode, the evil Dr. Lacto had captured our hero,
    SSS Develop <>, who said:
    >Hello,
    >
    >When i run the following script, I get error:
    >
    >
    >Day '31' out of range 1..28 at exp.pl line 18
    >
    >
    >
    >-----------
    >use strict;
    >use warnings;
    >use Time::Local;
    >
    >my $date_string = '2011-01-31 17:30:55';
    >my ($date, $time) = split /\s+/, $string;


    Global symbol "$string" requires explicit package name at - line 6.
    Execution of - aborted due to compilation errors.

    --hymie! http://lactose.homelinux.net/~hymie
    -------------------------------------------------------------------------------
     
    hymie!, Oct 21, 2011
    #2
    1. Advertising

  3. SSS Develop

    SSS Develop Guest

    On Oct 21, 5:51 pm, (hymie!) wrote:
    > In our last episode, the evil Dr. Lacto had captured our hero,
    >   SSS Develop <>, who said:
    >
    >
    >
    >
    >
    >
    >
    >
    >
    > >Hello,

    >
    > >When i run the following script, I get error:

    >
    > >Day '31' out of range 1..28 at exp.pl line 18

    >
    > >-----------
    > >use strict;
    > >use warnings;
    > >use Time::Local;

    >
    > >my $date_string  = '2011-01-31 17:30:55';
    > >my ($date, $time) = split /\s+/, $string;

    >
    > Global symbol "$string" requires explicit package name at - line 6.
    > Execution of - aborted due to compilation errors.
    >
    > --hymie!    http://lactose.homelinux.net/~hymie   
    > --------------------------------------------------------------------------- ----



    opps... the correct version is as follow:

    ------------


    use strict;
    use warnings;
    use Time::Local;
    my $date_string = '2011-01-31 17:30:55';
    my ($date, $time) = split /\s+/, $date_string;
    my ($y, $m, $d) = split /-/, $date;
    my ($hr, $min, $sec) = split /:/, $time;
    print timelocal($sec,$min,$hr,$d,$m,$y);

    ---------
     
    SSS Develop, Oct 21, 2011
    #3
  4. SSS Develop

    hymie! Guest

    In our last episode, the evil Dr. Lacto had captured our hero,
    SSS Develop <>, who said:
    >Hello,
    >
    >When i run the following script, I get error:
    >
    >
    >Day '31' out of range 1..28 at exp.pl line 18


    >my $date_string = '2011-01-31 17:30:55';
    >my ($date, $time) = split /\s+/, $string;
    >
    >my ($y, $m, $d) = split /-/, $date;
    >my ($hr, $min, $sec) = split /:/, $time;
    >
    >print timelocal($sec,$min,$hr,$d,$m,$y);
    >
    >what could be issue?


    You mean, aside from the obvious problem that February 2011 only has
    28 days in it?

    --hymie! http://lactose.homelinux.net/~hymie
    -------------------------------------------------------------------------------
     
    hymie!, Oct 21, 2011
    #4
  5. SSS Develop

    hymie! Guest

    In our last episode, the evil Dr. Lacto had captured our hero,
    (hymie!), who said:
    >In our last episode, the evil Dr. Lacto had captured our hero,
    > SSS Develop <>, who said:
    >>Hello,
    >>
    >>When i run the following script, I get error:
    >>
    >>
    >>Day '31' out of range 1..28 at exp.pl line 18

    >
    >>my $date_string = '2011-01-31 17:30:55';
    >>my ($date, $time) = split /\s+/, $string;
    >>
    >>my ($y, $m, $d) = split /-/, $date;
    >>my ($hr, $min, $sec) = split /:/, $time;
    >>
    >>print timelocal($sec,$min,$hr,$d,$m,$y);
    >>
    >>what could be issue?

    >
    >You mean, aside from the obvious problem that February 2011 only has
    >28 days in it?


    I must have undo'd the last line of my post, which said something like:

    Or that you didn't read the documentation, which states
    >It is worth drawing particular attention to the expected
    >ranges for the values provided.


    --hymie! http://lactose.homelinux.net/~hymie
    -------------------------------------------------------------------------------
     
    hymie!, Oct 21, 2011
    #5
  6. SSS Develop <> wrote:
    > When i run the following script, I get error:


    > Day '31' out of range 1..28 at exp.pl line 18


    > -----------
    > use strict;
    > use warnings;
    > use Time::Local;


    > my $date_string = '2011-01-31 17:30:55';
    > my ($date, $time) = split /\s+/, $string;

    -----------------------------------^^^^^^^

    Better make that '$date_string'.

    > my ($y, $m, $d) = split /-/, $date;
    > my ($hr, $min, $sec) = split /:/, $time;


    > print timelocal($sec,$min,$hr,$d,$m,$y);

    -----------------------------------^^

    The month value is interpreted as the distance to January,
    so you need 0 for January, 1 for February etc. Thus you're
    asking here for February 31 - which isn't a valid date.

    Regards, Jens
    --
    \ Jens Thoms Toerring ___
    \__________________________ http://toerring.de
     
    Jens Thoms Toerring, Oct 21, 2011
    #6
  7. In article <>,
    SSS Develop <> wrote:
    > Hello,
    >
    > When i run the following script, I get error:
    >
    >
    > Day '31' out of range 1..28 at exp.pl line 18
    >
    > -----------
    > use strict;
    > use warnings;
    > use Time::Local;
    >
    > my $date_string = '2011-01-31 17:30:55';
    > my ($date, $time) = split /\s+/, $string;
    >
    > my ($y, $m, $d) = split /-/, $date;
    > my ($hr, $min, $sec) = split /:/, $time;
    >
    > print timelocal($sec,$min,$hr,$d,$m,$y);


    This line should be:

    print timelocal($sec,$min,$hr,$d,$m-1,$y-1900);

    Although I expect it will also accept just $y as the last arg.
    It's the $m-1 that is critical.

    Cheers
    Tony
    --
    Tony Mountifield
    Work: - http://www.softins.co.uk
    Play: - http://tony.mountifield.org
     
    Tony Mountifield, Oct 21, 2011
    #7
  8. Ben Morrow <> writes:
    > Quoth (Tony Mountifield):
    >> In article <>,
    >> SSS Develop <> wrote:
    >> >
    >> > When i run the following script, I get error:
    >> >
    >> > Day '31' out of range 1..28 at exp.pl line 18
    >> >
    >> > -----------
    >> > use strict;
    >> > use warnings;
    >> > use Time::Local;
    >> >
    >> > my $date_string = '2011-01-31 17:30:55';
    >> > my ($date, $time) = split /\s+/, $string;
    >> >
    >> > my ($y, $m, $d) = split /-/, $date;
    >> > my ($hr, $min, $sec) = split /:/, $time;

    >
    > It's usually a mistake to try to parse date formats yourself. Use
    > DateTime instead.



    Why would this 'usually be a mistake'?
     
    Rainer Weikusat, Oct 21, 2011
    #8
  9. Rainer Weikusat <> wrote:
    >Why would this 'usually be a mistake'?


    Because reusing existing, well-tested code instead of re-inventing the
    wheel is a sign of good software development.

    jue
     
    Jürgen Exner, Oct 22, 2011
    #9
  10. Jürgen Exner <> writes:
    > Rainer Weikusat <> wrote:
    >>Why would this 'usually be a mistake'?

    >
    > Because reusing existing, well-tested code instead of re-inventing
    > the wheel is a sign of good software development.


    And not using code by someone who believes to have invented something
    as important and lasting as 'the wheel' just because he has managed to
    split a string using a library routine would be a sign of common
    sense: People who overestimate the importance of their work that
    grossly quite obviously lack the compentence necessary for a sensible
    assessment.

    Totschlagargumente benutzt man, weil man sonst keine hat.
     
    Rainer Weikusat, Oct 22, 2011
    #10
  11. Ben Morrow <> writes:
    > Quoth Rainer Weikusat <>:
    >> Ben Morrow <> writes:
    >> >
    >> > It's usually a mistake to try to parse date formats yourself. Use
    >> > DateTime instead.

    >>
    >> Why would this 'usually be a mistake'?

    >
    > Because, as this thread has demonstrated, it's extremely easy to get it
    > wrong.


    Well then, what's wrong with the code in question:

    my $date_string = '2011-01-31 17:30:55';
    my ($date, $time) = split /\s+/, $date_string;

    my ($y, $m, $d) = split /-/, $date;
    my ($hr, $min, $sec) = split /:/, $time;

    This splits a complex string into components. I can't see any error in
    here.
     
    Rainer Weikusat, Oct 22, 2011
    #11
  12. Rainer Weikusat <> wrote:
    >Ben Morrow <> writes:
    >> Quoth Rainer Weikusat <>:
    >>> Ben Morrow <> writes:
    >>> >
    >>> > It's usually a mistake to try to parse date formats yourself. Use
    >>> > DateTime instead.
    >>>
    >>> Why would this 'usually be a mistake'?

    >>
    >> Because, as this thread has demonstrated, it's extremely easy to get it
    >> wrong.

    >
    >Well then, what's wrong with the code in question:
    >
    >my $date_string = '2011-01-31 17:30:55';
    >my ($date, $time) = split /\s+/, $date_string;
    >
    >my ($y, $m, $d) = split /-/, $date;
    >my ($hr, $min, $sec) = split /:/, $time;
    >
    >This splits a complex string into components. I can't see any error in
    >here.


    Nor can anyone else. You are missing the point by several miles.

    jue
     
    Jürgen Exner, Oct 22, 2011
    #12
  13. Rainer Weikusat <> wrote:
    >Jürgen Exner <> writes:
    >> Rainer Weikusat <> wrote:
    >>>Why would this 'usually be a mistake'?

    >>
    >> Because reusing existing, well-tested code instead of re-inventing
    >> the wheel is a sign of good software development.

    >
    >And not using code by someone who believes to have invented something
    >as important and lasting as 'the wheel' just because he has managed to
    >split a string using a library routine would be a sign of common
    >sense: People who overestimate the importance of their work that
    >grossly quite obviously lack the compentence necessary for a sensible
    >assessment.


    Obviously the task wasn't as simple as you are painting it, otherwise
    the OP wouldn't have made that error.
    On the other hand you are right: if the OP had used the module, then he
    would this honorable NG would have missed this great entertainment..

    >Totschlagargumente benutzt man, weil man sonst keine hat.


    "Kein Programm ist nutzlos. Es kann immer noch als schlechtes Beispiel
    dienen."
    No program is useless. It can always serve as an example for poor
    programming.

    jue
     
    Jürgen Exner, Oct 22, 2011
    #13
  14. Jürgen Exner <> writes:
    > Rainer Weikusat <> wrote:
    >>Ben Morrow <> writes:
    >>> Quoth Rainer Weikusat <>:
    >>>> Ben Morrow <> writes:
    >>>> >
    >>>> > It's usually a mistake to try to parse date formats yourself. Use
    >>>> > DateTime instead.
    >>>>
    >>>> Why would this 'usually be a mistake'?
    >>>
    >>> Because, as this thread has demonstrated, it's extremely easy to get it
    >>> wrong.

    >>
    >>Well then, what's wrong with the code in question:
    >>
    >>my $date_string = '2011-01-31 17:30:55';
    >>my ($date, $time) = split /\s+/, $date_string;
    >>
    >>my ($y, $m, $d) = split /-/, $date;
    >>my ($hr, $min, $sec) = split /:/, $time;
    >>
    >>This splits a complex string into components. I can't see any error in
    >>here.

    >
    > Nor can anyone else. You are missing the point by several miles.


    The code I requoted above led to a comment 'trying to parse date
    formats yourself is usually a mistake'. But no mistake was made when
    parsing this date. Instead, the OP made a mistake when calling a
    subroutine provided by some module. If error-prone activities are
    better avoided (highly debatable since humans learn by making mistakes
    and having to correct them), the obvious conclusion would be that
    'trying to use existing Perl modules' must be the thing which is
    'usually a mistake'. All of this together makes preciously little
    sense. Consequently, something must be missing here, namely,
    an argument supporting the 'is usually a mistake' assertion.
     
    Rainer Weikusat, Oct 22, 2011
    #14
  15. Jürgen Exner <> writes:
    > Rainer Weikusat <> wrote:
    >>Jürgen Exner <> writes:
    >>> Rainer Weikusat <> wrote:
    >>>>Why would this 'usually be a mistake'?
    >>>
    >>> Because reusing existing, well-tested code instead of re-inventing
    >>> the wheel is a sign of good software development.

    >>
    >>And not using code by someone who believes to have invented something
    >>as important and lasting as 'the wheel' just because he has managed to
    >>split a string using a library routine would be a sign of common
    >>sense: People who overestimate the importance of their work that
    >>grossly quite obviously lack the compentence necessary for a sensible
    >>assessment.

    >
    > Obviously the task wasn't as simple as you are painting it, otherwise
    > the OP wouldn't have made that error.


    The OP tried to use some non-core Perl module and apparently, without
    reading the documentation first. That's certainly an error (or at
    least bound to cause 'surprises') but not a coding error.
     
    Rainer Weikusat, Oct 22, 2011
    #15
  16. SSS Develop wrote:
    >
    > When i run the following script, I get error:
    >
    > Day '31' out of range 1..28 at exp.pl line 18


    perldoc -f localtime

    localtime EXPR
    localtime
    Converts a time as returned by the time function to a
    9-element list with the time analyzed for the local time
    zone. Typically used as follows:

    # 0 1 2 3 4 5 6 7 8
    ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =
    localtime(time);

    All list elements are numeric, and come straight out of the
    C `struct tm'. $sec, $min, and $hour are the seconds,
    minutes, and hours of the specified time.

    $mday is the day of the month, and $mon is the month
    itself, in the range 0..11 with 0 indicating January and 11
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    indicating December. This makes it easy to get a month
    ^^^^^^^^^^^^^^^^^^^
    name from a list:

    my @abbr = qw( Jan Feb Mar Apr May Jun Jul Aug Sep Oct
    Nov Dec );
    print "$abbr[$mon] $mday";
    # $mon=9, $mday=18 gives "Oct 18"

    $year is the number of years since 1900, not just the last
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    two digits of the year. That is, $year is 123 in year
    2023. The proper way to get a complete 4-digit year is
    simply:

    $year += 1900;

    Otherwise you create non-Y2K-compliant programs--and you
    wouldn't want to do that, would you?


    > use strict;
    > use warnings;
    > use Time::Local;
    >
    > my $date_string = '2011-01-31 17:30:55';
    > my ($date, $time) = split /\s+/, $string;
    >
    > my ($y, $m, $d) = split /-/, $date;
    > my ($hr, $min, $sec) = split /:/, $time;
    >
    > print timelocal($sec,$min,$hr,$d,$m,$y);


    my ( $y, $m, $d, $hr, $min, $sec ) = $date_string =~ /\d+/g

    print timelocal( $sec, $min, $hr, $d, $m - 1, $y - 1900 );



    John
    --
    Any intelligent fool can make things bigger and
    more complex... It takes a touch of genius -
    and a lot of courage to move in the opposite
    direction. -- Albert Einstein
     
    John W. Krahn, Oct 23, 2011
    #16
  17. SSS Develop

    SSS Develop Guest

    On Oct 23, 10:54 am, "John W. Krahn" <> wrote:
    > SSS Develop wrote:
    >
    > > When i run the following script, I get error:

    >
    > > Day '31' out of range 1..28 at exp.pl line 18

    >
    > perldoc -f localtime
    >
    >      localtime EXPR
    >      localtime
    >              Converts a time as returned by the time function to a
    >              9-element list with the time analyzed for the local time
    >              zone.  Typically used as follows:
    >
    >                  #  0    1    2     3    4    5     6     7     8
    >                  ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =
    >                                                          localtime(time);
    >
    >              All list elements are numeric, and come straight out of the
    >              C `struct tm'.  $sec, $min, and $hour are the seconds,
    >              minutes, and hours of the specified time.
    >
    >              $mday is the day of the month, and $mon is themonth
    >              itself, in the range 0..11 with 0 indicating January and 11
    >                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    >              indicating December.  This makes it easy to get a month
    >              ^^^^^^^^^^^^^^^^^^^
    >              name from a list:
    >
    >                  my @abbr = qw( Jan Feb Mar Apr May Jun Jul Aug Sep Oct
    > Nov Dec );
    >                  print "$abbr[$mon] $mday";
    >                  # $mon=9, $mday=18 gives "Oct 18"
    >
    >              $year is the number of years since 1900, not just the last
    >              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    >              two digits of the year.  That is, $year is 123 in year
    >              2023.  The proper way to get a complete 4-digit year is
    >              simply:
    >
    >                  $year += 1900;
    >
    >              Otherwise you create non-Y2K-compliant programs--and you
    >              wouldn't want to do that, would you?
    >
    > > use strict;
    > > use warnings;
    > > use Time::Local;

    >
    > > my $date_string  = '2011-01-31 17:30:55';
    > > my ($date, $time) = split /\s+/, $string;

    >
    > > my ($y, $m, $d) = split /-/, $date;
    > > my ($hr, $min, $sec) = split /:/, $time;

    >
    > > print timelocal($sec,$min,$hr,$d,$m,$y);

    >
    > my ( $y, $m, $d, $hr, $min, $sec ) = $date_string =~ /\d+/g
    >
    > print timelocal( $sec, $min, $hr, $d, $m - 1, $y - 1900 );
    >
    > John
    > --
    > Any intelligent fool can make things bigger and
    > more complex... It takes a touch of genius -
    > and a lot of courage to move in the opposite
    > direction.                   -- Albert Einstein


    Thank you,

    this works ! was stupid mistake of not going through the details of
    documentation :(


    ---sssdevelop
     
    SSS Develop, Oct 24, 2011
    #17
  18. SSS Develop

    Justin C Guest

    On 2011-10-22, Rainer Weikusat <> wrote:
    >
    > The code I requoted above led to a comment 'trying to parse date
    > formats yourself is usually a mistake'. But no mistake was made when
    > parsing this date. Instead...


    <Yawn>

    From:
    Score: -1000


    Justin.

    --
    Justin C, by the sea.
     
    Justin C, Oct 24, 2011
    #18
  19. Justin C <> writes:
    > On 2011-10-22, Rainer Weikusat <> wrote:
    >>
    >> The code I requoted above led to a comment 'trying to parse date
    >> formats yourself is usually a mistake'. But no mistake was made when
    >> parsing this date. Instead...

    >
    > <Yawn>
    >
    > From:
    > Score: -1000


    I usually score people down by -9999 because I really don't want to
    read nonsense texts from people who are much more interested in
    farting loudly in public than discuss language or programming issues.

    HTH.
     
    Rainer Weikusat, Oct 24, 2011
    #19
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Salim Afsar

    convert local time to another country time

    Salim Afsar, Mar 16, 2006, in forum: ASP .Net
    Replies:
    2
    Views:
    738
    Juan T. Llibre
    Mar 16, 2006
  2. flamesrock
    Replies:
    8
    Views:
    478
    Hendrik van Rooyen
    Nov 24, 2006
  3. Replies:
    1
    Views:
    805
    Paul Boddie
    Mar 11, 2007
  4. Jonathan Wood
    Replies:
    1
    Views:
    2,205
    Jonathan Wood
    Nov 1, 2009
  5. David Joseph Bonnici
    Replies:
    1
    Views:
    213
    Gunnar Hjalmarsson
    May 15, 2005
Loading...

Share This Page