Perl的时间处理

http://blog.csdn.net/zhangxinrun/article/details/5886950

1)

Date::Calc这里是冠军模块:

use strict;
use warnings;
use Date::Calc qw(Add_Delta_YMD);

my $startDate = '2000-01-01';
my ($startYear, $startMonth, $startDay) = $startDate =~ m/(/d-(/d)-/d)/;

# 1 year, 2 months, 3 days, after startDate
my $endDate = join('-', Add_Delta_YMD($startYear, $startMonth, $startDay, 1, 2, 3));

该模块有一个时间转换例程数量庞大,特别是那些三角洲处理。 DateTimeDate::Manip还值得检查。

 

2)

perldoc POSIX为函数mktime()

它将帮助您转换的日期和时间到一个简单的数字,这是UNIX时间(1月1日以来的秒数数字,1970年,我相信)。然后,只需减去3(天)24倍(在1小时)60倍(以1小时分钟)60次(在一分钟秒),或259200从这个数字秒,并使用localtime()转换的秒数返回一个字符串表示形式。

这可能是最好的解决办法*,因为它将处理的月份和年份自动改变。任何其他的解决方案将可能最终会被更多的检查后,看看我们跑出天1个月,或跑了几个月,一年保理复杂。

 


编辑:*之外的回顾CPAN

 

3)

日期::计算器可用于这些计算:

#!/usr/bin/perl

use strict;
use warnings;
use Date::Calc qw(Add_Delta_Days);

my ( $yyyy, $mm, $dd ) = ( 2009, 9, 2 );
my @date = Add_Delta_Days( $yyyy, $mm, $dd, -3 );
print join( '-', @date );

 

4)

有这么多的选择,这是适度的尴尬。它在某种程度上取决于什么其他计算,您可能需要在未来,以及是否和时区的时间永远是一个因素,以及类似的东西。

你可以看看这些任何

  • Date::Calc
  • Date::Manip
  • DateTime::* -又见datetime.perl.org尽管该网站似乎没有作出回应关于2009 - 09 - 01T22 :30 - 07:00)

是名副其实的三(套)模块。我建议DateTime或为了简单 - 但是,如果你将需要获得在将来,憧憬模块可能会更好。

 

5)

整洁的事情了mktime是它处理任何偏移时间。它使用1月= 0;和2009年= 109这个计划。因此,印月 - 1和全年 - 1900。

use POSIX qw<mktime>;

my ( $year, $month, $day ) = split &apos;-&apos;, $date;
my $three_day_prior = mktime( 0, 0, 0, $day - 3, $month - 1, $year - 1900 );

mktime( 0, 0, 0, 0, $month, $year - 1900 ); 可用于发现该月的最后一天为好。你只去一天下个月0。

 

6)

这是简单, Date::Simple

C:/>perl -MDate::Simple=today -e "print today()-3"
2009年8月30日

 

7)

DateTime的是典型的现代方式的Perl处理日期:

use DateTime;

my ($year, $month, $day) = split &apos;-&apos;, &apos;2009-09-01&apos;;

my $date = DateTime->new( year => $year, month => $month, day => $day );

$date->subtract( days => 3 );

# $date is now three days earlier (2009-08-29T00:00:00)

/ I3az /

 

---------------------------------------------------------------------------------

Simply using POSIX::mktime with tricks.

sample code:

  1. use POSIX qw(mktime);

  2. my $date = ''200907;
  3. my ($year, $month) = (substr($date,0,4), substr($date,4,2));

  4. # mktime(sec, min, hour, mday, mon, year, wday=0, yday=0, isdst=-1)
  5. # perldoc POSIX  -- to see more details

  6. # get time from the last day
  7. my $lastday_time = POSIX::mktime(
  8.     0, 0, 0,
  9.     0, $month, $year-1900,    # Tricks: set mday=0, mon (0=Jan.,,,11=Dec.)
  10.     0, 0, -1
  11. );

  12. # get the string of the lasytday
  13. my $lastday = (localtime($lastday_time))[3];
  14. print $year. $month. $lastday. "/n";

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值