如何显示当前日期和时间,包括微秒?

rub*_*uby 3 perl datetime

我需要在我的程序之一中以以下格式显示日期:

day - month - year "HH:MM:SS". <microseconds>
Run Code Online (Sandbox Code Playgroud)

我正在使用 Perl 5.8,我尝试使用纪元秒并将时间转换为微秒,如下所示:

my $epoch = time();
my $time1 = ($epoch - int($epoch)) *1e6
Run Code Online (Sandbox Code Playgroud)

但我得到这个输出:

25-05-2016 18:20:20.<incorrect number>
Run Code Online (Sandbox Code Playgroud)

xxf*_*xxx 5

Use Time::HiRes

Example:

#!/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;
}
Run Code Online (Sandbox Code Playgroud)

Output:

25-05-2016 17:42:16.411722
25-05-2016 17:42:17.414295
25-05-2016 17:42:18.415920
Run Code Online (Sandbox Code Playgroud)