2016-05-13 10 views
1

perlで1461241125.31307を変換するにはどうすればよいですか?私が試した:perlでUnixエポック時間を読み込み可能な形式に変換する方法

use Date::Parse; 
$unix_timestamp = '1461241125.31307'; 
my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = localtime($unix_timestamp); 
$mon += 1; 
$year += 1900; 
$unix_timestamp_normal = "$year-$mon-$mday $hour:$min:$sec"; 

結果:2016-4-21 5:18:45(時間の無いパディング)、それをIパッドを行うと、GMTそれを作る方法

。私は結果が言いたい2016-04-21 12:18:45


お返事ありがとうございます。

use DateTime; 
$unix_timestamp = '1461241125.31307'; 
my $dt = DateTime->from_epoch(epoch => $unix_timestamp); 
print $dt->strftime('%Y-%m-%d %H:%M:%S'),"\n"; 

答えて

1
use POSIX qw(strftime); 

my $epoch_ts = '1461241125.31307'; 

say strftime('%Y-%m-%d %H:%M:%S', gmtime($epoch_ts)); 
4

最も簡単な方法:

print scalar localtime $unix_timestamp; 

ドキュメント:

print scalar gmtime $unix_timestamp; 

ドキュメント:http://perldoc.perl.org/functions/gmtime.html(基本的には言う:すべてlocaltime等が挙げられるが、出力GMTについてはhttp://perldoc.perl.org/functions/localtime.html

gmtimeを使用GMT時間)

カスタム形式の場合は、DateTimeを試してみてください。

use DateTime; 

my $dt = DateTime->from_epoch(epoch => $unix_timestamp); 
print $dt->strftime('%Y-%s'); 

は、すべてのオプションのためhttp://search.cpan.org/perldoc?DateTimeを参照してください。フォーマットの多くは、事前に定義されたDateTimeのフォーマッタを使用して、より簡単に作成することができますhttp://search.cpan.org/search?query=DateTime%3A%3AFormat&mode=all

+0

POSIXのstrftimeのを使用してはるかに多くの軽量使うよりDateTimeの(そしてそれはコアです) – ikegami

0

使用gmtime代わりlocaltime

perldoc -f gmtimeの:

gmtime EXPR 
gmtime Works just like "localtime" but the returned values are localized 
     for the standard Greenwich time zone. 

     Note: When called in list context, $isdst, the last value returned 
     by gmtime, is always 0. There is no Daylight Saving Time in GMT. 

     Portability issues: "gmtime" in perlport. 
関連する問題