我需要在我的一个程序中以下列格式显示日期:
day - month - year "HH:MM:SS". <microseconds>
我正在使用Perl 5.8,我尝试使用epoch秒并将时间转换为微秒,如下所示:
my $epoch = time();
my $time1 = ($epoch - int($epoch)) *1e6
但我得到了这个输出:
25-05-2016 18:20:20.<incorrect number>
答案 0 :(得分:2)
为了得到你想要的东西,
use POSIX qw( strftime );
use Time::HiRes qw( time );
my $epoch = time();
my $microsecs = ($epoch - int($epoch)) *1e6;
say strftime("%d-%m-%Y %H:%M:%S", localtime($epoch)) . "." . sprintf("%06.0f", $microsecs);
输出:
25-05-2016 10:50:01.088676
sprintf
用于填充和舍入到最接近的微秒。
替代:
use POSIX qw( strftime );
use Time::HiRes qw( gettimeofday );
my ($epoch, $microsecs) = gettimeofday();
say strftime("%d-%m-%Y %H:%M:%S", localtime($epoch)) . "." . sprintf("%06d", $microsecs);
答案 1 :(得分:2)
示例:
#!/usr/bin/env perl
use strict;
use warnings;
use Time::HiRes qw( time );
use DateTime;
for (1..3) {
my $dt = DateTime->from_epoch( epoch => time() );
print $dt->strftime("%d-%m-%Y %H:%M:%S.%6N") . "\n";
sleep 1;
}
输出:
25-05-2016 17:42:16.411722
25-05-2016 17:42:17.414295
25-05-2016 17:42:18.415920