]> granicus.if.org Git - php/commitdiff
Make DatePeriod support DateTimeImmutable as well.
authorDerick Rethans <github@derickrethans.nl>
Thu, 20 Dec 2012 13:22:18 +0000 (13:22 +0000)
committerDerick Rethans <github@derickrethans.nl>
Mon, 14 Jan 2013 20:34:58 +0000 (20:34 +0000)
If the start element is a DateTimeImmutable object, then all returned objects
are also DateTimeImmutable objects. If the start element is a DateTime object,
then all returned objects are DateTime objects.

ext/date/php_date.c
ext/date/php_date.h
ext/date/tests/date_period-immutable.phpt [new file with mode: 0644]

index fc281ce0861d9c9e81e12d38c4f5ab616b063312..cc83130e1cf4079c0f2a4ea31b11a130368d1a49 100644 (file)
@@ -1847,7 +1847,7 @@ static void date_period_it_current_data(zend_object_iterator *iter, zval ***data
 
        /* Create new object */
        MAKE_STD_ZVAL(iterator->current);
-       php_date_instantiate(date_ce_date, iterator->current TSRMLS_CC);
+       php_date_instantiate(object->start_ce, iterator->current TSRMLS_CC);
        newdateobj = (php_date_obj *) zend_object_store_get_object(iterator->current TSRMLS_CC);
        newdateobj->time = timelib_time_ctor();
        *newdateobj->time = *it_time;
@@ -4182,6 +4182,7 @@ PHP_METHOD(DatePeriod, __construct)
                if (dpobj->end) {
                        timelib_update_ts(dpobj->end, NULL);
                }
+               dpobj->start_ce = date_ce_date;
        } else {
                /* init */
                intobj  = (php_interval_obj *) zend_object_store_get_object(interval TSRMLS_CC);
@@ -4197,6 +4198,7 @@ PHP_METHOD(DatePeriod, __construct)
                        clone->tz_info = dateobj->time->tz_info;
                }
                dpobj->start = clone;
+               dpobj->start_ce = Z_OBJCE_P(start);
 
                /* interval */
                dpobj->interval = timelib_rel_time_clone(intobj->diff);
index 19c692b57f3aa44a60b86a01f0ba3081468a2c9d..3af3fa42ede900e42a4ac45a191c4ac1f303fc98 100644 (file)
@@ -154,6 +154,7 @@ struct _php_interval_obj {
 struct _php_period_obj {
        zend_object       std;
        timelib_time     *start;
+       zend_class_entry *start_ce;
        timelib_time     *current;
        timelib_time     *end;
        timelib_rel_time *interval;
diff --git a/ext/date/tests/date_period-immutable.phpt b/ext/date/tests/date_period-immutable.phpt
new file mode 100644 (file)
index 0000000..0ec4b4a
--- /dev/null
@@ -0,0 +1,56 @@
+--TEST--
+DatePeriod
+--FILE--
+<?php
+date_default_timezone_set('UTC');
+$db1 = new DateTimeImmutable( '2008-01-01' );
+$db2 = new DateTime( '2008-01-01' );
+$de = new DateTime( '2008-03-31' );
+$di = DateInterval::createFromDateString( 'first day of next month' );
+
+foreach ( new DatePeriod( $db1, $di, $de ) as $dt )
+{
+       echo get_class( $dt ), "\n";
+       echo $dt->format( "l Y-m-d\n" );
+    echo $dt->modify( "3 tuesday" )->format( "l Y-m-d\n" );
+       echo $dt->format( "l Y-m-d\n\n" );
+}
+
+foreach ( new DatePeriod( $db2, $di, $de ) as $dt )
+{
+       echo get_class( $dt ), "\n";
+       echo $dt->format( "l Y-m-d\n" );
+    echo $dt->modify( "3 tuesday" )->format( "l Y-m-d\n" );
+       echo $dt->format( "l Y-m-d\n\n" );
+}
+?>
+--EXPECT--
+DateTimeImmutable
+Tuesday 2008-01-01
+Tuesday 2008-01-15
+Tuesday 2008-01-01
+
+DateTimeImmutable
+Friday 2008-02-01
+Tuesday 2008-02-19
+Friday 2008-02-01
+
+DateTimeImmutable
+Saturday 2008-03-01
+Tuesday 2008-03-18
+Saturday 2008-03-01
+
+DateTime
+Tuesday 2008-01-01
+Tuesday 2008-01-15
+Tuesday 2008-01-15
+
+DateTime
+Friday 2008-02-01
+Tuesday 2008-02-19
+Tuesday 2008-02-19
+
+DateTime
+Saturday 2008-03-01
+Tuesday 2008-03-18
+Tuesday 2008-03-18