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

Hi. I want to take a given date, supplied as a text string in YYY-MM-DD format, and determine if it's >= the current day. I'm having trouble with the timezone handling. The strptime seems to return the result in gmtime. I'm using Time::Piece::Plus to get the current day, but that's in localtime. What's the simplest way to get them both in gmtime, since I really don't care about the time or timezones. Adding a timezone to the strptime format string seems ugly.
#!/usr/bin/env perl use strict; use warnings; use Time::Piece::Plus; my $today = Time::Piece::Plus->today; my $date = Time::Piece::Plus->strptime("2015-04-20", '%Y-%m-%d'); print "Date: $date tzoffset=", $date->tzoffset, "\n"; print "Today: $today tzoffset=", $today->tzoffset, "\n"; print "Diff: ", ($date - $today), "\n";
Output:
Date: Mon Apr 20 00:00:00 2015 tzoffset=0 Today: Mon Apr 20 00:00:00 2015 tzoffset=-25200 Diff: -25200

Replies are listed 'Best First'.
Re: Ignoring timezones when comparing dates with Time::Piece
by choroba (Cardinal) on Apr 20, 2015 at 23:20 UTC
    You can set a different time zone:
    { local $ENV{TZ} = 'GMT'; print scalar 'Time::Piece'->localtime; }
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: Ignoring timezones when comparing dates with Time::Piece::Plus
by Anonymous Monk on Apr 20, 2015 at 23:24 UTC

    The Time::Piece::Plus docs mention timezones all over the place, and if you look at the source you can see

    my $self = $args->{as_localtime} ? $class->localtime() : $class->g +mtime(); my $parsed = $self->strptime($args->{str}, '%Y-%m-%d');

    So there you go  gmtime()->strptime(...)

    Now the question is, does this work or do something real?

    I don't know because I DateTime

      Awesome, thanks! Calling strptime on localtime() was the key:
      #!/usr/bin/env perl use strict; use warnings; use Time::Piece::Plus; my $today = localtime()->today; my $date = localtime()->strptime("2015-04-20", '%Y-%m-%d'); print "Date: $date tzoffset=", $date->tzoffset, "\n"; print "Today: $today tzoffset=", $today->tzoffset, "\n"; print "Diff: ", ($date - $today), "\n";
      Output:
      Date: Mon Apr 20 00:00:00 2015 tzoffset=-25200 Today: Mon Apr 20 00:00:00 2015 tzoffset=-25200 Diff: 0
      Not sure what you mean by timezones mentioned all over the place.
      $ cpanm Time::Piece Time::Piece is up to date. (1.29) $ cpanm Time::Piece::Plus Time::Piece::Plus is up to date. (0.05) $ perldoc -t Time::Piece | grep -i zone $t->tzoffset # timezone offset in a Time::Seconds o +bject $ perldoc -t Time::Piece::Plus | grep -i zone $