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
Code follows:
#!/usr/bin/perl -w
use strict;
use warnings::unused;
use Data::Dumper;
use XML::Simple;
use File::Basename;
use Cwd;
use File::stat;
use Time::Local;
use Time::Piece;
my $homedir = "/tmp";
my @t = localtime(time);
my $utcsec = timelocal(@t);
my $wxfile = 'display.php?stid=KSWO';
#I want that file to end up in a specific directory so:
my $wxpath = $homedir . "/" . $wxfile;
my $day = 86400; #seconds
my $hour = 3600;
my $gmt_offset_in_seconds = timegm(@t) - timelocal(@t);
my $tzoffset = $gmt_offset_in_seconds / 3600;
#many thanks to whoever wrote the quick way to determine TZ
#offset from utc
my $wxlast_update_time;
#Grab conditions from NOAA if the file is stale or missing.
if ( !stat($wxpath) ) { #The file is not there.
system(
"curl -s -o $wxpath '
http://w1.weather.gov/xml/current_obs/$wxfile'");
#Change the mtime to a quarter past last hour.
my $when = timelocal( 0, 15, $t[2], $t[3], $t[4], ( $t[5] - 100 ) );
utime $when, $when, "$wxpath";
} #The file is not there.
else { #what normally happens
my $wxstatRef = stat($wxpath);
$wxlast_update_time = $wxstatRef->mtime();
my $wxage = ( $utcsec - $wxlast_update_time );
if ( $wxage >= $hour ) { #File needs to be refreshed.
#The file should not be more than 3600 seconds old.
system(
"curl -s -o $wxpath '
http://w1.weather.gov/xml/current_obs/$wxfile'"
);
#Change the mtime to a quarter past.
my $when = timelocal( 0, 15, $t[2], $t[3], $t[4], ( $t[5] - 100 ) );
utime $when, $when, "$wxpath";
} #File needs to be refreshed.
} #what normally happens
# create object
my $xml = new XML::Simple;
# read XML file
my $data = $xml->XMLin("$wxpath");
#print Dumper($data);
my @obtime = split( /\,+/, $data->{observation_time_rfc822} );
my $t1 = Time::Piece->strptime( '$obtime[1]', '%D %b %H:%M:%S %z' );
exit(0);