diff options
Diffstat (limited to 'ext/date/php_date.c')
-rw-r--r-- | ext/date/php_date.c | 171 |
1 files changed, 78 insertions, 93 deletions
diff --git a/ext/date/php_date.c b/ext/date/php_date.c index ab6c288943..77cad2d415 100644 --- a/ext/date/php_date.c +++ b/ext/date/php_date.c @@ -310,6 +310,11 @@ ZEND_END_ARG_INFO() ZEND_BEGIN_ARG_INFO(arginfo_date_method_timestamp_get, 0) ZEND_END_ARG_INFO() + +ZEND_BEGIN_ARG_INFO_EX(arginfo_date_method_create_from_immutable, 0, 0, 1) + ZEND_ARG_INFO(0, DateTimeImmutable) +ZEND_END_ARG_INFO() + ZEND_BEGIN_ARG_INFO_EX(arginfo_date_method_create_from_mutable, 0, 0, 1) ZEND_ARG_INFO(0, DateTime) ZEND_END_ARG_INFO() @@ -394,7 +399,7 @@ ZEND_END_ARG_INFO() /* }}} */ /* {{{ Function table */ -const zend_function_entry date_functions[] = { +static const zend_function_entry date_functions[] = { PHP_FE(strtotime, arginfo_strtotime) PHP_FE(date, arginfo_date) PHP_FE(idate, arginfo_idate) @@ -469,10 +474,11 @@ static const zend_function_entry date_funcs_interface[] = { PHP_FE_END }; -const zend_function_entry date_funcs_date[] = { +static const zend_function_entry date_funcs_date[] = { PHP_ME(DateTime, __construct, arginfo_date_create, ZEND_ACC_CTOR|ZEND_ACC_PUBLIC) PHP_ME(DateTime, __wakeup, NULL, ZEND_ACC_PUBLIC) PHP_ME(DateTime, __set_state, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC) + PHP_ME(DateTime, createFromImmutable, arginfo_date_method_create_from_immutable, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC) PHP_ME_MAPPING(createFromFormat, date_create_from_format, arginfo_date_create_from_format, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC) PHP_ME_MAPPING(getLastErrors, date_get_last_errors, arginfo_date_get_last_errors, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC) PHP_ME_MAPPING(format, date_format, arginfo_date_method_format, 0) @@ -491,7 +497,7 @@ const zend_function_entry date_funcs_date[] = { PHP_FE_END }; -const zend_function_entry date_funcs_immutable[] = { +static const zend_function_entry date_funcs_immutable[] = { PHP_ME(DateTimeImmutable, __construct, arginfo_date_create, ZEND_ACC_CTOR|ZEND_ACC_PUBLIC) PHP_ME(DateTime, __wakeup, NULL, ZEND_ACC_PUBLIC) PHP_ME(DateTimeImmutable, __set_state, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC) @@ -514,7 +520,7 @@ const zend_function_entry date_funcs_immutable[] = { PHP_FE_END }; -const zend_function_entry date_funcs_timezone[] = { +static 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) @@ -527,7 +533,7 @@ const zend_function_entry date_funcs_timezone[] = { PHP_FE_END }; -const zend_function_entry date_funcs_interval[] = { +static const zend_function_entry date_funcs_interval[] = { PHP_ME(DateInterval, __construct, arginfo_date_interval_construct, ZEND_ACC_CTOR|ZEND_ACC_PUBLIC) PHP_ME(DateInterval, __wakeup, NULL, ZEND_ACC_PUBLIC) PHP_ME(DateInterval, __set_state, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC) @@ -536,7 +542,7 @@ const zend_function_entry date_funcs_interval[] = { PHP_FE_END }; -const zend_function_entry date_funcs_period[] = { +static const zend_function_entry date_funcs_period[] = { PHP_ME(DatePeriod, __construct, arginfo_date_period_construct, ZEND_ACC_CTOR|ZEND_ACC_PUBLIC) PHP_ME(DatePeriod, __wakeup, NULL, ZEND_ACC_PUBLIC) PHP_ME(DatePeriod, __set_state, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC) @@ -1035,21 +1041,21 @@ PHPAPI timelib_tzinfo *get_timezone_info(void) /* {{{ date() and gmdate() data */ #include "zend_smart_str.h" -static char *mon_full_names[] = { +static const char * const mon_full_names[] = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" }; -static char *mon_short_names[] = { +static const char * const mon_short_names[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; -static char *day_full_names[] = { +static const char * const day_full_names[] = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" }; -static char *day_short_names[] = { +static const char * const day_short_names[] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" }; @@ -1069,7 +1075,7 @@ static char *english_suffix(timelib_sll number) /* }}} */ /* {{{ day of week helpers */ -char *php_date_full_day_name(timelib_sll y, timelib_sll m, timelib_sll d) +static const char *php_date_full_day_name(timelib_sll y, timelib_sll m, timelib_sll d) { timelib_sll day_of_week = timelib_day_of_week(y, m, d); if (day_of_week < 0) { @@ -1078,7 +1084,7 @@ char *php_date_full_day_name(timelib_sll y, timelib_sll m, timelib_sll d) return day_full_names[day_of_week]; } -char *php_date_short_day_name(timelib_sll y, timelib_sll m, timelib_sll d) +static const char *php_date_short_day_name(timelib_sll y, timelib_sll m, timelib_sll d) { timelib_sll day_of_week = timelib_day_of_week(y, m, d); if (day_of_week < 0) { @@ -1330,9 +1336,9 @@ PHPAPI int php_idate(char format, time_t ts, int localtime) offset->is_dst = t->dst; offset->abbr = timelib_malloc(9); /* GMT±xxxx\0 */ snprintf(offset->abbr, 9, "GMT%c%02d%02d", - !localtime ? ((offset->offset < 0) ? '-' : '+') : '+', - !localtime ? abs(offset->offset / 3600) : 0, - !localtime ? abs((offset->offset % 3600) / 60) : 0 ); + (offset->offset < 0) ? '-' : '+', + abs(offset->offset / 3600), + abs((offset->offset % 3600) / 60)); } else { offset = timelib_get_time_zone_info(t->sse, t->tz_info); } @@ -1752,6 +1758,10 @@ PHP_FUNCTION(gmstrftime) Return current UNIX timestamp */ PHP_FUNCTION(time) { + if (zend_parse_parameters_none() == FAILURE) { + return; + } + RETURN_LONG((zend_long)time(NULL)); } /* }}} */ @@ -1976,7 +1986,7 @@ static void date_period_it_rewind(zend_object_iterator *iter) /* }}} */ /* iterator handler table */ -zend_object_iterator_funcs date_period_it_funcs = { +static const zend_object_iterator_funcs date_period_it_funcs = { date_period_it_dtor, date_period_it_has_more, date_period_it_current_data, @@ -2025,8 +2035,7 @@ static int date_interval_has_property(zval *object, zval *member, int type, void int retval = 0; if (UNEXPECTED(Z_TYPE_P(member) != IS_STRING)) { - ZVAL_COPY(&tmp_member, member); - convert_to_string(&tmp_member); + ZVAL_STR(&tmp_member, zval_get_string_func(member)); member = &tmp_member; cache_slot = NULL; } @@ -2049,7 +2058,7 @@ static int date_interval_has_property(zval *object, zval *member, int type, void } else if (type == 1) { retval = zend_is_true(prop); } else if (type == 0) { - retval = (Z_TYPE(*prop) != IS_NULL); + retval = (Z_TYPE_P(prop) != IS_NULL); } } else { retval = (zend_get_std_object_handlers())->has_property(object, member, type, cache_slot); @@ -2175,30 +2184,21 @@ static void date_register_classes(void) /* {{{ */ REGISTER_PERIOD_CLASS_CONST_STRING("EXCLUDE_START_DATE", PHP_DATE_PERIOD_EXCLUDE_START_DATE); } /* }}} */ -static inline zend_object *date_object_new_date_ex(zend_class_entry *class_type, int init_props) /* {{{ */ +static zend_object *date_object_new_date(zend_class_entry *class_type) /* {{{ */ { - php_date_obj *intern; - - intern = ecalloc(1, sizeof(php_date_obj) + zend_object_properties_size(class_type)); + php_date_obj *intern = zend_object_alloc(sizeof(php_date_obj), class_type); zend_object_std_init(&intern->std, class_type); - if (init_props) { - object_properties_init(&intern->std, class_type); - } + object_properties_init(&intern->std, class_type); intern->std.handlers = &date_object_handlers_date; return &intern->std; } /* }}} */ -static zend_object *date_object_new_date(zend_class_entry *class_type) /* {{{ */ -{ - return date_object_new_date_ex(class_type, 1); -} /* }}} */ - static zend_object *date_object_clone_date(zval *this_ptr) /* {{{ */ { php_date_obj *old_obj = Z_PHPDATE_P(this_ptr); - php_date_obj *new_obj = php_date_obj_from_obj(date_object_new_date_ex(old_obj->std.ce, 0)); + php_date_obj *new_obj = php_date_obj_from_obj(date_object_new_date(old_obj->std.ce)); zend_objects_clone_members(&new_obj->std, &old_obj->std); if (!old_obj->time) { @@ -2306,30 +2306,21 @@ static HashTable *date_object_get_properties(zval *object) /* {{{ */ return props; } /* }}} */ -static inline zend_object *date_object_new_timezone_ex(zend_class_entry *class_type, int init_props) /* {{{ */ +static zend_object *date_object_new_timezone(zend_class_entry *class_type) /* {{{ */ { - php_timezone_obj *intern; - - intern = ecalloc(1, sizeof(php_timezone_obj) + zend_object_properties_size(class_type)); + php_timezone_obj *intern = zend_object_alloc(sizeof(php_timezone_obj), class_type); zend_object_std_init(&intern->std, class_type); - if (init_props) { - object_properties_init(&intern->std, class_type); - } + object_properties_init(&intern->std, class_type); intern->std.handlers = &date_object_handlers_timezone; return &intern->std; } /* }}} */ -static zend_object *date_object_new_timezone(zend_class_entry *class_type) /* {{{ */ -{ - return date_object_new_timezone_ex(class_type, 1); -} /* }}} */ - static zend_object *date_object_clone_timezone(zval *this_ptr) /* {{{ */ { php_timezone_obj *old_obj = Z_PHPTIMEZONE_P(this_ptr); - php_timezone_obj *new_obj = php_timezone_obj_from_obj(date_object_new_timezone_ex(old_obj->std.ce, 0)); + php_timezone_obj *new_obj = php_timezone_obj_from_obj(date_object_new_timezone(old_obj->std.ce)); zend_objects_clone_members(&new_obj->std, &old_obj->std); if (!old_obj->initialized) { @@ -2397,30 +2388,21 @@ static HashTable *date_object_get_properties_timezone(zval *object) /* {{{ */ return props; } /* }}} */ -static inline zend_object *date_object_new_interval_ex(zend_class_entry *class_type, int init_props) /* {{{ */ +static zend_object *date_object_new_interval(zend_class_entry *class_type) /* {{{ */ { - php_interval_obj *intern; - - intern = ecalloc(1, sizeof(php_interval_obj) + zend_object_properties_size(class_type)); + php_interval_obj *intern = zend_object_alloc(sizeof(php_interval_obj), class_type); zend_object_std_init(&intern->std, class_type); - if (init_props) { - object_properties_init(&intern->std, class_type); - } + object_properties_init(&intern->std, class_type); intern->std.handlers = &date_object_handlers_interval; return &intern->std; } /* }}} */ -static zend_object *date_object_new_interval(zend_class_entry *class_type) /* {{{ */ -{ - return date_object_new_interval_ex(class_type, 1); -} /* }}} */ - static zend_object *date_object_clone_interval(zval *this_ptr) /* {{{ */ { php_interval_obj *old_obj = Z_PHPINTERVAL_P(this_ptr); - php_interval_obj *new_obj = php_interval_obj_from_obj(date_object_new_interval_ex(old_obj->std.ce, 0)); + php_interval_obj *new_obj = php_interval_obj_from_obj(date_object_new_interval(old_obj->std.ce)); zend_objects_clone_members(&new_obj->std, &old_obj->std); new_obj->initialized = old_obj->initialized; @@ -2483,31 +2465,22 @@ static HashTable *date_object_get_properties_interval(zval *object) /* {{{ */ return props; } /* }}} */ -static inline zend_object *date_object_new_period_ex(zend_class_entry *class_type, int init_props) /* {{{ */ +static zend_object *date_object_new_period(zend_class_entry *class_type) /* {{{ */ { - php_period_obj *intern; - - intern = ecalloc(1, sizeof(php_period_obj) + zend_object_properties_size(class_type)); + php_period_obj *intern = zend_object_alloc(sizeof(php_period_obj), class_type); zend_object_std_init(&intern->std, class_type); - if (init_props) { - object_properties_init(&intern->std, class_type); - } + object_properties_init(&intern->std, class_type); intern->std.handlers = &date_object_handlers_period; return &intern->std; } /* }}} */ -static zend_object *date_object_new_period(zend_class_entry *class_type) /* {{{ */ -{ - return date_object_new_period_ex(class_type, 1); -} /* }}} */ - static zend_object *date_object_clone_period(zval *this_ptr) /* {{{ */ { php_period_obj *old_obj = Z_PHPPERIOD_P(this_ptr); - php_period_obj *new_obj = php_period_obj_from_obj(date_object_new_period_ex(old_obj->std.ce, 0)); + php_period_obj *new_obj = php_period_obj_from_obj(date_object_new_period(old_obj->std.ce)); zend_objects_clone_members(&new_obj->std, &old_obj->std); new_obj->initialized = old_obj->initialized; @@ -2842,7 +2815,28 @@ PHP_METHOD(DateTimeImmutable, __construct) } /* }}} */ -/* {{{ proto DateTimeImmutable::createFromMutable(DateTimeZone object) +/* {{{ proto DateTime::createFromImmutable(DateTimeImmutable object) + Creates new DateTime object from an existing immutable DateTimeImmutable object. +*/ +PHP_METHOD(DateTime, createFromImmutable) +{ + zval *datetimeimmutable_object = NULL; + php_date_obj *new_obj = NULL; + php_date_obj *old_obj = NULL; + + ZEND_PARSE_PARAMETERS_START(1, 1) + Z_PARAM_OBJECT_OF_CLASS(datetimeimmutable_object, date_ce_immutable) + ZEND_PARSE_PARAMETERS_END(); + + php_date_instantiate(date_ce_date, return_value); + old_obj = Z_PHPDATE_P(datetimeimmutable_object); + new_obj = Z_PHPDATE_P(return_value); + + new_obj->time = timelib_time_clone(old_obj->time); +} +/* }}} */ + +/* {{{ proto DateTimeImmutable::createFromMutable(DateTime object) Creates new DateTimeImmutable object from an existing mutable DateTime object. */ PHP_METHOD(DateTimeImmutable, createFromMutable) @@ -2859,14 +2853,7 @@ PHP_METHOD(DateTimeImmutable, createFromMutable) old_obj = Z_PHPDATE_P(datetime_object); new_obj = Z_PHPDATE_P(return_value); - new_obj->time = timelib_time_ctor(); - *new_obj->time = *old_obj->time; - if (old_obj->time->tz_abbr) { - new_obj->time->tz_abbr = timelib_strdup(old_obj->time->tz_abbr); - } - if (old_obj->time->tz_info) { - new_obj->time->tz_info = old_obj->time->tz_info; - } + new_obj->time = timelib_time_clone(old_obj->time); } /* }}} */ @@ -3400,7 +3387,7 @@ PHP_FUNCTION(date_timezone_get) } dateobj = Z_PHPDATE_P(object); DATE_CHECK_INITIALIZED(dateobj->time, DateTime); - if (dateobj->time->is_localtime/* && dateobj->time->tz_info*/) { + if (dateobj->time->is_localtime) { php_date_instantiate(date_ce_timezone, return_value); tzobj = Z_PHPTIMEZONE_P(return_value); set_timezone_from_timelib_time(tzobj, dateobj->time); @@ -3484,7 +3471,7 @@ PHP_FUNCTION(date_offset_get) } dateobj = Z_PHPDATE_P(object); DATE_CHECK_INITIALIZED(dateobj->time, DateTime); - if (dateobj->time->is_localtime/* && dateobj->time->tz_info*/) { + if (dateobj->time->is_localtime) { switch (dateobj->time->zone_type) { case TIMELIB_ZONETYPE_ID: offset = timelib_get_time_zone_info(dateobj->time->sse, dateobj->time->tz_info); @@ -4136,9 +4123,7 @@ zval *date_interval_read_property(zval *object, zval *member, int type, void **c double fvalue = -1; if (Z_TYPE_P(member) != IS_STRING) { - tmp_member = *member; - zval_copy_ctor(&tmp_member); - convert_to_string(&tmp_member); + ZVAL_STR(&tmp_member, zval_get_string_func(member)); member = &tmp_member; cache_slot = NULL; } @@ -4206,9 +4191,7 @@ void date_interval_write_property(zval *object, zval *member, zval *value, void zval tmp_member; if (Z_TYPE_P(member) != IS_STRING) { - tmp_member = *member; - zval_copy_ctor(&tmp_member); - convert_to_string(&tmp_member); + ZVAL_STR(&tmp_member, zval_get_string_func(member)); member = &tmp_member; cache_slot = NULL; } @@ -4257,9 +4240,7 @@ static zval *date_interval_get_property_ptr_ptr(zval *object, zval *member, int zval tmp_member, *ret; if (Z_TYPE_P(member) != IS_STRING) { - tmp_member = *member; - zval_copy_ctor(&tmp_member); - convert_to_string(&tmp_member); + ZVAL_STR(&tmp_member, zval_get_string_func(member)); member = &tmp_member; cache_slot = NULL; } @@ -4329,9 +4310,10 @@ static int php_date_interval_initialize_from_hash(zval **return_value, php_inter do { \ zval *z_arg = zend_hash_str_find(myht, element, sizeof(element) - 1); \ if (z_arg && Z_TYPE_P(z_arg) <= IS_STRING) { \ - zend_string *str = zval_get_string(z_arg); \ + zend_string *tmp_str; \ + zend_string *str = zval_get_tmp_string(z_arg, &tmp_str); \ DATE_A64I((*intobj)->diff->member, ZSTR_VAL(str)); \ - zend_string_release(str); \ + zend_tmp_string_release(tmp_str); \ } else { \ (*intobj)->diff->member = -1LL; \ } \ @@ -4850,6 +4832,9 @@ PHP_FUNCTION(date_default_timezone_set) PHP_FUNCTION(date_default_timezone_get) { timelib_tzinfo *default_tz; + if (zend_parse_parameters_none() == FAILURE) { + return; + } default_tz = get_timezone_info(); RETVAL_STRING(default_tz->name); |