您可以在Perl中使用time()函数获取纪元时间,即自给定日期以来的秒数,在Unix中为1970年1月1日。
#!/usr/local/bin/perl
$epoc = time();
print "Number of seconds since Jan 1, 1970 - $epoc\n";
输出结果
执行以上代码后,将产生以下结果-
Number of seconds since Jan 1, 1970 - 1361022130
您可以将给定的秒数转换为日期和时间字符串,如下所示:
#!/usr/local/bin/perl
$datestring = localtime();
print "Current date and time $datestring\n";
$epoc = time();
$epoc = $epoc - 24 * 60 * 60; # one day before of current date.
$datestring = localtime($epoc);
print "Yesterday's date and time $datestring\n";
输出结果
执行以上代码后,将产生以下结果-
Current date and time Tue Jun 5 05:54:43 2018
Yesterday's date and time Mon Jun 4 05:54:43 2018