Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
145 views
in Technique[技术] by (71.8m points)

datetime - Perl script using Time::Piece terminates without output, without errors - how to debug?

I'm using Perl 5.22 on Windows 10. I have data with time/date strings that looks like

12/31/09 08:40:00A

The data is in local time, and does not indicate whether the time is or is not in Daylight Savings Time, so I'm trying to determine that. I am first trying to parse the string to a time value so I can use

localtime(time);

to return the DST status as the 9th value.

I'm trying to parse the time/date string with Time::Piece:

  my $parsedtime = Time::Piece->strptime($timestampstr, '%m/%d/%y %I:%M:%S%p');

When this line is un-commented, the script exits to the command line without any error or output. Even the "Opening filename" output to the console at the very start of the program does not appear.

If I comment out the line above (and the references to $parsedtime) the rest of the script runs, outputs to files, and exits normally.

What can I do to debug the use of Time::Piece?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

To construct a local time, you need to use

Time::Piece::localtime->strptime(...)

rather than

Time::Piece->strptime(...)

So,

use Time::Piece qw( localtime );

my $tp = localtime->strptime("$ARGV[0]M", "%m/%d/%y %I:%M:%S%p");
my $is_dst = ( localtime($tp->epoch) )[8];
say $is_dst ? 1 : 0;

Output:

$ ./a '6/31/09 08:40:00A'
1

$ ./a '12/31/09 08:40:00A'
0

Note that your timestamp format is ambiguous. In a place that uses DST, there is one hour a year for which it will return the wrong result.


About the problem in the comments,

use strict;
use warnings;
use feature qw( say );

use DateTime::Format::Strptime qw( );

my $format = DateTime::Format::Strptime->new(
   pattern   => '%m/%d/%y %I:%M:%S%p %Z',
   locale    => 'en',
   zone_map  => { CST => '-0600', CDT => '-0500' }, # Handle non-standard time zone names.
   time_zone => 'America/Chicago',                  # Optional. Convert result to this tz.
   strict    => 1,
   on_error  => 'croak',
);

while (<DATA>) {
   chomp;
   my $dt = $format->parse_datetime($_);
   my $epoch = $dt->epoch;
   my $local_dt_str = $dt->strftime("%Y-%m-%dT%H:%M:%S%z");
   $dt->set_time_zone('UTC');
   my $utc_dt_str = $dt->strftime("%Y-%m-%dT%H:%M:%SZ");
   say "$epoch $local_dt_str $utc_dt_str";
}

__DATA__
11/03/19 01:00:00AM CDT
11/03/19 01:59:58AM CDT
11/03/19 01:59:59AM CDT
11/03/19 01:00:00AM CST
11/03/19 01:00:01AM CST
11/03/19 01:59:59AM CST

Output:

1572760800 2019-11-03T01:00:00-0500 2019-11-03T06:00:00Z
1572764398 2019-11-03T01:59:58-0500 2019-11-03T06:59:58Z
1572764399 2019-11-03T01:59:59-0500 2019-11-03T06:59:59Z
1572764400 2019-11-03T01:00:00-0600 2019-11-03T07:00:00Z
1572764401 2019-11-03T01:00:01-0600 2019-11-03T07:00:01Z
1572767999 2019-11-03T01:59:59-0600 2019-11-03T07:59:59Z

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...