我正在嘗試使用 perl v5.26 中的 Time::Piece->strptime() 決議以下資料:
my $str = "Mon Feb 21 02:54:49 IST 2022";
my $time_zone = substr($str, 20, 3);
my $date_time = Time::Piece->strptime($str, "%a %b %e %X $time_zone %Y");
print "Todays date/time is : $date_time";
但是,當我執行上面的代碼片段時,我收到以下錯誤:
Error parsing time at /usr/local/lib64/perl5/Time/Piece.pm line 598.
我不確定我錯過了什么,任何幫助都會非常有幫助。
uj5u.com熱心網友回復:
%X沒有 finalAM/PM僅適用于字串的末尾。
my $str = "Mon Feb 21 IST 2022 02:54:49";
my $time_zone = substr($str, 11, 3);
warn $time_zone;
my $date_time = Time::Piece->strptime($str, "%a %b %e $time_zone %Y %X");
print "Todays date/time is : $date_time";
要么
my $str = "Mon Feb 21 02:54:49 AM IST 2022";
my $time_zone = substr($str, 23, 3);
my $date_time = Time::Piece->strptime($str, "%a %b %e %X $time_zone %Y");
print "Todays date/time is : $date_time";
但是我的測驗表明這種行為取決于 Perl 版本,所以可能根本不要使用%X(same as %I:%M:%S %p) 并切換到%T(same as %H:%M:%S)。
my $str = "Mon Feb 21 02:54:49 IST 2022";
my $time_zone = substr($str, 20, 3);
my $date_time = Time::Piece->strptime($str, "%a %b %e %T $time_zone %Y");
print "Todays date/time is : $date_time";
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/431167.html
