如何在Perl中解析日期并转换时区? [英] How can I parse dates and convert time zones in Perl?

查看:205
本文介绍了如何在Perl中解析日期并转换时区?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Perl中使用了 localtime 功能,以获取当前的日期和时间但需要在现有的日期解析。我有一个格林尼治标准时间的格式如下:20090103 12:00我想将其解析成可以处理的日期对象,然后将GMT时间/日期转换为当前的时区,这是当前的东部时间。所以我想将20090103 12:00转换为20090103 7:00,任何关于如何做到这一点的信息将不胜感激。

解决方案因为Perl内置的日期处理界面很笨拙,而且通过了十几个变量,更好的方法是使用 DateTime Time :: Piece DateTime是一个全能唱歌,全跳舞的Perl日期对象,您最终可能会使用它,但Time :: Piece更简单,完全适合于此任务,具有运送5.10的优点,技术是基本上是相同的。



这是使用Time :: Piece和 strptime

 #!/ usr / bin / perl 

使用5.10.0;

使用strict;
使用警告;

使用Time :: Piece;

#从命令行读取日期。
我的$ date = shift;

#使用strptime()格式解析使用strptime()格式的日期。
我的$ time = Time :: Piece-> strptime($ date,%Y%m%d%H:%M);

#这是解析,但仍然在GMT。
说$ time-> datetime;

#获取您当地的时区偏移量并将其添加到当前时间。
$ time + = $ time-> localtime-> tzoffset;

#这里是本地化的。
说$ time-> datetime;

相比之下,这里是旁边的方式。



由于格式是固定的,正则表达式将会很好,但如果格式更改,则必须调整正则表达式。

  my($ year,$ mon,$ day,$ hour,$ min)= 
$ date =〜/ ^(\d {4})(\d {2} )(\d {2})\(\d {2}):( \d {2})$ / x;

然后将其转换为Unix时代(1970年1月1日以来的秒)

 使用Time :: Local; 
#请注意,所有内部的Perl日期处理函数从0开始的月份
#和从1900开始的一年。指责C(或者指责Larry为
#parroting C)。
我的$ time = timegm(0,$ min,$ hour,$ day,$ mon-1,$ year - 1900);

然后回到当地时间。

 (undef,$ min,$ hour,$ day,$ mon,$ year)= localtime($ time); 

我的$ local_date = sprintf%d%02d%02d%02d:%02d\\\

$ year + 1900,$ mon + 1,$ day,$ hour, $ min;


I've used the localtime function in Perl to get the current date and time but need to parse in existing dates. I have a GMT date in the following format: "20090103 12:00" I'd like to parse it into a date object I can work with and then convert the GMT time/date into my current time zone which is currently Eastern Standard Time. So I'd like to convert "20090103 12:00" to "20090103 7:00" any info on how to do this would be greatly appreciated.

解决方案

Because the Perl built in date handling interfaces are kind of clunky and you wind up passing around a half dozen variables, the better way is to use either DateTime or Time::Piece. DateTime is the all-singing, all-dancing Perl date object, and you'll probably eventually want to use it, but Time::Piece is simpler and perfectly adequate to this task, has the advantage of shipping with 5.10 and the technique is basically the same for both.

Here's the simple, flexible way using Time::Piece and strptime.

#!/usr/bin/perl

use 5.10.0;

use strict;
use warnings;

use Time::Piece;

# Read the date from the command line.
my $date = shift;

# Parse the date using strptime(), which uses strftime() formats.
my $time = Time::Piece->strptime($date, "%Y%m%d %H:%M");

# Here it is, parsed but still in GMT.
say $time->datetime;

# Get your local time zone offset and add it to the time.
$time += $time->localtime->tzoffset;

# And here it is localized.
say $time->datetime;

And here's the by-hand way, for contrast.

Since the format is fixed, a regular expression will do just fine, but if the format changes you'll have to tweak the regex.

my($year, $mon, $day, $hour, $min) = 
    $date =~ /^(\d{4}) (\d{2}) (\d{2})\ (\d{2}):(\d{2})$/x;

Then convert it to Unix epoch time (seconds since Jan 1st, 1970)

use Time::Local;
# Note that all the internal Perl date handling functions take month
# from 0 and the year starting at 1900.  Blame C (or blame Larry for
# parroting C).
my $time = timegm(0, $min, $hour, $day, $mon - 1, $year - 1900);

And then back to your local time.

(undef, $min, $hour, $day, $mon, $year) = localtime($time);

my $local_date = sprintf "%d%02d%02d %02d:%02d\n",
    $year + 1900, $mon + 1, $day, $hour, $min;

这篇关于如何在Perl中解析日期并转换时区?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆