解决Zend Date DST错误 [英] Work Around for Zend Date DST Bug
本文介绍了解决Zend Date DST错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在设置穿越DST更改的时间时在Zend_Date
中发现了中的错误 a>.
I discovered a bug in Zend_Date
when setting a time that crosses the DST change.
下面的代码说明了这个问题:
Here's code that illustrates the problem:
date_default_timezone_set('America/New_York');
echo '<pre>';
// DST BEGINS '2012-03-11 02:00:00' - "Spring Forward"
$a = new Zend_Date('2012-03-11 00:00:00', 'yyyy-MM-dd HH:mm:ss');
$a->setTime('04:00:00', 'HH:mm:ss');
echo $a->toString('yyyy-MM-dd HH:mm:ss', 'iso')
. ' // expected: 2012-03-11 04:00:00' . PHP_EOL;
$b = new Zend_Date('2012-03-11 04:00:00', 'yyyy-MM-dd HH:mm:ss');
$b->setTime('00:00:00', 'HH:mm:ss');
echo $b->toString('yyyy-MM-dd HH:mm:ss', 'iso')
. ' // expected: 2012-03-11 00:00:00' . PHP_EOL;
// DST ENDS '2012-11-04 02:00:00' - "Fall Back"
$c = new Zend_Date('2012-11-04 00:00:00', 'yyyy-MM-dd HH:mm:ss');
$c->setTime('04:00:00', 'HH:mm:ss');
echo $c->toString('yyyy-MM-dd HH:mm:ss', 'iso')
. ' // expected: 2012-11-06 04:00:00' . PHP_EOL;
$d = new Zend_Date('2012-11-04 04:00:00', 'yyyy-MM-dd HH:mm:ss');
$d->setTime('00:00:00', 'HH:mm:ss');
echo $d->toString('yyyy-MM-dd HH:mm:ss', 'iso')
. ' // expected: 2012-11-06 00:00:00' . PHP_EOL;
echo '</pre>';
胜利:
2012-03-11 05:00:00 // expected: 2012-03-11 04:00:00
2012-03-10 23:00:00 // expected: 2012-03-11 00:00:00
2012-11-04 03:00:00 // expected: 2012-11-06 04:00:00
2012-11-04 01:00:00 // expected: 2012-11-06 00:00:00
我需要解决这个错误,我很困惑.
I need to work around this bug, and I am stumped.
解决方案
基于Zend_Date
:
Based on the answer from nerdzila, and research on a bug introduced by his solution, I now have this in my sub-classed Zend_Date
:
/**
* Call the function twice to work around the DST bug
* http://zendframework.com/issues/browse/ZF-10584
* https://stackoverflow.com/questions/7181702/work-around-for-zend-date-dst-bug
* https://stackoverflow.com/questions/8593660/zend-date-dst-bug-test-whether-a-date-is-a-time-change-date
* TODO: remove this once the bug is fixed
*
* @param string|integer|array|Zend_Date $time Time to set
* @param string $format OPTIONAL Timeformat for parsing input
* @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
* @return My_Date Provides fluid interface
* @throws Zend_Date_Exception
*/
public function setTime($time, $format = null, $locale = null)
{
// start time zone juggling so that localtime() returns the correct results
$tzOrig = date_default_timezone_get();
date_default_timezone_set($this->getTimezone());
// capture orignal info
$timeInfoOrg = localtime($this->getTimestamp(), true);
// set the time
parent::setTime($time, $format, $locale);
// if the dst has changed, perform workaround
$timeInfoNew = localtime($this->getTimestamp(), true);
if ((0 < $timeInfoOrg['tm_isdst']) != (0 < $timeInfoNew['tm_isdst'])) {
// set the time again
parent::setTime($time, $format, $locale);
// if the day changed, set it back
if ($timeInfoOrg['tm_yday'] != $timeInfoNew['tm_yday']) {
// localtime() year date is zero indexed, add one
$this->setDayOfYear($timeInfoOrg['tm_yday'] + 1);
}
}
// end time zone juggling
date_default_timezone_set($tzOrig);
// fluent
return $this;
}
修复错误后,我就可以将其删除.
I can simply remove it once the bug is fixed.
推荐答案
这是一个不幸的错误.我通过在每个日期两次调用setTime()
来解决此问题:
That's an unfortunate bug. I got around it by calling setTime()
twice for each date:
...
$a->setTime('04:00:00', 'HH:mm:ss');
$a->setTime('04:00:00', 'HH:mm:ss');
...
$b->setTime('00:00:00', 'HH:mm:ss');
$b->setTime('00:00:00', 'HH:mm:ss');
...
$c->setTime('04:00:00', 'HH:mm:ss');
$c->setTime('04:00:00', 'HH:mm:ss');
...
$d->setTime('00:00:00', 'HH:mm:ss');
$d->setTime('00:00:00', 'HH:mm:ss');
...
我的结果:
2012-03-11 04:00:00 // expected: 2012-03-11 04:00:00
2012-03-10 00:00:00 // expected: 2012-03-11 00:00:00
2012-11-04 04:00:00 // expected: 2012-11-04 04:00:00
2012-11-04 00:00:00 // expected: 2012-11-04 00:00:00
这篇关于解决Zend Date DST错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文