如何使用当地时间获取昨天的日期?
如何调整它以使用当地时间获取昨天的日期?
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], $mday, $year+1900; #Returns => 'Aug 17 2010'
return $currentDateTime;
}
~
How do I tweak this to get yesterday's date using localtime?
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], $mday, $year+1900; #Returns => 'Aug 17 2010'
return $currentDateTime;
}
~
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
第二行的表达式返回一个
DateTime
对象。The expression on the second line returns a
DateTime
object.尽管从当前时间减去一天的秒数很诱人,但有时这会产生错误的答案(闰秒、夏令时,以及可能的其他)。我发现让
strftime
(在 Perl 5 核心模块中可用POSIX
)为我处理所有这些事情。As tempting as it is to just subtract a day's worth of seconds from the current time, there are times when this will yield the wrong answer (leap seconds, DST, and possibly others). I find it easier to just let
strftime
(available in the Perl 5 core modulePOSIX
) take care of all of that for me.DST 问题可以通过从今天中午开始计算 3600 秒而不是当前时间来解决:
鉴于 Chas 建议的 strftime 解决方案的“未指定”记录行为,如果您无法测试,这种方法可能会更好跨多个平台的预期但不保证的结果。
The DST problem can be worked around by taking 3600s from midday today instead of the current time:
In light of the "unspecified" documented behaviour of the strftime solution suggested by Chas, this approach might be better if you're not able to test for expected-but-not-guaranteed results across multiple platforms.
使用 Time::Piece。
请注意,在某些临界情况下,例如夏令时开始,这可能会出错。
在这种情况下,以下版本的行为确实正确:
或者,您可以使用 DateTime 模块,如另一个答案中所述。但这不是核心模块。
use Time::Piece.
Note that this can go wrong in certain borderline cases, such as the start of daylight saving time.
The following version does behave correct in such cases:
Alternatively, you can use the DateTime module as described in a different answer. That is not a core module, though.
大多数用户建议的解决方案是错误的!
您能做的最糟糕的事情就是假设1天= 86400秒。
示例:时区是 America/New_York,日期是 Mon Apr 3 00:30:00 2006
timelocal 为我们提供 1144038600
哎呀!
正确且唯一的解决方案是让系统函数规范化值
,或者让日期时间框架
(DateTime、Class::Date 等)
执行相同的操作。就是这样。
Solution suggested by most users is wrong!
The worst thing you can do is to assume that 1 day = 86400 seconds.
Example: Timezone is America/New_York, date is Mon Apr 3 00:30:00 2006
timelocal gives us 1144038600
oops!
The right and the only solution is to let system function normalize values
Or let datetime frameworks
(DateTime, Class::Date, etc)
do the same.That's it.
我的 $yesterday = time();
$yesterday = $yesterday - (24*60*60);
24 表示 24 小时,60 表示小时 60 分钟,60 表示分钟 60 秒
time()
将返回实际时间戳,246060 将删除一天的秒数之后只需执行以下操作:
当地时间($昨天);
my $yesterday = time();
$yesterday = $yesterday - (24*60*60);
24 as 24 hours, 60 as 60 minutes in hour and 60 as 60 seconds in minute
time()
will return actual timestamp, and 246060 will remove seconds for exactly one dayAfter this simply do:
localtime($yesterday);