diff options
author | Derick Rethans <derick@php.net> | 2005-07-03 20:45:08 +0000 |
---|---|---|
committer | Derick Rethans <derick@php.net> | 2005-07-03 20:45:08 +0000 |
commit | 8aa3554d1937a501841635bf636768fd6e07b08f (patch) | |
tree | 85ce4aacc8441289aa6406d1cd5cfddcedb00933 | |
parent | 0ef991e5ae409ecddedc6cfbb8231236ef84b936 (diff) | |
download | php-git-8aa3554d1937a501841635bf636768fd6e07b08f.tar.gz |
- Reimplemented time(), getdate() and localtime() functions with new datetime
library.
-rw-r--r-- | ext/date/php_date.c | 97 | ||||
-rw-r--r-- | ext/date/php_date.h | 4 | ||||
-rw-r--r-- | ext/standard/basic_functions.c | 4 | ||||
-rw-r--r-- | ext/standard/datetime.c | 113 |
4 files changed, 101 insertions, 117 deletions
diff --git a/ext/date/php_date.c b/ext/date/php_date.c index f91ad7cdec..798f77e321 100644 --- a/ext/date/php_date.c +++ b/ext/date/php_date.c @@ -29,14 +29,22 @@ #include <time.h> function_entry date_functions[] = { + PHP_FE(strtotime, NULL) PHP_FE(date, NULL) PHP_FE(gmdate, NULL) PHP_FE(mktime, NULL) PHP_FE(checkdate, NULL) PHP_FE(gmstrftime, NULL) + +#if HAVE_STRFTIME PHP_FE(strftime, NULL) PHP_FE(gmmktime, NULL) - PHP_FE(strtotime, NULL) +#endif + + PHP_FE(time, NULL) + PHP_FE(localtime, NULL) + PHP_FE(getdate, NULL) + PHP_FE(date_timezone_set, NULL) PHP_FE(date_timezone_get, NULL) {NULL, NULL, NULL} @@ -641,6 +649,93 @@ PHP_FUNCTION(gmstrftime) /* }}} */ #endif +/* {{{ proto int time(void) + Return current UNIX timestamp */ +PHP_FUNCTION(time) +{ + RETURN_LONG((long)time(NULL)); +} +/* }}} */ + +/* {{{ proto array localtime([int timestamp [, bool associative_array]]) + Returns the results of the C system call localtime as an associative array if the associative_array argument is set to 1 other wise it is a regular array */ +PHP_FUNCTION(localtime) +{ + long timestamp = (long)time(NULL); + int associative = 0; + timelib_tzinfo *tzi; + timelib_time *ts; + + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|lb", ×tamp, &associative) == FAILURE) { + RETURN_FALSE; + } + + tzi = get_timezone_info(TSRMLS_C); + ts = timelib_time_ctor(); + timelib_unixtime2local(ts, (timelib_sll) timestamp, tzi); + + array_init(return_value); + + if (associative) { + add_assoc_long(return_value, "tm_sec", ts->s); + add_assoc_long(return_value, "tm_min", ts->i); + add_assoc_long(return_value, "tm_hour", ts->h); + add_assoc_long(return_value, "tm_mday", ts->d); + add_assoc_long(return_value, "tm_mon", ts->m - 1); + add_assoc_long(return_value, "tm_year", ts->y - 1900); + add_assoc_long(return_value, "tm_wday", timelib_day_of_week(ts->y, ts->m, ts->d)); + add_assoc_long(return_value, "tm_yday", timelib_day_of_year(ts->y, ts->m, ts->d)); + add_assoc_long(return_value, "tm_isdst", ts->dst); + } else { + add_next_index_long(return_value, ts->s); + add_next_index_long(return_value, ts->i); + add_next_index_long(return_value, ts->h); + add_next_index_long(return_value, ts->d); + add_next_index_long(return_value, ts->m - 1); + add_next_index_long(return_value, ts->y- 1900); + add_next_index_long(return_value, timelib_day_of_week(ts->y, ts->m, ts->d)); + add_next_index_long(return_value, timelib_day_of_year(ts->y, ts->m, ts->d)); + add_next_index_long(return_value, ts->dst); + } + + timelib_time_dtor(ts); +} +/* }}} */ + +/* {{{ proto array getdate([int timestamp]) + Get date/time information */ +PHP_FUNCTION(getdate) +{ + long timestamp = (long)time(NULL); + timelib_tzinfo *tzi; + timelib_time *ts; + + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", ×tamp) == FAILURE) { + RETURN_FALSE; + } + + tzi = get_timezone_info(TSRMLS_C); + ts = timelib_time_ctor(); + timelib_unixtime2local(ts, (timelib_sll) timestamp, tzi); + + array_init(return_value); + + add_assoc_long(return_value, "seconds", ts->s); + add_assoc_long(return_value, "minutes", ts->i); + add_assoc_long(return_value, "hours", ts->h); + add_assoc_long(return_value, "mday", ts->d); + add_assoc_long(return_value, "wday", timelib_day_of_week(ts->y, ts->m, ts->d)); + add_assoc_long(return_value, "mon", ts->m); + add_assoc_long(return_value, "year", ts->y); + add_assoc_long(return_value, "yday", timelib_day_of_year(ts->y, ts->m, ts->d)); + add_assoc_string(return_value, "weekday", day_full_names[timelib_day_of_week(ts->y, ts->m, ts->d)], 1); + add_assoc_string(return_value, "month", mon_full_names[ts->m - 1], 1); + add_index_long(return_value, 0, timestamp); + + timelib_time_dtor(ts); +} +/* }}} */ + PHP_FUNCTION(date_timezone_set) { char *zone; diff --git a/ext/date/php_date.h b/ext/date/php_date.h index 3da151f541..4fd126b610 100644 --- a/ext/date/php_date.h +++ b/ext/date/php_date.h @@ -38,6 +38,10 @@ PHP_FUNCTION(strftime); PHP_FUNCTION(gmstrftime); #endif +PHP_FUNCTION(time); +PHP_FUNCTION(localtime); +PHP_FUNCTION(getdate); + PHP_FUNCTION(date_timezone_set); PHP_FUNCTION(date_timezone_get); diff --git a/ext/standard/basic_functions.c b/ext/standard/basic_functions.c index 6203334a3a..e0a7878e2e 100644 --- a/ext/standard/basic_functions.c +++ b/ext/standard/basic_functions.c @@ -168,14 +168,12 @@ function_entry basic_functions[] = { PHP_FE(time_nanosleep, NULL) PHP_FE(time_sleep_until, NULL) #endif - PHP_FE(time, NULL) + #if HAVE_STRPTIME PHP_FE(strptime, NULL) #endif PHP_FE(idate, NULL) - PHP_FE(getdate, NULL) - PHP_FE(localtime, NULL) PHP_FE(flush, NULL) PHP_FE(wordwrap, NULL) diff --git a/ext/standard/datetime.c b/ext/standard/datetime.c index 8a2ceecf17..d690201d56 100644 --- a/ext/standard/datetime.c +++ b/ext/standard/datetime.c @@ -69,14 +69,6 @@ static int phpday_tab[2][12] = { #define isleap(year) ((((year) % 4) == 0 && ((year) % 100) != 0) || ((year) % 400)==0) #define YEAR_BASE 1900 -/* {{{ proto int time(void) - Return current UNIX timestamp */ -PHP_FUNCTION(time) -{ - RETURN_LONG((long)time(NULL)); -} -/* }}} */ - /* {{{ php_idate */ PHPAPI int php_idate(char format, int timestamp, int gm) @@ -218,111 +210,6 @@ PHP_FUNCTION(idate) } /* }}} */ -/* {{{ proto array localtime([int timestamp [, bool associative_array]]) - Returns the results of the C system call localtime as an associative array if the associative_array argument is set to 1 other wise it is a regular array */ -PHP_FUNCTION(localtime) -{ - zval **timestamp_arg, **assoc_array_arg; - struct tm *ta, tmbuf; - time_t timestamp; - int assoc_array = 0; - int arg_count = ZEND_NUM_ARGS(); - - if (arg_count < 0 || arg_count > 2 || - zend_get_parameters_ex(arg_count, ×tamp_arg, &assoc_array_arg) == FAILURE) { - WRONG_PARAM_COUNT; - } - - switch (arg_count) { - case 0: - timestamp = (long)time(NULL); - break; - case 1: - convert_to_long_ex(timestamp_arg); - timestamp = Z_LVAL_PP(timestamp_arg); - break; - case 2: - convert_to_long_ex(timestamp_arg); - convert_to_long_ex(assoc_array_arg); - timestamp = Z_LVAL_PP(timestamp_arg); - assoc_array = Z_LVAL_PP(assoc_array_arg); - break; - } - -#ifdef PHP_WIN32 - if (timestamp < 0) { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "Windows does not support negative values for this function"); - RETURN_FALSE - } -#endif - - if (NULL == (ta = php_localtime_r(×tamp, &tmbuf))) { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid local time"); - RETURN_FALSE; - } - array_init(return_value); - - if (assoc_array) { - add_assoc_long(return_value, "tm_sec", ta->tm_sec); - add_assoc_long(return_value, "tm_min", ta->tm_min); - add_assoc_long(return_value, "tm_hour", ta->tm_hour); - add_assoc_long(return_value, "tm_mday", ta->tm_mday); - add_assoc_long(return_value, "tm_mon", ta->tm_mon); - add_assoc_long(return_value, "tm_year", ta->tm_year); - add_assoc_long(return_value, "tm_wday", ta->tm_wday); - add_assoc_long(return_value, "tm_yday", ta->tm_yday); - add_assoc_long(return_value, "tm_isdst", ta->tm_isdst); - } else { - add_next_index_long(return_value, ta->tm_sec); - add_next_index_long(return_value, ta->tm_min); - add_next_index_long(return_value, ta->tm_hour); - add_next_index_long(return_value, ta->tm_mday); - add_next_index_long(return_value, ta->tm_mon); - add_next_index_long(return_value, ta->tm_year); - add_next_index_long(return_value, ta->tm_wday); - add_next_index_long(return_value, ta->tm_yday); - add_next_index_long(return_value, ta->tm_isdst); - } -} -/* }}} */ - -/* {{{ proto array getdate([int timestamp]) - Get date/time information */ -PHP_FUNCTION(getdate) -{ - pval **timestamp_arg; - struct tm *ta, tmbuf; - time_t timestamp; - - if (ZEND_NUM_ARGS() == 0) { - timestamp = time(NULL); - } else if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, ×tamp_arg) == FAILURE) { - WRONG_PARAM_COUNT; - } else { - convert_to_long_ex(timestamp_arg); - timestamp = Z_LVAL_PP(timestamp_arg); - } - - ta = php_localtime_r(×tamp, &tmbuf); - if (!ta) { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot perform date calculation"); - return; - } - array_init(return_value); - add_assoc_long(return_value, "seconds", ta->tm_sec); - add_assoc_long(return_value, "minutes", ta->tm_min); - add_assoc_long(return_value, "hours", ta->tm_hour); - add_assoc_long(return_value, "mday", ta->tm_mday); - add_assoc_long(return_value, "wday", ta->tm_wday); - add_assoc_long(return_value, "mon", ta->tm_mon + 1); - add_assoc_long(return_value, "year", ta->tm_year + 1900); - add_assoc_long(return_value, "yday", ta->tm_yday); - add_assoc_string(return_value, "weekday", day_full_names[ta->tm_wday], 1); - add_assoc_string(return_value, "month", mon_full_names[ta->tm_mon], 1); - add_index_long(return_value, 0, timestamp); -} -/* }}} */ - /* {{{ php_std_date Return date string in standard format for http headers */ PHPAPI char *php_std_date(time_t t TSRMLS_DC) |