Skip to content

Commit

Permalink
Zend_Date::setTime fix for DST change zendframework/zf1#682
Browse files Browse the repository at this point in the history
  • Loading branch information
falkenhawk committed Oct 26, 2016
1 parent ea91e9d commit 579f366
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ This package is a part of the Zend Framework 1. Each component was separated and
**Autoloading!** Explicit `require_once` calls in the source code has been commented out to rely on composer autoloading, this reduces the number of included files to a minimum.

**Migration!** Zend Framework 2 has been around for a while now, and migrating all your projects takes a lot of time. Using these packages makes it easier to migrate each component separately. Also, some packages doesn't exist in zf2 (such as the zend-search-lucene), now you can continue using that package without requiring the entire framework.

## Changes after official 1.12.20 release

- Zend_Date::setTime fix for DST change zendframework/zf1#682

## How to use

Expand Down
37 changes: 36 additions & 1 deletion library/Zend/Date.php
Original file line number Diff line number Diff line change
Expand Up @@ -2845,6 +2845,13 @@ private function _time($calc, $time, $format, $locale)
* For example: dd.MMMM.yyTHH:mm' and 'ss sec'-> 10.May.07T25:11 and 44 sec => 1h11min44sec + 1 day
* Returned is the new date object and the existing date is left as it was before
*
* ADDED: Call the function twice to work around the DST bug
* http://zendframework.com/issues/browse/ZF-10584
* https://github.com/zendframework/zf1/issues/682
* http://stackoverflow.com/questions/7181702/work-around-for-zend-date-dst-bug
* http://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
Expand All @@ -2853,7 +2860,35 @@ private function _time($calc, $time, $format, $locale)
*/
public function setTime($time, $format = null, $locale = null)
{
return $this->_time('set', $time, $format, $locale);
//return $this->_time('set', $time, $format, $locale);

// 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
$ret = $this->_time('set', $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
$ret = $this->_time('set', $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 $ret;
}


Expand Down

0 comments on commit 579f366

Please sign in to comment.