php-src/ext/intl/tests/timezone_toDateTimeZone_basic.phpt
Gustavo André dos Santos Lopes 4cfd9995da Added IntlTimeZone::fromDateTimeZone() and ::toDateTimeZone.
IntlTimeZone::fromDateTimeZone(DateTimeZone $dtz) converts from an
ext/date TimeZone to an IntlTimeZone. The conversion is done by feeding
the time zone name (essentially what would be given by
DateTimeZone::getName()) to ICU's TimeZone::createTimeZone except if it's
an offset time zone. In that case, the offset is read from the ext/date
time zone object structure and an appopriate id (of the form
GMT<+|-><HH:MM>) is given to ICU's TimeZone::createTimeZone. Not all
ext/date time zones are recognized for ICU. For instance, WEST is not.
Note that these kind of abbreviations, as far as I can tell, can only be
created via ext/date DateTime, not directly through DateTimeZone's
constructor.

For IntlTimeZone::toDateTimeZone(), the behavior is symmetrical.
We instantiate a DateTimeZone and then call its constructor if we don't
have an offset time zone, otherwise we mess with its structure. If the
timezone is not valid for ext/date, then we allow the exception of
DateTimeZone constructor to propagate.
2012-05-17 17:23:51 +02:00

39 lines
944 B
PHP

--TEST--
IntlTimeZone::toDateTimeZone(): basic test
--SKIPIF--
<?php
if (!extension_loaded('intl'))
die('skip intl extension not enabled');
--FILE--
<?php
ini_set("intl.error_level", E_WARNING);
ini_set("intl.default_locale", "nl");
date_default_timezone_set('Europe/Lisbon');
function do_test(IntlTimeZone $tz, $proc = false) {
var_dump($tz->getID(), $tz->getRawOffset());
if (!$proc)
$dtz = $tz->toDateTimeZone();
else
$dtz = intltz_to_date_time_zone($tz);
var_dump($dtz->getName(), $dtz->getOffset(new DateTime('2012-01-01 00:00:00')));
}
do_test(IntlTimeZone::createTimeZone('CET'));
do_test(IntlTimeZone::createTimeZone('Europe/Amsterdam'));
do_test(IntlTimeZone::createTimeZone('GMT+0405'), true);
--EXPECTF--
string(3) "CET"
int(3600000)
string(13) "Europe/Berlin"
int(3600)
string(16) "Europe/Amsterdam"
int(3600000)
string(16) "Europe/Amsterdam"
int(3600)
string(%s) "GMT+04%s5"
int(14700000)
string(6) "+04:05"
int(14700)