Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re^3: Time::Piece reversibility

by cguevara (Vicar)
on Jun 18, 2017 at 20:19 UTC ( #1193064=note: print w/replies, xml ) Need Help??


in reply to Re^2: Time::Piece reversibility
in thread Time::Piece reversibility

#!/usr/bin/env perl use strict; use warnings; use Test::More ('no_plan'); use Time::Piece; $ENV{'TZ'} = 'America/Los_Angeles'; my $datestr = '2017-06-19 10:07:42'; my $t = localtime->strptime($datestr, '%Y-%m-%d %H:%M:%S'); is($Time::Piece::VERSION, 1.31); is($t->epoch, 1497892062); is($t->tzoffset, -25200); is($t->strftime, 'Mon, 19 Jun 2017 10:07:42 PDT'); is($t->strftime('%Y-%m-%d %H:%M:%S'), $datestr);

Replies are listed 'Best First'.
Re^4: Time::Piece reversibility
by mla12 (Acolyte) on Jun 18, 2017 at 20:52 UTC

    Thanks again. So localtime()->strptime does behave differently.

    So localtime()->strptime's handling of time zones is the underlying issue. Certainly these two should be equivalent, no?

    #!/usr/bin/env perl use strict; use warnings; use Test::More ('no_plan'); use Time::Piece; $ENV{'TZ'} = 'America/Los_Angeles'; my @tests = ( ['2017-06-19 10:07:42', '%Y-%m-%d %H:%M:%S'], ['2017-06-19 10:07:42-0700', '%Y-%m-%d %H:%M:%S%z'], ); is($Time::Piece::VERSION, 1.31); foreach my $test (@tests) { my ($datestr, $fmt) = @$test; my $t = localtime->strptime($datestr, $fmt); is($t->epoch, 1497892062); is($t->tzoffset, -25200); is($t->strftime, 'Mon, 19 Jun 2017 10:07:42 PDT'); is($t->strftime('%Y-%m-%d %H:%M:%S'), $datestr); }

    Result

    ok 1 ok 2 ok 3 ok 4 ok 5 not ok 6 # Failed test at ./try7 line 22. # got: '1497917262' # expected: '1497892062' ok 7 not ok 8 # Failed test at ./try7 line 24. # got: 'Mon, 19 Jun 2017 17:07:42 PDT' # expected: 'Mon, 19 Jun 2017 10:07:42 PDT' not ok 9 # Failed test at ./try7 line 25. # got: '2017-06-19 17:07:42' # expected: '2017-06-19 10:07:42-0700' 1..9 # Looks like you failed 3 tests of 9.

      I've only used localtime->strptime() to get a localtime instance from a time without a time zone.

      To get a localtime instance directly from a time with a time zone, I just combine localtime and Time::Piece->strptime()->epoch.

      #!/usr/bin/env perl use strict; use warnings; use Test::More ('no_plan'); use Time::Piece; $ENV{'TZ'} = 'America/Los_Angeles'; my $datestr = '2017-06-19 10:07:42-0700'; my $t = localtime(Time::Piece->strptime($datestr, '%Y-%m-%d %H:%M:%S%z +')->epoch); is($Time::Piece::VERSION, 1.31); is($t->epoch, 1497892062); is($t->tzoffset, -25200); is($t->strftime, 'Mon, 19 Jun 2017 10:07:42 PDT'); is($t->strftime('%Y-%m-%d %H:%M:%S%z'), $datestr);

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1193064]
help
Chatterbox?
[Tanktalus]: or posting the last hour of cb... well, maybe I should double check that first :)
[Tanktalus]: yup, it's working. :)
[choroba]: https://github. com/choroba/pm-cb/ commit/7b57f513596 7bf8a29d74f1c307de 9a76894cdcf
[choroba]: Also, a thread here on PM mentioned that one of perlmonks.com or www.perlmonks.com should now work
[choroba]: Tidings
[Tanktalus]: So, I can log in, I can update last hour of cb, I can read the cb, I just can't post a message to it :(

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (5)
As of 2018-07-15 21:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?















    Results (326 votes). Check out past polls.

    Notices?