简体   繁体   English

如何检查 unix 时间提前 1 天

[英]How to check the unix time is 1 day older

How to check the UNIX time is 1 day older如何检查 UNIX 时间提前 1 天

for ex: UNIX time is 1308715355例如:UNIX 时间为 1308715355

I want to find that that time 1 day older or not with now我想找到那个时间是否比现在大 1 天

If you consider 2011-06-27T01:05 to be less than one day older than 2011-06-28T01:00,如果您认为 2011-06-27T01:05 比 2011-06-28T01:00 早不到一天,

use DateTime;

my $epoch = '1308715355';

my $dt_ref = DateTime->from_epoch(epoch => $epoch, time_zone => 'local');
my $dt_now = DateTime->now(time_zone => 'local');

$dt_ref->add(days => 1);
if ($dt_ref <= $dt_now) {
   # At least one day old.
}

If you consider 2011-06-27 to be one day older than 2011-06-28 regardless of times of day,如果您认为 2011-06-27 比 2011-06-28 大一天,无论一天中的什么时间,

use DateTime;

my $epoch = '1308715355';

my $dt_ref = DateTime->from_epoch(epoch => $epoch, time_zone => 'local')
   ->truncate( to => 'days' );
my $dt_now = DateTime->today(time_zone => 'local');

$dt_ref->add(days => 1);
if ($dt_ref <= $dt_now) {
   # At least one day old.
}

Upd : Grammar error fix: s/older/older than/更新:语法错误修复:s/older/older than/

> date -dtomorrow +%s; date +%s
1309366474
1309280074

This should handle, BTW, the DST problem that evan mentioned.顺便说一句,这应该处理埃文提到的 DST 问题。

You didn't mention your desired solution space, so hopefully this would suffice.你没有提到你想要的解决方案空间,所以希望这就足够了。

Comparing with the number of seconds for a day is generally adequate (60 * 60 * 24 = 86400).与一天的秒数相比通常就足够了(60 * 60 * 24 = 86400)。 However, if this always needs to work exactly, you need to take into account when you switch to and from DST.但是,如果这始终需要准确工作,则在切换到 DST 和从 DST 切换时需要考虑到这一点。 Those days are 23 & 25 hours.那些日子是 23 和 25 小时。

Most people tend to overlook daylight savings time.大多数人倾向于忽略夏令时。 This can cause a lot of issues if you need precision when dealing with time.如果您在处理时间时需要精确,这可能会导致很多问题。

use strict;
use warnings;
use DateTime;

my $epoch = '1308715355';

my $dt = DateTime->from_epoch(epoch => $epoch, time_zone => 'local')
                 ->add(days => 1);
print $dt; # '2011-06-22T23:02:35' -- for *my* time zone (CDT)
print $dt->epoch; # '1308801755'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM