Perl相当于PHP的strtotime()? [英] Perl equivalent of PHP's strtotime()?

查看:124
本文介绍了Perl相当于PHP的strtotime()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我意识到Perl有strftime,它允许你传递格式化对象。我想知道如果我能找到的功能更像以下(从PHP):

  $ string1 =6月6日,2012; 
$ string2 =2012年6月6日;
if(strtotime($ string1)== strtotime($ string2)){
echoBLAMMO!;
}
//将回显BLAMMO!

这样做的原因是业务需求,用户提供的日期需要进行比较才能进行验证扩展逻辑(这个日期也在另一个daterange内也提供了等等)。现在,我意识到我可以写一个完整的图书馆,我意识到日期解析有许多潜在的缺陷,你不应该信任用户输入,但这里有一些基本假设。


  1. 输入实际上是从符合自己的日期格式的内部规格的任意数量的软件包输出的。他们都遵循一些标准,但这些标准在程序之间并不统一。话虽如此,我应该总是比较同一个程序的两个日期,但是我可能永远不知道他们可能遵循什么格式。

  2. 我意识到任何给定系统的标准可能是不同的是,但这里的假设是,我们将所有日期都加入到同一件事情中,所以我们可以信任一致的实现,希望在CPAN或另一个容易更新的模块中使用。


解决方案

日期::解析提供 str2time ,这样做。



该文档列出了一些示例可以解析:

  1995:01:24T09:08:17.1823213 ISO-8601 
1995-01-24T09:08 :17.1823213
星期三,16六月94 07:29:35 CST逗号和日期名称是可选的
Thu,13 Oct 94 10:13:13 -0700
Wed,9 Nov 1994 09: 50:32 -0500(EST)()中的文本将被忽略。
21 dec 17:05将在当前时区解析
21-dec 17:05
21 / dec 17:05
21 / dec / 93 17:05
1999 10:02:18GMT
16 Nov 94 22:28:20 PST


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.

解决方案

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 

这篇关于Perl相当于PHP的strtotime()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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