#!/usr/bin/env perl use strict; use warnings; use Test::More tests => 20; BEGIN { $ENV{TZ} = 'PST8PDT'; use_ok('Time::Piece'); use_ok('Time::Local'); } # From perlport: # The value for $offset in Unix will be 0, but in Mac OS Classic will be # some large number. $offset can then be added to a Unix time value to # get what should be the proper value on any system. my $epoch_offset = Time::Local::timegm(0, 0, 0, 1, 0, 70); my $datestr = '2017-06-19 10:07:42'; my $fmt = '%Y-%m-%d %H:%M:%S'; # strptime as static method. Should parse as gmtime. my $t = Time::Piece->strptime($datestr, $fmt); my $method = 'Time::Piece->strptime'; note("Date '$datestr', $method"); is $t->tzoffset, 0, "$method, tzoffset"; is($t->epoch, $epoch_offset + 1497866862, "$method, epoch"); is($t->strftime, 'Mon, 19 Jun 2017 10:07:42 UTC', "$method, strftime"); # Should be same as above $t = gmtime->strptime($datestr, $fmt); $method = 'gmtime->strptime'; note("Date '$datestr', $method"); is $t->tzoffset, 0, "$method, tzoffset"; is($t->epoch, $epoch_offset + 1497866862, "$method, epoch"); is($t->strftime, 'Mon, 19 Jun 2017 10:07:42 UTC', "$method, strftime"); # Since zone isn't specified in date string, should assume local time zone $t = localtime->strptime($datestr, $fmt); $method = 'localtime->strptime'; note("Date '$datestr', $method"); is $t->tzoffset, -25_200, "$method, tzoffset"; is($t->epoch, $epoch_offset + 1497892062, "$method, epoch"); is($t->strftime, 'Mon, 19 Jun 2017 10:07:42 PDT', "$method, strftime"); # epoch seconds should always be the same, since this date string # has an explicit offset from UTC $datestr = '2017-06-19 10:07:42-0500'; $fmt = '%Y-%m-%d %H:%M:%S%z'; # strptime as static method. Should parse as gmtime. $t = Time::Piece->strptime($datestr, $fmt); $method = 'Time::Piece->strptime'; note("Date '$datestr', $method"); is $t->tzoffset, 0, "$method, tzoffset"; is($t->epoch, $epoch_offset + 1497884862, "$method, epoch"); is($t->strftime, 'Mon, 19 Jun 2017 15:07:42 UTC', "$method, strftime"); $t = gmtime->strptime($datestr, $fmt); $method = 'gmtime->strptime'; note("Date '$datestr', $method"); is $t->tzoffset, 0, "$method, tzoffset"; is($t->epoch, $epoch_offset + 1497884862, "$method, epoch"); is($t->strftime, 'Mon, 19 Jun 2017 15:07:42 UTC', "$method, strftime"); $t = localtime->strptime($datestr, $fmt); # Hack to get this test to work until fix applied: #$t = gmtime->strptime($datestr, $fmt); #$t = localtime($t->epoch); $method = 'localtime->strptime'; note("Date '$datestr', $method"); is $t->tzoffset, -25_200, "$method, tzoffset"; is($t->epoch, $epoch_offset + 1497884862, "$method, epoch"); is($t->strftime, 'Mon, 19 Jun 2017 08:07:42 PDT', "$method, strftime");