From 2352d10de296496486bd209d7bb03c735450815e Mon Sep 17 00:00:00 2001 From: Derick Rethans Date: Fri, 2 May 2008 12:48:19 +0000 Subject: [PATCH] - Added DateInterval::createFromDateString() that creates an interval from the relative parts of a date/time string. - Fixed an issue where special relative bits were not applied. #- @DOC --- ext/date/lib/parse_date.c | 2 +- ext/date/lib/tm2unixtime.c | 3 --- ext/date/php_date.c | 50 +++++++++++++++++++++++++++++--------- ext/date/php_date.h | 1 + 4 files changed, 41 insertions(+), 15 deletions(-) diff --git a/ext/date/lib/parse_date.c b/ext/date/lib/parse_date.c index 776d90776f..15a4c85715 100644 --- a/ext/date/lib/parse_date.c +++ b/ext/date/lib/parse_date.c @@ -1,4 +1,4 @@ -/* Generated by re2c 0.13.4 on Thu May 1 18:13:58 2008 */ +/* Generated by re2c 0.13.4 on Fri May 2 14:36:50 2008 */ #line 1 "ext/date/lib/parse_date.re" /* +----------------------------------------------------------------------+ 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 52b138025c..a80b21304f 100644 --- a/ext/date/php_date.c +++ b/ext/date/php_date.c @@ -198,6 +198,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 */ @@ -242,10 +243,11 @@ const zend_function_entry date_funcs_timezone[] = { PHP_ME_MAPPING(listIdentifiers, timezone_identifiers_list, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC) {NULL, NULL, NULL} }; - + 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} }; @@ -1755,6 +1757,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; @@ -1762,7 +1765,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); @@ -2378,8 +2383,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) { @@ -2585,22 +2590,20 @@ void php_date_do_return_parsed_time(INTERNAL_FUNCTION_PARAMETERS, timelib_time * break; } } - if (parsed_time->have_relative || parsed_time->have_weekday_relative || parsed_time->have_special_relative || parsed_time->relative.first_last_day_of) { + if (parsed_time->have_relative) { MAKE_STD_ZVAL(element); array_init(element); - } - if (parsed_time->have_relative) { add_ascii_assoc_long(element, "year", parsed_time->relative.y); add_ascii_assoc_long(element, "month", parsed_time->relative.m); add_ascii_assoc_long(element, "day", parsed_time->relative.d); add_ascii_assoc_long(element, "hour", parsed_time->relative.h); add_ascii_assoc_long(element, "minute", parsed_time->relative.i); add_ascii_assoc_long(element, "second", parsed_time->relative.s); - if (parsed_time->have_weekday_relative) { + if (parsed_time->relative.have_weekday_relative) { add_ascii_assoc_long(element, "weekday", parsed_time->relative.weekday); } - if (parsed_time->have_special_relative && (parsed_time->special.type == TIMELIB_SPECIAL_WEEKDAY)) { - add_ascii_assoc_long(element, "weekdays", parsed_time->special.amount); + if (parsed_time->relative.have_special_relative && (parsed_time->relative.special.type == TIMELIB_SPECIAL_WEEKDAY)) { + add_ascii_assoc_long(element, "weekdays", parsed_time->relative.special.amount); } if (parsed_time->relative.first_last_day_of) { add_ascii_assoc_bool(element, parsed_time->relative.first_last_day_of == 1 ? "first_day_of_month" : "last_day_of_month", 1); @@ -3260,7 +3263,7 @@ PHP_FUNCTION(timezone_transitions_get) #define add_last() add(tzobj->tzi.tz->timecnt - 1, timestamp_begin) array_init(return_value); - + if (timestamp_begin == LONG_MIN) { add_nominal(); begin = 0; @@ -3437,6 +3440,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 3c60275eb1..5c09b50c17 100644 --- a/ext/date/php_date.h +++ b/ext/date/php_date.h @@ -85,6 +85,7 @@ PHP_METHOD(timezone, abbreviations_list); PHP_METHOD(DateInterval, __construct); PHP_FUNCTION(date_interval_format); +PHP_FUNCTION(date_interval_create_from_date_string); PHP_METHOD(DatePeriod, __construct); -- 2.40.0