如何将datetime转换为不同的格式? [perl] [英] How do I convert datetime to a different format? [perl]
问题描述
需要帮助解析日期时间戳,并按日期和时间分割。
Need help parsing the datetime stamp and splitting it up by date and time.
use strict;
use warnings;
use Time::Piece;
my $string = "05:57:03 08/31/10 MDT";
print $string,"\n";
my $time = Time::Piece->strptime($string, "%H:%M:%S");
my $date = Time::Piece->strptime($string, "%Y/%m/%d");
print $time,$date,"\n";
谢谢! 此外,我如何知道这是使用代码的星期几?
推荐答案
能够使用如下代码:
my $t = Time::Piece->strptime($string, "%H:%M:%S %m/%d/%y %Z");
但是,至少在我的系统上,我必须将时区MST更改为GMT,以将其更改为比赛;如果我把它留在你的例子中,我会收到一个错误:
However, on my system at least, I have to change the time zone MST to GMT for it to match; if I leave it as in your example, I get an error:
Perl> my $t = Time::Piece->strptime("05:57:03 08/31/10 DST", "%H:%M:%S %m/%d/%y %Z");
[!] Runtime error: Error parsing time at /usr/local/lib/perl/5.10.0/Time/Piece.pm line 469.
如果它适用于你,你会有一个Time :: Piece对象,你可以在其上调用eg $ t-> day_of_week
作为一个数字, $ t-> day
'Tue'或 $ t-> fullday
'Tuesday'。
If it works for you, though, you'll have a Time::Piece object, on which you can call e.g. $t->day_of_week
for the day of the week as a number, $t->day
for e.g. 'Tue', or $t->fullday
for e.g. 'Tuesday'.
请参阅 Time :: Piece ,了解您可以致电的方法。
See the documentation for Time::Piece for details on the methods you can call.
这篇关于如何将datetime转换为不同的格式? [perl]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!