如何使用本地时间进行调整以获取昨天的日期?
use strict; sub spGetCurrentDateTime; print spGetCurrentDateTime; sub spGetCurrentDateTime { my ($sec,$min,$hour,$mday,$mon,$year) = localtime(); my @abbr = qw( Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec ); my $currentDateTime = sprintf "%s %02d %4d",$abbr[$mon],$year+1900; #Returns => 'Aug 17 2010' return $currentDateTime; }
〜
解决方法
DST问题可以通过从今天中午3600秒而不是当前时间来解决:
#!/usr/bin/perl use strict; use warnings; use Time::Local; sub spGetYesterdaysDate; print spGetYesterdaysDate; sub spGetYesterdaysDate { my ($sec,$year) = localtime(); my $yesterday_midday=timelocal(0,12,$year) - 24*60*60; ($sec,$year) = localtime($yesterday_midday); my @abbr = qw( Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec ); my $YesterdaysDate = sprintf "%s %02d %4d",$year+1900; return $YesterdaysDate; }
鉴于Chas建议的strftime解决方案的“未指定”记录的行为,如果您无法在多个平台上测试预期但不保证的结果,则此方法可能会更好.