summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorStanislav Malyshev <stas@php.net>2014-11-30 20:37:37 -0800
committerStanislav Malyshev <stas@php.net>2014-11-30 20:37:37 -0800
commit9924a085586348f76a8cc1f7942d854c4d3d82e5 (patch)
tree3d943be527d9c3c19566d9ede0e18b6623a5d53c
parentcd55c19695187a0935f61f80e199227dccd9984e (diff)
parent846a72a73aa2b275861f95416ce46c0f1cb603af (diff)
downloadphp-git-9924a085586348f76a8cc1f7942d854c4d3d82e5.tar.gz
Merge branch 'pull-request/878'
* pull-request/878: #68268: DatePeriod: Getter for start date, end date and interval
-rw-r--r--ext/date/php_date.c78
-rw-r--r--ext/date/php_date.h3
-rw-r--r--ext/date/tests/DatePeriod_getter.phpt25
3 files changed, 106 insertions, 0 deletions
diff --git a/ext/date/php_date.c b/ext/date/php_date.c
index 28a43e6678..8f2e8c8fc5 100644
--- a/ext/date/php_date.c
+++ b/ext/date/php_date.c
@@ -533,6 +533,9 @@ 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)
+ PHP_ME(DatePeriod, getStartDate, NULL, ZEND_ACC_PUBLIC)
+ PHP_ME(DatePeriod, getEndDate, NULL, ZEND_ACC_PUBLIC)
+ PHP_ME(DatePeriod, getDateInterval, NULL, ZEND_ACC_PUBLIC)
PHP_FE_END
};
@@ -4415,6 +4418,81 @@ PHP_METHOD(DatePeriod, __construct)
}
/* }}} */
+/* {{{ proto DatePeriod::getStartDate()
+ Get start date.
+*/
+PHP_METHOD(DatePeriod, getStartDate)
+{
+ php_period_obj *dpobj;
+ php_date_obj *dateobj;
+
+ if (zend_parse_parameters_none() == FAILURE) {
+ return;
+ }
+
+ dpobj = Z_PHPPERIOD_P(getThis());
+
+ php_date_instantiate(dpobj->start_ce, return_value TSRMLS_CC);
+ dateobj = Z_PHPDATE_P(return_value);
+ dateobj->time = timelib_time_ctor();
+ *dateobj->time = *dpobj->start;
+ if (dpobj->start->tz_abbr) {
+ dateobj->time->tz_abbr = strdup(dpobj->start->tz_abbr);
+ }
+ if (dpobj->start->tz_info) {
+ dateobj->time->tz_info = dpobj->start->tz_info;
+ }
+}
+/* }}} */
+
+/* {{{ proto DatePeriod::getEndDate()
+ Get end date.
+*/
+PHP_METHOD(DatePeriod, getEndDate)
+{
+ php_period_obj *dpobj;
+ php_date_obj *dateobj;
+
+ if (zend_parse_parameters_none() == FAILURE) {
+ return;
+ }
+
+ dpobj = Z_PHPPERIOD_P(getThis());
+
+ php_date_instantiate(dpobj->start_ce, return_value TSRMLS_CC);
+ dateobj = Z_PHPDATE_P(return_value);
+ dateobj->time = timelib_time_ctor();
+ *dateobj->time = *dpobj->end;
+ if (dpobj->end->tz_abbr) {
+ dateobj->time->tz_abbr = strdup(dpobj->end->tz_abbr);
+ }
+ if (dpobj->end->tz_info) {
+ dateobj->time->tz_info = dpobj->end->tz_info;
+ }
+}
+/* }}} */
+
+/* {{{ proto DatePeriod::getDateInterval()
+ Get date interval.
+*/
+PHP_METHOD(DatePeriod, getDateInterval)
+{
+ php_period_obj *dpobj;
+ php_interval_obj *diobj;
+
+ if (zend_parse_parameters_none() == FAILURE) {
+ return;
+ }
+
+ dpobj = Z_PHPPERIOD_P(getThis());
+
+ php_date_instantiate(date_ce_interval, return_value TSRMLS_CC);
+ diobj = Z_PHPINTERVAL_P(return_value);
+ diobj->diff = timelib_rel_time_clone(dpobj->interval);
+ diobj->initialized = 1;
+}
+/* }}} */
+
static int check_id_allowed(char *id, zend_long what) /* {{{ */
{
if (what & PHP_DATE_TIMEZONE_GROUP_AFRICA && strncasecmp(id, "Africa/", 7) == 0) return 1;
diff --git a/ext/date/php_date.h b/ext/date/php_date.h
index ebbf9a9027..134beef24d 100644
--- a/ext/date/php_date.h
+++ b/ext/date/php_date.h
@@ -106,6 +106,9 @@ PHP_FUNCTION(date_interval_create_from_date_string);
PHP_METHOD(DatePeriod, __construct);
PHP_METHOD(DatePeriod, __wakeup);
PHP_METHOD(DatePeriod, __set_state);
+PHP_METHOD(DatePeriod, getStartDate);
+PHP_METHOD(DatePeriod, getEndDate);
+PHP_METHOD(DatePeriod, getDateInterval);
/* Options and Configuration */
PHP_FUNCTION(date_default_timezone_set);
diff --git a/ext/date/tests/DatePeriod_getter.phpt b/ext/date/tests/DatePeriod_getter.phpt
new file mode 100644
index 0000000000..22006d1ae8
--- /dev/null
+++ b/ext/date/tests/DatePeriod_getter.phpt
@@ -0,0 +1,25 @@
+--TEST--
+DatePeriod: Test getter
+--INI--
+date.timezone=UTC
+--FILE--
+<?php
+$start = new DateTime('2000-01-01 00:00:00', new DateTimeZone('Europe/Berlin'));
+$end = new DateTime('2000-01-31 00:00:00', new DateTimeZone('UTC'));
+$interval = new DateInterval('P1D');
+$period = new DatePeriod($start, $interval, $end);
+
+var_dump($period->getStartDate()->format('Y-m-d H:i:s'));
+var_dump($period->getStartDate()->getTimeZone()->getName());
+
+var_dump($period->getEndDate()->format('Y-m-d H:i:s'));
+var_dump($period->getEndDate()->getTimeZone()->getName());
+
+var_dump($period->getDateInterval()->format('%R%y-%m-%d-%h-%i-%s'));
+?>
+--EXPECTF--
+string(19) "2000-01-01 00:00:00"
+string(13) "Europe/Berlin"
+string(19) "2000-01-31 00:00:00"
+string(3) "UTC"
+string(12) "+0-0-1-0-0-0"