Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi there Monks!
I am trying to convert a time to my time zone "America/New_York", but somehow the code is taking the time I am converting to and subtracting instead of adding to it. Here is the code:
#!/usr/bin/perl -w use strict; use DateTime; use DateTime::TimeZone; use DateTime::Format::MySQL; my $somewhere_time = "2011-06-09 08:19:12"; # pacific time? my $dt = DateTime::Format::MySQL->parse_datetime($somewhere_time); $dt->set_time_zone('UTC'); $dt->set_time_zone('America/New_York'); print DateTime::Format::MySQL->format_datetime($dt),"\n"; # I should be expecting to get: 2011-06-09 10:19:12
Any thoughts?
Thanks!

Replies are listed 'Best First'.
Re: Time Convert Question
by MidLifeXis (Monsignor) on Jun 09, 2011 at 16:04 UTC

    What time are you actually getting? If it works as you are assuming ($somewhere_time, as initialized, in UTC), and the sequential set_time_zone calls assign the original time to UTC and then convert it to NY, I would expect it to show as 04:19:12, not 10:19:12. NY is currently -0400 (behind UTC), not +0200. This assumes that the first call to set_time_zone sets the initial time zone, and the second converts it to NY.

    What value are you actually seeing?

    --MidLifeXis

      You are right, I am getting "2011-06-09 04:19:12"
      How would a set this to the right time zone, "NY is currently -0400 (behind UTC), not +0200", thanks for your response.

        Given the other example on this thread, it appears that the first time zone assigned to a floating time is adopted as the time's TZ. Assign the TZ for the floating time value to wherever that value was generated. For all we know, the timezone for that string could be the Aleutian Islands, Anchorage, Alaska, Tokyo, London, or Timbuktu. Until there is something to anchor it to, you cannot do a comparison that has any meaning.

        --MidLifeXis

Re: Time Convert Question
by Anonymous Monk on Jun 09, 2011 at 16:50 UTC
    The timestamp you're parsing doesn't contain timezone information
    #!/usr/bin/perl -- use warnings; use strict; use DateTime; use DateTime::TimeZone; use DateTime::Format::MySQL; my @Tzs = ( 'America/Los_Angeles', 'UTC', 'America/New_York', ); my $somewhere_time = "2011-06-09 08:19:12"; # pacific time? my $dt = DateTime::Format::MySQL->parse_datetime($somewhere_time); print $dt->_TZLP; Tzss( $dt ); print ' ' x 21, DateTime::Format::MySQL->format_datetime($dt),"\n\n"; # I should be expecting to get: 2011-06-09 10:19:12 Tzss( DateTime->now ); sub Tzss { my $dt = shift; for my $tz ( @Tzs ) { $dt->set_time_zone( $tz ); print $dt->_TZLP; } } sub DateTime::_TZLP { sprintf "%-20s %s\n", $_[0]->time_zone_long_name, $_[0]->strftime("%F %T%z"); } __END__ floating 2011-06-09 08:19:12+0000 America/Los_Angeles 2011-06-09 08:19:12-0700 UTC 2011-06-09 15:19:12+0000 America/New_York 2011-06-09 11:19:12-0400 2011-06-09 11:19:12 America/Los_Angeles 2011-06-09 09:43:52-0700 UTC 2011-06-09 16:43:52+0000 America/New_York 2011-06-09 12:43:52-0400
      Actually a quick fix to this is to add this line at the beginning of the Perl code:
      $ENV{'TZ'} = 'America/New_York';

      How's that!?
        How's that!?

        What are you hoping to accomplish with that? America/New_York is not pacific time