From: Derick Rethans Date: Fri, 2 May 2008 12:49:16 +0000 (+0000) Subject: - MFH: Added DateInterval::createFromDateString() that creates an interval X-Git-Tag: BEFORE_NEW_PARAMETER_PARSE~306 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=149848846deb5b971678c50e18a511ec4cc958f7;p=php - MFH: Added DateInterval::createFromDateString() that creates an interval from the relative parts of a date/time string. - MFH: Fixed an issue where special relative bits were not applied. --- diff --git a/ext/date/lib/tm2unixtime.c b/ext/date/lib/tm2unixtime.c index a78d6ec78f..c56a1bdce9 100644 --- a/ext/date/lib/tm2unixtime.c +++ b/ext/date/lib/tm2unixtime.c @@ -193,9 +193,6 @@ static void do_adjust_relative(timelib_time* time) break; } do_normalize(time); - - memset(&(time->relative), 0, sizeof(time->relative)); - time->have_relative = 0; } static void do_adjust_special_weekday(timelib_time* time) diff --git a/ext/date/php_date.c b/ext/date/php_date.c index 2fe4799249..5652e7adec 100644 --- a/ext/date/php_date.c +++ b/ext/date/php_date.c @@ -196,6 +196,7 @@ const zend_function_entry date_functions[] = { PHP_FE(timezone_identifiers_list, NULL) PHP_FE(timezone_abbreviations_list, NULL) + PHP_FE(date_interval_create_from_date_string, NULL) PHP_FE(date_interval_format, NULL) /* Options and Configuration */ @@ -244,6 +245,7 @@ const zend_function_entry date_funcs_timezone[] = { const zend_function_entry date_funcs_interval[] = { PHP_ME(DateInterval, __construct, NULL, ZEND_ACC_CTOR|ZEND_ACC_PUBLIC) PHP_ME_MAPPING(format, date_interval_format, NULL, 0) + PHP_ME_MAPPING(createFromDateString, date_interval_create_from_date_string, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC) {NULL, NULL, NULL} }; @@ -1629,6 +1631,7 @@ static void date_period_it_current_data(zend_object_iterator *iter, zval ***data /* apply modification if it's not the first iteration */ if (!object->include_start_date || iterator->current_index > 0) { + it_time->have_relative = 1; it_time->relative.y = object->interval->y; it_time->relative.m = object->interval->m; it_time->relative.d = object->interval->d; @@ -1636,7 +1639,9 @@ static void date_period_it_current_data(zend_object_iterator *iter, zval ***data it_time->relative.i = object->interval->i; it_time->relative.s = object->interval->s; it_time->relative.weekday = object->interval->weekday; - it_time->have_relative = 1; + it_time->relative.special = object->interval->special; + it_time->relative.have_weekday_relative = object->interval->have_weekday_relative; + it_time->relative.have_special_relative = object->interval->have_special_relative; it_time->sse_uptodate = 0; timelib_update_ts(it_time, NULL); timelib_update_from_sse(it_time); @@ -2246,8 +2251,8 @@ PHP_FUNCTION(date_create) } /* }}} */ -/* {{{ proto DateTime date_create(string format, string time[, DateTimeZone object]) - Returns new DateTime object +/* {{{ proto DateTime date_create_from_format(string format, string time[, DateTimeZone object]) + Returns new DateTime object formatted according to the specified format */ PHP_FUNCTION(date_create_from_format) { @@ -3254,7 +3259,7 @@ PHP_METHOD(DateInterval, __construct) timelib_rel_time *reltime; php_set_error_handling(EH_THROW, NULL TSRMLS_CC); - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &interval_string, &interval_string_length) == SUCCESS) { + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &interval_string, &interval_string_length) == SUCCESS) { if (date_interval_initialize(&reltime, interval_string, interval_string_length TSRMLS_CC) == SUCCESS) { diobj = zend_object_store_get_object(getThis() TSRMLS_CC); diobj->diff = reltime; @@ -3267,6 +3272,31 @@ PHP_METHOD(DateInterval, __construct) } /* }}} */ +/* {{{ proto DateInterval date_interval_create_from_date_string(string time) + Uses the normal date parsers and sets up a DateInterval from the relative parts of the parsed string +*/ +PHP_FUNCTION(date_interval_create_from_date_string) +{ + char *time_str = NULL; + int time_str_len = 0; + timelib_time *time; + timelib_error_container *err = NULL; + php_interval_obj *diobj; + + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &time_str, &time_str_len) == FAILURE) { + RETURN_FALSE; + } + + date_instantiate(date_ce_interval, return_value TSRMLS_CC); + + time = timelib_strtotime(time_str, time_str_len, &err, DATE_TIMEZONEDB); + diobj = (php_interval_obj *) zend_object_store_get_object(return_value TSRMLS_CC); + diobj->diff = timelib_rel_time_clone(&time->relative); + timelib_time_dtor(time); + timelib_error_container_dtor(err); +} +/* }}} */ + /* {{{ date_interval_format - */ static char *date_interval_format(char *format, int format_len, timelib_rel_time *t) { diff --git a/ext/date/php_date.h b/ext/date/php_date.h index caa1697737..30d0e4350e 100644 --- a/ext/date/php_date.h +++ b/ext/date/php_date.h @@ -81,6 +81,7 @@ PHP_FUNCTION(timezone_abbreviations_list); PHP_METHOD(DateInterval, __construct); PHP_FUNCTION(date_interval_format); +PHP_FUNCTION(date_interval_create_from_date_string); PHP_METHOD(DatePeriod, __construct);