/ext/date/php_date.c
C | 4980 lines | 3689 code | 743 blank | 548 comment | 623 complexity | ed96ad48232b445354bc8eb1598b541e MD5 | raw file
Possible License(s): BSD-2-Clause, BSD-3-Clause, MPL-2.0-no-copyleft-exception, LGPL-2.1
Large files files are truncated, but you can click here to view the full file
- /*
- +----------------------------------------------------------------------+
- | Copyright (c) The PHP Group |
- +----------------------------------------------------------------------+
- | This source file is subject to version 3.01 of the PHP license, |
- | that is bundled with this package in the file LICENSE, and is |
- | available through the world-wide-web at the following url: |
- | http://www.php.net/license/3_01.txt |
- | If you did not receive a copy of the PHP license and are unable to |
- | obtain it through the world-wide-web, please send a note to |
- | license@php.net so we can mail you a copy immediately. |
- +----------------------------------------------------------------------+
- | Authors: Derick Rethans <derick@derickrethans.nl> |
- +----------------------------------------------------------------------+
- */
- #include "php.h"
- #include "php_streams.h"
- #include "php_main.h"
- #include "php_globals.h"
- #include "php_ini.h"
- #include "ext/standard/info.h"
- #include "ext/standard/php_versioning.h"
- #include "ext/standard/php_math.h"
- #include "php_date.h"
- #include "zend_interfaces.h"
- #include "lib/timelib.h"
- #include "lib/timelib_private.h"
- #ifndef PHP_WIN32
- #include <time.h>
- #else
- #include "win32/time.h"
- #endif
- #ifdef PHP_WIN32
- static __inline __int64 php_date_llabs( __int64 i ) { return i >= 0? i: -i; }
- #elif defined(__GNUC__) && __GNUC__ < 3
- static __inline __int64_t php_date_llabs( __int64_t i ) { return i >= 0 ? i : -i; }
- #else
- static inline long long php_date_llabs( long long i ) { return i >= 0 ? i : -i; }
- #endif
- #ifdef PHP_WIN32
- #define DATE_I64_BUF_LEN 65
- # define DATE_I64A(i, s, len) _i64toa_s(i, s, len, 10)
- # define DATE_A64I(i, s) i = _atoi64(s)
- #else
- #define DATE_I64_BUF_LEN 65
- # define DATE_I64A(i, s, len) \
- do { \
- int st = snprintf(s, len, "%lld", i); \
- s[st] = '\0'; \
- } while (0);
- #ifdef HAVE_ATOLL
- # define DATE_A64I(i, s) i = atoll(s)
- #else
- # define DATE_A64I(i, s) i = strtoll(s, NULL, 10)
- #endif
- #endif
- PHPAPI time_t php_time()
- {
- #ifdef HAVE_GETTIMEOFDAY
- struct timeval tm;
- if (UNEXPECTED(gettimeofday(&tm, NULL) != SUCCESS)) {
- /* fallback, can't reasonably happen */
- return time(NULL);
- }
- return tm.tv_sec;
- #else
- return time(NULL);
- #endif
- }
- #include "php_date_arginfo.h"
- static char* guess_timezone(const timelib_tzdb *tzdb);
- static void date_register_classes(void);
- /* }}} */
- ZEND_DECLARE_MODULE_GLOBALS(date)
- static PHP_GINIT_FUNCTION(date);
- /* True global */
- timelib_tzdb *php_date_global_timezone_db;
- int php_date_global_timezone_db_enabled;
- #define DATE_DEFAULT_LATITUDE "31.7667"
- #define DATE_DEFAULT_LONGITUDE "35.2333"
- /* on 90'50; common sunset declaration (start of sun body appear) */
- #define DATE_SUNSET_ZENITH "90.833333"
- /* on 90'50; common sunrise declaration (sun body disappeared) */
- #define DATE_SUNRISE_ZENITH "90.833333"
- static PHP_INI_MH(OnUpdate_date_timezone);
- /* {{{ INI Settings */
- PHP_INI_BEGIN()
- STD_PHP_INI_ENTRY("date.timezone", "", PHP_INI_ALL, OnUpdate_date_timezone, default_timezone, zend_date_globals, date_globals)
- PHP_INI_ENTRY("date.default_latitude", DATE_DEFAULT_LATITUDE, PHP_INI_ALL, NULL)
- PHP_INI_ENTRY("date.default_longitude", DATE_DEFAULT_LONGITUDE, PHP_INI_ALL, NULL)
- PHP_INI_ENTRY("date.sunset_zenith", DATE_SUNSET_ZENITH, PHP_INI_ALL, NULL)
- PHP_INI_ENTRY("date.sunrise_zenith", DATE_SUNRISE_ZENITH, PHP_INI_ALL, NULL)
- PHP_INI_END()
- /* }}} */
- zend_class_entry *date_ce_date, *date_ce_timezone, *date_ce_interval, *date_ce_period;
- zend_class_entry *date_ce_immutable, *date_ce_interface;
- PHPAPI zend_class_entry *php_date_get_date_ce(void)
- {
- return date_ce_date;
- }
- PHPAPI zend_class_entry *php_date_get_immutable_ce(void)
- {
- return date_ce_immutable;
- }
- PHPAPI zend_class_entry *php_date_get_interface_ce(void)
- {
- return date_ce_interface;
- }
- PHPAPI zend_class_entry *php_date_get_timezone_ce(void)
- {
- return date_ce_timezone;
- }
- PHPAPI zend_class_entry *php_date_get_interval_ce(void)
- {
- return date_ce_interval;
- }
- PHPAPI zend_class_entry *php_date_get_period_ce(void)
- {
- return date_ce_period;
- }
- static zend_object_handlers date_object_handlers_date;
- static zend_object_handlers date_object_handlers_immutable;
- static zend_object_handlers date_object_handlers_timezone;
- static zend_object_handlers date_object_handlers_interval;
- static zend_object_handlers date_object_handlers_period;
- #define DATE_CHECK_INITIALIZED(member, class_name) \
- if (!(member)) { \
- zend_throw_error(NULL, "The " #class_name " object has not been correctly initialized by its constructor"); \
- RETURN_THROWS(); \
- }
- static void date_object_free_storage_date(zend_object *object);
- static void date_object_free_storage_timezone(zend_object *object);
- static void date_object_free_storage_interval(zend_object *object);
- static void date_object_free_storage_period(zend_object *object);
- static zend_object *date_object_new_date(zend_class_entry *class_type);
- static zend_object *date_object_new_timezone(zend_class_entry *class_type);
- static zend_object *date_object_new_interval(zend_class_entry *class_type);
- static zend_object *date_object_new_period(zend_class_entry *class_type);
- static zend_object *date_object_clone_date(zend_object *this_ptr);
- static zend_object *date_object_clone_timezone(zend_object *this_ptr);
- static zend_object *date_object_clone_interval(zend_object *this_ptr);
- static zend_object *date_object_clone_period(zend_object *this_ptr);
- static int date_object_compare_date(zval *d1, zval *d2);
- static HashTable *date_object_get_gc(zend_object *object, zval **table, int *n);
- static HashTable *date_object_get_properties_for(zend_object *object, zend_prop_purpose purpose);
- static HashTable *date_object_get_gc_interval(zend_object *object, zval **table, int *n);
- static HashTable *date_object_get_properties_interval(zend_object *object);
- static HashTable *date_object_get_gc_period(zend_object *object, zval **table, int *n);
- static HashTable *date_object_get_properties_period(zend_object *object);
- static HashTable *date_object_get_properties_for_timezone(zend_object *object, zend_prop_purpose purpose);
- static HashTable *date_object_get_gc_timezone(zend_object *object, zval **table, int *n);
- static HashTable *date_object_get_debug_info_timezone(zend_object *object, int *is_temp);
- static void php_timezone_to_string(php_timezone_obj *tzobj, zval *zv);
- static int date_interval_compare_objects(zval *o1, zval *o2);
- static zval *date_interval_read_property(zend_object *object, zend_string *member, int type, void **cache_slot, zval *rv);
- static zval *date_interval_write_property(zend_object *object, zend_string *member, zval *value, void **cache_slot);
- static zval *date_interval_get_property_ptr_ptr(zend_object *object, zend_string *member, int type, void **cache_slot);
- static zval *date_period_read_property(zend_object *object, zend_string *name, int type, void **cache_slot, zval *rv);
- static zval *date_period_write_property(zend_object *object, zend_string *name, zval *value, void **cache_slot);
- static zval *date_period_get_property_ptr_ptr(zend_object *object, zend_string *name, int type, void **cache_slot);
- static int date_object_compare_timezone(zval *tz1, zval *tz2);
- /* {{{ Module struct */
- zend_module_entry date_module_entry = {
- STANDARD_MODULE_HEADER_EX,
- NULL,
- NULL,
- "date", /* extension name */
- ext_functions, /* function list */
- PHP_MINIT(date), /* process startup */
- PHP_MSHUTDOWN(date), /* process shutdown */
- PHP_RINIT(date), /* request startup */
- PHP_RSHUTDOWN(date), /* request shutdown */
- PHP_MINFO(date), /* extension info */
- PHP_DATE_VERSION, /* extension version */
- PHP_MODULE_GLOBALS(date), /* globals descriptor */
- PHP_GINIT(date), /* globals ctor */
- NULL, /* globals dtor */
- NULL, /* post deactivate */
- STANDARD_MODULE_PROPERTIES_EX
- };
- /* }}} */
- /* {{{ PHP_GINIT_FUNCTION */
- static PHP_GINIT_FUNCTION(date)
- {
- date_globals->default_timezone = NULL;
- date_globals->timezone = NULL;
- date_globals->tzcache = NULL;
- date_globals->timezone_valid = 0;
- }
- /* }}} */
- static void _php_date_tzinfo_dtor(zval *zv) /* {{{ */
- {
- timelib_tzinfo *tzi = (timelib_tzinfo*)Z_PTR_P(zv);
- timelib_tzinfo_dtor(tzi);
- } /* }}} */
- /* {{{ PHP_RINIT_FUNCTION */
- PHP_RINIT_FUNCTION(date)
- {
- if (DATEG(timezone)) {
- efree(DATEG(timezone));
- }
- DATEG(timezone) = NULL;
- DATEG(tzcache) = NULL;
- DATEG(last_errors) = NULL;
- return SUCCESS;
- }
- /* }}} */
- /* {{{ PHP_RSHUTDOWN_FUNCTION */
- PHP_RSHUTDOWN_FUNCTION(date)
- {
- if (DATEG(timezone)) {
- efree(DATEG(timezone));
- }
- DATEG(timezone) = NULL;
- if(DATEG(tzcache)) {
- zend_hash_destroy(DATEG(tzcache));
- FREE_HASHTABLE(DATEG(tzcache));
- DATEG(tzcache) = NULL;
- }
- if (DATEG(last_errors)) {
- timelib_error_container_dtor(DATEG(last_errors));
- DATEG(last_errors) = NULL;
- }
- return SUCCESS;
- }
- /* }}} */
- #define DATE_TIMEZONEDB php_date_global_timezone_db ? php_date_global_timezone_db : timelib_builtin_db()
- /*
- * RFC822, Section 5.1: http://www.ietf.org/rfc/rfc822.txt
- * date-time = [ day "," ] date time ; dd mm yy hh:mm:ss zzz
- * day = "Mon" / "Tue" / "Wed" / "Thu" / "Fri" / "Sat" / "Sun"
- * date = 1*2DIGIT month 2DIGIT ; day month year e.g. 20 Jun 82
- * month = "Jan" / "Feb" / "Mar" / "Apr" / "May" / "Jun" / "Jul" / "Aug" / "Sep" / "Oct" / "Nov" / "Dec"
- * time = hour zone ; ANSI and Military
- * hour = 2DIGIT ":" 2DIGIT [":" 2DIGIT] ; 00:00:00 - 23:59:59
- * zone = "UT" / "GMT" / "EST" / "EDT" / "CST" / "CDT" / "MST" / "MDT" / "PST" / "PDT" / 1ALPHA / ( ("+" / "-") 4DIGIT )
- */
- #define DATE_FORMAT_RFC822 "D, d M y H:i:s O"
- /*
- * RFC850, Section 2.1.4: http://www.ietf.org/rfc/rfc850.txt
- * Format must be acceptable both to the ARPANET and to the getdate routine.
- * One format that is acceptable to both is Weekday, DD-Mon-YY HH:MM:SS TIMEZONE
- * TIMEZONE can be any timezone name (3 or more letters)
- */
- #define DATE_FORMAT_RFC850 "l, d-M-y H:i:s T"
- /*
- * RFC1036, Section 2.1.2: http://www.ietf.org/rfc/rfc1036.txt
- * Its format must be acceptable both in RFC-822 and to the getdate(3)
- * Wdy, DD Mon YY HH:MM:SS TIMEZONE
- * There is no hope of having a complete list of timezones. Universal
- * Time (GMT), the North American timezones (PST, PDT, MST, MDT, CST,
- * CDT, EST, EDT) and the +/-hhmm offset specified in RFC-822 should be supported.
- */
- #define DATE_FORMAT_RFC1036 "D, d M y H:i:s O"
- /*
- * RFC1123, Section 5.2.14: http://www.ietf.org/rfc/rfc1123.txt
- * RFC-822 Date and Time Specification: RFC-822 Section 5
- * The syntax for the date is hereby changed to: date = 1*2DIGIT month 2*4DIGIT
- */
- #define DATE_FORMAT_RFC1123 "D, d M Y H:i:s O"
- /*
- * RFC7231, Section 7.1.1: http://tools.ietf.org/html/rfc7231
- */
- #define DATE_FORMAT_RFC7231 "D, d M Y H:i:s \\G\\M\\T"
- /*
- * RFC2822, Section 3.3: http://www.ietf.org/rfc/rfc2822.txt
- * FWS = ([*WSP CRLF] 1*WSP) / ; Folding white space
- * CFWS = *([FWS] comment) (([FWS] comment) / FWS)
- *
- * date-time = [ day-of-week "," ] date FWS time [CFWS]
- * day-of-week = ([FWS] day-name)
- * day-name = "Mon" / "Tue" / "Wed" / "Thu" / "Fri" / "Sat" / "Sun"
- * date = day month year
- * year = 4*DIGIT
- * month = (FWS month-name FWS)
- * month-name = "Jan" / "Feb" / "Mar" / "Apr" / "May" / "Jun" / "Jul" / "Aug" / "Sep" / "Oct" / "Nov" / "Dec"
- * day = ([FWS] 1*2DIGIT)
- * time = time-of-day FWS zone
- * time-of-day = hour ":" minute [ ":" second ]
- * hour = 2DIGIT
- * minute = 2DIGIT
- * second = 2DIGIT
- * zone = (( "+" / "-" ) 4DIGIT)
- */
- #define DATE_FORMAT_RFC2822 "D, d M Y H:i:s O"
- /*
- * RFC3339, Section 5.6: http://www.ietf.org/rfc/rfc3339.txt
- * date-fullyear = 4DIGIT
- * date-month = 2DIGIT ; 01-12
- * date-mday = 2DIGIT ; 01-28, 01-29, 01-30, 01-31 based on month/year
- *
- * time-hour = 2DIGIT ; 00-23
- * time-minute = 2DIGIT ; 00-59
- * time-second = 2DIGIT ; 00-58, 00-59, 00-60 based on leap second rules
- *
- * time-secfrac = "." 1*DIGIT
- * time-numoffset = ("+" / "-") time-hour ":" time-minute
- * time-offset = "Z" / time-numoffset
- *
- * partial-time = time-hour ":" time-minute ":" time-second [time-secfrac]
- * full-date = date-fullyear "-" date-month "-" date-mday
- * full-time = partial-time time-offset
- *
- * date-time = full-date "T" full-time
- */
- #define DATE_FORMAT_RFC3339 "Y-m-d\\TH:i:sP"
- #define DATE_FORMAT_ISO8601 "Y-m-d\\TH:i:sO"
- /*
- * RFC3339, Appendix A: http://www.ietf.org/rfc/rfc3339.txt
- * ISO 8601 also requires (in section 5.3.1.3) that a decimal fraction
- * be proceeded by a "0" if less than unity. Annex B.2 of ISO 8601
- * gives examples where the decimal fractions are not preceded by a "0".
- * This grammar assumes section 5.3.1.3 is correct and that Annex B.2 is
- * in error.
- */
- #define DATE_FORMAT_RFC3339_EXTENDED "Y-m-d\\TH:i:s.vP"
- /*
- * This comes from various sources that like to contradict. I'm going with the
- * format here because of:
- * http://msdn.microsoft.com/en-us/library/windows/desktop/aa384321%28v=vs.85%29.aspx
- * and http://curl.haxx.se/rfc/cookie_spec.html
- */
- #define DATE_FORMAT_COOKIE "l, d-M-Y H:i:s T"
- #define SUNFUNCS_RET_TIMESTAMP 0
- #define SUNFUNCS_RET_STRING 1
- #define SUNFUNCS_RET_DOUBLE 2
- /* {{{ PHP_MINIT_FUNCTION */
- PHP_MINIT_FUNCTION(date)
- {
- REGISTER_INI_ENTRIES();
- date_register_classes();
- /*
- * RFC4287, Section 3.3: http://www.ietf.org/rfc/rfc4287.txt
- * A Date construct is an element whose content MUST conform to the
- * "date-time" production in [RFC3339]. In addition, an uppercase "T"
- * character MUST be used to separate date and time, and an uppercase
- * "Z" character MUST be present in the absence of a numeric time zone offset.
- */
- REGISTER_STRING_CONSTANT("DATE_ATOM", DATE_FORMAT_RFC3339, CONST_CS | CONST_PERSISTENT);
- /*
- * Preliminary specification: http://wp.netscape.com/newsref/std/cookie_spec.html
- * "This is based on RFC 822, RFC 850, RFC 1036, and RFC 1123,
- * with the variations that the only legal time zone is GMT
- * and the separators between the elements of the date must be dashes."
- */
- REGISTER_STRING_CONSTANT("DATE_COOKIE", DATE_FORMAT_COOKIE, CONST_CS | CONST_PERSISTENT);
- REGISTER_STRING_CONSTANT("DATE_ISO8601", DATE_FORMAT_ISO8601, CONST_CS | CONST_PERSISTENT);
- REGISTER_STRING_CONSTANT("DATE_RFC822", DATE_FORMAT_RFC822, CONST_CS | CONST_PERSISTENT);
- REGISTER_STRING_CONSTANT("DATE_RFC850", DATE_FORMAT_RFC850, CONST_CS | CONST_PERSISTENT);
- REGISTER_STRING_CONSTANT("DATE_RFC1036", DATE_FORMAT_RFC1036, CONST_CS | CONST_PERSISTENT);
- REGISTER_STRING_CONSTANT("DATE_RFC1123", DATE_FORMAT_RFC1123, CONST_CS | CONST_PERSISTENT);
- REGISTER_STRING_CONSTANT("DATE_RFC7231", DATE_FORMAT_RFC7231, CONST_CS | CONST_PERSISTENT);
- REGISTER_STRING_CONSTANT("DATE_RFC2822", DATE_FORMAT_RFC2822, CONST_CS | CONST_PERSISTENT);
- REGISTER_STRING_CONSTANT("DATE_RFC3339", DATE_FORMAT_RFC3339, CONST_CS | CONST_PERSISTENT);
- REGISTER_STRING_CONSTANT("DATE_RFC3339_EXTENDED", DATE_FORMAT_RFC3339_EXTENDED, CONST_CS | CONST_PERSISTENT);
- /*
- * RSS 2.0 Specification: http://blogs.law.harvard.edu/tech/rss
- * "All date-times in RSS conform to the Date and Time Specification of RFC 822,
- * with the exception that the year may be expressed with two characters or four characters (four preferred)"
- */
- REGISTER_STRING_CONSTANT("DATE_RSS", DATE_FORMAT_RFC1123, CONST_CS | CONST_PERSISTENT);
- REGISTER_STRING_CONSTANT("DATE_W3C", DATE_FORMAT_RFC3339, CONST_CS | CONST_PERSISTENT);
- REGISTER_LONG_CONSTANT("SUNFUNCS_RET_TIMESTAMP", SUNFUNCS_RET_TIMESTAMP, CONST_CS | CONST_PERSISTENT);
- REGISTER_LONG_CONSTANT("SUNFUNCS_RET_STRING", SUNFUNCS_RET_STRING, CONST_CS | CONST_PERSISTENT);
- REGISTER_LONG_CONSTANT("SUNFUNCS_RET_DOUBLE", SUNFUNCS_RET_DOUBLE, CONST_CS | CONST_PERSISTENT);
- php_date_global_timezone_db = NULL;
- php_date_global_timezone_db_enabled = 0;
- DATEG(last_errors) = NULL;
- return SUCCESS;
- }
- /* }}} */
- /* {{{ PHP_MSHUTDOWN_FUNCTION */
- PHP_MSHUTDOWN_FUNCTION(date)
- {
- UNREGISTER_INI_ENTRIES();
- if (DATEG(last_errors)) {
- timelib_error_container_dtor(DATEG(last_errors));
- }
- #ifndef ZTS
- DATEG(default_timezone) = NULL;
- #endif
- return SUCCESS;
- }
- /* }}} */
- /* {{{ PHP_MINFO_FUNCTION */
- PHP_MINFO_FUNCTION(date)
- {
- const timelib_tzdb *tzdb = DATE_TIMEZONEDB;
- php_info_print_table_start();
- php_info_print_table_row(2, "date/time support", "enabled");
- php_info_print_table_row(2, "timelib version", TIMELIB_ASCII_VERSION);
- php_info_print_table_row(2, "\"Olson\" Timezone Database Version", tzdb->version);
- php_info_print_table_row(2, "Timezone Database", php_date_global_timezone_db_enabled ? "external" : "internal");
- php_info_print_table_row(2, "Default timezone", guess_timezone(tzdb));
- php_info_print_table_end();
- DISPLAY_INI_ENTRIES();
- }
- /* }}} */
- /* {{{ Timezone Cache functions */
- static timelib_tzinfo *php_date_parse_tzfile(char *formal_tzname, const timelib_tzdb *tzdb)
- {
- timelib_tzinfo *tzi;
- int dummy_error_code;
- if(!DATEG(tzcache)) {
- ALLOC_HASHTABLE(DATEG(tzcache));
- zend_hash_init(DATEG(tzcache), 4, NULL, _php_date_tzinfo_dtor, 0);
- }
- if ((tzi = zend_hash_str_find_ptr(DATEG(tzcache), formal_tzname, strlen(formal_tzname))) != NULL) {
- return tzi;
- }
- tzi = timelib_parse_tzfile(formal_tzname, tzdb, &dummy_error_code);
- if (tzi) {
- zend_hash_str_add_ptr(DATEG(tzcache), formal_tzname, strlen(formal_tzname), tzi);
- }
- return tzi;
- }
- timelib_tzinfo *php_date_parse_tzfile_wrapper(char *formal_tzname, const timelib_tzdb *tzdb, int *dummy_error_code)
- {
- return php_date_parse_tzfile(formal_tzname, tzdb);
- }
- /* }}} */
- /* Callback to check the date.timezone only when changed increases performance */
- /* {{{ static PHP_INI_MH(OnUpdate_date_timezone) */
- static PHP_INI_MH(OnUpdate_date_timezone)
- {
- if (OnUpdateString(entry, new_value, mh_arg1, mh_arg2, mh_arg3, stage) == FAILURE) {
- return FAILURE;
- }
- DATEG(timezone_valid) = 0;
- if (stage == PHP_INI_STAGE_RUNTIME) {
- if (!timelib_timezone_id_is_valid(DATEG(default_timezone), DATE_TIMEZONEDB)) {
- if (DATEG(default_timezone) && *DATEG(default_timezone)) {
- php_error_docref(NULL, E_WARNING, "Invalid date.timezone value '%s', we selected the timezone 'UTC' for now.", DATEG(default_timezone));
- }
- } else {
- DATEG(timezone_valid) = 1;
- }
- }
- return SUCCESS;
- }
- /* }}} */
- /* {{{ Helper functions */
- static char* guess_timezone(const timelib_tzdb *tzdb)
- {
- /* Checking configure timezone */
- if (DATEG(timezone) && (strlen(DATEG(timezone))) > 0) {
- return DATEG(timezone);
- }
- /* Check config setting for default timezone */
- if (!DATEG(default_timezone)) {
- /* Special case: ext/date wasn't initialized yet */
- zval *ztz;
- if (NULL != (ztz = cfg_get_entry("date.timezone", sizeof("date.timezone")))
- && Z_TYPE_P(ztz) == IS_STRING && Z_STRLEN_P(ztz) > 0 && timelib_timezone_id_is_valid(Z_STRVAL_P(ztz), tzdb)) {
- return Z_STRVAL_P(ztz);
- }
- } else if (*DATEG(default_timezone)) {
- if (DATEG(timezone_valid) == 1) {
- return DATEG(default_timezone);
- }
- if (!timelib_timezone_id_is_valid(DATEG(default_timezone), tzdb)) {
- php_error_docref(NULL, E_WARNING, "Invalid date.timezone value '%s', we selected the timezone 'UTC' for now.", DATEG(default_timezone));
- return "UTC";
- }
- DATEG(timezone_valid) = 1;
- return DATEG(default_timezone);
- }
- /* Fallback to UTC */
- return "UTC";
- }
- PHPAPI timelib_tzinfo *get_timezone_info(void)
- {
- char *tz;
- timelib_tzinfo *tzi;
- tz = guess_timezone(DATE_TIMEZONEDB);
- tzi = php_date_parse_tzfile(tz, DATE_TIMEZONEDB);
- if (! tzi) {
- php_error_docref(NULL, E_ERROR, "Timezone database is corrupt - this should *never* happen!");
- }
- return tzi;
- }
- /* }}} */
- /* {{{ date() and gmdate() data */
- #include "zend_smart_str.h"
- static const char * const mon_full_names[] = {
- "January", "February", "March", "April",
- "May", "June", "July", "August",
- "September", "October", "November", "December"
- };
- static const char * const mon_short_names[] = {
- "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
- };
- static const char * const day_full_names[] = {
- "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"
- };
- static const char * const day_short_names[] = {
- "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
- };
- static char *english_suffix(timelib_sll number)
- {
- if (number >= 10 && number <= 19) {
- return "th";
- } else {
- switch (number % 10) {
- case 1: return "st";
- case 2: return "nd";
- case 3: return "rd";
- }
- }
- return "th";
- }
- /* }}} */
- /* {{{ day of week helpers */
- static const char *php_date_full_day_name(timelib_sll y, timelib_sll m, timelib_sll d)
- {
- timelib_sll day_of_week = timelib_day_of_week(y, m, d);
- if (day_of_week < 0) {
- return "Unknown";
- }
- return day_full_names[day_of_week];
- }
- static const char *php_date_short_day_name(timelib_sll y, timelib_sll m, timelib_sll d)
- {
- timelib_sll day_of_week = timelib_day_of_week(y, m, d);
- if (day_of_week < 0) {
- return "Unknown";
- }
- return day_short_names[day_of_week];
- }
- /* }}} */
- /* {{{ date_format - (gm)date helper */
- static zend_string *date_format(char *format, size_t format_len, timelib_time *t, int localtime)
- {
- smart_str string = {0};
- size_t i;
- int length = 0;
- char buffer[97];
- timelib_time_offset *offset = NULL;
- timelib_sll isoweek, isoyear;
- int rfc_colon;
- int weekYearSet = 0;
- if (!format_len) {
- return ZSTR_EMPTY_ALLOC();
- }
- if (localtime) {
- if (t->zone_type == TIMELIB_ZONETYPE_ABBR) {
- offset = timelib_time_offset_ctor();
- offset->offset = (t->z + (t->dst * 3600));
- offset->leap_secs = 0;
- offset->is_dst = t->dst;
- offset->abbr = timelib_strdup(t->tz_abbr);
- } else if (t->zone_type == TIMELIB_ZONETYPE_OFFSET) {
- offset = timelib_time_offset_ctor();
- offset->offset = (t->z);
- offset->leap_secs = 0;
- offset->is_dst = 0;
- offset->abbr = timelib_malloc(9); /* GMT±xxxx\0 */
- snprintf(offset->abbr, 9, "GMT%c%02d%02d",
- (offset->offset < 0) ? '-' : '+',
- abs(offset->offset / 3600),
- abs((offset->offset % 3600) / 60));
- } else {
- offset = timelib_get_time_zone_info(t->sse, t->tz_info);
- }
- }
- for (i = 0; i < format_len; i++) {
- rfc_colon = 0;
- switch (format[i]) {
- /* day */
- case 'd': length = slprintf(buffer, sizeof(buffer), "%02d", (int) t->d); break;
- case 'D': length = slprintf(buffer, sizeof(buffer), "%s", php_date_short_day_name(t->y, t->m, t->d)); break;
- case 'j': length = slprintf(buffer, sizeof(buffer), "%d", (int) t->d); break;
- case 'l': length = slprintf(buffer, sizeof(buffer), "%s", php_date_full_day_name(t->y, t->m, t->d)); break;
- case 'S': length = slprintf(buffer, sizeof(buffer), "%s", english_suffix(t->d)); break;
- case 'w': length = slprintf(buffer, sizeof(buffer), "%d", (int) timelib_day_of_week(t->y, t->m, t->d)); break;
- case 'N': length = slprintf(buffer, sizeof(buffer), "%d", (int) timelib_iso_day_of_week(t->y, t->m, t->d)); break;
- case 'z': length = slprintf(buffer, sizeof(buffer), "%d", (int) timelib_day_of_year(t->y, t->m, t->d)); break;
- /* week */
- case 'W':
- if(!weekYearSet) { timelib_isoweek_from_date(t->y, t->m, t->d, &isoweek, &isoyear); weekYearSet = 1; }
- length = slprintf(buffer, sizeof(buffer), "%02d", (int) isoweek); break; /* iso weeknr */
- case 'o':
- if(!weekYearSet) { timelib_isoweek_from_date(t->y, t->m, t->d, &isoweek, &isoyear); weekYearSet = 1; }
- length = slprintf(buffer, sizeof(buffer), ZEND_LONG_FMT, (zend_long) isoyear); break; /* iso year */
- /* month */
- case 'F': length = slprintf(buffer, sizeof(buffer), "%s", mon_full_names[t->m - 1]); break;
- case 'm': length = slprintf(buffer, sizeof(buffer), "%02d", (int) t->m); break;
- case 'M': length = slprintf(buffer, sizeof(buffer), "%s", mon_short_names[t->m - 1]); break;
- case 'n': length = slprintf(buffer, sizeof(buffer), "%d", (int) t->m); break;
- case 't': length = slprintf(buffer, sizeof(buffer), "%d", (int) timelib_days_in_month(t->y, t->m)); break;
- /* year */
- case 'L': length = slprintf(buffer, sizeof(buffer), "%d", timelib_is_leap((int) t->y)); break;
- case 'y': length = slprintf(buffer, sizeof(buffer), "%02d", (int) (t->y % 100)); break;
- case 'Y': length = slprintf(buffer, sizeof(buffer), "%s%04lld", t->y < 0 ? "-" : "", php_date_llabs((timelib_sll) t->y)); break;
- /* time */
- case 'a': length = slprintf(buffer, sizeof(buffer), "%s", t->h >= 12 ? "pm" : "am"); break;
- case 'A': length = slprintf(buffer, sizeof(buffer), "%s", t->h >= 12 ? "PM" : "AM"); break;
- case 'B': {
- int retval = ((((long)t->sse)-(((long)t->sse) - ((((long)t->sse) % 86400) + 3600))) * 10);
- if (retval < 0) {
- retval += 864000;
- }
- /* Make sure to do this on a positive int to avoid rounding errors */
- retval = (retval / 864) % 1000;
- length = slprintf(buffer, sizeof(buffer), "%03d", retval);
- break;
- }
- case 'g': length = slprintf(buffer, sizeof(buffer), "%d", (t->h % 12) ? (int) t->h % 12 : 12); break;
- case 'G': length = slprintf(buffer, sizeof(buffer), "%d", (int) t->h); break;
- case 'h': length = slprintf(buffer, sizeof(buffer), "%02d", (t->h % 12) ? (int) t->h % 12 : 12); break;
- case 'H': length = slprintf(buffer, sizeof(buffer), "%02d", (int) t->h); break;
- case 'i': length = slprintf(buffer, sizeof(buffer), "%02d", (int) t->i); break;
- case 's': length = slprintf(buffer, sizeof(buffer), "%02d", (int) t->s); break;
- case 'u': length = slprintf(buffer, sizeof(buffer), "%06d", (int) floor(t->us)); break;
- case 'v': length = slprintf(buffer, sizeof(buffer), "%03d", (int) floor(t->us / 1000)); break;
- /* timezone */
- case 'I': length = slprintf(buffer, sizeof(buffer), "%d", localtime ? offset->is_dst : 0); break;
- case 'P': rfc_colon = 1; /* break intentionally missing */
- case 'O': length = slprintf(buffer, sizeof(buffer), "%c%02d%s%02d",
- localtime ? ((offset->offset < 0) ? '-' : '+') : '+',
- localtime ? abs(offset->offset / 3600) : 0,
- rfc_colon ? ":" : "",
- localtime ? abs((offset->offset % 3600) / 60) : 0
- );
- break;
- case 'T': length = slprintf(buffer, sizeof(buffer), "%s", localtime ? offset->abbr : "GMT"); break;
- case 'e': if (!localtime) {
- length = slprintf(buffer, sizeof(buffer), "%s", "UTC");
- } else {
- switch (t->zone_type) {
- case TIMELIB_ZONETYPE_ID:
- length = slprintf(buffer, sizeof(buffer), "%s", t->tz_info->name);
- break;
- case TIMELIB_ZONETYPE_ABBR:
- length = slprintf(buffer, sizeof(buffer), "%s", offset->abbr);
- break;
- case TIMELIB_ZONETYPE_OFFSET:
- length = slprintf(buffer, sizeof(buffer), "%c%02d:%02d",
- ((offset->offset < 0) ? '-' : '+'),
- abs(offset->offset / 3600),
- abs((offset->offset % 3600) / 60)
- );
- break;
- }
- }
- break;
- case 'Z': length = slprintf(buffer, sizeof(buffer), "%d", localtime ? offset->offset : 0); break;
- /* full date/time */
- case 'c': length = slprintf(buffer, sizeof(buffer), "%04" ZEND_LONG_FMT_SPEC "-%02d-%02dT%02d:%02d:%02d%c%02d:%02d",
- (zend_long) t->y, (int) t->m, (int) t->d,
- (int) t->h, (int) t->i, (int) t->s,
- localtime ? ((offset->offset < 0) ? '-' : '+') : '+',
- localtime ? abs(offset->offset / 3600) : 0,
- localtime ? abs((offset->offset % 3600) / 60) : 0
- );
- break;
- case 'r': length = slprintf(buffer, sizeof(buffer), "%3s, %02d %3s %04" ZEND_LONG_FMT_SPEC " %02d:%02d:%02d %c%02d%02d",
- php_date_short_day_name(t->y, t->m, t->d),
- (int) t->d, mon_short_names[t->m - 1],
- (zend_long) t->y, (int) t->h, (int) t->i, (int) t->s,
- localtime ? ((offset->offset < 0) ? '-' : '+') : '+',
- localtime ? abs(offset->offset / 3600) : 0,
- localtime ? abs((offset->offset % 3600) / 60) : 0
- );
- break;
- case 'U': length = slprintf(buffer, sizeof(buffer), "%lld", (timelib_sll) t->sse); break;
- case '\\': if (i < format_len) i++; /* break intentionally missing */
- default: buffer[0] = format[i]; buffer[1] = '\0'; length = 1; break;
- }
- smart_str_appendl(&string, buffer, length);
- }
- smart_str_0(&string);
- if (localtime) {
- timelib_time_offset_dtor(offset);
- }
- return string.s;
- }
- static void php_date(INTERNAL_FUNCTION_PARAMETERS, int localtime)
- {
- zend_string *format;
- zend_long ts;
- zend_bool ts_is_null = 1;
- ZEND_PARSE_PARAMETERS_START(1, 2)
- Z_PARAM_STR(format)
- Z_PARAM_OPTIONAL
- Z_PARAM_LONG_OR_NULL(ts, ts_is_null)
- ZEND_PARSE_PARAMETERS_END();
- if (ts_is_null) {
- ts = php_time();
- }
- RETURN_STR(php_format_date(ZSTR_VAL(format), ZSTR_LEN(format), ts, localtime));
- }
- /* }}} */
- PHPAPI zend_string *php_format_date(char *format, size_t format_len, time_t ts, int localtime) /* {{{ */
- {
- timelib_time *t;
- timelib_tzinfo *tzi;
- zend_string *string;
- t = timelib_time_ctor();
- if (localtime) {
- tzi = get_timezone_info();
- t->tz_info = tzi;
- t->zone_type = TIMELIB_ZONETYPE_ID;
- timelib_unixtime2local(t, ts);
- } else {
- tzi = NULL;
- timelib_unixtime2gmt(t, ts);
- }
- string = date_format(format, format_len, t, localtime);
- timelib_time_dtor(t);
- return string;
- }
- /* }}} */
- /* {{{ php_idate
- */
- PHPAPI int php_idate(char format, time_t ts, int localtime)
- {
- timelib_time *t;
- timelib_tzinfo *tzi;
- int retval = -1;
- timelib_time_offset *offset = NULL;
- timelib_sll isoweek, isoyear;
- t = timelib_time_ctor();
- if (!localtime) {
- tzi = get_timezone_info();
- t->tz_info = tzi;
- t->zone_type = TIMELIB_ZONETYPE_ID;
- timelib_unixtime2local(t, ts);
- } else {
- tzi = NULL;
- timelib_unixtime2gmt(t, ts);
- }
- if (!localtime) {
- if (t->zone_type == TIMELIB_ZONETYPE_ABBR) {
- offset = timelib_time_offset_ctor();
- offset->offset = (t->z + (t->dst * 3600));
- offset->leap_secs = 0;
- offset->is_dst = t->dst;
- offset->abbr = timelib_strdup(t->tz_abbr);
- } else if (t->zone_type == TIMELIB_ZONETYPE_OFFSET) {
- offset = timelib_time_offset_ctor();
- offset->offset = (t->z + (t->dst * 3600));
- offset->leap_secs = 0;
- offset->is_dst = t->dst;
- offset->abbr = timelib_malloc(9); /* GMT±xxxx\0 */
- snprintf(offset->abbr, 9, "GMT%c%02d%02d",
- (offset->offset < 0) ? '-' : '+',
- abs(offset->offset / 3600),
- abs((offset->offset % 3600) / 60));
- } else {
- offset = timelib_get_time_zone_info(t->sse, t->tz_info);
- }
- }
- timelib_isoweek_from_date(t->y, t->m, t->d, &isoweek, &isoyear);
- switch (format) {
- /* day */
- case 'd': case 'j': retval = (int) t->d; break;
- case 'w': retval = (int) timelib_day_of_week(t->y, t->m, t->d); break;
- case 'z': retval = (int) timelib_day_of_year(t->y, t->m, t->d); break;
- /* week */
- case 'W': retval = (int) isoweek; break; /* iso weeknr */
- /* month */
- case 'm': case 'n': retval = (int) t->m; break;
- case 't': retval = (int) timelib_days_in_month(t->y, t->m); break;
- /* year */
- case 'L': retval = (int) timelib_is_leap((int) t->y); break;
- case 'y': retval = (int) (t->y % 100); break;
- case 'Y': retval = (int) t->y; break;
- /* Swatch Beat a.k.a. Internet Time */
- case 'B':
- retval = ((((long)t->sse)-(((long)t->sse) - ((((long)t->sse) % 86400) + 3600))) * 10);
- if (retval < 0) {
- retval += 864000;
- }
- /* Make sure to do this on a positive int to avoid rounding errors */
- retval = (retval / 864) % 1000;
- break;
- /* time */
- case 'g': case 'h': retval = (int) ((t->h % 12) ? (int) t->h % 12 : 12); break;
- case 'H': case 'G': retval = (int) t->h; break;
- case 'i': retval = (int) t->i; break;
- case 's': retval = (int) t->s; break;
- /* timezone */
- case 'I': retval = (int) (!localtime ? offset->is_dst : 0); break;
- case 'Z': retval = (int) (!localtime ? offset->offset : 0); break;
- case 'U': retval = (int) t->sse; break;
- }
- if (!localtime) {
- timelib_time_offset_dtor(offset);
- }
- timelib_time_dtor(t);
- return retval;
- }
- /* }}} */
- /* {{{ proto string date(string format [, int timestamp])
- Format a local date/time */
- PHP_FUNCTION(date)
- {
- php_date(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
- }
- /* }}} */
- /* {{{ proto string gmdate(string format [, int timestamp])
- Format a GMT date/time */
- PHP_FUNCTION(gmdate)
- {
- php_date(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
- }
- /* }}} */
- /* {{{ proto int idate(string format [, int timestamp])
- Format a local time/date as integer */
- PHP_FUNCTION(idate)
- {
- zend_string *format;
- zend_long ts;
- zend_bool ts_is_null = 1;
- int ret;
- ZEND_PARSE_PARAMETERS_START(1, 2)
- Z_PARAM_STR(format)
- Z_PARAM_OPTIONAL
- Z_PARAM_LONG_OR_NULL(ts, ts_is_null)
- ZEND_PARSE_PARAMETERS_END();
- if (ZSTR_LEN(format) != 1) {
- php_error_docref(NULL, E_WARNING, "idate format is one char");
- RETURN_FALSE;
- }
- if (ts_is_null) {
- ts = php_time();
- }
- ret = php_idate(ZSTR_VAL(format)[0], ts, 0);
- if (ret == -1) {
- php_error_docref(NULL, E_WARNING, "Unrecognized date format token.");
- RETURN_FALSE;
- }
- RETURN_LONG(ret);
- }
- /* }}} */
- /* {{{ php_date_set_tzdb - NOT THREADSAFE */
- PHPAPI void php_date_set_tzdb(timelib_tzdb *tzdb)
- {
- const timelib_tzdb *builtin = timelib_builtin_db();
- if (php_version_compare(tzdb->version, builtin->version) > 0) {
- php_date_global_timezone_db = tzdb;
- php_date_global_timezone_db_enabled = 1;
- }
- }
- /* }}} */
- /* {{{ php_parse_date: Backwards compatibility function */
- PHPAPI zend_long php_parse_date(char *string, zend_long *now)
- {
- timelib_time *parsed_time;
- timelib_error_container *error = NULL;
- int error2;
- zend_long retval;
- parsed_time = timelib_strtotime(string, strlen(string), &error, DATE_TIMEZONEDB, php_date_parse_tzfile_wrapper);
- if (error->error_count) {
- timelib_time_dtor(parsed_time);
- timelib_error_container_dtor(error);
- return -1;
- }
- timelib_error_container_dtor(error);
- timelib_update_ts(parsed_time, NULL);
- retval = timelib_date_to_int(parsed_time, &error2);
- timelib_time_dtor(parsed_time);
- if (error2) {
- return -1;
- }
- return retval;
- }
- /* }}} */
- /* {{{ proto int strtotime(string time [, int now ])
- Convert string representation of date and time to a timestamp */
- PHP_FUNCTION(strtotime)
- {
- zend_string *times;
- int error1, error2;
- timelib_error_container *error;
- zend_long preset_ts, ts;
- zend_bool preset_ts_is_null = 1;
- timelib_time *t, *now;
- timelib_tzinfo *tzi;
- ZEND_PARSE_PARAMETERS_START(1, 2)
- Z_PARAM_STR(times)
- Z_PARAM_OPTIONAL
- Z_PARAM_LONG_OR_NULL(preset_ts, preset_ts_is_null)
- ZEND_PARSE_PARAMETERS_END();
- tzi = get_timezone_info();
- now = timelib_time_ctor();
- now->tz_info = tzi;
- now->zone_type = TIMELIB_ZONETYPE_ID;
- timelib_unixtime2local(now,
- !preset_ts_is_null ? (timelib_sll) preset_ts : (timelib_sll) php_time());
- t = timelib_strtotime(ZSTR_VAL(times), ZSTR_LEN(times), &error,
- DATE_TIMEZONEDB, php_date_parse_tzfile_wrapper);
- error1 = error->error_count;
- timelib_error_container_dtor(error);
- timelib_fill_holes(t, now, TIMELIB_NO_CLONE);
- timelib_update_ts(t, tzi);
- ts = timelib_date_to_int(t, &error2);
- timelib_time_dtor(now);
- timelib_time_dtor(t);
- if (error1 || error2) {
- RETURN_FALSE;
- } else {
- RETURN_LONG(ts);
- }
- }
- /* }}} */
- /* {{{ php_mktime - (gm)mktime helper */
- PHPAPI void php_mktime(INTERNAL_FUNCTION_PARAMETERS, int gmt)
- {
- zend_long hou, min, sec, mon, day, yea;
- zend_bool min_is_null = 1, sec_is_null = 1, mon_is_null = 1, day_is_null = 1, yea_is_null = 1;
- timelib_time *now;
- timelib_tzinfo *tzi = NULL;
- zend_long ts, adjust_seconds = 0;
- int error;
- ZEND_PARSE_PARAMETERS_START(1, 6)
- Z_PARAM_LONG(hou)
- Z_PARAM_OPTIONAL
- Z_PARAM_LONG_OR_NULL(min, min_is_null)
- Z_PARAM_LONG_OR_NULL(sec, sec_is_null)
- Z_PARAM_LONG_OR_NULL(mon, mon_is_null)
- Z_PARAM_LONG_OR_NULL(day, day_is_null)
- Z_PARAM_LONG_OR_NULL(yea, yea_is_null)
- ZEND_PARSE_PARAMETERS_END();
- /* Initialize structure with current time */
- now = timelib_time_ctor();
- if (gmt) {
- timelib_unixtime2gmt(now, (timelib_sll) php_time());
- } else {
- tzi = get_timezone_info();
- now->tz_info = tzi;
- now->zone_type = TIMELIB_ZONETYPE_ID;
- timelib_unixtime2local(now, (timelib_sll) php_time());
- }
- now->h = hou;
- if (!min_is_null) {
- now->i = min;
- }
- if (!sec_is_null) {
- now->s = sec;
- }
- if (!mon_is_null) {
- now->m = mon;
- }
- if (!day_is_null) {
- now->d = day;
- }
- if (!yea_is_null) {
- if (yea >= 0 && yea < 70) {
- yea += 2000;
- } else if (yea >= 70 && yea <= 100) {
- yea += 1900;
- }
- now->y = yea;
- }
- /* Update the timestamp */
- if (gmt) {
- timelib_update_ts(now, NULL);
- } else {
- timelib_update_ts(now, tzi);
- }
- /* Clean up and return */
- ts = timelib_date_to_int(now, &error);
- ts += adjust_seconds;
- timelib_time_dtor(now);
- if (error) {
- RETURN_FALSE;
- } else {
- RETURN_LONG(ts);
- }
- }
- /* }}} */
- /* {{{ proto int mktime(int hour [, int min [, int sec [, int mon [, int day [, int year]]]]])
- Get UNIX timestamp for a date */
- PHP_FUNCTION(mktime)
- {
- php_mktime(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
- }
- /* }}} */
- /* {{{ proto int gmmktime(int hour [, int min [, int sec [, int mon [, int day [, int year]]]]])
- Get UNIX timestamp for a GMT date */
- PHP_FUNCTION(gmmktime)
- {
- php_mktime(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
- }
- /* }}} */
- /* {{{ proto bool checkdate(int month, int day, int year)
- Returns true(1) if it is a valid date in gregorian calendar */
- PHP_FUNCTION(checkdate)
- {
- zend_long m, d, y;
- ZEND_PARSE_PARAMETERS_START(3, 3)
- Z_PARAM_LONG(m)
- Z_PARAM_LONG(d)
- Z_PARAM_LONG(y)
- ZEND_PARSE_PARAMETERS_END();
- if (y < 1 || y > 32767 || !timelib_valid_date(y, m, d)) {
- RETURN_FALSE;
- }
- RETURN_TRUE; /* True : This month, day, year arguments are valid */
- }
- /* }}} */
- /* {{{ php_strftime - (gm)strftime helper */
- PHPAPI void php_strftime(INTERNAL_FUNCTION_PARAMETERS, int gmt)
- {
- zend_string *format;
- zend_long timestamp;
- zend_bool timestamp_is_null = 1;
- struct tm ta;
- int max_reallocs = 5;
- size_t buf_len = 256, real_len;
- timelib_time *ts;
- timelib_tzinfo *tzi;
- timelib_time_offset *offset = NULL;
- zend_string *buf;
- ZEND_PARSE_PARAMETERS_START(1, 2)
- Z_PARAM_STR(format)
- Z_PARAM_OPTIONAL
- Z_PARAM_LONG_OR_NULL(timestamp, timestamp_is_null)
- ZEND_PARSE_PARAMETERS_END();
- if (ZSTR_LEN(format) == 0) {
- RETURN_FALSE;
- }
- if (timestamp_is_null) {
- timestamp = (zend_long) php_time();
- }
- ts = timelib_time_ctor();
- if (gmt) {
- tzi = NULL;
- timelib_unixtime2gmt(ts, (timelib_sll) timestamp);
- } else {
- tzi = get_timezone_info();
- ts->tz_info = tzi;
- ts->zone_type = TIMELIB_ZONETYPE_ID;
- timelib_unixtime2local(ts, (timelib_sll) timestamp);
- }
- ta.tm_sec = ts->s;
- ta.tm_min = ts->i;
- ta.tm_hour = ts->h;
- ta.tm_mday = ts->d;
- ta.tm_mon = ts->m - 1;
- ta.tm_year = ts->y - 1900;
- ta.tm_wday = timelib_day_of_week(ts->y, ts->m, ts->d);
- ta.tm_yday = timelib_day_of_year(ts->y, ts->m, ts->d);
- if (gmt) {
- ta.tm_isdst = 0;
- #if HAVE_STRUCT_TM_TM_GMTOFF
- ta.tm_gmtoff = 0;
- #endif
- #if HAVE_STRUCT_TM_TM_ZONE
- ta.tm_zone = "GMT";
- #endif
- } else {
- offset = timelib_get_time_zone_info(timestamp, tzi);
- ta.tm_isdst = offset->is_dst;
- #if HAVE_STRUCT_TM_TM_GMTOFF
- ta.tm_gmtoff = offset->offset;
- #endif
- #if HAVE_STRUCT_TM_TM_ZONE
- ta.tm_zone = offset->abbr;
- #endif
- }
- /* VS2012 crt has a bug where strftime crash with %z and %Z format when the
- initial buffer is too small. See
- http://connect.microsoft.com/VisualStudio/feedback/details/759720/vs2012-strftime-crash-with-z-formatting-code */
- buf = zend_string_alloc(buf_len, 0);
- while ((real_len = strftime(ZSTR_VAL(buf), buf_len, ZSTR_VAL(format), &ta)) == buf_len || real_len == 0) {
- buf_len *= 2;
- buf = zend_string_extend(buf, buf_len, 0);
- if (!--max_reallocs) {
- break;
- }
- }
- #ifdef PHP_WIN32
- /* VS2012 strftime() returns number of characters, not bytes.
- See VC++11 bug id 766205. */
- if (real_len > 0) {
- real_len = strlen(buf->val);
- }
- #endif
- timelib_time_dtor(ts);
- if (!gmt) {
- timelib_time_offset_dtor(offset);
- }
- if (real_len && real_len != buf_len) {
- buf = zend_string_truncate(buf, real_len, 0);
- RETURN_NEW_STR(buf);
- }
- zend_string_efree(buf);
- RETURN_FALSE;
- }
- /* }}} */
- /* {{{ proto string strftime(string format [, int timestamp])
- Format a local time/date according to locale settings */
- PHP_FUNCTION(strftime)
- {
- php_strftime(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
- }
- /* }}} */
- /* {{{ proto string gmstrftime(string format [, int timestamp])
- Format a GMT/UCT time/date according to locale settings */
- PHP_FUNCTION(gmstrftime)
- {
- php_strftime(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
- }
- /* }}} */
- /* {{{ proto int time(void)
- Return current UNIX timestamp */
- PHP_FUNCTION(time)
- {
- ZEND_PARSE_PARAMETERS_NONE();
- RETURN_LONG((zend_long)php_time());
- }
- /* }}} */
- /* {{{ 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)
- {
- zend_long timestamp;
- zend_bool timestamp_is_null = 1;
- zend_bool associative = 0;
- timelib_tzinfo *tzi;
- timelib_time *ts;
- ZEND_PARSE_PARAMETERS_START(0, 2)
- Z_PARAM_OPTIONAL
- Z_PARAM_LONG_OR_NULL(timestamp, timestamp_is_null)
- Z_PARAM_BOOL(associative)
- ZEND_PARSE_PARAMETERS_END();
- if (timestamp_is_null) {
- timestamp = (zend_long) php_time();
- }
- tzi = get_timezone_info();
- ts = timelib_time_ctor();
- ts->tz_info = tzi;
- ts->zone_type = TIMELIB_ZONETYPE_ID;
- timelib_unixtime2local(ts, (timelib_sll) timestamp);
- 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)
- {
- zend_long timestamp;
- zend_bool timestamp_is_null = 1;
- timelib_tzinfo *tzi;
- timelib_time *ts;
- ZEND_PARSE_PARAMETERS_START(0, 1)
- Z_PARAM_OPTIONAL
- Z_PARAM_LONG_OR_NULL(timestamp, timestamp_is_null)
- ZEND_PARSE_PARAMETERS_END();
- if (timestamp_is_null) {
- timestamp = (zend_long) php_time();
- }
- tzi = get_timezone_info();
- ts = timelib_time_ctor();
- ts->tz_info = tzi;
- ts->zone_type = TIMELIB_ZONETYPE_ID;
- timelib_unixtime2local(ts, (timelib_sll) timestamp);
- 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", php_date_full_day_name(ts->y, ts->m, ts->d));
- add_assoc_string(return_value, "month", mon_full_names[ts->m - 1]);
- add_index_long(return_value, 0, timestamp);
- timelib_time_dtor(ts);
- }
- /* }}} */
- #define PHP_DATE_TIMEZONE_GROUP_AFRICA 0x0001
- #define PHP_DATE_TIMEZONE_GROUP_AMERICA 0x0002
- #define PHP_DATE_TIMEZONE_GROUP_ANTARCTICA 0x0004
- #define PHP_DATE_TIMEZONE_GROUP_ARCTIC 0x0008
- #define PHP_DATE_TIMEZONE_GROUP_ASIA 0x0010
- #define PHP_DATE_TIMEZONE_GROUP_ATLANTIC 0x0020
- #define PHP_DATE_TIMEZONE_GROUP_AUSTRALIA 0x0040
- #define PHP_DATE_TIMEZONE_GROUP_EUROPE 0x0080
- #define PHP_DATE_TIMEZONE_GROUP_INDIAN 0x0100
- #define PHP_DATE_TIMEZONE_GROUP_PACIFIC 0x0200
- #define PHP_DATE_TIMEZONE_GROUP_UTC 0x0400
- #define PHP_DATE_TIMEZONE_GROUP_ALL 0x07FF
- #define PHP_DATE_TIMEZONE_GROUP_ALL_W_BC 0x0FFF
- #define PHP_DATE_TIMEZONE_PER_COUNTRY 0x1000
- #define PHP_DATE_PERIOD_EXCLUDE_START_DATE 0x0001
- /* define an overloaded iterator structure */
- typedef struct {
- zend_object_iterator intern;
- zval current;
- php_period_obj *object;
- int current_index;
- } date_period_it;
- /* {{{ date_period_it_invalidate_current */
- static void date_period_it_invalidate_current(zend_object_iterator *iter)
- {
- date_period_it *iterator = (date_period_it *)iter;
- if (Z_TYPE(iterator->current) != IS_UNDEF) {
- zval_ptr_dtor(&iterator->current);
- ZVAL_UNDEF(&iterator->current);
- }
- }
- /* }}} */
- /* {{{ date_period_it_dtor */
- static void date_period_it_dtor(zend_object_iterator *iter)
- {
- date_period_it *iterator = (date_period_it *)iter;
- date_period_it_invalidate_current(iter);
- zval_ptr_dtor(&iterator->intern.data);
- }
- /* }}} */
- /* {{{ date_period_it_has_more */
- static int date_period_it_has_more(zend_object_iterator *iter)
- {
- date_period_it *iterator = (date_period_it *)iter;
- php_period_obj *object = Z_PHPPERIOD_P(&iterator->intern.data);
- timelib_time *it_time = object->current;
- /* apply modification if it's not the first iteration */
- if (!object->include_start_date || iterator->current_index > 0) {
- it_time->have_relative = 1;
- it_time->relative = *object->interval;
- it_time->sse_uptodate = 0;
- timelib_update_ts(it_time, NULL);
- timelib_update_from_sse(it_time);
- }
- if (object->end) {
- return object->current->sse < object->end->sse ? SUCCESS : FAILURE;
- } else {
- return (iterator->current_index < object->recurrences) ? SUCCESS : FAILURE;
- }
- }
- /* }}} */
- /* {{{ date_period_it_current_data */
- static zval *date_period_it_current_data(zend_object_iterator *iter)
- {
- date_period_it *iterator = (date_period_it *)iter;
- php_period_obj *object = Z_PHPPERIOD_P(&iterator->intern.data);
- timelib_time *it_time = object->current;
- php_date_obj *newdateobj;
- /* Create new object */
- php_date_instantiate(object->start_ce, &iterator->current);
- newdateobj = Z_PHPDATE_P(&iterator->current);
- newdateobj->time = timelib_time_ctor();
- *newdateobj->time = *it_time;
- if (it_time->tz_abbr) {
- newdateobj->time->tz_abbr = timelib_strdup(it_time->tz_abbr);
- }
- if (it_time->tz_info) {
- newdateobj->time->tz_info = it_time->tz_info;
- }
- return &iterator->current;
- }
- /* }}} */
- /* {{{ date_period_it_current_key */
- static void date_period_it_current_key(zend_object_iterator *iter, zval *key)
- {
- date_period_it *iterator = (date_period_it *)iter;
- ZVAL_LONG(key, iterator->current_index);
- }
- /* }}} */
- /* {{{ date_period_it_move_forward */
- static void date_period_it_move_forward(zend_object_iterator *iter)
- {
- date_period_it *iterator = (date_period_it *)iter;
- iterator->current_index++;
- date_period_it_invalidate_current(iter);
- }
- /* }}} */
- /* {{{ date_period_it_rewind */
- static void date_period_it_rewind(zend_object_iterator *iter)
- {
- date_period_it *iterator = (date_period_it *)iter;
- iterator->current_index = 0;
- if (iterator->object->current) {
- timelib_time_dtor(iterator->object->current);
- }
- if (!iterator->object->start) {
- zend_throw_error(NULL, "DatePeriod has not been initialized correctly");
- return;
- }
- iterator->object->current = timelib_time_clone(iterator->object->start);
- date_period_it_invalidate_current(iter);
- }
- /* }}} */
- /* iterator handler table */
- static const zend_object_iterator_funcs date_period_it_funcs = {
- date_period_it_dtor,
- date_period_it_has_more,
- date_period_it_current_data,
- date_period_it_current_key,
- date_period_it_move_forward,
- date_period_it_rewind,
- date_period_it_invalidate_current
- };
- zend_object_iterator *date_object_period_get_iterator(zend_class_entry *ce, zval *object, int by_ref) /* {{{ */
- {
- date_period_it *iterator;
- if (by_ref) {
- zend_throw_error(NULL, "An iterator cannot be used with foreach by reference");
- return NULL;
- }
- iterator = emalloc(sizeof(date_period_it));
- zend_iterator_init((zend_object_iterator*)iterator);
- Z_ADDREF_P(object);
- ZVAL_OBJ(&iterator->intern.data, Z_OBJ_P(object));
- iterator->intern.funcs = &date_period_it_funcs;
- iterator->object = Z_PHPPERIOD_P(object);
- ZVAL_UNDEF(&iterator->current);
- return (zend_object_iterator*)iterator;
- } /* }}} */
- static int implement_date_interface_handler(zend_class_entry *interface, zend_class_entry *implementor) /* {{{ */
- {
- if (implementor->type == ZEND_USER_CLASS &&
- !instanceof_function(implementor, date_ce_date) &&
- !instanceof_function(implementor, date_ce_immutable)
- ) {
- …
Large files files are truncated, but you can click here to view the full file