2014-08-23 09:47
浏览 103

PHP DateTime :: format('I')不准确的bug?

I'm wondering if this is a bug, or if I missed something.

While creating a method to isolate the timestamp when DST switches from on to off and vice versa, I noticed a potential bug with DateTime::format('I'). When DST ends and the time is switched backwards one hour, DateTime::format('I') reports the switch one hour too late.

In the 'America/Montreal' timezone, we can look at these timestamps when DST ends:

1414904400, Sun, 02 Nov 2014 01:00:00 -0400 (DST on)
1414908000, Sun, 02 Nov 2014 01:00:00 -0500 (DST off)
1414911600, Sun, 02 Nov 2014 02:00:00 -0500 (DST off)

Notice the DST ends on the second timestamp when 01:00:00 is repeated.

But, using DateTime::format('I') the ending is reported on the third timestamp.

$timezone = new DateTimezone('America/Montreal');
$datetime = new DateTime('now', $timezone);
echo $datetime->format('I');
echo $datetime->format('I');
echo $datetime->format('I');

returns 110 (should be 100)

If we use date('I') we get the correct answer.

echo date('I', 1414904400);
echo date('I', 1414908000);
echo date('I', 1414911600);

returns 100 (as expected)

When DST begins DateTime::format('I') works fine, it only seems to fail when DST ends.

Also, I have tested this for a few different years in a few different timezones, but I have no idea of the depth of this potential bug. Much more testing is needed.

Anyone know what this is?

图片转代码服务由CSDN问答提供 功能建议


创建一种方法来隔离DST从开启切换到关闭时的时间戳,反之亦然,我发现了DateTime :: format('I')的潜在错误。 当DST结束并且时间向后切换一小时时,DateTime :: format('I')报告开关太晚一小时。

在'America / Montreal'时区,我们 在DST结束时可以查看这些时间戳:

1414904400,Sun,02 Nov 2014 01:00:00 -0400(DST on)
1414908000,Sun,02 Nov 2014 01 :00:00-0500(DST关闭)
1414911600,Sun,2014年11月2日02:00:00 -0500(DST关闭)

注意DST在第二天结束 重复01:00:00时的时间戳。

但是,使用DateTime :: format('I'),结尾将在第三个时间戳上报告。

  $ timezone = new DateTimezone('America / Montreal'); 
 $ datetime = new DateTime('now',$ timezone); 
 $ datetime->  setTimestamp(1414904400); 
echo $ datetime-> format('I'); 
 $ datetime-> setTimestamp(1414908000); 
echo $ datetime-> format('I'); 
 $ datetime-  > setTimestamp(1414911600); 
echo $ datetime-> format('I'); 

返回110(应为100) \ n


  date_default_timezone_set('America / Montreal'); 
echo date('I  ',1414904400); 
echo date('I',1414908000); 
echo date('I',1414911600); 


当DST开始时,DateTime :: format('I')工作正常,DST结束时似乎只会失败。

另外,我在几个不同的时区测试了几年,但我不知道这个潜在错误的深度。 需要进行更多的测试。


  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongmei9203 2014-08-23 17:33

    While creating a method to isolate the timestamp when DST switches from on to off and vice versa ...

    That method already exists as DateTimeZone::GetTransitions. Documentation here.

    You may also be interested in the related problem of determining whether a particular date/time is within a transition period, which is described in this answer.

    点赞 打赏 评论