从 MST 到目标时区的时区转换在 Perl 中不起作用
Timezone conversion from MST to target timezone not working in Perl
my $apptStartDateTime = "20210401100000";
my $formatter = DateTime::Format::Strptime->new(pattern => "%Y%m%d%H%M%S", time_zone => "MST");
my $dt_obj = $formatter->parse_datetime($apptStartDateTime);
$dt_obj->strftime("%Y%m%d%H%M%S"), "\n"; #prints 20210401100000
# to convert to a different zone:
$dt_obj->set_time_zone("America/Los_Angeles");
$dt_obj->strftime("%Y%m%d%H%M%S"), "\n";#prints 20210401100000
以上不会从 MST 转换为 America/Los_Angeles。有人可以帮忙吗?我是 Perl 的新手。另外,上面的代码会处理夏令时吗?
我认为您打算使用美国山区时间,该时间由 America/Denver
标识。但即使日期时间是美国山地时间,也不一定总能给你正确的答案。
如果你参考这个list of tz database time zones,你会看到那个
- 美国山区标准时间 (
MST
) 为 UTC-7。
- 美国太平洋时间 (
America/Los_Angeles
) 冬季为 UTC-8,夏季为 UTC-7。
的正确转换
2021-04-01 10:00:00 -07:00 (MST)
因此
2021-04-01 10:00:00 -07:00 (America/Los_Angeles)
DateTime 正确转换了日期时间。
也许您打算使用美国山区时间,该时间由 America/Denver
标识(冬季为 UTC-7,夏季为 UTC-6)。然而,这引入了歧义。每年有一个小时,由于 DST 更改期间的重叠,您会得到错误的答案。
例如,
2020-11-01 02:30:00 (America/Denver)
可以指两者
2020-11-01 02:30:00 -05:00 (America/Denver) Before "fall back"
2020-11-01 02:30:00 -06:00 (America/Denver) After "fall back"
日期时间应以 UTC 格式传输或提供日期时间与 UTC 的偏移量。例如,使用标准 RFC3339 格式,您可以使用任何
2020-11-01T02:30:00-05:00
2020-11-01T07:30:00+00:00
2020-11-01T07:30:00Z
对于前者和任何
2020-11-01T02:30:00-06:00
2020-11-01T08:30:00+00:00
2020-11-01T08:30:00Z
为后者。然后,您可以继续使用 DateTime::Format::Strptime(使用 %z
),或者使用 DateTime::Format::RFC3339.
my $apptStartDateTime = "20210401100000";
my $formatter = DateTime::Format::Strptime->new(pattern => "%Y%m%d%H%M%S", time_zone => "MST");
my $dt_obj = $formatter->parse_datetime($apptStartDateTime);
$dt_obj->strftime("%Y%m%d%H%M%S"), "\n"; #prints 20210401100000
# to convert to a different zone:
$dt_obj->set_time_zone("America/Los_Angeles");
$dt_obj->strftime("%Y%m%d%H%M%S"), "\n";#prints 20210401100000
以上不会从 MST 转换为 America/Los_Angeles。有人可以帮忙吗?我是 Perl 的新手。另外,上面的代码会处理夏令时吗?
我认为您打算使用美国山区时间,该时间由 America/Denver
标识。但即使日期时间是美国山地时间,也不一定总能给你正确的答案。
如果你参考这个list of tz database time zones,你会看到那个
- 美国山区标准时间 (
MST
) 为 UTC-7。 - 美国太平洋时间 (
America/Los_Angeles
) 冬季为 UTC-8,夏季为 UTC-7。
2021-04-01 10:00:00 -07:00 (MST)
因此
2021-04-01 10:00:00 -07:00 (America/Los_Angeles)
DateTime 正确转换了日期时间。
也许您打算使用美国山区时间,该时间由 America/Denver
标识(冬季为 UTC-7,夏季为 UTC-6)。然而,这引入了歧义。每年有一个小时,由于 DST 更改期间的重叠,您会得到错误的答案。
例如,
2020-11-01 02:30:00 (America/Denver)
可以指两者
2020-11-01 02:30:00 -05:00 (America/Denver) Before "fall back"
2020-11-01 02:30:00 -06:00 (America/Denver) After "fall back"
日期时间应以 UTC 格式传输或提供日期时间与 UTC 的偏移量。例如,使用标准 RFC3339 格式,您可以使用任何
2020-11-01T02:30:00-05:00
2020-11-01T07:30:00+00:00
2020-11-01T07:30:00Z
对于前者和任何
2020-11-01T02:30:00-06:00
2020-11-01T08:30:00+00:00
2020-11-01T08:30:00Z
为后者。然后,您可以继续使用 DateTime::Format::Strptime(使用 %z
),或者使用 DateTime::Format::RFC3339.