Skip to content

Commit

Permalink
[jan] Add Horde_Date::getTimezoneAlias().
Browse files Browse the repository at this point in the history
  • Loading branch information
yunosh committed Mar 15, 2016
1 parent df37acc commit 2905198
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 10 deletions.
57 changes: 53 additions & 4 deletions framework/Date/lib/Horde/Date.php
Expand Up @@ -243,6 +243,23 @@ class Horde_Date
'E. Europe' => 'Asia/Nicosia',
);

/**
* These aliases map timezone abbreviations to those understood by PHP.
*
* @todo This list better moves somewhere else
* @see getTimezoneAlias()
* @var array
*/
protected static $_timezoneAbbreviations = array();

/**
* A list of (Olson) timezone identifiers understood by PHP.
*
* @todo This list better moves somewhere else
* @see getTimezoneAlias()
* @var array
*/
protected static $_timezoneIdentifiers = array();

/**
* Default format for __toString()
Expand Down Expand Up @@ -646,6 +663,41 @@ public function sub($factor)
return $this->add($factor);
}

/**
* Returns the normalized (Olson) timezone name of a timezone alias.
*
* We currently support Windows and Lotus timezone names, and timezone
* abbreviations.
*
* @since Horde_Date 2.3.0
*
* @param string $timezone Some timezone alias.
*
* @return string The Olson timezone name, or the original value, if no
* alias found.
*/
public static function getTimezoneAlias($timezone)
{
if (empty(self::$_timezoneIdentifiers)) {
self::$_timezoneIdentifiers = array_flip(DateTimeZone::listIdentifiers());
}
if (isset(self::$_timezoneIdentifiers[$timezone])) {
return $timezone;
}
/* Workaround for standard cases of bug #11688 */
if (isset(self::$_timezoneAliases[$timezone])) {
$timezone = self::$_timezoneAliases[$timezone];
}
if (empty(self::$_timezoneAbbreviations)) {
self::$_timezoneAbbreviations = DateTimeZone::listAbbreviations();
}
$lower = Horde_String::lower($timezone);
if (isset(self::$_timezoneAbbreviations[$lower])) {
$timezone = reset(self::$_timezoneAbbreviations[$lower])['timezone_id'];
}
return $timezone;
}

/**
* Converts this object to a different timezone.
*
Expand All @@ -656,10 +708,7 @@ public function sub($factor)
public function setTimezone($timezone)
{
$date = $this->toDateTime();
/* Workaround for standard cases of bug #11688 */
if (array_key_exists($timezone, self::$_timezoneAliases)) {
$timezone = self::$_timezoneAliases[$timezone];
}
$timezone = self::getTimezoneAlias($timezone);
$date->setTimezone(new DateTimeZone($timezone));
$this->_timezone = $timezone;
$this->_year = (int)$date->format('Y');
Expand Down
14 changes: 8 additions & 6 deletions framework/Date/package.xml
Expand Up @@ -16,17 +16,18 @@
<email>chuck@horde.org</email>
<active>yes</active>
</lead>
<date>2016-02-01</date>
<date>2016-03-15</date>
<version>
<release>2.2.1</release>
<api>2.2.0</api>
<release>2.3.0</release>
<api>2.3.0</api>
</version>
<stability>
<release>stable</release>
<api>stable</api>
</stability>
<license uri="http://www.horde.org/licenses/lgpl21">LGPL-2.1</license>
<notes>
* [jan] Add Horde_Date::getTimezoneAlias().
* [jan] Update Greek translation (Limperis Antonis &lt;limperis@cti.gr&gt;).
</notes>
<contents>
Expand Down Expand Up @@ -1066,14 +1067,15 @@
</release>
<release>
<version>
<release>2.2.1</release>
<api>2.2.0</api></version>
<release>2.3.0</release>
<api>2.3.0</api></version>
<stability>
<release>stable</release>
<api>stable</api></stability>
<date>2016-02-01</date>
<date>2016-03-15</date>
<license uri="http://www.horde.org/licenses/lgpl21">LGPL-2.1</license>
<notes>
* [jan] Add Horde_Date::getTimezoneAlias().
* [jan] Update Greek translation (Limperis Antonis &lt;limperis@cti.gr&gt;).
</notes>
</release>
Expand Down
31 changes: 31 additions & 0 deletions framework/Date/test/Horde/Date/DateTest.php
Expand Up @@ -281,6 +281,26 @@ public function testStrftimeUnsupported()
$date->strftime('%a'));
}

public function testGetTimezoneAlias()
{
$this->assertEquals(
'Europe/Berlin',
Horde_Date::getTimezoneAlias('W. Europe Standard Time')
);
$this->assertEquals(
'Europe/Berlin',
Horde_Date::getTimezoneAlias('W. Europe')
);
$this->assertEquals(
'Europe/Berlin',
Horde_Date::getTimezoneAlias('CET')
);
$this->assertEquals(
'UTC',
Horde_Date::getTimezoneAlias('UTC')
);
}

public function testSetTimezone()
{
$oldTimezone = date_default_timezone_get();
Expand All @@ -298,6 +318,17 @@ public function testSetTimezone()
$date->setTimezone('Europe/Berlin');
$this->assertEquals('2001-02-03 05:05:06', (string)$date);

$date->setTimezone('W. Europe');
$this->assertEquals('2001-02-03 05:05:06', (string)$date);

$date->setTimezone('CET');
$this->assertEquals('2001-02-03 05:05:06', (string)$date);

$date = new Horde_Date('20010203040506', 'CET');
$this->assertEquals('2001-02-03 04:05:06', (string)$date);
$date->setTimezone('Europe/Berlin');
$this->assertEquals('2001-02-03 04:05:06', (string)$date);

date_default_timezone_set($oldTimezone);
}

Expand Down

0 comments on commit 2905198

Please sign in to comment.