]> granicus.if.org Git - php/commitdiff
- MFH: Added DateInterval::createFromDateString() that creates an interval
authorDerick Rethans <derick@php.net>
Fri, 2 May 2008 12:49:16 +0000 (12:49 +0000)
committerDerick Rethans <derick@php.net>
Fri, 2 May 2008 12:49:16 +0000 (12:49 +0000)
  from the relative parts of a date/time string.
- MFH: Fixed an issue where special relative bits were not applied.

ext/date/lib/tm2unixtime.c
ext/date/php_date.c
ext/date/php_date.h

index a78d6ec78fbb4efd8d7b5d9261449a934668379d..c56a1bdce96683bf513892d328ecf155f675abe9 100644 (file)
@@ -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)
index 2fe47992494d0908d075f62f3c07e883d764dfad..5652e7adec6dfc73e743f3fe1f56f89e9c5ba821 100644 (file)
@@ -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)
 {
index caa1697737bf6972609555d8b60c538b0444fe24..30d0e4350ed466263acd2018f9002028bc21dc1e 100644 (file)
@@ -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);