Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Perl equivalent of PHP's strtotime()?

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.
like image 731
NateDSaint Avatar asked Dec 01 '22 22:12

NateDSaint


2 Answers

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 
like image 88
Tim Avatar answered Dec 04 '22 11:12

Tim


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

https://metacpan.org/pod/DateTime

like image 24
Andy Lester Avatar answered Dec 04 '22 10:12

Andy Lester