Perl equivalent of PHP's strtotime()?

瘦欲@ 提交于 2019-12-12 11:24:03

问题


I realize that Perl has strftime, which allows you to pass a formatting object. The functionality I'm wondering if I can find is more like the following (from PHP):

$string1 = "Jun 6, 2012";
$string2 = "June 06 2012";
if (strtotime($string1) == strtotime($string2)) {
  echo "BLAMMO!";
}
// will echo "BLAMMO!"

The reason for this is a business need in which user-provided dates need to be compared for validation and extended logic (does this date fall within another daterange also provided, etc). Now, I realize I can write an entire library devoted to doing this, and I realize there are any number of potential pitfalls with date-parsing and you should never trust user input, but here are some basic assumptions.

  1. The input is actually output from any number of software packages that conform to their own internal specifications for date formatting. They all follow some standard, but those standards are not uniformly normalized between programs. That being said, I should always be comparing two dates from the same program, but I may never know what format they may follow.
  2. I realize the standards of any given system are likely to be different, but the assumption here is that we're feeding ALL of our dates into the same thing, so we can trust a consistent implementation, hopefully something in CPAN or another easily updated module.

回答1:


Date::Parse supplies str2time, which does this.

The documentation lists some examples that the module can parse:

 1995:01:24T09:08:17.1823213           ISO-8601
 1995-01-24T09:08:17.1823213
 Wed, 16 Jun 94 07:29:35 CST           Comma and day name are optional 
 Thu, 13 Oct 94 10:13:13 -0700
 Wed, 9 Nov 1994 09:50:32 -0500 (EST)  Text in ()'s will be ignored.
 21 dec 17:05                          Will be parsed in the current time zone
 21-dec 17:05
 21/dec 17:05
 21/dec/93 17:05
 1999 10:02:18 "GMT"
 16 Nov 94 22:28:20 PST 



回答2:


The standard for date/time manipulation in Perl is the DateTime project.

https://metacpan.org/pod/DateTime




回答3:


strptime for Perl can be obtained in the core module Time::Piece. This offer a core module solution whereas the rich DateTime module unfortunately isn't part of core Perl.



来源:https://stackoverflow.com/questions/13689650/perl-equivalent-of-phps-strtotime

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!