Skip to content

Bug 54567 DateTimeZone serialize/unserialize #208

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
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
35 changes: 0 additions & 35 deletions ext/date/lib/parse_date.re
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,6 @@ typedef struct _timelib_relunit {
int multiplier;
} timelib_relunit;

#define HOUR(a) (int)(a * 60)

/* The timezone table. */
const static timelib_tz_lookup_table timelib_timezone_lookup[] = {
#include "timezonemap.h"
Expand Down Expand Up @@ -530,39 +528,6 @@ static timelib_ull timelib_get_unsigned_nr(char **ptr, int max_length)
return dir * timelib_get_nr(ptr, max_length);
}

static long timelib_parse_tz_cor(char **ptr)
{
char *begin = *ptr, *end;
long tmp;

while (isdigit(**ptr) || **ptr == ':') {
++*ptr;
}
end = *ptr;
switch (end - begin) {
case 1:
case 2:
return HOUR(strtol(begin, NULL, 10));
break;
case 3:
case 4:
if (begin[1] == ':') {
tmp = HOUR(strtol(begin, NULL, 10)) + strtol(begin + 2, NULL, 10);
return tmp;
} else if (begin[2] == ':') {
tmp = HOUR(strtol(begin, NULL, 10)) + strtol(begin + 3, NULL, 10);
return tmp;
} else {
tmp = strtol(begin, NULL, 10);
return HOUR(tmp / 100) + tmp % 100;
}
case 5:
tmp = HOUR(strtol(begin, NULL, 10)) + strtol(begin + 3, NULL, 10);
return tmp;
}
return 0;
}

static timelib_sll timelib_lookup_relative_text(char **ptr, int *behavior)
{
char *word;
Expand Down
35 changes: 0 additions & 35 deletions ext/date/lib/parse_iso_intervals.re
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,6 @@ typedef struct Scanner {
int have_end_date;
} Scanner;

#define HOUR(a) (int)(a * 60)

static void add_warning(Scanner *s, char *error)
{
s->errors->warning_count++;
Expand Down Expand Up @@ -176,39 +174,6 @@ static timelib_ull timelib_get_unsigned_nr(char **ptr, int max_length)
return dir * timelib_get_nr(ptr, max_length);
}

static long timelib_parse_tz_cor(char **ptr)
{
char *begin = *ptr, *end;
long tmp;

while (isdigit(**ptr) || **ptr == ':') {
++*ptr;
}
end = *ptr;
switch (end - begin) {
case 1:
case 2:
return HOUR(strtol(begin, NULL, 10));
break;
case 3:
case 4:
if (begin[1] == ':') {
tmp = HOUR(strtol(begin, NULL, 10)) + strtol(begin + 2, NULL, 10);
return tmp;
} else if (begin[2] == ':') {
tmp = HOUR(strtol(begin, NULL, 10)) + strtol(begin + 3, NULL, 10);
return tmp;
} else {
tmp = strtol(begin, NULL, 10);
return HOUR(tmp / 100) + tmp % 100;
}
case 5:
tmp = HOUR(strtol(begin, NULL, 10)) + strtol(begin + 3, NULL, 10);
return tmp;
}
return 0;
}

static void timelib_eat_spaces(char **ptr)
{
while (**ptr == ' ' || **ptr == '\t') {
Expand Down
34 changes: 34 additions & 0 deletions ext/date/lib/timelib.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@

#define TIMELIB_LLABS(y) (y < 0 ? (y * -1) : y)

#define HOUR(a) (int)(a * 60)

timelib_time* timelib_time_ctor(void)
{
timelib_time *t;
Expand Down Expand Up @@ -284,3 +286,35 @@ void timelib_dump_rel_time(timelib_rel_time *d)
printf("\n");
}

long timelib_parse_tz_cor(char **ptr)
{
char *begin = *ptr, *end;
long tmp;

while (isdigit(**ptr) || **ptr == ':') {
++*ptr;
}
end = *ptr;
switch (end - begin) {
case 1:
case 2:
return HOUR(strtol(begin, NULL, 10));
break;
case 3:
case 4:
if (begin[1] == ':') {
tmp = HOUR(strtol(begin, NULL, 10)) + strtol(begin + 2, NULL, 10);
return tmp;
} else if (begin[2] == ':') {
tmp = HOUR(strtol(begin, NULL, 10)) + strtol(begin + 3, NULL, 10);
return tmp;
} else {
tmp = strtol(begin, NULL, 10);
return HOUR(tmp / 100) + tmp % 100;
}
case 5:
tmp = HOUR(strtol(begin, NULL, 10)) + strtol(begin + 3, NULL, 10);
return tmp;
}
return 0;
}
2 changes: 2 additions & 0 deletions ext/date/lib/timelib.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ timelib_time *timelib_parse_from_format(char *format, char *s, int len, timelib_
void timelib_fill_holes(timelib_time *parsed, timelib_time *now, int options);
char *timelib_timezone_id_from_abbr(const char *abbr, long gmtoffset, int isdst);
const timelib_tz_lookup_table *timelib_timezone_abbreviations_list(void);
long timelib_parse_tz_cor(char**);

/* From parse_iso_intervals.re */
void timelib_strtointerval(char *s, int len,
Expand Down Expand Up @@ -129,6 +130,7 @@ void timelib_dump_date(timelib_time *d, int options);
void timelib_dump_rel_time(timelib_rel_time *d);

void timelib_decimal_hour_to_hms(double h, int *hour, int *min, int *sec);
long timelib_parse_tz_cor(char **ptr);

/* from astro.c */
double timelib_ts_to_juliandate(timelib_sll ts);
Expand Down
134 changes: 134 additions & 0 deletions ext/date/php_date.c
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,8 @@ const zend_function_entry date_funcs_date[] = {

const zend_function_entry date_funcs_timezone[] = {
PHP_ME(DateTimeZone, __construct, arginfo_timezone_open, ZEND_ACC_CTOR|ZEND_ACC_PUBLIC)
PHP_ME(DateTimeZone, __wakeup, NULL, ZEND_ACC_PUBLIC)
PHP_ME(DateTimeZone, __set_state, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
PHP_ME_MAPPING(getName, timezone_name_get, arginfo_timezone_method_name_get, 0)
PHP_ME_MAPPING(getOffset, timezone_offset_get, arginfo_timezone_method_offset_get, 0)
PHP_ME_MAPPING(getTransitions, timezone_transitions_get, arginfo_timezone_method_transitions_get, 0)
Expand Down Expand Up @@ -569,6 +571,8 @@ static HashTable *date_object_get_gc(zval *object, zval ***table, int *n TSRMLS_
static HashTable *date_object_get_properties(zval *object TSRMLS_DC);
static HashTable *date_object_get_gc_interval(zval *object, zval ***table, int *n TSRMLS_DC);
static HashTable *date_object_get_properties_interval(zval *object TSRMLS_DC);
static HashTable *date_object_get_properties_timezone(zval *object TSRMLS_DC);
static HashTable *date_object_get_gc_timezone(zval *object, zval ***table, int *n TSRMLS_DC);

zval *date_interval_read_property(zval *object, zval *member, int type, const zend_literal *key TSRMLS_DC);
void date_interval_write_property(zval *object, zval *member, zval *value, const zend_literal *key TSRMLS_DC);
Expand Down Expand Up @@ -1944,6 +1948,8 @@ static void date_register_classes(TSRMLS_D)
date_ce_timezone = zend_register_internal_class_ex(&ce_timezone, NULL, NULL TSRMLS_CC);
memcpy(&date_object_handlers_timezone, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
date_object_handlers_timezone.clone_obj = date_object_clone_timezone;
date_object_handlers_timezone.get_properties = date_object_get_properties_timezone;
date_object_handlers_timezone.get_gc = date_object_get_gc_timezone;

#define REGISTER_TIMEZONE_CLASS_CONST_STRING(const_name, value) \
zend_declare_class_constant_long(date_ce_timezone, const_name, sizeof(const_name)-1, value TSRMLS_CC);
Expand Down Expand Up @@ -2070,6 +2076,14 @@ static HashTable *date_object_get_gc(zval *object, zval ***table, int *n TSRMLS_
return zend_std_get_properties(object TSRMLS_CC);
}

static HashTable *date_object_get_gc_timezone(zval *object, zval ***table, int *n TSRMLS_DC)
{

*table = NULL;
*n = 0;
return zend_std_get_properties(object TSRMLS_CC);
}

static HashTable *date_object_get_properties(zval *object TSRMLS_DC)
{
HashTable *props;
Expand Down Expand Up @@ -2178,6 +2192,50 @@ static zend_object_value date_object_clone_timezone(zval *this_ptr TSRMLS_DC)
return new_ov;
}

static HashTable *date_object_get_properties_timezone(zval *object TSRMLS_DC)
{
HashTable *props;
zval *zv;
php_timezone_obj *tzobj;


tzobj = (php_timezone_obj *) zend_object_store_get_object(object TSRMLS_CC);

props = zend_std_get_properties(object TSRMLS_CC);

if (!tzobj->initialized) {
return props;
}

MAKE_STD_ZVAL(zv);
ZVAL_LONG(zv, tzobj->type);
zend_hash_update(props, "timezone_type", 14, &zv, sizeof(zval), NULL);

MAKE_STD_ZVAL(zv);
switch (tzobj->type) {
case TIMELIB_ZONETYPE_ID:
ZVAL_STRING(zv, tzobj->tzi.tz->name, 1);
break;
case TIMELIB_ZONETYPE_OFFSET: {
char *tmpstr = emalloc(sizeof("UTC+05:00"));

snprintf(tmpstr, sizeof("+05:00"), "%c%02d:%02d",
tzobj->tzi.utc_offset > 0 ? '-' : '+',
abs(tzobj->tzi.utc_offset / 60),
abs((tzobj->tzi.utc_offset % 60)));

ZVAL_STRING(zv, tmpstr, 0);
}
break;
case TIMELIB_ZONETYPE_ABBR:
ZVAL_STRING(zv, tzobj->tzi.z.abbr, 1);
break;
}
zend_hash_update(props, "timezone", 9, &zv, sizeof(zval), NULL);

return props;
}

static inline zend_object_value date_object_new_interval_ex(zend_class_entry *class_type, php_interval_obj **ptr TSRMLS_DC)
{
php_interval_obj *intern;
Expand Down Expand Up @@ -3241,6 +3299,82 @@ PHP_METHOD(DateTimeZone, __construct)
}
/* }}} */

static int php_date_timezone_initialize_from_hash(zval **return_value, php_timezone_obj **tzobj, HashTable *myht TSRMLS_DC)
{
zval **z_timezone = NULL;
zval **z_timezone_type = NULL;
timelib_tzinfo *tzi;
char **offset;

if (zend_hash_find(myht, "timezone_type", 14, (void**) &z_timezone_type) == SUCCESS) {
if (zend_hash_find(myht, "timezone", 9, (void**) &z_timezone) == SUCCESS) {
convert_to_long(*z_timezone_type);
switch (Z_LVAL_PP(z_timezone_type)) {
case TIMELIB_ZONETYPE_OFFSET:
offset = malloc(sizeof(char) * (Z_STRLEN_PP(z_timezone) + 1));
*offset = (Z_STRVAL_PP(z_timezone));
if(**offset == '+'){
++*offset;
(*tzobj)->tzi.utc_offset = -1 * timelib_parse_tz_cor((char **)offset);
} else {
++*offset;
(*tzobj)->tzi.utc_offset = timelib_parse_tz_cor((char **)offset);
}
free(offset);
(*tzobj)->type = TIMELIB_ZONETYPE_OFFSET;
(*tzobj)->initialized = 1;
return SUCCESS;
break;
case TIMELIB_ZONETYPE_ABBR:
case TIMELIB_ZONETYPE_ID:
if (SUCCESS == timezone_initialize(&tzi, Z_STRVAL_PP(z_timezone) TSRMLS_CC)) {
(*tzobj)->type = TIMELIB_ZONETYPE_ID;
(*tzobj)->tzi.tz = tzi;
(*tzobj)->initialized = 1;
return SUCCESS;
}
}
}
}
return FAILURE;
}

/* {{{ proto DateTimeZone::__set_state()
* */
PHP_METHOD(DateTimeZone, __set_state)
{
php_timezone_obj *tzobj;
zval *array;
HashTable *myht;

if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a", &array) == FAILURE) {
RETURN_FALSE;
}

myht = HASH_OF(array);

php_date_instantiate(date_ce_timezone, return_value TSRMLS_CC);
tzobj = (php_timezone_obj *) zend_object_store_get_object(return_value TSRMLS_CC);
php_date_timezone_initialize_from_hash(&return_value, &tzobj, myht TSRMLS_CC);
}
/* }}} */

/* {{{ proto DateTimeZone::__wakeup()
* */
PHP_METHOD(DateTimeZone, __wakeup)
{
zval *object = getThis();
php_timezone_obj *tzobj;
HashTable *myht;

tzobj = (php_timezone_obj *) zend_object_store_get_object(object TSRMLS_CC);

myht = Z_OBJPROP_P(object);

php_date_timezone_initialize_from_hash(&return_value, &tzobj, myht TSRMLS_CC);
}
/* }}} */

/* {{{ proto string timezone_name_get(DateTimeZone object)
Returns the name of the timezone.
*/
Expand Down
3 changes: 3 additions & 0 deletions ext/date/php_date.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ PHP_FUNCTION(date_timestamp_set);
PHP_FUNCTION(date_timestamp_get);

PHP_METHOD(DateTimeZone, __construct);
PHP_METHOD(DateTimeZone, __wakeup);
PHP_METHOD(DateTimeZone, __set_state);
PHP_FUNCTION(timezone_open);
PHP_FUNCTION(timezone_name_get);
PHP_FUNCTION(timezone_name_from_abbr);
Expand Down Expand Up @@ -129,6 +131,7 @@ struct _php_timezone_obj {
int dst;
} z;
} tzi;
HashTable *props;
};

struct _php_interval_obj {
Expand Down
6 changes: 5 additions & 1 deletion ext/date/tests/014.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@ object(DateTime)#%d (3) {
["timezone"]=>
string(3) "UTC"
}
object(DateTimeZone)#%d (0) {
object(DateTimeZone)#%d (2) {
["timezone_type"]=>
int(3)
["timezone"]=>
string(3) "UTC"
}

Warning: timezone_offset_get() expects exactly 2 parameters, 0 given in %s on line %d
Expand Down
12 changes: 10 additions & 2 deletions ext/date/tests/DateTimeZone_clone_basic1.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,17 @@ if ($clone != $orig) {
===DONE===
--EXPECTF--
*** Testing clone on DateTime objects ***
object(DateTimeZone)#%d (0) {
object(DateTimeZone)#%d (2) {
["timezone_type"]=>
int(3)
["timezone"]=>
string(3) "UTC"
}
object(DateTimeZone)#%d (0) {
object(DateTimeZone)#%d (2) {
["timezone_type"]=>
int(3)
["timezone"]=>
string(3) "UTC"
}
TEST PASSED : Objects equal but not indetical
===DONE===
Expand Down
Loading