mirror of
https://github.com/php/php-src.git
synced 2025-08-16 05:58:45 +02:00
- Reimplemented time(), getdate() and localtime() functions with new datetime
library.
This commit is contained in:
parent
0ef991e5ae
commit
8aa3554d19
4 changed files with 101 additions and 117 deletions
|
@ -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;
|
||||
|
|
|
@ -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);
|
||||
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue