如何在Perl中将日期

如何在Perl中将日期

本文介绍了如何在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);

注意参数相反的顺序,而1月是0月。
对于更多选项,请参阅CPAN中的模块。有关解析的信息,请参阅模块。如果您真的需要查看日期解析,可能会有所帮助,虽然它自己的文件警告你远离它,因为它携带很多行李(例如,它知道像常见的公共假期),其他解决方案要快得多。

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模块。

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位数字XX是两位数字,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时间 - 秒)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 19:42