Skip to content
This repository was archived by the owner on May 16, 2018. It is now read-only.

Fix DateObject->subMonth() bug related to non-UTC timezones #693

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 24 additions & 60 deletions library/Zend/Date.php
Original file line number Diff line number Diff line change
Expand Up @@ -1274,6 +1274,20 @@ private function _assign($calc, $date, $comp = 0, $dst = false)
return $this->getUnixTimestamp();
}

private function _getExtendMonthOffset($hour, $minute, $second, $month, $day, $year)
{
if (self::$_options['extend_month'] === false) {
$tempDate = new Zend_Date($this->mktime($hour, $minute, $second, $month, $day, $year, false));
$tempDate->setTimezone($this->getTimezone());
$mday = (int)$tempDate->get('d');

if ($mday != $day) {
return ($mday < $day) ? -$mday : ($mday - $day);
}
}

return 0;
}

/**
* Calculates the date or object
Expand Down Expand Up @@ -1635,21 +1649,11 @@ private function _calculate($calc, $date, $part, $locale)
if ($calc == 'add') {
$date += $found;
$calc = 'set';
if (self::$_options['extend_month'] == false) {
$parts = $this->getDateParts($this->mktime($hour, $minute, $second, $date, $day, $year, false));
if ($parts['mday'] != $day) {
$fixday = ($parts['mday'] < $day) ? -$parts['mday'] : ($parts['mday'] - $day);
}
}
$fixday = $this->_getExtendMonthOffset($hour, $minute, $second, $date, $day, $year);
} else if ($calc == 'sub') {
$date = $month - $found;
$calc = 'set';
if (self::$_options['extend_month'] == false) {
$parts = $this->getDateParts($this->mktime($hour, $minute, $second, $date, $day, $year, false));
if ($parts['mday'] != $day) {
$fixday = ($parts['mday'] < $day) ? -$parts['mday'] : ($parts['mday'] - $day);
}
}
$fixday = $this->_getExtendMonthOffset($hour, $minute, $second, $date, $day, $year);
}
return $this->_assign($calc, $this->mktime(0, 0, 0, $date, $day + $fixday, $year, true),
$this->mktime(0, 0, 0, $month, $day, $year, true), $hour);
Expand All @@ -1666,21 +1670,11 @@ private function _calculate($calc, $date, $part, $locale)
if ($calc == 'add') {
$date += $month;
$calc = 'set';
if (self::$_options['extend_month'] == false) {
$parts = $this->getDateParts($this->mktime($hour, $minute, $second, $date, $day, $year, false));
if ($parts['mday'] != $day) {
$fixday = ($parts['mday'] < $day) ? -$parts['mday'] : ($parts['mday'] - $day);
}
}
$fixday = $this->_getExtendMonthOffset($hour, $minute, $second, $date, $day, $year);
} else if ($calc == 'sub') {
$date = $month - $date;
$calc = 'set';
if (self::$_options['extend_month'] == false) {
$parts = $this->getDateParts($this->mktime($hour, $minute, $second, $date, $day, $year, false));
if ($parts['mday'] != $day) {
$fixday = ($parts['mday'] < $day) ? -$parts['mday'] : ($parts['mday'] - $day);
}
}
$fixday = $this->_getExtendMonthOffset($hour, $minute, $second, $date, $day, $year);
}
return $this->_assign($calc, $this->mktime(0, 0, 0, $date, $day + $fixday, $year, true),
$this->mktime(0, 0, 0, $month, $day, $year, true), $hour);
Expand Down Expand Up @@ -1708,21 +1702,11 @@ private function _calculate($calc, $date, $part, $locale)
if ($calc == 'add') {
$date += $found;
$calc = 'set';
if (self::$_options['extend_month'] === false) {
$parts = $this->getDateParts($this->mktime($hour, $minute, $second, $date, $day, $year, false));
if ($parts['mday'] != $day) {
$fixday = ($parts['mday'] < $day) ? -$parts['mday'] : ($parts['mday'] - $day);
}
}
$fixday = $this->_getExtendMonthOffset($hour, $minute, $second, $date, $day, $year);
} else if ($calc == 'sub') {
$date = $month - $found;
$calc = 'set';
if (self::$_options['extend_month'] === false) {
$parts = $this->getDateParts($this->mktime($hour, $minute, $second, $date, $day, $year, false));
if ($parts['mday'] != $day) {
$fixday = ($parts['mday'] < $day) ? -$parts['mday'] : ($parts['mday'] - $day);
}
}
$fixday = $this->_getExtendMonthOffset($hour, $minute, $second, $date, $day, $year);
}
return $this->_assign($calc, $this->mktime(0, 0, 0, $date, $day + $fixday, $year, true),
$this->mktime(0, 0, 0, $month, $day, $year, true), $hour);
Expand All @@ -1739,21 +1723,11 @@ private function _calculate($calc, $date, $part, $locale)
if ($calc === 'add') {
$date += $month;
$calc = 'set';
if (self::$_options['extend_month'] === false) {
$parts = $this->getDateParts($this->mktime($hour, $minute, $second, $date, $day, $year, false));
if ($parts['mday'] != $day) {
$fixday = ($parts['mday'] < $day) ? -$parts['mday'] : ($parts['mday'] - $day);
}
}
$fixday = $this->_getExtendMonthOffset($hour, $minute, $second, $date, $day, $year);
} else if ($calc === 'sub') {
$date = $month - $date;
$calc = 'set';
if (self::$_options['extend_month'] === false) {
$parts = $this->getDateParts($this->mktime($hour, $minute, $second, $date, $day, $year, false));
if ($parts['mday'] != $day) {
$fixday = ($parts['mday'] < $day) ? -$parts['mday'] : ($parts['mday'] - $day);
}
}
$fixday = $this->_getExtendMonthOffset($hour, $minute, $second, $date, $day, $year);
}

return $this->_assign($calc, $this->mktime(0, 0, 0, $date, $day + $fixday, $year, true),
Expand Down Expand Up @@ -1787,21 +1761,11 @@ private function _calculate($calc, $date, $part, $locale)
if ($calc === 'add') {
$date += $found;
$calc = 'set';
if (self::$_options['extend_month'] === false) {
$parts = $this->getDateParts($this->mktime($hour, $minute, $second, $date, $day, $year, false));
if ($parts['mday'] != $day) {
$fixday = ($parts['mday'] < $day) ? -$parts['mday'] : ($parts['mday'] - $day);
}
}
$fixday = $this->_getExtendMonthOffset($hour, $minute, $second, $date, $day, $year);
} else if ($calc === 'sub') {
$date = $month - $found;
$calc = 'set';
if (self::$_options['extend_month'] === false) {
$parts = $this->getDateParts($this->mktime($hour, $minute, $second, $date, $day, $year, false));
if ($parts['mday'] != $day) {
$fixday = ($parts['mday'] < $day) ? -$parts['mday'] : ($parts['mday'] - $day);
}
}
$fixday = $this->_getExtendMonthOffset($hour, $minute, $second, $date, $day, $year);
}
return $this->_assign($calc, $this->mktime(0, 0, 0, $date, $day + $fixday, $year, true),
$this->mktime(0, 0, 0, $month, $day, $year, true), $hour);
Expand Down
11 changes: 11 additions & 0 deletions tests/Zend/DateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4380,6 +4380,17 @@ public function testSubMonth()
$this->assertSame('2019-12-01T04:00:00+05:00', $date->get(Zend_Date::W3C));
$date->subMonth(12);
$this->assertSame('2018-12-01T04:00:00+05:00', $date->get(Zend_Date::W3C));

$oldzone = date_default_timezone_get();
date_default_timezone_set('UTC');

// "2016-05-01T00:00:00+04:00"
$date = new Zend_Date(1462046400, $locale);
$date->setTimezone('Asia/Muscat');

$date->subMonth(1);
$this->assertSame('2016-04-01T00:00:00+04:00', $date->get(Zend_Date::W3C));
date_default_timezone_set($oldzone);
}

/**
Expand Down