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

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

问题描述

给定一个日期/时间作为(年、月、日、时、分、秒)的数组,你将如何将其转换为纪元时间,即自 1970-01-01 00:00 以来的秒数格林威治标准时间:00?

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::格式::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-01-01 00:00:00 GMT 以来的秒数)或 -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 中将日期/时间转换为纪元时间(Unix 时间/自 1970 年以来的秒数)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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