HTTP::Date

Discussion in 'Perl Misc' started by Tgone, Apr 20, 2006.

  1. Tgone

    Tgone Guest

    Hello,

    I'm having problems with HTTP::Date displaying the wrong hour and
    seconds.

    -------------------------------------------------------------
    $timestr = "2006-04-29 15:00:00";
    my ($year, $month, $day, $hour, $min, $sec, $tz) =
    parse_date(time2str($timestr));
    print "$month/$day/$year @ $hour:$min\n";
    -------------------------------------------------------------

    This returns: 4/20/2006 @ 02:24

    Any ideas? Thanks.
     
    Tgone, Apr 20, 2006
    #1
    1. Advertising

  2. Tgone wrote:
    > I'm having problems with HTTP::Date displaying the wrong hour and
    > seconds.
    >
    > -------------------------------------------------------------
    > $timestr = "2006-04-29 15:00:00";
    > my ($year, $month, $day, $hour, $min, $sec, $tz) =
    > parse_date(time2str($timestr));


    Why do you think that time2str() is useful there?

    > print "$month/$day/$year @ $hour:$min\n";
    > -------------------------------------------------------------
    >
    > This returns: 4/20/2006 @ 02:24
    >
    > Any ideas?


    Yes, you didn't read the docs for the module you are using carefully
    enough before you posted here. Did you check the docs at all, or did you
    just copy and paste some random code from somewhere?

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Apr 20, 2006
    #2
    1. Advertising

  3. "Tgone" <> writes:

    > I'm having problems with HTTP::Date displaying the wrong hour and
    > seconds.
    >
    > -------------------------------------------------------------
    > $timestr = "2006-04-29 15:00:00";
    > my ($year, $month, $day, $hour, $min, $sec, $tz) =
    > parse_date(time2str($timestr));
    > print "$month/$day/$year @ $hour:$min\n";
    > -------------------------------------------------------------
    >
    > This returns: 4/20/2006 @ 02:24


    On my system I get this:

    1/01/1970 @ 00:33

    The above code passes a string to time2str(), which as the HTTP::Date docs
    state, "converts a machine time (seconds since epoch) to a string".

    If I change your code to this:

    $timestr = "2006-04-29 15:00:00";
    my ($year, $month, $day, $hour, $min, $sec, $tz) =
    parse_date(time2str(str2time($timestr)));
    print "$month/$day/$year @ $hour:$min\n";

    Then I get this:

    4/29/2006 @ 19:00

    The "19:00" is a result of str2time() defaulting to the local time zone,
    whereas parse_date() expects GMT. If I pass an explicit time zone 'EDT'
    to str2time(), I get the following:

    4/20/2006 @ 03:30

    Aha! The docs for str2time() state that, if a time zone other than "GMT"
    or a numeric value is given, the module Time::Zone must be installed for
    the string to be parsed correctly.

    Now, if I specify GMT:

    str2time($timestr, 'GMT')

    I get this:

    4/29/2006 @ 15:00

    Note the hoops I had to jump through, just to duplicate your bug - what
    you posted was obviously not your real code. So the above answer is just
    a guess. If you want real answers, post real code.

    sherm--

    --
    Cocoa programming in Perl: http://camelbones.sourceforge.net
    Hire me! My resume: http://www.dot-app.org
     
    Sherm Pendley, Apr 20, 2006
    #3
  4. Sherm Pendley <> writes:

    > Aha! The docs for str2time() state that, if a time zone other than "GMT"
    > or a numeric value is given, the module Time::Zone must be installed for
    > the string to be parsed correctly.


    Followup question: Shouldn't str2time() whine about an unknown time zone,
    instead of silently returning bogus results?

    sherm--

    --
    Cocoa programming in Perl: http://camelbones.sourceforge.net
    Hire me! My resume: http://www.dot-app.org
     
    Sherm Pendley, Apr 20, 2006
    #4
  5. Sherm Pendley wrote:
    > "Tgone" <> writes:
    >>I'm having problems with HTTP::Date displaying the wrong hour and
    >>seconds.
    >>
    >>-------------------------------------------------------------
    >>$timestr = "2006-04-29 15:00:00";
    >>my ($year, $month, $day, $hour, $min, $sec, $tz) =
    >>parse_date(time2str($timestr));
    >>print "$month/$day/$year @ $hour:$min\n";
    >>-------------------------------------------------------------

    >
    > If I change your code to this:
    >
    > $timestr = "2006-04-29 15:00:00";
    > my ($year, $month, $day, $hour, $min, $sec, $tz) =
    > parse_date(time2str(str2time($timestr)));


    Why not just parse_date($timestr) ?

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Apr 20, 2006
    #5
  6. Gunnar Hjalmarsson <> writes:

    > Sherm Pendley wrote:
    >> "Tgone" <> writes:
    >>>I'm having problems with HTTP::Date displaying the wrong hour and
    >>>seconds.
    >>>
    >>>-------------------------------------------------------------
    >>>$timestr = "2006-04-29 15:00:00";
    >>>my ($year, $month, $day, $hour, $min, $sec, $tz) =
    >>>parse_date(time2str($timestr));
    >>>print "$month/$day/$year @ $hour:$min\n";
    >>>-------------------------------------------------------------

    >> If I change your code to this:
    >> $timestr = "2006-04-29 15:00:00";
    >> my ($year, $month, $day, $hour, $min, $sec, $tz) =
    >> parse_date(time2str(str2time($timestr)));

    >
    > Why not just parse_date($timestr) ?


    Because that produces the correct output, and my goal was to figure out
    how the buggy output was being produced - the code he posted produces
    completely different results.

    sherm--

    --
    Cocoa programming in Perl: http://camelbones.sourceforge.net
    Hire me! My resume: http://www.dot-app.org
     
    Sherm Pendley, Apr 20, 2006
    #6
  7. Tgone

    Tgone Guest

    it should be parse_date($timestr) not parse_date(time2str($timestr))

    thanks for the help.
     
    Tgone, Apr 20, 2006
    #7
  8. "Tgone" <> writes:

    > it should be parse_date($timestr) not parse_date(time2str($timestr))


    No kidding, sherlock. I know what the *correct* code should be - my post
    was trying to figure out what the *real* code was, since the code you
    posted here did not produce the output you claimed.

    sherm--

    --
    Cocoa programming in Perl: http://camelbones.sourceforge.net
    Hire me! My resume: http://www.dot-app.org
     
    Sherm Pendley, Apr 20, 2006
    #8
    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. Matt
    Replies:
    1
    Views:
    606
    Matthew Speed
    Nov 8, 2003
  2. Peter Grison

    Date, date date date....

    Peter Grison, May 28, 2004, in forum: Java
    Replies:
    10
    Views:
    3,276
    Michael Borgwardt
    May 30, 2004
  3. Matt
    Replies:
    2
    Views:
    519
    Pete Becker
    Nov 8, 2003
  4. Matt
    Replies:
    3
    Views:
    724
    Richard Heathfield
    Nov 8, 2003
  5. Matt
    Replies:
    11
    Views:
    407
    Aaron Bertrand [MVP]
    Nov 8, 2003
Loading...

Share This Page