我编写了这个 Perl 脚本来将文件中的日期和时间转换为纪元时间。
原始日期格式为
dd-mm-yyyy hh:MM:ss
我这样做的原因是因为我必须将这些值写入数据库,并且列只接受整数值。
use strict;
use warnings;
use Time::Local;
$datestring = '07-06-2019 21:13:00';
my ($dd, $mm, $yyyy, $hh, $min, $sec) = split /\W+/, $datestring;
my $epoch = timelocal($sec,$min,$hh,$dd,$mm-1,$yyyy-1900);
print("$epoch\n");
将日期
07-06-2019 21:13:00
翻译为 1559934780
问题: 这些值现在显示在用户无法读取的前端中,前端没有脚本实用程序,我只能使用许多不同的计算公式来使其可读。
是否有一种完全手动的方法,只需使用计算器/计算即可将纪元时间转换回用户可读的日期和时间?
核心Time::Piece可以根据您当地的时区进行双向转换。
use strict;
use warnings;
use Time::Piece;
my $datestring = '07-06-2019 21:13:00';
my $time = localtime->strptime($datestring, '%d-%m-%Y %H:%M:%S');
my $epoch = $time->epoch;
...
my $time = localtime($epoch);
my $datestring = $time->strftime('%d-%m-%Y %H:%M:%S');
请参阅任何标准 strftime 或 strptime 手册页,了解通常接受的格式说明符 - 不幸的是,Time::Piece 接受的格式说明符没有记录在案。
如果不访问 Perl 5.10 以来的核心 Time::Piece,您可以使用内置的 localtime 函数,它只是稍微复杂一点(如 timelocal)。
use strict;
use warnings;
my $epoch = 1559934780;
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime $epoch;
my $date_string = sprintf '%02d-%02d-%04d %02d:%02d:%02d',
$mday, $mon+1, $year+1900, $hour, $min, $sec;
在 DateTime.pm 的早期版本中,您会发现一个子 _rd2greg,它是我为 Rich Bowen 早期且长期不存在的 Project Reefknot 编写的 jd2greg。这需要从 1 年初开始的几天,并生成年、月和日。您可以将纪元时间除以 86400(一天中的秒数),然后添加 719163(截至 1970 年的天数)来传递给它。
这里是jd2greg和对应的greg2jd:
=head2 jd2greg
($year, $month, $day) = jd2greg( $jd );
Convert number of days on or after Jan 1, 1 CE (Gregorian) to
gregorian year,month,day.
=cut
sub jd2greg {
use integer;
my $d = shift;
my $yadj = 0;
my ( $c, $y, $m );
# add 306 days to make relative to Mar 1, 0; also adjust $d to be within
# a range (1..2**28-1) where our calculations will work with 32bit ints
if ( $d > 2**28 - 307 ) {
# avoid overflow if $d close to maxint
$yadj = ( $d - 146097 + 306 ) / 146097 + 1;
$d -= $yadj * 146097 - 306;
} elsif ( ( $d += 306 ) <= 0 )
{
$yadj =
-( -$d / 146097 + 1 ); # avoid ambiguity in C division of negatives
$d -= $yadj * 146097;
}
$c =
( $d * 4 - 1 ) / 146097; # calc # of centuries $d is after 29 Feb of yr 0
$d -= $c * 146097 / 4; # (4 centuries = 146097 days)
$y = ( $d * 4 - 1 ) / 1461; # calc number of years into the century,
$d -= $y * 1461 / 4; # again March-based (4 yrs =~ 146[01] days)
$m =
( $d * 12 + 1093 ) / 367; # get the month (3..14 represent March through
$d -= ( $m * 367 - 1094 ) / 12; # February of following year)
$y += $c * 100 + $yadj * 400; # get the real year, which is off by
++$y, $m -= 12 if $m > 12; # one if month is January or February
return ( $y, $m, $d );
}
=head2 greg2jd
$jd = greg2jd( $year, $month, $day );
Convert gregorian year,month,day to days on or after Jan 1, 1 CE
(Gregorian). Normalization is performed (e.g. month of 28 means
April two years after given year) for month < 1 or > 12 or day < 1
or > last day of month.
=cut
sub greg2jd {
use integer;
my ( $y, $m, $d ) = @_;
my $adj;
# make month in range 3..14 (treat Jan & Feb as months 13..14 of prev year)
if ( $m <= 2 ) {
$y -= ( $adj = ( 14 - $m ) / 12 );
$m += 12 * $adj;
} elsif ( $m > 14 )
{
$y += ( $adj = ( $m - 3 ) / 12 );
$m -= 12 * $adj;
}
# make year positive (oh, for a use integer 'sane_div'!)
if ( $y < 0 ) {
$d -= 146097 * ( $adj = ( 399 - $y ) / 400 );
$y += 400 * $adj;
}
# add: day of month, days of previous 0-11 month period that began w/March,
# days of previous 0-399 year period that began w/March of a 400-multiple
# year), days of any 400-year periods before that, and 306 days to adjust
# from Mar 1, year 0-relative to Jan 1, year 1-relative (whew)
$d += ( $m * 367 - 1094 ) / 12 + $y % 100 * 1461 / 4 +
( $y / 100 * 36524 + $y / 400 ) - 306;
}
只是一个快速说明:这里有一个快速实用函数,可以根据实际定义生成一些关键常量,而不必硬编码这么多常量:
function _() {
return \
DAYS_IN_400YRS = 4 * (\
DAYS_IN_100YRS = 25 * (\
DAYS_IN_4_YRS = 4 * (\
DAYS_IN_CMMNYR = 8 * (8 * (5) + 5) + 5 \
) + 1) - 1) + 1
}'
146097
DAYS_IN_400YRS: 146097
DAYS_IN_100YRS: 36524
DAYS_IN_4_YRS: 1461
DAYS_IN_CMMNYR: 365
或者如果你想做得非常全面:
function __() {
return \
_SECS_IN_400YRS = (\
_DAYS_IN_400YRS = 4 * (\
_DAYS_IN_100YRS = 25 * (\
_DAYS_IN_4_YRS = 4 * (\
_DAYS_IN_CMMNYR = (\
_DT_LONG_MNTHS_IN_YR = 7) * (\
_DAYS_IN_LONG_MNTH = (\
_DAYS_IN_SHRT_MNTH = (\
_DAYS_IN_LEAP_FEBR = (\
_DAYS_IN_CMMN_FEBR = (\
_DT_WEEKS_IN_FEB = 4 ) * (\
_DT_DAYS_IN_WEEK = 7 )) + 1 ) + 1 ) + 1 ) + (\
_DT_STND_MNTHS_IN_YR = 4 ) * \
_DAYS_IN_SHRT_MNTH + \
_DAYS_IN_CMMN_FEBR ) + 1 ) - 1 ) + 1 ) \
* (\
_SECS_IN_DY = (_TM_HRS_IN_DY = 24 ) * (\
_SECS_IN_HR = (_TM_MNS_IN_HR = 60 ) * (\
_SECS_IN_MN = 60 ) ) )
}
_DAYS_IN_400YRS: 146097
_DAYS_IN_100YRS: 36524
_DAYS_IN_4_YRS: 1461
_DAYS_IN_CMMNYR: 365
_DAYS_IN_LONG_MNTH: 31
_DAYS_IN_SHRT_MNTH: 30
_DAYS_IN_LEAP_FEBR: 29
_DAYS_IN_CMMN_FEBR: 28
_DT_DAYS_IN_WEEK: 7
_DT_WEEKS_IN_FEB: 4
_DT_STND_MNTHS_IN_YR: 4
_DT_LONG_MNTHS_IN_YR: 7
_SECS_IN_400YRS: 12622780800
_SECS_IN_DY: 86400
_SECS_IN_HR: 3600
_SECS_IN_MN: 60
_TM_MNS_IN_HR: 60
_TM_HRS_IN_DY: 24