com php-src: Fixed bug #45543: DateTime::setTimezone can not set timezones without ID.: ext/date/lib/timelib.h ext/date/lib/timelib_stru cts.h
ext/date/lib/unixtime2tm.c ext/date/php_date .c ext/date/php_date.h ext/date/tests/bug4554 3.phpt

From: Date: Sun, 26 Jan 2014 13:01:58 +0000
Subject: com php-src: Fixed bug #45543: DateTime::setTimezone can not set timezones without ID.: ext/date/lib/timelib.h ext/date/lib/timelib_stru cts.h
ext/date/lib/unixtime2tm.c ext/date/php_date .c ext/date/php_date.h ext/date/tests/bug4554 3.phpt
Groups: php.cvs 
Request: Send a blank email to php-cvs+get-75795@lists.php.net to get a copy of this message
Commit: 22dba2f5f3211efe6c3b9bb24734c811ca64c68c Author: Derick Rethans <github@derickrethans.nl> Sun, 26 Jan 2014 13:58:13 +0100 Parents: 4e308abf28d542de0f1251b5e5c523d4701af4b4 Branches: PHP-5.4 Link: http://git.php.net/?p=php-src.git;a=commitdiff;h=22dba2f5f3211efe6c3b9bb24734c811ca64c68c Log: Fixed bug #45543: DateTime::setTimezone can not set timezones without ID. Bugs: https://bugs.php.net/45543 Changed paths: M ext/date/lib/timelib.h M ext/date/lib/timelib_structs.h M ext/date/lib/unixtime2tm.c M ext/date/php_date.c M ext/date/php_date.h A ext/date/tests/bug45543.phpt Diff: diff --git a/ext/date/lib/timelib.h b/ext/date/lib/timelib..h index 0396e62..533c886 100644 --- a/ext/date/lib/timelib.h +++ b/ext/date/lib/timelib.h @@ -91,6 +91,8 @@ int timelib_apply_localtime(timelib_time *t, unsigned int localtime); void timelib_unixtime2gmt(timelib_time* tm, timelib_sll ts); void timelib_unixtime2local(timelib_time *tm, timelib_sll ts); void timelib_update_from_sse(timelib_time *tm); +void timelib_set_timezone_from_offset(timelib_time *t, timelib_sll utc_offset); +void timelib_set_timezone_from_abbr(timelib_time *t, timelib_abbr_info abbr_info); void timelib_set_timezone(timelib_time *t, timelib_tzinfo *tz); /* From parse_tz.c */ diff --git a/ext/date/lib/timelib_structs.h b/ext/date/lib/timelib_structs.h index 2648c52..5185e7b 100644 --- a/ext/date/lib/timelib_structs.h +++ b/ext/date/lib/timelib_structs.h @@ -172,6 +172,12 @@ typedef struct timelib_time { * 2 TimeZone abbreviation */ } timelib_time; +typedef struct timelib_abbr_info { + timelib_sll utc_offset; + char *abbr; + int dst; +} timelib_abbr_info; + typedef struct timelib_error_message { int position; char character; diff --git a/ext/date/lib/unixtime2tm.c b/ext/date/lib/unixtime2tm.c index eefbaa8..9870313 100644 --- a/ext/date/lib/unixtime2tm.c +++ b/ext/date/lib/unixtime2tm.c @@ -214,6 +214,34 @@ void timelib_unixtime2local(timelib_time *tm, timelib_sll ts) tm->have_zone = 1; } +void timelib_set_timezone_from_offset(timelib_time *t, timelib_sll utc_offset) +{ + if (t->tz_abbr) { + free(t->tz_abbr); + } + t->tz_abbr = NULL; + + t->z = utc_offset; + t->have_zone = 1; + t->zone_type = TIMELIB_ZONETYPE_OFFSET; + t->dst = 0; + t->tz_info = NULL; +} + +void timelib_set_timezone_from_abbr(timelib_time *t, timelib_abbr_info abbr_info) +{ + if (t->tz_abbr) { + free(t->tz_abbr); + } + t->tz_abbr = strdup(abbr_info..abbr); + + t->z = abbr_info.utc_offset; + t->have_zone = 1; + t->zone_type = TIMELIB_ZONETYPE_ABBR; + t->dst = abbr_info.dst; + t->tz_info = NULL; +} + void timelib_set_timezone(timelib_time *t, timelib_tzinfo *tz) { timelib_time_offset *gmt_offset; diff --git a/ext/date/php_date.c b/ext/date/php_date.c index 1655057..c056633 100644 --- a/ext/date/php_date.c +++ b/ext/date/php_date.c @@ -2992,11 +2992,18 @@ PHP_FUNCTION(date_timezone_set) dateobj = (php_date_obj *) zend_object_store_get_object(object TSRMLS_CC); DATE_CHECK_INITIALIZED(dateobj->time, DateTime); tzobj = (php_timezone_obj *) zend_object_store_get_object(timezone_object TSRMLS_CC); - if (tzobj->type != TIMELIB_ZONETYPE_ID) { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "Can only do this for zones with ID for now"); - return; + + switch (tzobj->type) { + case TIMELIB_ZONETYPE_OFFSET: + timelib_set_timezone_from_offset(dateobj->time, tzobj->tzi.utc_offset); + break; + case TIMELIB_ZONETYPE_ABBR: + timelib_set_timezone_from_abbr(dateobj->time, tzobj->tzi.z); + break; + case TIMELIB_ZONETYPE_ID: + timelib_set_timezone(dateobj->time, tzobj->tzi.tz); + break; } - timelib_set_timezone(dateobj->time, tzobj->tzi.tz); timelib_unixtime2local(dateobj->time, dateobj->time->sse); RETURN_ZVAL(object, 1, 0); diff --git a/ext/date/php_date.h b/ext/date/php_date.h index 1d6a94d..4ce730b 100644 --- a/ext/date/php_date.h +++ b/ext/date/php_date.h @@ -122,14 +122,9 @@ struct _php_timezone_obj { int initialized; int type; union { - timelib_tzinfo *tz; /* TIMELIB_ZONETYPE_ID; */ - timelib_sll utc_offset; /* TIMELIB_ZONETYPE_OFFSET */ - struct /* TIMELIB_ZONETYPE_ABBR */ - { - timelib_sll utc_offset; - char *abbr; - int dst; - } z; + timelib_tzinfo *tz; /* TIMELIB_ZONETYPE_ID */ + timelib_sll utc_offset; /* TIMELIB_ZONETYPE_OFFSET */ + timelib_abbr_info z; /* TIMELIB_ZONETYPE_ABBR */ } tzi; }; diff --git a/ext/date/tests/bug45543.phpt b/ext/date/tests/bug45543.phpt new file mode 100644 index 0000000..8e36e62 --- /dev/null +++ b/ext/date/tests/bug45543.phpt @@ -0,0 +1,34 @@ +--TEST-- +Test for bug #45543: DateTime::setTimezone can not set timezones without ID. +--INI-- +date.timezone=UTC +--FILE-- +<?php +$test_dates = array( + '2008-01-01 12:00:00 PDT', + '2008-01-01 12:00:00 +02:00', +); + +foreach ($test_dates as $test_date) +{ + $d1 = new DateTime($test_date); + $d2 = new DateTime('2008-01-01 12:00:00 UTC'); + echo $d1->format(DATE_ISO8601), PHP_EOL; + echo $d2->format(DATE_ISO8601), PHP_EOL; + $tz = $d1->getTimeZone(); + $d2->setTimeZone($tz); + echo $d1->format(DATE_ISO8601), PHP_EOL; + echo $d2->format(DATE_ISO8601), PHP_EOL; + echo PHP_EOL; +} +--EXPECT-- +2008-01-01T12:00:00-0700 +2008-01-01T12:00:00+0000 +2008-01-01T12:00:00-0700 +2008-01-01T05:00:00-0700 + +2008-01-01T12:00:00+0200 +2008-01-01T12:00:00+0000 +2008-01-01T12:00:00+0200 +2008-01-01T14:00:00+0200 +

« previous php.cvs (#75795) next »