如何在Perl中将日期/时间转换为纪元时间(自1970年以来为Unix时间/秒)? [英] How do I convert a date/time to epoch time (unix time/seconds since 1970) in Perl?

查看:119
本文介绍了如何在Perl中将日期/时间转换为纪元时间(自1970年以来为Unix时间/秒)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定日期/时间数组(年,月,日,时,分,秒),如何将其转换为纪元时间,即自1970-01-01 00:00起的秒数:00 GMT?

Given a date/time as an array of (year, month, day, hour, minute, second), how would you convert it to epoch time, i.e., the number of seconds since 1970-01-01 00:00:00 GMT?

奖金问题:如果以字符串形式给出日期/时间,您将如何首先将其解析为(y,m,d,h,m,s)数组?

Bonus question: If given the date/time as a string, how would you first parse it into the (y,m,d,h,m,s) array?

推荐答案

这是获取Unix时间的最简单方法:

This is the simplest way to get unix time:

use Time::Local;
timelocal($second,$minute,$hour,$day,$month-1,$year);

请注意参数的相反顺序,并且一月是月份0. 有关更多选项,请参阅CPAN中的 DateTime 模块.

Note the reverse order of the arguments and that January is month 0. For many more options, see the DateTime module from CPAN.

关于解析,请参见CPAN中的 Date :: Parse 模块.如果您真的很需要日期解析,那么 Date :: Manip 可能会有所帮助,尽管它自己的文档警告您远离它,因为它携带了很多行李(例如,它知道诸如常见的商务假期之类的事情),而其他解决方案则要快得多.

As for parsing, see the Date::Parse module from CPAN. If you really need to get fancy with date parsing, the Date::Manip may be helpful, though its own documentation warns you away from it since it carries a lot of baggage (it knows things like common business holidays, for example) and other solutions are much faster.

如果您碰巧知道要解析的日期/时间格式,则可以使用一个简单的正则表达式就可以了,但是最好使用适当的CPAN模块.例如,如果您知道日期将始终按YMDHMS顺序排列,请使用CPAN模块 DateTime: :Format :: ISO8601 .

If you happen to know something about the format of the date/times you'll be parsing then a simple regular expression may suffice but you're probably better off using an appropriate CPAN module. For example, if you know the dates will always be in YMDHMS order, use the CPAN module DateTime::Format::ISO8601.

作为我自己的参考,如果没有其他要求,以下是我在应用程序中使用的一个函数,在该应用程序中,我知道日期将始终按YMDHMS顺序排列,而"HMS"部分的全部或部分可选.它接受任何定界符(例如"2009-02-15"或"2009.02.15").它返回相应的unix时间(格林尼治标准时间1970年1月1日00:00:00以来的秒数),如果无法解析,则返回-1(这意味着您最好确保永远不会合法地解析日期1969- 12-31 23:59:59).它还假定以两位数字表示的年份XX,最高为"69"表示"20XX",否则为"19XX"(例如,"50-02-15"表示2050-02-15,而"75-02-15"表示1975- 02-15).

For my own reference, if nothing else, below is a function I use for an application where I know the dates will always be in YMDHMS order with all or part of the "HMS" part optional. It accepts any delimiters (eg, "2009-02-15" or "2009.02.15"). It returns the corresponding unix time (seconds since 1970-01-01 00:00:00 GMT) or -1 if it couldn't parse it (which means you better be sure you'll never legitimately need to parse the date 1969-12-31 23:59:59). It also presumes two-digit years XX up to "69" refer to "20XX", otherwise "19XX" (eg, "50-02-15" means 2050-02-15 but "75-02-15" means 1975-02-15).

use Time::Local;

sub parsedate { 
  my($s) = @_;
  my($year, $month, $day, $hour, $minute, $second);

  if($s =~ m{^\s*(\d{1,4})\W*0*(\d{1,2})\W*0*(\d{1,2})\W*0*
                 (\d{0,2})\W*0*(\d{0,2})\W*0*(\d{0,2})}x) {
    $year = $1;  $month = $2;   $day = $3;
    $hour = $4;  $minute = $5;  $second = $6;
    $hour |= 0;  $minute |= 0;  $second |= 0;  # defaults.
    $year = ($year<100 ? ($year<70 ? 2000+$year : 1900+$year) : $year);
    return timelocal($second,$minute,$hour,$day,$month-1,$year);  
  }
  return -1;
}

这篇关于如何在Perl中将日期/时间转换为纪元时间(自1970年以来为Unix时间/秒)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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