davies has asked for the wisdom of the Perl Monks concerning the following question:
X: I want to write timestamps to a text file in a reasonably human-friendly format and then read them back, getting the same epoch second for comparison with localtime.
Y: my first attempt with Time::Piece:
use strict; use warnings; use Time::Piece; use feature 'say'; my $fmt = '%d %b %Y %T %Z'; say localtime->strftime; say localtime->strftime($fmt); my $str = '03 Sep 2022 16:10:17 GMT Daylight Time'; my $strtime = Time::Piece->strptime($str, $fmt); say localtime->epoch; say $strtime->epoch;
This returns:
Sat, 03 Sep 2022 16:10:35 GMT Daylight Time 03 Sep 2022 16:10:35 GMT Daylight Time Garbage at end of string in strptime: Daylight Time at C:/Strawberry/ +perl/lib/Time/Piece.pm line 598. Perhaps a format flag did not match the actual input? at C:/Strawberry +/perl/lib/Time/Piece.pm line 598. 1662217835 1662221417
This is out by the timezone offset. The docs tell me to look at https://www.unix.com/man-page/FreeBSD/3/strftime/, which says that %z will be treated as a numerical offset. For me, this gives the same as upper case Z, although I can't rule out the influence of Windows on this. DateTime says it won't parse dates and gives no indication I could see of how to handle localtime.
I'd be happy with UTC, but Time::Piece does not seem to support it, which is why I think I may have an XY problem. I can't believe that no-one has invented this wheel, but my searches have been fruitless and all the wheels I have tried seem square. I know that clocks moving back at the end of next month will bite me. All help gratefully received.
Regards,
John Davies
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Reading a timestamp and getting the written value
by haukex (Archbishop) on Sep 03, 2022 at 15:51 UTC | |
by davies (Monsignor) on Sep 03, 2022 at 16:13 UTC |