On 10/30/2018 10:24 PM, Martin McCormick wrote:
I can not seem to send Time::Piece any syntax it likes.
The file I am reading sends a time stamp that should
conform to RFC822 date stamps. An example of a stamp follows:
main::(lwx:204): my @obtime = split( /\,+/, $data->{observation_time_
rfc822} );
DB<2> print $obtime[1]
30 Oct 2018 20:53:00 -0500
DB<2> n
main::(lwx:205): my $t1 = Time::Piece->strptime( '$obtime[1]', '%D %b
%H:%M:%S %z' );
DB<2> n
Error parsing time at /usr/lib/i386-linux-gnu/perl/5.24/Time/Piece.pm line 481.
at /usr/lib/i386-linux-gnu/perl/5.24/Time/Piece.pm line 481.
Time::Piece::strptime("Time::Piece", "\$obtime[1]", "%D %b %H:%M:%S
%z")
called at lwx line 205
The code that caused all that follows:
my @obtime = split( /\,+/, $data->{observation_time_rfc822} );
my $t1 = Time::Piece->strptime( '$obtime[1]', '%D %b %H:%M:%S %z' );
Another thing I tried was just
my $t1 = Time::Piece->strptime( '$obtime[1]', '%z' );
The man page for strptime lists all the possible %flags
one can send and the last one is %z for RFC822
%z An RFC-822/ISO 8601 standard timezone specification.
I was not sure if that meant it would pull in all the fields
shown above. If it had, I wouldn't be posting this message so I
tried the longer form
my $t1 = Time::Piece->strptime( '$obtime[1]', '%D %b %H:%M:%S %z' );
There is that -500 offset from utc, soon to become a -600 this
Sunday.
The following code will demonstrate the only outcome I have been
able to get. Those of you who followed earlier messages in the
thread about time stamps will recognise this code which now has a
few more lines and the data dumper is commented out but it
demonstrates what is happening.
If there is some way to see what Time::Piece thinks I am
trying to tell it to do then one might be able to figure out what
is wrong but a miss is the same as close so I can't tell if I am
getting warm.
Thanks for any ideas.
Martin McCormick
None of your arguments are right. For one, you are using the '%D' flag
which is the same as saying '%m/%d/%y'
This works fine from a quick command line test:
perl -e 'use Time::Piece; $t = "30 Oct 2018 20:53:00 -0500"; print
Time::Piece->strptime( $t, "%d %b %Y %H:%M:%S %z" );'
Wed Oct 31 01:53:00 2018
But notice since you specified '%z', that that offset was added to the
returned time... That might be what you want, or it might not be. And
also note Time::Piece->strptime returns an object that is set to UTC
whereas localtime->strptime returns an object not in UTC. If you really
want to get crazy with parsing times, DateTime is usually recommended:
https://metacpan.org/pod/DateTime or just
https://metacpan.org/pod/DateTime::Format::Strptime
--Samuel Smith
--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/