如何使用Perl将纪元时间转换为UTC时间?

问题描述 投票:2回答:5

我需要使用Perl将时代日期和时间字符串转换为UTC日期和时间。

请分享您的想法。

#!/usr/bin/perl
my $start_time='1448841600';
my $stop_time='1448863200';

以上两个日期和时间均为纪元格式,应将其转换为UTC日期和时间。

perl datetime epoch
5个回答
5
投票

您可以使用gmtime进行转换,并使用strftime进行格式化。

  use POSIX 'strftime';

  strftime "%d-%m-%Y-%H:%M:%S", gmtime('1448841600');

4
投票

我建议使用Time::Piece模块来处理日期。

Time::Piece

您还可以按照以下步骤进行时区数学计算:#!/usr/bin/env perl use strict; use warnings; use Time::Piece; my $start_time=Time::Piece -> new(1448841600); print $start_time,"\n"; print $start_time -> epoch,"\n"; print $start_time -> strftime ( "%Y-%m-%d %H:%M:%S" ),"\n"; #nb - you can also use localtime/gmtime: my $end_time = gmtime(1448863200); print $end_time,"\n"; print $end_time->epoch,"\n";


3
投票

3
投票

如果您需要在这些日期时间进行更多操作,http://perldoc.perl.org/functions/gmtime.html

use DateTime

2
投票

您可以使用use DateTime; my $start_time ='1448841600'; my $stop_time ='1448863200'; my $start = DateTime->from_epoch(epoch=>$start_time)->set_time_zone('UTC'); my $stop = DateTime->from_epoch(epoch=>$stop_time)->set_time_zone('UTC'); say $start->strftime("%F %T %Z"); # 2015-11-30 00:00:00 UTC say $stop->strftime("%F %T %Z"); # 2015-11-30 06:00:00 UTC 功能:

gmtime

输出:

my $time = "1448841600";
my @months = ("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
my ($sec, $min, $hour, $day,$month,$year) = (gmtime($time))[0,1,2,3,4,5]; 
print "Time ".$time." converts to ".$months[$month]." ".$day.", ".($year+1900);
print " ".$hour.":".$min.":".$sec."\n";
© www.soinside.com 2019 - 2024. All rights reserved.