com php-src: #68268: DatePeriod: Getter for start date, end date and interval: ext/date/php_date.c ext/date/php_date.h e
xt/date/tests/DatePeriod_getter.phpt
| From: | Stanislav Malyshev | Date: | Mon, 20 Oct 2014 08:27:56 +0000 |
| Subject: | com php-src: #68268: DatePeriod: Getter for start date, end date and interval: ext/date/php_date.c ext/date/php_date.h e xt/date/tests/DatePeriod_getter.phpt |
||
| Groups: | php.cvs | ||
| Request: | Send a blank email to php-cvs+get-83417@lists.php.net to get a copy of this message | ||
Commit: 846a72a73aa2b275861f95416ce46c0f1cb603af
Author: m.bennewitz <marc.bennewitz@unister.de> Mon, 20 Oct 2014 08:27:56 +0000
Parents: 4b892439e61634b4bdcbf0bd54ef8577575875fd
Branches: master
Link: http://git.php.net/?p=php-src.git;a=commitdiff;h=846a72a73aa2b275861f95416ce46c0f1cb603af
Log:
#68268: DatePeriod: Getter for start date, end date and interval
Bugs:
https://bugs.php.net/68268
Changed paths:
M ext/date/php_date.c
M ext/date/php_date.h
A ext/date/tests/DatePeriod_getter.phpt
Diff:
diff --git a/ext/date/php_date.c b/ext/date/php_date.c
index f2ced7b..22b1163 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
};
@@ -4414,6 +4417,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 aa46aa1..00e18d3 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 0000000..22006d1
--- /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"