我写了这个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
问题: 现在,这些值显示在用户不可读的前端中,该前端没有脚本实用程序,我只能使用许多不同的计算公式来使它们可读。
是否存在一种完全手动的方法,只需使用计算器/计算即可将纪元时间转换回用户可读的日期和时间?
答案 0 :(得分:3)
核心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接受的格式说明符。
没有访问Time :: Piece(这是Perl 5.10的核心功能),您可以使用内置的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;
答案 1 :(得分:2)
在DateTime.pm的早期版本中,您会找到一个_rd2greg子程序,我为Rich Bowen较早且已长期停产的项目Reefknot写为jd2greg。从第一年开始算起需要花费几天的时间,并产生年,月和日。您可以将一个纪元时间除以下限的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;
}