使用Time :: Piece的Perl脚本无输出终止,无错误-如何调试? [英] Perl script using Time::Piece terminates without output, without errors - how to debug?

查看:54
本文介绍了使用Time :: Piece的Perl脚本无输出终止,无错误-如何调试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Windows 10上使用Perl 5.22.我的数据带有类似日期的时间/日期字符串

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);

localtime(time);

将DST状态返回为第9个值.

我正在尝试使用Time :: Piece:解析时间/日期字符串:

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.

如果我注释掉上面的行(以及对$ parsedtime的引用),则脚本的其余部分将运行,输出到文件并正常退出.

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

我该如何调试Time :: Piece的使用?

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

推荐答案

要构建本地时间,您需要使用

To construct a local time, you need to use

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

而不是

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

所以

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;

输出:

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

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

请注意,您的时间戳格式不明确.在使用DST的地方,一年中有一个小时会返回错误的结果.

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.

关于评论中的问题,

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

输出:

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

这篇关于使用Time :: Piece的Perl脚本无输出终止,无错误-如何调试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆