PageRenderTime 421ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 1ms

/ext/date/php_date.c

http://github.com/php/php-src
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
  1. /*
  2. +----------------------------------------------------------------------+
  3. | Copyright (c) The PHP Group |
  4. +----------------------------------------------------------------------+
  5. | This source file is subject to version 3.01 of the PHP license, |
  6. | that is bundled with this package in the file LICENSE, and is |
  7. | available through the world-wide-web at the following url: |
  8. | http://www.php.net/license/3_01.txt |
  9. | If you did not receive a copy of the PHP license and are unable to |
  10. | obtain it through the world-wide-web, please send a note to |
  11. | license@php.net so we can mail you a copy immediately. |
  12. +----------------------------------------------------------------------+
  13. | Authors: Derick Rethans <derick@derickrethans.nl> |
  14. +----------------------------------------------------------------------+
  15. */
  16. #include "php.h"
  17. #include "php_streams.h"
  18. #include "php_main.h"
  19. #include "php_globals.h"
  20. #include "php_ini.h"
  21. #include "ext/standard/info.h"
  22. #include "ext/standard/php_versioning.h"
  23. #include "ext/standard/php_math.h"
  24. #include "php_date.h"
  25. #include "zend_interfaces.h"
  26. #include "lib/timelib.h"
  27. #include "lib/timelib_private.h"
  28. #ifndef PHP_WIN32
  29. #include <time.h>
  30. #else
  31. #include "win32/time.h"
  32. #endif
  33. #ifdef PHP_WIN32
  34. static __inline __int64 php_date_llabs( __int64 i ) { return i >= 0? i: -i; }
  35. #elif defined(__GNUC__) && __GNUC__ < 3
  36. static __inline __int64_t php_date_llabs( __int64_t i ) { return i >= 0 ? i : -i; }
  37. #else
  38. static inline long long php_date_llabs( long long i ) { return i >= 0 ? i : -i; }
  39. #endif
  40. #ifdef PHP_WIN32
  41. #define DATE_I64_BUF_LEN 65
  42. # define DATE_I64A(i, s, len) _i64toa_s(i, s, len, 10)
  43. # define DATE_A64I(i, s) i = _atoi64(s)
  44. #else
  45. #define DATE_I64_BUF_LEN 65
  46. # define DATE_I64A(i, s, len) \
  47. do { \
  48. int st = snprintf(s, len, "%lld", i); \
  49. s[st] = '\0'; \
  50. } while (0);
  51. #ifdef HAVE_ATOLL
  52. # define DATE_A64I(i, s) i = atoll(s)
  53. #else
  54. # define DATE_A64I(i, s) i = strtoll(s, NULL, 10)
  55. #endif
  56. #endif
  57. PHPAPI time_t php_time()
  58. {
  59. #ifdef HAVE_GETTIMEOFDAY
  60. struct timeval tm;
  61. if (UNEXPECTED(gettimeofday(&tm, NULL) != SUCCESS)) {
  62. /* fallback, can't reasonably happen */
  63. return time(NULL);
  64. }
  65. return tm.tv_sec;
  66. #else
  67. return time(NULL);
  68. #endif
  69. }
  70. #include "php_date_arginfo.h"
  71. static char* guess_timezone(const timelib_tzdb *tzdb);
  72. static void date_register_classes(void);
  73. /* }}} */
  74. ZEND_DECLARE_MODULE_GLOBALS(date)
  75. static PHP_GINIT_FUNCTION(date);
  76. /* True global */
  77. timelib_tzdb *php_date_global_timezone_db;
  78. int php_date_global_timezone_db_enabled;
  79. #define DATE_DEFAULT_LATITUDE "31.7667"
  80. #define DATE_DEFAULT_LONGITUDE "35.2333"
  81. /* on 90'50; common sunset declaration (start of sun body appear) */
  82. #define DATE_SUNSET_ZENITH "90.833333"
  83. /* on 90'50; common sunrise declaration (sun body disappeared) */
  84. #define DATE_SUNRISE_ZENITH "90.833333"
  85. static PHP_INI_MH(OnUpdate_date_timezone);
  86. /* {{{ INI Settings */
  87. PHP_INI_BEGIN()
  88. STD_PHP_INI_ENTRY("date.timezone", "", PHP_INI_ALL, OnUpdate_date_timezone, default_timezone, zend_date_globals, date_globals)
  89. PHP_INI_ENTRY("date.default_latitude", DATE_DEFAULT_LATITUDE, PHP_INI_ALL, NULL)
  90. PHP_INI_ENTRY("date.default_longitude", DATE_DEFAULT_LONGITUDE, PHP_INI_ALL, NULL)
  91. PHP_INI_ENTRY("date.sunset_zenith", DATE_SUNSET_ZENITH, PHP_INI_ALL, NULL)
  92. PHP_INI_ENTRY("date.sunrise_zenith", DATE_SUNRISE_ZENITH, PHP_INI_ALL, NULL)
  93. PHP_INI_END()
  94. /* }}} */
  95. zend_class_entry *date_ce_date, *date_ce_timezone, *date_ce_interval, *date_ce_period;
  96. zend_class_entry *date_ce_immutable, *date_ce_interface;
  97. PHPAPI zend_class_entry *php_date_get_date_ce(void)
  98. {
  99. return date_ce_date;
  100. }
  101. PHPAPI zend_class_entry *php_date_get_immutable_ce(void)
  102. {
  103. return date_ce_immutable;
  104. }
  105. PHPAPI zend_class_entry *php_date_get_interface_ce(void)
  106. {
  107. return date_ce_interface;
  108. }
  109. PHPAPI zend_class_entry *php_date_get_timezone_ce(void)
  110. {
  111. return date_ce_timezone;
  112. }
  113. PHPAPI zend_class_entry *php_date_get_interval_ce(void)
  114. {
  115. return date_ce_interval;
  116. }
  117. PHPAPI zend_class_entry *php_date_get_period_ce(void)
  118. {
  119. return date_ce_period;
  120. }
  121. static zend_object_handlers date_object_handlers_date;
  122. static zend_object_handlers date_object_handlers_immutable;
  123. static zend_object_handlers date_object_handlers_timezone;
  124. static zend_object_handlers date_object_handlers_interval;
  125. static zend_object_handlers date_object_handlers_period;
  126. #define DATE_CHECK_INITIALIZED(member, class_name) \
  127. if (!(member)) { \
  128. zend_throw_error(NULL, "The " #class_name " object has not been correctly initialized by its constructor"); \
  129. RETURN_THROWS(); \
  130. }
  131. static void date_object_free_storage_date(zend_object *object);
  132. static void date_object_free_storage_timezone(zend_object *object);
  133. static void date_object_free_storage_interval(zend_object *object);
  134. static void date_object_free_storage_period(zend_object *object);
  135. static zend_object *date_object_new_date(zend_class_entry *class_type);
  136. static zend_object *date_object_new_timezone(zend_class_entry *class_type);
  137. static zend_object *date_object_new_interval(zend_class_entry *class_type);
  138. static zend_object *date_object_new_period(zend_class_entry *class_type);
  139. static zend_object *date_object_clone_date(zend_object *this_ptr);
  140. static zend_object *date_object_clone_timezone(zend_object *this_ptr);
  141. static zend_object *date_object_clone_interval(zend_object *this_ptr);
  142. static zend_object *date_object_clone_period(zend_object *this_ptr);
  143. static int date_object_compare_date(zval *d1, zval *d2);
  144. static HashTable *date_object_get_gc(zend_object *object, zval **table, int *n);
  145. static HashTable *date_object_get_properties_for(zend_object *object, zend_prop_purpose purpose);
  146. static HashTable *date_object_get_gc_interval(zend_object *object, zval **table, int *n);
  147. static HashTable *date_object_get_properties_interval(zend_object *object);
  148. static HashTable *date_object_get_gc_period(zend_object *object, zval **table, int *n);
  149. static HashTable *date_object_get_properties_period(zend_object *object);
  150. static HashTable *date_object_get_properties_for_timezone(zend_object *object, zend_prop_purpose purpose);
  151. static HashTable *date_object_get_gc_timezone(zend_object *object, zval **table, int *n);
  152. static HashTable *date_object_get_debug_info_timezone(zend_object *object, int *is_temp);
  153. static void php_timezone_to_string(php_timezone_obj *tzobj, zval *zv);
  154. static int date_interval_compare_objects(zval *o1, zval *o2);
  155. static zval *date_interval_read_property(zend_object *object, zend_string *member, int type, void **cache_slot, zval *rv);
  156. static zval *date_interval_write_property(zend_object *object, zend_string *member, zval *value, void **cache_slot);
  157. static zval *date_interval_get_property_ptr_ptr(zend_object *object, zend_string *member, int type, void **cache_slot);
  158. static zval *date_period_read_property(zend_object *object, zend_string *name, int type, void **cache_slot, zval *rv);
  159. static zval *date_period_write_property(zend_object *object, zend_string *name, zval *value, void **cache_slot);
  160. static zval *date_period_get_property_ptr_ptr(zend_object *object, zend_string *name, int type, void **cache_slot);
  161. static int date_object_compare_timezone(zval *tz1, zval *tz2);
  162. /* {{{ Module struct */
  163. zend_module_entry date_module_entry = {
  164. STANDARD_MODULE_HEADER_EX,
  165. NULL,
  166. NULL,
  167. "date", /* extension name */
  168. ext_functions, /* function list */
  169. PHP_MINIT(date), /* process startup */
  170. PHP_MSHUTDOWN(date), /* process shutdown */
  171. PHP_RINIT(date), /* request startup */
  172. PHP_RSHUTDOWN(date), /* request shutdown */
  173. PHP_MINFO(date), /* extension info */
  174. PHP_DATE_VERSION, /* extension version */
  175. PHP_MODULE_GLOBALS(date), /* globals descriptor */
  176. PHP_GINIT(date), /* globals ctor */
  177. NULL, /* globals dtor */
  178. NULL, /* post deactivate */
  179. STANDARD_MODULE_PROPERTIES_EX
  180. };
  181. /* }}} */
  182. /* {{{ PHP_GINIT_FUNCTION */
  183. static PHP_GINIT_FUNCTION(date)
  184. {
  185. date_globals->default_timezone = NULL;
  186. date_globals->timezone = NULL;
  187. date_globals->tzcache = NULL;
  188. date_globals->timezone_valid = 0;
  189. }
  190. /* }}} */
  191. static void _php_date_tzinfo_dtor(zval *zv) /* {{{ */
  192. {
  193. timelib_tzinfo *tzi = (timelib_tzinfo*)Z_PTR_P(zv);
  194. timelib_tzinfo_dtor(tzi);
  195. } /* }}} */
  196. /* {{{ PHP_RINIT_FUNCTION */
  197. PHP_RINIT_FUNCTION(date)
  198. {
  199. if (DATEG(timezone)) {
  200. efree(DATEG(timezone));
  201. }
  202. DATEG(timezone) = NULL;
  203. DATEG(tzcache) = NULL;
  204. DATEG(last_errors) = NULL;
  205. return SUCCESS;
  206. }
  207. /* }}} */
  208. /* {{{ PHP_RSHUTDOWN_FUNCTION */
  209. PHP_RSHUTDOWN_FUNCTION(date)
  210. {
  211. if (DATEG(timezone)) {
  212. efree(DATEG(timezone));
  213. }
  214. DATEG(timezone) = NULL;
  215. if(DATEG(tzcache)) {
  216. zend_hash_destroy(DATEG(tzcache));
  217. FREE_HASHTABLE(DATEG(tzcache));
  218. DATEG(tzcache) = NULL;
  219. }
  220. if (DATEG(last_errors)) {
  221. timelib_error_container_dtor(DATEG(last_errors));
  222. DATEG(last_errors) = NULL;
  223. }
  224. return SUCCESS;
  225. }
  226. /* }}} */
  227. #define DATE_TIMEZONEDB php_date_global_timezone_db ? php_date_global_timezone_db : timelib_builtin_db()
  228. /*
  229. * RFC822, Section 5.1: http://www.ietf.org/rfc/rfc822.txt
  230. * date-time = [ day "," ] date time ; dd mm yy hh:mm:ss zzz
  231. * day = "Mon" / "Tue" / "Wed" / "Thu" / "Fri" / "Sat" / "Sun"
  232. * date = 1*2DIGIT month 2DIGIT ; day month year e.g. 20 Jun 82
  233. * month = "Jan" / "Feb" / "Mar" / "Apr" / "May" / "Jun" / "Jul" / "Aug" / "Sep" / "Oct" / "Nov" / "Dec"
  234. * time = hour zone ; ANSI and Military
  235. * hour = 2DIGIT ":" 2DIGIT [":" 2DIGIT] ; 00:00:00 - 23:59:59
  236. * zone = "UT" / "GMT" / "EST" / "EDT" / "CST" / "CDT" / "MST" / "MDT" / "PST" / "PDT" / 1ALPHA / ( ("+" / "-") 4DIGIT )
  237. */
  238. #define DATE_FORMAT_RFC822 "D, d M y H:i:s O"
  239. /*
  240. * RFC850, Section 2.1.4: http://www.ietf.org/rfc/rfc850.txt
  241. * Format must be acceptable both to the ARPANET and to the getdate routine.
  242. * One format that is acceptable to both is Weekday, DD-Mon-YY HH:MM:SS TIMEZONE
  243. * TIMEZONE can be any timezone name (3 or more letters)
  244. */
  245. #define DATE_FORMAT_RFC850 "l, d-M-y H:i:s T"
  246. /*
  247. * RFC1036, Section 2.1.2: http://www.ietf.org/rfc/rfc1036.txt
  248. * Its format must be acceptable both in RFC-822 and to the getdate(3)
  249. * Wdy, DD Mon YY HH:MM:SS TIMEZONE
  250. * There is no hope of having a complete list of timezones. Universal
  251. * Time (GMT), the North American timezones (PST, PDT, MST, MDT, CST,
  252. * CDT, EST, EDT) and the +/-hhmm offset specified in RFC-822 should be supported.
  253. */
  254. #define DATE_FORMAT_RFC1036 "D, d M y H:i:s O"
  255. /*
  256. * RFC1123, Section 5.2.14: http://www.ietf.org/rfc/rfc1123.txt
  257. * RFC-822 Date and Time Specification: RFC-822 Section 5
  258. * The syntax for the date is hereby changed to: date = 1*2DIGIT month 2*4DIGIT
  259. */
  260. #define DATE_FORMAT_RFC1123 "D, d M Y H:i:s O"
  261. /*
  262. * RFC7231, Section 7.1.1: http://tools.ietf.org/html/rfc7231
  263. */
  264. #define DATE_FORMAT_RFC7231 "D, d M Y H:i:s \\G\\M\\T"
  265. /*
  266. * RFC2822, Section 3.3: http://www.ietf.org/rfc/rfc2822.txt
  267. * FWS = ([*WSP CRLF] 1*WSP) / ; Folding white space
  268. * CFWS = *([FWS] comment) (([FWS] comment) / FWS)
  269. *
  270. * date-time = [ day-of-week "," ] date FWS time [CFWS]
  271. * day-of-week = ([FWS] day-name)
  272. * day-name = "Mon" / "Tue" / "Wed" / "Thu" / "Fri" / "Sat" / "Sun"
  273. * date = day month year
  274. * year = 4*DIGIT
  275. * month = (FWS month-name FWS)
  276. * month-name = "Jan" / "Feb" / "Mar" / "Apr" / "May" / "Jun" / "Jul" / "Aug" / "Sep" / "Oct" / "Nov" / "Dec"
  277. * day = ([FWS] 1*2DIGIT)
  278. * time = time-of-day FWS zone
  279. * time-of-day = hour ":" minute [ ":" second ]
  280. * hour = 2DIGIT
  281. * minute = 2DIGIT
  282. * second = 2DIGIT
  283. * zone = (( "+" / "-" ) 4DIGIT)
  284. */
  285. #define DATE_FORMAT_RFC2822 "D, d M Y H:i:s O"
  286. /*
  287. * RFC3339, Section 5.6: http://www.ietf.org/rfc/rfc3339.txt
  288. * date-fullyear = 4DIGIT
  289. * date-month = 2DIGIT ; 01-12
  290. * date-mday = 2DIGIT ; 01-28, 01-29, 01-30, 01-31 based on month/year
  291. *
  292. * time-hour = 2DIGIT ; 00-23
  293. * time-minute = 2DIGIT ; 00-59
  294. * time-second = 2DIGIT ; 00-58, 00-59, 00-60 based on leap second rules
  295. *
  296. * time-secfrac = "." 1*DIGIT
  297. * time-numoffset = ("+" / "-") time-hour ":" time-minute
  298. * time-offset = "Z" / time-numoffset
  299. *
  300. * partial-time = time-hour ":" time-minute ":" time-second [time-secfrac]
  301. * full-date = date-fullyear "-" date-month "-" date-mday
  302. * full-time = partial-time time-offset
  303. *
  304. * date-time = full-date "T" full-time
  305. */
  306. #define DATE_FORMAT_RFC3339 "Y-m-d\\TH:i:sP"
  307. #define DATE_FORMAT_ISO8601 "Y-m-d\\TH:i:sO"
  308. /*
  309. * RFC3339, Appendix A: http://www.ietf.org/rfc/rfc3339.txt
  310. * ISO 8601 also requires (in section 5.3.1.3) that a decimal fraction
  311. * be proceeded by a "0" if less than unity. Annex B.2 of ISO 8601
  312. * gives examples where the decimal fractions are not preceded by a "0".
  313. * This grammar assumes section 5.3.1.3 is correct and that Annex B.2 is
  314. * in error.
  315. */
  316. #define DATE_FORMAT_RFC3339_EXTENDED "Y-m-d\\TH:i:s.vP"
  317. /*
  318. * This comes from various sources that like to contradict. I'm going with the
  319. * format here because of:
  320. * http://msdn.microsoft.com/en-us/library/windows/desktop/aa384321%28v=vs.85%29.aspx
  321. * and http://curl.haxx.se/rfc/cookie_spec.html
  322. */
  323. #define DATE_FORMAT_COOKIE "l, d-M-Y H:i:s T"
  324. #define SUNFUNCS_RET_TIMESTAMP 0
  325. #define SUNFUNCS_RET_STRING 1
  326. #define SUNFUNCS_RET_DOUBLE 2
  327. /* {{{ PHP_MINIT_FUNCTION */
  328. PHP_MINIT_FUNCTION(date)
  329. {
  330. REGISTER_INI_ENTRIES();
  331. date_register_classes();
  332. /*
  333. * RFC4287, Section 3.3: http://www.ietf.org/rfc/rfc4287.txt
  334. * A Date construct is an element whose content MUST conform to the
  335. * "date-time" production in [RFC3339]. In addition, an uppercase "T"
  336. * character MUST be used to separate date and time, and an uppercase
  337. * "Z" character MUST be present in the absence of a numeric time zone offset.
  338. */
  339. REGISTER_STRING_CONSTANT("DATE_ATOM", DATE_FORMAT_RFC3339, CONST_CS | CONST_PERSISTENT);
  340. /*
  341. * Preliminary specification: http://wp.netscape.com/newsref/std/cookie_spec.html
  342. * "This is based on RFC 822, RFC 850, RFC 1036, and RFC 1123,
  343. * with the variations that the only legal time zone is GMT
  344. * and the separators between the elements of the date must be dashes."
  345. */
  346. REGISTER_STRING_CONSTANT("DATE_COOKIE", DATE_FORMAT_COOKIE, CONST_CS | CONST_PERSISTENT);
  347. REGISTER_STRING_CONSTANT("DATE_ISO8601", DATE_FORMAT_ISO8601, CONST_CS | CONST_PERSISTENT);
  348. REGISTER_STRING_CONSTANT("DATE_RFC822", DATE_FORMAT_RFC822, CONST_CS | CONST_PERSISTENT);
  349. REGISTER_STRING_CONSTANT("DATE_RFC850", DATE_FORMAT_RFC850, CONST_CS | CONST_PERSISTENT);
  350. REGISTER_STRING_CONSTANT("DATE_RFC1036", DATE_FORMAT_RFC1036, CONST_CS | CONST_PERSISTENT);
  351. REGISTER_STRING_CONSTANT("DATE_RFC1123", DATE_FORMAT_RFC1123, CONST_CS | CONST_PERSISTENT);
  352. REGISTER_STRING_CONSTANT("DATE_RFC7231", DATE_FORMAT_RFC7231, CONST_CS | CONST_PERSISTENT);
  353. REGISTER_STRING_CONSTANT("DATE_RFC2822", DATE_FORMAT_RFC2822, CONST_CS | CONST_PERSISTENT);
  354. REGISTER_STRING_CONSTANT("DATE_RFC3339", DATE_FORMAT_RFC3339, CONST_CS | CONST_PERSISTENT);
  355. REGISTER_STRING_CONSTANT("DATE_RFC3339_EXTENDED", DATE_FORMAT_RFC3339_EXTENDED, CONST_CS | CONST_PERSISTENT);
  356. /*
  357. * RSS 2.0 Specification: http://blogs.law.harvard.edu/tech/rss
  358. * "All date-times in RSS conform to the Date and Time Specification of RFC 822,
  359. * with the exception that the year may be expressed with two characters or four characters (four preferred)"
  360. */
  361. REGISTER_STRING_CONSTANT("DATE_RSS", DATE_FORMAT_RFC1123, CONST_CS | CONST_PERSISTENT);
  362. REGISTER_STRING_CONSTANT("DATE_W3C", DATE_FORMAT_RFC3339, CONST_CS | CONST_PERSISTENT);
  363. REGISTER_LONG_CONSTANT("SUNFUNCS_RET_TIMESTAMP", SUNFUNCS_RET_TIMESTAMP, CONST_CS | CONST_PERSISTENT);
  364. REGISTER_LONG_CONSTANT("SUNFUNCS_RET_STRING", SUNFUNCS_RET_STRING, CONST_CS | CONST_PERSISTENT);
  365. REGISTER_LONG_CONSTANT("SUNFUNCS_RET_DOUBLE", SUNFUNCS_RET_DOUBLE, CONST_CS | CONST_PERSISTENT);
  366. php_date_global_timezone_db = NULL;
  367. php_date_global_timezone_db_enabled = 0;
  368. DATEG(last_errors) = NULL;
  369. return SUCCESS;
  370. }
  371. /* }}} */
  372. /* {{{ PHP_MSHUTDOWN_FUNCTION */
  373. PHP_MSHUTDOWN_FUNCTION(date)
  374. {
  375. UNREGISTER_INI_ENTRIES();
  376. if (DATEG(last_errors)) {
  377. timelib_error_container_dtor(DATEG(last_errors));
  378. }
  379. #ifndef ZTS
  380. DATEG(default_timezone) = NULL;
  381. #endif
  382. return SUCCESS;
  383. }
  384. /* }}} */
  385. /* {{{ PHP_MINFO_FUNCTION */
  386. PHP_MINFO_FUNCTION(date)
  387. {
  388. const timelib_tzdb *tzdb = DATE_TIMEZONEDB;
  389. php_info_print_table_start();
  390. php_info_print_table_row(2, "date/time support", "enabled");
  391. php_info_print_table_row(2, "timelib version", TIMELIB_ASCII_VERSION);
  392. php_info_print_table_row(2, "\"Olson\" Timezone Database Version", tzdb->version);
  393. php_info_print_table_row(2, "Timezone Database", php_date_global_timezone_db_enabled ? "external" : "internal");
  394. php_info_print_table_row(2, "Default timezone", guess_timezone(tzdb));
  395. php_info_print_table_end();
  396. DISPLAY_INI_ENTRIES();
  397. }
  398. /* }}} */
  399. /* {{{ Timezone Cache functions */
  400. static timelib_tzinfo *php_date_parse_tzfile(char *formal_tzname, const timelib_tzdb *tzdb)
  401. {
  402. timelib_tzinfo *tzi;
  403. int dummy_error_code;
  404. if(!DATEG(tzcache)) {
  405. ALLOC_HASHTABLE(DATEG(tzcache));
  406. zend_hash_init(DATEG(tzcache), 4, NULL, _php_date_tzinfo_dtor, 0);
  407. }
  408. if ((tzi = zend_hash_str_find_ptr(DATEG(tzcache), formal_tzname, strlen(formal_tzname))) != NULL) {
  409. return tzi;
  410. }
  411. tzi = timelib_parse_tzfile(formal_tzname, tzdb, &dummy_error_code);
  412. if (tzi) {
  413. zend_hash_str_add_ptr(DATEG(tzcache), formal_tzname, strlen(formal_tzname), tzi);
  414. }
  415. return tzi;
  416. }
  417. timelib_tzinfo *php_date_parse_tzfile_wrapper(char *formal_tzname, const timelib_tzdb *tzdb, int *dummy_error_code)
  418. {
  419. return php_date_parse_tzfile(formal_tzname, tzdb);
  420. }
  421. /* }}} */
  422. /* Callback to check the date.timezone only when changed increases performance */
  423. /* {{{ static PHP_INI_MH(OnUpdate_date_timezone) */
  424. static PHP_INI_MH(OnUpdate_date_timezone)
  425. {
  426. if (OnUpdateString(entry, new_value, mh_arg1, mh_arg2, mh_arg3, stage) == FAILURE) {
  427. return FAILURE;
  428. }
  429. DATEG(timezone_valid) = 0;
  430. if (stage == PHP_INI_STAGE_RUNTIME) {
  431. if (!timelib_timezone_id_is_valid(DATEG(default_timezone), DATE_TIMEZONEDB)) {
  432. if (DATEG(default_timezone) && *DATEG(default_timezone)) {
  433. php_error_docref(NULL, E_WARNING, "Invalid date.timezone value '%s', we selected the timezone 'UTC' for now.", DATEG(default_timezone));
  434. }
  435. } else {
  436. DATEG(timezone_valid) = 1;
  437. }
  438. }
  439. return SUCCESS;
  440. }
  441. /* }}} */
  442. /* {{{ Helper functions */
  443. static char* guess_timezone(const timelib_tzdb *tzdb)
  444. {
  445. /* Checking configure timezone */
  446. if (DATEG(timezone) && (strlen(DATEG(timezone))) > 0) {
  447. return DATEG(timezone);
  448. }
  449. /* Check config setting for default timezone */
  450. if (!DATEG(default_timezone)) {
  451. /* Special case: ext/date wasn't initialized yet */
  452. zval *ztz;
  453. if (NULL != (ztz = cfg_get_entry("date.timezone", sizeof("date.timezone")))
  454. && Z_TYPE_P(ztz) == IS_STRING && Z_STRLEN_P(ztz) > 0 && timelib_timezone_id_is_valid(Z_STRVAL_P(ztz), tzdb)) {
  455. return Z_STRVAL_P(ztz);
  456. }
  457. } else if (*DATEG(default_timezone)) {
  458. if (DATEG(timezone_valid) == 1) {
  459. return DATEG(default_timezone);
  460. }
  461. if (!timelib_timezone_id_is_valid(DATEG(default_timezone), tzdb)) {
  462. php_error_docref(NULL, E_WARNING, "Invalid date.timezone value '%s', we selected the timezone 'UTC' for now.", DATEG(default_timezone));
  463. return "UTC";
  464. }
  465. DATEG(timezone_valid) = 1;
  466. return DATEG(default_timezone);
  467. }
  468. /* Fallback to UTC */
  469. return "UTC";
  470. }
  471. PHPAPI timelib_tzinfo *get_timezone_info(void)
  472. {
  473. char *tz;
  474. timelib_tzinfo *tzi;
  475. tz = guess_timezone(DATE_TIMEZONEDB);
  476. tzi = php_date_parse_tzfile(tz, DATE_TIMEZONEDB);
  477. if (! tzi) {
  478. php_error_docref(NULL, E_ERROR, "Timezone database is corrupt - this should *never* happen!");
  479. }
  480. return tzi;
  481. }
  482. /* }}} */
  483. /* {{{ date() and gmdate() data */
  484. #include "zend_smart_str.h"
  485. static const char * const mon_full_names[] = {
  486. "January", "February", "March", "April",
  487. "May", "June", "July", "August",
  488. "September", "October", "November", "December"
  489. };
  490. static const char * const mon_short_names[] = {
  491. "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
  492. };
  493. static const char * const day_full_names[] = {
  494. "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"
  495. };
  496. static const char * const day_short_names[] = {
  497. "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
  498. };
  499. static char *english_suffix(timelib_sll number)
  500. {
  501. if (number >= 10 && number <= 19) {
  502. return "th";
  503. } else {
  504. switch (number % 10) {
  505. case 1: return "st";
  506. case 2: return "nd";
  507. case 3: return "rd";
  508. }
  509. }
  510. return "th";
  511. }
  512. /* }}} */
  513. /* {{{ day of week helpers */
  514. static const char *php_date_full_day_name(timelib_sll y, timelib_sll m, timelib_sll d)
  515. {
  516. timelib_sll day_of_week = timelib_day_of_week(y, m, d);
  517. if (day_of_week < 0) {
  518. return "Unknown";
  519. }
  520. return day_full_names[day_of_week];
  521. }
  522. static const char *php_date_short_day_name(timelib_sll y, timelib_sll m, timelib_sll d)
  523. {
  524. timelib_sll day_of_week = timelib_day_of_week(y, m, d);
  525. if (day_of_week < 0) {
  526. return "Unknown";
  527. }
  528. return day_short_names[day_of_week];
  529. }
  530. /* }}} */
  531. /* {{{ date_format - (gm)date helper */
  532. static zend_string *date_format(char *format, size_t format_len, timelib_time *t, int localtime)
  533. {
  534. smart_str string = {0};
  535. size_t i;
  536. int length = 0;
  537. char buffer[97];
  538. timelib_time_offset *offset = NULL;
  539. timelib_sll isoweek, isoyear;
  540. int rfc_colon;
  541. int weekYearSet = 0;
  542. if (!format_len) {
  543. return ZSTR_EMPTY_ALLOC();
  544. }
  545. if (localtime) {
  546. if (t->zone_type == TIMELIB_ZONETYPE_ABBR) {
  547. offset = timelib_time_offset_ctor();
  548. offset->offset = (t->z + (t->dst * 3600));
  549. offset->leap_secs = 0;
  550. offset->is_dst = t->dst;
  551. offset->abbr = timelib_strdup(t->tz_abbr);
  552. } else if (t->zone_type == TIMELIB_ZONETYPE_OFFSET) {
  553. offset = timelib_time_offset_ctor();
  554. offset->offset = (t->z);
  555. offset->leap_secs = 0;
  556. offset->is_dst = 0;
  557. offset->abbr = timelib_malloc(9); /* GMT±xxxx\0 */
  558. snprintf(offset->abbr, 9, "GMT%c%02d%02d",
  559. (offset->offset < 0) ? '-' : '+',
  560. abs(offset->offset / 3600),
  561. abs((offset->offset % 3600) / 60));
  562. } else {
  563. offset = timelib_get_time_zone_info(t->sse, t->tz_info);
  564. }
  565. }
  566. for (i = 0; i < format_len; i++) {
  567. rfc_colon = 0;
  568. switch (format[i]) {
  569. /* day */
  570. case 'd': length = slprintf(buffer, sizeof(buffer), "%02d", (int) t->d); break;
  571. case 'D': length = slprintf(buffer, sizeof(buffer), "%s", php_date_short_day_name(t->y, t->m, t->d)); break;
  572. case 'j': length = slprintf(buffer, sizeof(buffer), "%d", (int) t->d); break;
  573. case 'l': length = slprintf(buffer, sizeof(buffer), "%s", php_date_full_day_name(t->y, t->m, t->d)); break;
  574. case 'S': length = slprintf(buffer, sizeof(buffer), "%s", english_suffix(t->d)); break;
  575. case 'w': length = slprintf(buffer, sizeof(buffer), "%d", (int) timelib_day_of_week(t->y, t->m, t->d)); break;
  576. case 'N': length = slprintf(buffer, sizeof(buffer), "%d", (int) timelib_iso_day_of_week(t->y, t->m, t->d)); break;
  577. case 'z': length = slprintf(buffer, sizeof(buffer), "%d", (int) timelib_day_of_year(t->y, t->m, t->d)); break;
  578. /* week */
  579. case 'W':
  580. if(!weekYearSet) { timelib_isoweek_from_date(t->y, t->m, t->d, &isoweek, &isoyear); weekYearSet = 1; }
  581. length = slprintf(buffer, sizeof(buffer), "%02d", (int) isoweek); break; /* iso weeknr */
  582. case 'o':
  583. if(!weekYearSet) { timelib_isoweek_from_date(t->y, t->m, t->d, &isoweek, &isoyear); weekYearSet = 1; }
  584. length = slprintf(buffer, sizeof(buffer), ZEND_LONG_FMT, (zend_long) isoyear); break; /* iso year */
  585. /* month */
  586. case 'F': length = slprintf(buffer, sizeof(buffer), "%s", mon_full_names[t->m - 1]); break;
  587. case 'm': length = slprintf(buffer, sizeof(buffer), "%02d", (int) t->m); break;
  588. case 'M': length = slprintf(buffer, sizeof(buffer), "%s", mon_short_names[t->m - 1]); break;
  589. case 'n': length = slprintf(buffer, sizeof(buffer), "%d", (int) t->m); break;
  590. case 't': length = slprintf(buffer, sizeof(buffer), "%d", (int) timelib_days_in_month(t->y, t->m)); break;
  591. /* year */
  592. case 'L': length = slprintf(buffer, sizeof(buffer), "%d", timelib_is_leap((int) t->y)); break;
  593. case 'y': length = slprintf(buffer, sizeof(buffer), "%02d", (int) (t->y % 100)); break;
  594. case 'Y': length = slprintf(buffer, sizeof(buffer), "%s%04lld", t->y < 0 ? "-" : "", php_date_llabs((timelib_sll) t->y)); break;
  595. /* time */
  596. case 'a': length = slprintf(buffer, sizeof(buffer), "%s", t->h >= 12 ? "pm" : "am"); break;
  597. case 'A': length = slprintf(buffer, sizeof(buffer), "%s", t->h >= 12 ? "PM" : "AM"); break;
  598. case 'B': {
  599. int retval = ((((long)t->sse)-(((long)t->sse) - ((((long)t->sse) % 86400) + 3600))) * 10);
  600. if (retval < 0) {
  601. retval += 864000;
  602. }
  603. /* Make sure to do this on a positive int to avoid rounding errors */
  604. retval = (retval / 864) % 1000;
  605. length = slprintf(buffer, sizeof(buffer), "%03d", retval);
  606. break;
  607. }
  608. case 'g': length = slprintf(buffer, sizeof(buffer), "%d", (t->h % 12) ? (int) t->h % 12 : 12); break;
  609. case 'G': length = slprintf(buffer, sizeof(buffer), "%d", (int) t->h); break;
  610. case 'h': length = slprintf(buffer, sizeof(buffer), "%02d", (t->h % 12) ? (int) t->h % 12 : 12); break;
  611. case 'H': length = slprintf(buffer, sizeof(buffer), "%02d", (int) t->h); break;
  612. case 'i': length = slprintf(buffer, sizeof(buffer), "%02d", (int) t->i); break;
  613. case 's': length = slprintf(buffer, sizeof(buffer), "%02d", (int) t->s); break;
  614. case 'u': length = slprintf(buffer, sizeof(buffer), "%06d", (int) floor(t->us)); break;
  615. case 'v': length = slprintf(buffer, sizeof(buffer), "%03d", (int) floor(t->us / 1000)); break;
  616. /* timezone */
  617. case 'I': length = slprintf(buffer, sizeof(buffer), "%d", localtime ? offset->is_dst : 0); break;
  618. case 'P': rfc_colon = 1; /* break intentionally missing */
  619. case 'O': length = slprintf(buffer, sizeof(buffer), "%c%02d%s%02d",
  620. localtime ? ((offset->offset < 0) ? '-' : '+') : '+',
  621. localtime ? abs(offset->offset / 3600) : 0,
  622. rfc_colon ? ":" : "",
  623. localtime ? abs((offset->offset % 3600) / 60) : 0
  624. );
  625. break;
  626. case 'T': length = slprintf(buffer, sizeof(buffer), "%s", localtime ? offset->abbr : "GMT"); break;
  627. case 'e': if (!localtime) {
  628. length = slprintf(buffer, sizeof(buffer), "%s", "UTC");
  629. } else {
  630. switch (t->zone_type) {
  631. case TIMELIB_ZONETYPE_ID:
  632. length = slprintf(buffer, sizeof(buffer), "%s", t->tz_info->name);
  633. break;
  634. case TIMELIB_ZONETYPE_ABBR:
  635. length = slprintf(buffer, sizeof(buffer), "%s", offset->abbr);
  636. break;
  637. case TIMELIB_ZONETYPE_OFFSET:
  638. length = slprintf(buffer, sizeof(buffer), "%c%02d:%02d",
  639. ((offset->offset < 0) ? '-' : '+'),
  640. abs(offset->offset / 3600),
  641. abs((offset->offset % 3600) / 60)
  642. );
  643. break;
  644. }
  645. }
  646. break;
  647. case 'Z': length = slprintf(buffer, sizeof(buffer), "%d", localtime ? offset->offset : 0); break;
  648. /* full date/time */
  649. case 'c': length = slprintf(buffer, sizeof(buffer), "%04" ZEND_LONG_FMT_SPEC "-%02d-%02dT%02d:%02d:%02d%c%02d:%02d",
  650. (zend_long) t->y, (int) t->m, (int) t->d,
  651. (int) t->h, (int) t->i, (int) t->s,
  652. localtime ? ((offset->offset < 0) ? '-' : '+') : '+',
  653. localtime ? abs(offset->offset / 3600) : 0,
  654. localtime ? abs((offset->offset % 3600) / 60) : 0
  655. );
  656. break;
  657. case 'r': length = slprintf(buffer, sizeof(buffer), "%3s, %02d %3s %04" ZEND_LONG_FMT_SPEC " %02d:%02d:%02d %c%02d%02d",
  658. php_date_short_day_name(t->y, t->m, t->d),
  659. (int) t->d, mon_short_names[t->m - 1],
  660. (zend_long) t->y, (int) t->h, (int) t->i, (int) t->s,
  661. localtime ? ((offset->offset < 0) ? '-' : '+') : '+',
  662. localtime ? abs(offset->offset / 3600) : 0,
  663. localtime ? abs((offset->offset % 3600) / 60) : 0
  664. );
  665. break;
  666. case 'U': length = slprintf(buffer, sizeof(buffer), "%lld", (timelib_sll) t->sse); break;
  667. case '\\': if (i < format_len) i++; /* break intentionally missing */
  668. default: buffer[0] = format[i]; buffer[1] = '\0'; length = 1; break;
  669. }
  670. smart_str_appendl(&string, buffer, length);
  671. }
  672. smart_str_0(&string);
  673. if (localtime) {
  674. timelib_time_offset_dtor(offset);
  675. }
  676. return string.s;
  677. }
  678. static void php_date(INTERNAL_FUNCTION_PARAMETERS, int localtime)
  679. {
  680. zend_string *format;
  681. zend_long ts;
  682. zend_bool ts_is_null = 1;
  683. ZEND_PARSE_PARAMETERS_START(1, 2)
  684. Z_PARAM_STR(format)
  685. Z_PARAM_OPTIONAL
  686. Z_PARAM_LONG_OR_NULL(ts, ts_is_null)
  687. ZEND_PARSE_PARAMETERS_END();
  688. if (ts_is_null) {
  689. ts = php_time();
  690. }
  691. RETURN_STR(php_format_date(ZSTR_VAL(format), ZSTR_LEN(format), ts, localtime));
  692. }
  693. /* }}} */
  694. PHPAPI zend_string *php_format_date(char *format, size_t format_len, time_t ts, int localtime) /* {{{ */
  695. {
  696. timelib_time *t;
  697. timelib_tzinfo *tzi;
  698. zend_string *string;
  699. t = timelib_time_ctor();
  700. if (localtime) {
  701. tzi = get_timezone_info();
  702. t->tz_info = tzi;
  703. t->zone_type = TIMELIB_ZONETYPE_ID;
  704. timelib_unixtime2local(t, ts);
  705. } else {
  706. tzi = NULL;
  707. timelib_unixtime2gmt(t, ts);
  708. }
  709. string = date_format(format, format_len, t, localtime);
  710. timelib_time_dtor(t);
  711. return string;
  712. }
  713. /* }}} */
  714. /* {{{ php_idate
  715. */
  716. PHPAPI int php_idate(char format, time_t ts, int localtime)
  717. {
  718. timelib_time *t;
  719. timelib_tzinfo *tzi;
  720. int retval = -1;
  721. timelib_time_offset *offset = NULL;
  722. timelib_sll isoweek, isoyear;
  723. t = timelib_time_ctor();
  724. if (!localtime) {
  725. tzi = get_timezone_info();
  726. t->tz_info = tzi;
  727. t->zone_type = TIMELIB_ZONETYPE_ID;
  728. timelib_unixtime2local(t, ts);
  729. } else {
  730. tzi = NULL;
  731. timelib_unixtime2gmt(t, ts);
  732. }
  733. if (!localtime) {
  734. if (t->zone_type == TIMELIB_ZONETYPE_ABBR) {
  735. offset = timelib_time_offset_ctor();
  736. offset->offset = (t->z + (t->dst * 3600));
  737. offset->leap_secs = 0;
  738. offset->is_dst = t->dst;
  739. offset->abbr = timelib_strdup(t->tz_abbr);
  740. } else if (t->zone_type == TIMELIB_ZONETYPE_OFFSET) {
  741. offset = timelib_time_offset_ctor();
  742. offset->offset = (t->z + (t->dst * 3600));
  743. offset->leap_secs = 0;
  744. offset->is_dst = t->dst;
  745. offset->abbr = timelib_malloc(9); /* GMT±xxxx\0 */
  746. snprintf(offset->abbr, 9, "GMT%c%02d%02d",
  747. (offset->offset < 0) ? '-' : '+',
  748. abs(offset->offset / 3600),
  749. abs((offset->offset % 3600) / 60));
  750. } else {
  751. offset = timelib_get_time_zone_info(t->sse, t->tz_info);
  752. }
  753. }
  754. timelib_isoweek_from_date(t->y, t->m, t->d, &isoweek, &isoyear);
  755. switch (format) {
  756. /* day */
  757. case 'd': case 'j': retval = (int) t->d; break;
  758. case 'w': retval = (int) timelib_day_of_week(t->y, t->m, t->d); break;
  759. case 'z': retval = (int) timelib_day_of_year(t->y, t->m, t->d); break;
  760. /* week */
  761. case 'W': retval = (int) isoweek; break; /* iso weeknr */
  762. /* month */
  763. case 'm': case 'n': retval = (int) t->m; break;
  764. case 't': retval = (int) timelib_days_in_month(t->y, t->m); break;
  765. /* year */
  766. case 'L': retval = (int) timelib_is_leap((int) t->y); break;
  767. case 'y': retval = (int) (t->y % 100); break;
  768. case 'Y': retval = (int) t->y; break;
  769. /* Swatch Beat a.k.a. Internet Time */
  770. case 'B':
  771. retval = ((((long)t->sse)-(((long)t->sse) - ((((long)t->sse) % 86400) + 3600))) * 10);
  772. if (retval < 0) {
  773. retval += 864000;
  774. }
  775. /* Make sure to do this on a positive int to avoid rounding errors */
  776. retval = (retval / 864) % 1000;
  777. break;
  778. /* time */
  779. case 'g': case 'h': retval = (int) ((t->h % 12) ? (int) t->h % 12 : 12); break;
  780. case 'H': case 'G': retval = (int) t->h; break;
  781. case 'i': retval = (int) t->i; break;
  782. case 's': retval = (int) t->s; break;
  783. /* timezone */
  784. case 'I': retval = (int) (!localtime ? offset->is_dst : 0); break;
  785. case 'Z': retval = (int) (!localtime ? offset->offset : 0); break;
  786. case 'U': retval = (int) t->sse; break;
  787. }
  788. if (!localtime) {
  789. timelib_time_offset_dtor(offset);
  790. }
  791. timelib_time_dtor(t);
  792. return retval;
  793. }
  794. /* }}} */
  795. /* {{{ proto string date(string format [, int timestamp])
  796. Format a local date/time */
  797. PHP_FUNCTION(date)
  798. {
  799. php_date(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
  800. }
  801. /* }}} */
  802. /* {{{ proto string gmdate(string format [, int timestamp])
  803. Format a GMT date/time */
  804. PHP_FUNCTION(gmdate)
  805. {
  806. php_date(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
  807. }
  808. /* }}} */
  809. /* {{{ proto int idate(string format [, int timestamp])
  810. Format a local time/date as integer */
  811. PHP_FUNCTION(idate)
  812. {
  813. zend_string *format;
  814. zend_long ts;
  815. zend_bool ts_is_null = 1;
  816. int ret;
  817. ZEND_PARSE_PARAMETERS_START(1, 2)
  818. Z_PARAM_STR(format)
  819. Z_PARAM_OPTIONAL
  820. Z_PARAM_LONG_OR_NULL(ts, ts_is_null)
  821. ZEND_PARSE_PARAMETERS_END();
  822. if (ZSTR_LEN(format) != 1) {
  823. php_error_docref(NULL, E_WARNING, "idate format is one char");
  824. RETURN_FALSE;
  825. }
  826. if (ts_is_null) {
  827. ts = php_time();
  828. }
  829. ret = php_idate(ZSTR_VAL(format)[0], ts, 0);
  830. if (ret == -1) {
  831. php_error_docref(NULL, E_WARNING, "Unrecognized date format token.");
  832. RETURN_FALSE;
  833. }
  834. RETURN_LONG(ret);
  835. }
  836. /* }}} */
  837. /* {{{ php_date_set_tzdb - NOT THREADSAFE */
  838. PHPAPI void php_date_set_tzdb(timelib_tzdb *tzdb)
  839. {
  840. const timelib_tzdb *builtin = timelib_builtin_db();
  841. if (php_version_compare(tzdb->version, builtin->version) > 0) {
  842. php_date_global_timezone_db = tzdb;
  843. php_date_global_timezone_db_enabled = 1;
  844. }
  845. }
  846. /* }}} */
  847. /* {{{ php_parse_date: Backwards compatibility function */
  848. PHPAPI zend_long php_parse_date(char *string, zend_long *now)
  849. {
  850. timelib_time *parsed_time;
  851. timelib_error_container *error = NULL;
  852. int error2;
  853. zend_long retval;
  854. parsed_time = timelib_strtotime(string, strlen(string), &error, DATE_TIMEZONEDB, php_date_parse_tzfile_wrapper);
  855. if (error->error_count) {
  856. timelib_time_dtor(parsed_time);
  857. timelib_error_container_dtor(error);
  858. return -1;
  859. }
  860. timelib_error_container_dtor(error);
  861. timelib_update_ts(parsed_time, NULL);
  862. retval = timelib_date_to_int(parsed_time, &error2);
  863. timelib_time_dtor(parsed_time);
  864. if (error2) {
  865. return -1;
  866. }
  867. return retval;
  868. }
  869. /* }}} */
  870. /* {{{ proto int strtotime(string time [, int now ])
  871. Convert string representation of date and time to a timestamp */
  872. PHP_FUNCTION(strtotime)
  873. {
  874. zend_string *times;
  875. int error1, error2;
  876. timelib_error_container *error;
  877. zend_long preset_ts, ts;
  878. zend_bool preset_ts_is_null = 1;
  879. timelib_time *t, *now;
  880. timelib_tzinfo *tzi;
  881. ZEND_PARSE_PARAMETERS_START(1, 2)
  882. Z_PARAM_STR(times)
  883. Z_PARAM_OPTIONAL
  884. Z_PARAM_LONG_OR_NULL(preset_ts, preset_ts_is_null)
  885. ZEND_PARSE_PARAMETERS_END();
  886. tzi = get_timezone_info();
  887. now = timelib_time_ctor();
  888. now->tz_info = tzi;
  889. now->zone_type = TIMELIB_ZONETYPE_ID;
  890. timelib_unixtime2local(now,
  891. !preset_ts_is_null ? (timelib_sll) preset_ts : (timelib_sll) php_time());
  892. t = timelib_strtotime(ZSTR_VAL(times), ZSTR_LEN(times), &error,
  893. DATE_TIMEZONEDB, php_date_parse_tzfile_wrapper);
  894. error1 = error->error_count;
  895. timelib_error_container_dtor(error);
  896. timelib_fill_holes(t, now, TIMELIB_NO_CLONE);
  897. timelib_update_ts(t, tzi);
  898. ts = timelib_date_to_int(t, &error2);
  899. timelib_time_dtor(now);
  900. timelib_time_dtor(t);
  901. if (error1 || error2) {
  902. RETURN_FALSE;
  903. } else {
  904. RETURN_LONG(ts);
  905. }
  906. }
  907. /* }}} */
  908. /* {{{ php_mktime - (gm)mktime helper */
  909. PHPAPI void php_mktime(INTERNAL_FUNCTION_PARAMETERS, int gmt)
  910. {
  911. zend_long hou, min, sec, mon, day, yea;
  912. zend_bool min_is_null = 1, sec_is_null = 1, mon_is_null = 1, day_is_null = 1, yea_is_null = 1;
  913. timelib_time *now;
  914. timelib_tzinfo *tzi = NULL;
  915. zend_long ts, adjust_seconds = 0;
  916. int error;
  917. ZEND_PARSE_PARAMETERS_START(1, 6)
  918. Z_PARAM_LONG(hou)
  919. Z_PARAM_OPTIONAL
  920. Z_PARAM_LONG_OR_NULL(min, min_is_null)
  921. Z_PARAM_LONG_OR_NULL(sec, sec_is_null)
  922. Z_PARAM_LONG_OR_NULL(mon, mon_is_null)
  923. Z_PARAM_LONG_OR_NULL(day, day_is_null)
  924. Z_PARAM_LONG_OR_NULL(yea, yea_is_null)
  925. ZEND_PARSE_PARAMETERS_END();
  926. /* Initialize structure with current time */
  927. now = timelib_time_ctor();
  928. if (gmt) {
  929. timelib_unixtime2gmt(now, (timelib_sll) php_time());
  930. } else {
  931. tzi = get_timezone_info();
  932. now->tz_info = tzi;
  933. now->zone_type = TIMELIB_ZONETYPE_ID;
  934. timelib_unixtime2local(now, (timelib_sll) php_time());
  935. }
  936. now->h = hou;
  937. if (!min_is_null) {
  938. now->i = min;
  939. }
  940. if (!sec_is_null) {
  941. now->s = sec;
  942. }
  943. if (!mon_is_null) {
  944. now->m = mon;
  945. }
  946. if (!day_is_null) {
  947. now->d = day;
  948. }
  949. if (!yea_is_null) {
  950. if (yea >= 0 && yea < 70) {
  951. yea += 2000;
  952. } else if (yea >= 70 && yea <= 100) {
  953. yea += 1900;
  954. }
  955. now->y = yea;
  956. }
  957. /* Update the timestamp */
  958. if (gmt) {
  959. timelib_update_ts(now, NULL);
  960. } else {
  961. timelib_update_ts(now, tzi);
  962. }
  963. /* Clean up and return */
  964. ts = timelib_date_to_int(now, &error);
  965. ts += adjust_seconds;
  966. timelib_time_dtor(now);
  967. if (error) {
  968. RETURN_FALSE;
  969. } else {
  970. RETURN_LONG(ts);
  971. }
  972. }
  973. /* }}} */
  974. /* {{{ proto int mktime(int hour [, int min [, int sec [, int mon [, int day [, int year]]]]])
  975. Get UNIX timestamp for a date */
  976. PHP_FUNCTION(mktime)
  977. {
  978. php_mktime(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
  979. }
  980. /* }}} */
  981. /* {{{ proto int gmmktime(int hour [, int min [, int sec [, int mon [, int day [, int year]]]]])
  982. Get UNIX timestamp for a GMT date */
  983. PHP_FUNCTION(gmmktime)
  984. {
  985. php_mktime(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
  986. }
  987. /* }}} */
  988. /* {{{ proto bool checkdate(int month, int day, int year)
  989. Returns true(1) if it is a valid date in gregorian calendar */
  990. PHP_FUNCTION(checkdate)
  991. {
  992. zend_long m, d, y;
  993. ZEND_PARSE_PARAMETERS_START(3, 3)
  994. Z_PARAM_LONG(m)
  995. Z_PARAM_LONG(d)
  996. Z_PARAM_LONG(y)
  997. ZEND_PARSE_PARAMETERS_END();
  998. if (y < 1 || y > 32767 || !timelib_valid_date(y, m, d)) {
  999. RETURN_FALSE;
  1000. }
  1001. RETURN_TRUE; /* True : This month, day, year arguments are valid */
  1002. }
  1003. /* }}} */
  1004. /* {{{ php_strftime - (gm)strftime helper */
  1005. PHPAPI void php_strftime(INTERNAL_FUNCTION_PARAMETERS, int gmt)
  1006. {
  1007. zend_string *format;
  1008. zend_long timestamp;
  1009. zend_bool timestamp_is_null = 1;
  1010. struct tm ta;
  1011. int max_reallocs = 5;
  1012. size_t buf_len = 256, real_len;
  1013. timelib_time *ts;
  1014. timelib_tzinfo *tzi;
  1015. timelib_time_offset *offset = NULL;
  1016. zend_string *buf;
  1017. ZEND_PARSE_PARAMETERS_START(1, 2)
  1018. Z_PARAM_STR(format)
  1019. Z_PARAM_OPTIONAL
  1020. Z_PARAM_LONG_OR_NULL(timestamp, timestamp_is_null)
  1021. ZEND_PARSE_PARAMETERS_END();
  1022. if (ZSTR_LEN(format) == 0) {
  1023. RETURN_FALSE;
  1024. }
  1025. if (timestamp_is_null) {
  1026. timestamp = (zend_long) php_time();
  1027. }
  1028. ts = timelib_time_ctor();
  1029. if (gmt) {
  1030. tzi = NULL;
  1031. timelib_unixtime2gmt(ts, (timelib_sll) timestamp);
  1032. } else {
  1033. tzi = get_timezone_info();
  1034. ts->tz_info = tzi;
  1035. ts->zone_type = TIMELIB_ZONETYPE_ID;
  1036. timelib_unixtime2local(ts, (timelib_sll) timestamp);
  1037. }
  1038. ta.tm_sec = ts->s;
  1039. ta.tm_min = ts->i;
  1040. ta.tm_hour = ts->h;
  1041. ta.tm_mday = ts->d;
  1042. ta.tm_mon = ts->m - 1;
  1043. ta.tm_year = ts->y - 1900;
  1044. ta.tm_wday = timelib_day_of_week(ts->y, ts->m, ts->d);
  1045. ta.tm_yday = timelib_day_of_year(ts->y, ts->m, ts->d);
  1046. if (gmt) {
  1047. ta.tm_isdst = 0;
  1048. #if HAVE_STRUCT_TM_TM_GMTOFF
  1049. ta.tm_gmtoff = 0;
  1050. #endif
  1051. #if HAVE_STRUCT_TM_TM_ZONE
  1052. ta.tm_zone = "GMT";
  1053. #endif
  1054. } else {
  1055. offset = timelib_get_time_zone_info(timestamp, tzi);
  1056. ta.tm_isdst = offset->is_dst;
  1057. #if HAVE_STRUCT_TM_TM_GMTOFF
  1058. ta.tm_gmtoff = offset->offset;
  1059. #endif
  1060. #if HAVE_STRUCT_TM_TM_ZONE
  1061. ta.tm_zone = offset->abbr;
  1062. #endif
  1063. }
  1064. /* VS2012 crt has a bug where strftime crash with %z and %Z format when the
  1065. initial buffer is too small. See
  1066. http://connect.microsoft.com/VisualStudio/feedback/details/759720/vs2012-strftime-crash-with-z-formatting-code */
  1067. buf = zend_string_alloc(buf_len, 0);
  1068. while ((real_len = strftime(ZSTR_VAL(buf), buf_len, ZSTR_VAL(format), &ta)) == buf_len || real_len == 0) {
  1069. buf_len *= 2;
  1070. buf = zend_string_extend(buf, buf_len, 0);
  1071. if (!--max_reallocs) {
  1072. break;
  1073. }
  1074. }
  1075. #ifdef PHP_WIN32
  1076. /* VS2012 strftime() returns number of characters, not bytes.
  1077. See VC++11 bug id 766205. */
  1078. if (real_len > 0) {
  1079. real_len = strlen(buf->val);
  1080. }
  1081. #endif
  1082. timelib_time_dtor(ts);
  1083. if (!gmt) {
  1084. timelib_time_offset_dtor(offset);
  1085. }
  1086. if (real_len && real_len != buf_len) {
  1087. buf = zend_string_truncate(buf, real_len, 0);
  1088. RETURN_NEW_STR(buf);
  1089. }
  1090. zend_string_efree(buf);
  1091. RETURN_FALSE;
  1092. }
  1093. /* }}} */
  1094. /* {{{ proto string strftime(string format [, int timestamp])
  1095. Format a local time/date according to locale settings */
  1096. PHP_FUNCTION(strftime)
  1097. {
  1098. php_strftime(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
  1099. }
  1100. /* }}} */
  1101. /* {{{ proto string gmstrftime(string format [, int timestamp])
  1102. Format a GMT/UCT time/date according to locale settings */
  1103. PHP_FUNCTION(gmstrftime)
  1104. {
  1105. php_strftime(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
  1106. }
  1107. /* }}} */
  1108. /* {{{ proto int time(void)
  1109. Return current UNIX timestamp */
  1110. PHP_FUNCTION(time)
  1111. {
  1112. ZEND_PARSE_PARAMETERS_NONE();
  1113. RETURN_LONG((zend_long)php_time());
  1114. }
  1115. /* }}} */
  1116. /* {{{ proto array localtime([int timestamp [, bool associative_array]])
  1117. 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 */
  1118. PHP_FUNCTION(localtime)
  1119. {
  1120. zend_long timestamp;
  1121. zend_bool timestamp_is_null = 1;
  1122. zend_bool associative = 0;
  1123. timelib_tzinfo *tzi;
  1124. timelib_time *ts;
  1125. ZEND_PARSE_PARAMETERS_START(0, 2)
  1126. Z_PARAM_OPTIONAL
  1127. Z_PARAM_LONG_OR_NULL(timestamp, timestamp_is_null)
  1128. Z_PARAM_BOOL(associative)
  1129. ZEND_PARSE_PARAMETERS_END();
  1130. if (timestamp_is_null) {
  1131. timestamp = (zend_long) php_time();
  1132. }
  1133. tzi = get_timezone_info();
  1134. ts = timelib_time_ctor();
  1135. ts->tz_info = tzi;
  1136. ts->zone_type = TIMELIB_ZONETYPE_ID;
  1137. timelib_unixtime2local(ts, (timelib_sll) timestamp);
  1138. array_init(return_value);
  1139. if (associative) {
  1140. add_assoc_long(return_value, "tm_sec", ts->s);
  1141. add_assoc_long(return_value, "tm_min", ts->i);
  1142. add_assoc_long(return_value, "tm_hour", ts->h);
  1143. add_assoc_long(return_value, "tm_mday", ts->d);
  1144. add_assoc_long(return_value, "tm_mon", ts->m - 1);
  1145. add_assoc_long(return_value, "tm_year", ts->y - 1900);
  1146. add_assoc_long(return_value, "tm_wday", timelib_day_of_week(ts->y, ts->m, ts->d));
  1147. add_assoc_long(return_value, "tm_yday", timelib_day_of_year(ts->y, ts->m, ts->d));
  1148. add_assoc_long(return_value, "tm_isdst", ts->dst);
  1149. } else {
  1150. add_next_index_long(return_value, ts->s);
  1151. add_next_index_long(return_value, ts->i);
  1152. add_next_index_long(return_value, ts->h);
  1153. add_next_index_long(return_value, ts->d);
  1154. add_next_index_long(return_value, ts->m - 1);
  1155. add_next_index_long(return_value, ts->y- 1900);
  1156. add_next_index_long(return_value, timelib_day_of_week(ts->y, ts->m, ts->d));
  1157. add_next_index_long(return_value, timelib_day_of_year(ts->y, ts->m, ts->d));
  1158. add_next_index_long(return_value, ts->dst);
  1159. }
  1160. timelib_time_dtor(ts);
  1161. }
  1162. /* }}} */
  1163. /* {{{ proto array getdate([int timestamp])
  1164. Get date/time information */
  1165. PHP_FUNCTION(getdate)
  1166. {
  1167. zend_long timestamp;
  1168. zend_bool timestamp_is_null = 1;
  1169. timelib_tzinfo *tzi;
  1170. timelib_time *ts;
  1171. ZEND_PARSE_PARAMETERS_START(0, 1)
  1172. Z_PARAM_OPTIONAL
  1173. Z_PARAM_LONG_OR_NULL(timestamp, timestamp_is_null)
  1174. ZEND_PARSE_PARAMETERS_END();
  1175. if (timestamp_is_null) {
  1176. timestamp = (zend_long) php_time();
  1177. }
  1178. tzi = get_timezone_info();
  1179. ts = timelib_time_ctor();
  1180. ts->tz_info = tzi;
  1181. ts->zone_type = TIMELIB_ZONETYPE_ID;
  1182. timelib_unixtime2local(ts, (timelib_sll) timestamp);
  1183. array_init(return_value);
  1184. add_assoc_long(return_value, "seconds", ts->s);
  1185. add_assoc_long(return_value, "minutes", ts->i);
  1186. add_assoc_long(return_value, "hours", ts->h);
  1187. add_assoc_long(return_value, "mday", ts->d);
  1188. add_assoc_long(return_value, "wday", timelib_day_of_week(ts->y, ts->m, ts->d));
  1189. add_assoc_long(return_value, "mon", ts->m);
  1190. add_assoc_long(return_value, "year", ts->y);
  1191. add_assoc_long(return_value, "yday", timelib_day_of_year(ts->y, ts->m, ts->d));
  1192. add_assoc_string(return_value, "weekday", php_date_full_day_name(ts->y, ts->m, ts->d));
  1193. add_assoc_string(return_value, "month", mon_full_names[ts->m - 1]);
  1194. add_index_long(return_value, 0, timestamp);
  1195. timelib_time_dtor(ts);
  1196. }
  1197. /* }}} */
  1198. #define PHP_DATE_TIMEZONE_GROUP_AFRICA 0x0001
  1199. #define PHP_DATE_TIMEZONE_GROUP_AMERICA 0x0002
  1200. #define PHP_DATE_TIMEZONE_GROUP_ANTARCTICA 0x0004
  1201. #define PHP_DATE_TIMEZONE_GROUP_ARCTIC 0x0008
  1202. #define PHP_DATE_TIMEZONE_GROUP_ASIA 0x0010
  1203. #define PHP_DATE_TIMEZONE_GROUP_ATLANTIC 0x0020
  1204. #define PHP_DATE_TIMEZONE_GROUP_AUSTRALIA 0x0040
  1205. #define PHP_DATE_TIMEZONE_GROUP_EUROPE 0x0080
  1206. #define PHP_DATE_TIMEZONE_GROUP_INDIAN 0x0100
  1207. #define PHP_DATE_TIMEZONE_GROUP_PACIFIC 0x0200
  1208. #define PHP_DATE_TIMEZONE_GROUP_UTC 0x0400
  1209. #define PHP_DATE_TIMEZONE_GROUP_ALL 0x07FF
  1210. #define PHP_DATE_TIMEZONE_GROUP_ALL_W_BC 0x0FFF
  1211. #define PHP_DATE_TIMEZONE_PER_COUNTRY 0x1000
  1212. #define PHP_DATE_PERIOD_EXCLUDE_START_DATE 0x0001
  1213. /* define an overloaded iterator structure */
  1214. typedef struct {
  1215. zend_object_iterator intern;
  1216. zval current;
  1217. php_period_obj *object;
  1218. int current_index;
  1219. } date_period_it;
  1220. /* {{{ date_period_it_invalidate_current */
  1221. static void date_period_it_invalidate_current(zend_object_iterator *iter)
  1222. {
  1223. date_period_it *iterator = (date_period_it *)iter;
  1224. if (Z_TYPE(iterator->current) != IS_UNDEF) {
  1225. zval_ptr_dtor(&iterator->current);
  1226. ZVAL_UNDEF(&iterator->current);
  1227. }
  1228. }
  1229. /* }}} */
  1230. /* {{{ date_period_it_dtor */
  1231. static void date_period_it_dtor(zend_object_iterator *iter)
  1232. {
  1233. date_period_it *iterator = (date_period_it *)iter;
  1234. date_period_it_invalidate_current(iter);
  1235. zval_ptr_dtor(&iterator->intern.data);
  1236. }
  1237. /* }}} */
  1238. /* {{{ date_period_it_has_more */
  1239. static int date_period_it_has_more(zend_object_iterator *iter)
  1240. {
  1241. date_period_it *iterator = (date_period_it *)iter;
  1242. php_period_obj *object = Z_PHPPERIOD_P(&iterator->intern.data);
  1243. timelib_time *it_time = object->current;
  1244. /* apply modification if it's not the first iteration */
  1245. if (!object->include_start_date || iterator->current_index > 0) {
  1246. it_time->have_relative = 1;
  1247. it_time->relative = *object->interval;
  1248. it_time->sse_uptodate = 0;
  1249. timelib_update_ts(it_time, NULL);
  1250. timelib_update_from_sse(it_time);
  1251. }
  1252. if (object->end) {
  1253. return object->current->sse < object->end->sse ? SUCCESS : FAILURE;
  1254. } else {
  1255. return (iterator->current_index < object->recurrences) ? SUCCESS : FAILURE;
  1256. }
  1257. }
  1258. /* }}} */
  1259. /* {{{ date_period_it_current_data */
  1260. static zval *date_period_it_current_data(zend_object_iterator *iter)
  1261. {
  1262. date_period_it *iterator = (date_period_it *)iter;
  1263. php_period_obj *object = Z_PHPPERIOD_P(&iterator->intern.data);
  1264. timelib_time *it_time = object->current;
  1265. php_date_obj *newdateobj;
  1266. /* Create new object */
  1267. php_date_instantiate(object->start_ce, &iterator->current);
  1268. newdateobj = Z_PHPDATE_P(&iterator->current);
  1269. newdateobj->time = timelib_time_ctor();
  1270. *newdateobj->time = *it_time;
  1271. if (it_time->tz_abbr) {
  1272. newdateobj->time->tz_abbr = timelib_strdup(it_time->tz_abbr);
  1273. }
  1274. if (it_time->tz_info) {
  1275. newdateobj->time->tz_info = it_time->tz_info;
  1276. }
  1277. return &iterator->current;
  1278. }
  1279. /* }}} */
  1280. /* {{{ date_period_it_current_key */
  1281. static void date_period_it_current_key(zend_object_iterator *iter, zval *key)
  1282. {
  1283. date_period_it *iterator = (date_period_it *)iter;
  1284. ZVAL_LONG(key, iterator->current_index);
  1285. }
  1286. /* }}} */
  1287. /* {{{ date_period_it_move_forward */
  1288. static void date_period_it_move_forward(zend_object_iterator *iter)
  1289. {
  1290. date_period_it *iterator = (date_period_it *)iter;
  1291. iterator->current_index++;
  1292. date_period_it_invalidate_current(iter);
  1293. }
  1294. /* }}} */
  1295. /* {{{ date_period_it_rewind */
  1296. static void date_period_it_rewind(zend_object_iterator *iter)
  1297. {
  1298. date_period_it *iterator = (date_period_it *)iter;
  1299. iterator->current_index = 0;
  1300. if (iterator->object->current) {
  1301. timelib_time_dtor(iterator->object->current);
  1302. }
  1303. if (!iterator->object->start) {
  1304. zend_throw_error(NULL, "DatePeriod has not been initialized correctly");
  1305. return;
  1306. }
  1307. iterator->object->current = timelib_time_clone(iterator->object->start);
  1308. date_period_it_invalidate_current(iter);
  1309. }
  1310. /* }}} */
  1311. /* iterator handler table */
  1312. static const zend_object_iterator_funcs date_period_it_funcs = {
  1313. date_period_it_dtor,
  1314. date_period_it_has_more,
  1315. date_period_it_current_data,
  1316. date_period_it_current_key,
  1317. date_period_it_move_forward,
  1318. date_period_it_rewind,
  1319. date_period_it_invalidate_current
  1320. };
  1321. zend_object_iterator *date_object_period_get_iterator(zend_class_entry *ce, zval *object, int by_ref) /* {{{ */
  1322. {
  1323. date_period_it *iterator;
  1324. if (by_ref) {
  1325. zend_throw_error(NULL, "An iterator cannot be used with foreach by reference");
  1326. return NULL;
  1327. }
  1328. iterator = emalloc(sizeof(date_period_it));
  1329. zend_iterator_init((zend_object_iterator*)iterator);
  1330. Z_ADDREF_P(object);
  1331. ZVAL_OBJ(&iterator->intern.data, Z_OBJ_P(object));
  1332. iterator->intern.funcs = &date_period_it_funcs;
  1333. iterator->object = Z_PHPPERIOD_P(object);
  1334. ZVAL_UNDEF(&iterator->current);
  1335. return (zend_object_iterator*)iterator;
  1336. } /* }}} */
  1337. static int implement_date_interface_handler(zend_class_entry *interface, zend_class_entry *implementor) /* {{{ */
  1338. {
  1339. if (implementor->type == ZEND_USER_CLASS &&
  1340. !instanceof_function(implementor, date_ce_date) &&
  1341. !instanceof_function(implementor, date_ce_immutable)
  1342. ) {
  1343. zend_error(E_ERROR, "DateTimeInterface can't be implemented by user classes");
  1344. }
  1345. return SUCCESS;
  1346. } /* }}} */
  1347. static int date_interval_has_property(zend_object *object, zend_string *name, int type, void **cache_slot) /* {{{ */
  1348. {
  1349. php_interval_obj *obj;
  1350. zval rv;
  1351. zval *prop;
  1352. int retval = 0;
  1353. obj = php_interval_obj_from_obj(object);
  1354. if (!obj->initialized) {
  1355. retval = zend_std_has_property(object, name, type, cache_slot);
  1356. return retval;
  1357. }
  1358. prop = date_interval_read_property(object, name, BP_VAR_IS, cache_slot, &rv);
  1359. if (prop != &EG(uninitialized_zval)) {
  1360. if (type == 2) {
  1361. retval = 1;
  1362. } else if (type == 1) {
  1363. retval = zend_is_true(prop);
  1364. } else if (type == 0) {
  1365. retval = (Z_TYPE_P(prop) != IS_NULL);
  1366. }
  1367. } else {
  1368. retval = zend_std_has_property(object, name, type, cache_slot);
  1369. }
  1370. return retval;
  1371. }
  1372. /* }}} */
  1373. static void date_register_classes(void) /* {{{ */
  1374. {
  1375. zend_class_entry ce_date, ce_immutable, ce_timezone, ce_interval, ce_period, ce_interface;
  1376. INIT_CLASS_ENTRY(ce_interface, "DateTimeInterface", class_DateTimeInterface_methods);
  1377. date_ce_interface = zend_register_internal_interface(&ce_interface);
  1378. date_ce_interface->interface_gets_implemented = implement_date_interface_handler;
  1379. #define REGISTER_DATE_INTERFACE_CONST_STRING(const_name, value) \
  1380. zend_declare_class_constant_stringl(date_ce_interface, const_name, sizeof(const_name)-1, value, sizeof(value)-1);
  1381. REGISTER_DATE_INTERFACE_CONST_STRING("ATOM", DATE_FORMAT_RFC3339);
  1382. REGISTER_DATE_INTERFACE_CONST_STRING("COOKIE", DATE_FORMAT_COOKIE);
  1383. REGISTER_DATE_INTERFACE_CONST_STRING("ISO8601", DATE_FORMAT_ISO8601);
  1384. REGISTER_DATE_INTERFACE_CONST_STRING("RFC822", DATE_FORMAT_RFC822);
  1385. REGISTER_DATE_INTERFACE_CONST_STRING("RFC850", DATE_FORMAT_RFC850);
  1386. REGISTER_DATE_INTERFACE_CONST_STRING("RFC1036", DATE_FORMAT_RFC1036);
  1387. REGISTER_DATE_INTERFACE_CONST_STRING("RFC1123", DATE_FORMAT_RFC1123);
  1388. REGISTER_DATE_INTERFACE_CONST_STRING("RFC7231", DATE_FORMAT_RFC7231);
  1389. REGISTER_DATE_INTERFACE_CONST_STRING("RFC2822", DATE_FORMAT_RFC2822);
  1390. REGISTER_DATE_INTERFACE_CONST_STRING("RFC3339", DATE_FORMAT_RFC3339);
  1391. REGISTER_DATE_INTERFACE_CONST_STRING("RFC3339_EXTENDED", DATE_FORMAT_RFC3339_EXTENDED);
  1392. REGISTER_DATE_INTERFACE_CONST_STRING("RSS", DATE_FORMAT_RFC1123);
  1393. REGISTER_DATE_INTERFACE_CONST_STRING("W3C", DATE_FORMAT_RFC3339);
  1394. INIT_CLASS_ENTRY(ce_date, "DateTime", class_DateTime_methods);
  1395. ce_date.create_object = date_object_new_date;
  1396. date_ce_date = zend_register_internal_class_ex(&ce_date, NULL);
  1397. memcpy(&date_object_handlers_date, &std_object_handlers, sizeof(zend_object_handlers));
  1398. date_object_handlers_date.offset = XtOffsetOf(php_date_obj, std);
  1399. date_object_handlers_date.free_obj = date_object_free_storage_date;
  1400. date_object_handlers_date.clone_obj = date_object_clone_date;
  1401. date_object_handlers_date.compare = date_object_compare_date;
  1402. date_object_handlers_date.get_properties_for = date_object_get_properties_for;
  1403. date_object_handlers_date.get_gc = date_object_get_gc;
  1404. zend_class_implements(date_ce_date, 1, date_ce_interface);
  1405. INIT_CLASS_ENTRY(ce_immutable, "DateTimeImmutable", class_DateTimeImmutable_methods);
  1406. ce_immutable.create_object = date_object_new_date;
  1407. date_ce_immutable = zend_register_internal_class_ex(&ce_immutable, NULL);
  1408. memcpy(&date_object_handlers_immutable, &std_object_handlers, sizeof(zend_object_handlers));
  1409. date_object_handlers_immutable.clone_obj = date_object_clone_date;
  1410. date_object_handlers_immutable.compare = date_object_compare_date;
  1411. date_object_handlers_immutable.get_properties_for = date_object_get_properties_for;
  1412. date_object_handlers_immutable.get_gc = date_object_get_gc;
  1413. zend_class_implements(date_ce_immutable, 1, date_ce_interface);
  1414. INIT_CLASS_ENTRY(ce_timezone, "DateTimeZone", class_DateTimeZone_methods);
  1415. ce_timezone.create_object = date_object_new_timezone;
  1416. date_ce_timezone = zend_register_internal_class_ex(&ce_timezone, NULL);
  1417. memcpy(&date_object_handlers_timezone, &std_object_handlers, sizeof(zend_object_handlers));
  1418. date_object_handlers_timezone.offset = XtOffsetOf(php_timezone_obj, std);
  1419. date_object_handlers_timezone.free_obj = date_object_free_storage_timezone;
  1420. date_object_handlers_timezone.clone_obj = date_object_clone_timezone;
  1421. date_object_handlers_timezone.get_properties_for = date_object_get_properties_for_timezone;
  1422. date_object_handlers_timezone.get_gc = date_object_get_gc_timezone;
  1423. date_object_handlers_timezone.get_debug_info = date_object_get_debug_info_timezone;
  1424. date_object_handlers_timezone.compare = date_object_compare_timezone;
  1425. #define REGISTER_TIMEZONE_CLASS_CONST_STRING(const_name, value) \
  1426. zend_declare_class_constant_long(date_ce_timezone, const_name, sizeof(const_name)-1, value);
  1427. REGISTER_TIMEZONE_CLASS_CONST_STRING("AFRICA", PHP_DATE_TIMEZONE_GROUP_AFRICA);
  1428. REGISTER_TIMEZONE_CLASS_CONST_STRING("AMERICA", PHP_DATE_TIMEZONE_GROUP_AMERICA);
  1429. REGISTER_TIMEZONE_CLASS_CONST_STRING("ANTARCTICA", PHP_DATE_TIMEZONE_GROUP_ANTARCTICA);
  1430. REGISTER_TIMEZONE_CLASS_CONST_STRING("ARCTIC", PHP_DATE_TIMEZONE_GROUP_ARCTIC);
  1431. REGISTER_TIMEZONE_CLASS_CONST_STRING("ASIA", PHP_DATE_TIMEZONE_GROUP_ASIA);
  1432. REGISTER_TIMEZONE_CLASS_CONST_STRING("ATLANTIC", PHP_DATE_TIMEZONE_GROUP_ATLANTIC);
  1433. REGISTER_TIMEZONE_CLASS_CONST_STRING("AUSTRALIA", PHP_DATE_TIMEZONE_GROUP_AUSTRALIA);
  1434. REGISTER_TIMEZONE_CLASS_CONST_STRING("EUROPE", PHP_DATE_TIMEZONE_GROUP_EUROPE);
  1435. REGISTER_TIMEZONE_CLASS_CONST_STRING("INDIAN", PHP_DATE_TIMEZONE_GROUP_INDIAN);
  1436. REGISTER_TIMEZONE_CLASS_CONST_STRING("PACIFIC", PHP_DATE_TIMEZONE_GROUP_PACIFIC);
  1437. REGISTER_TIMEZONE_CLASS_CONST_STRING("UTC", PHP_DATE_TIMEZONE_GROUP_UTC);
  1438. REGISTER_TIMEZONE_CLASS_CONST_STRING("ALL", PHP_DATE_TIMEZONE_GROUP_ALL);
  1439. REGISTER_TIMEZONE_CLASS_CONST_STRING("ALL_WITH_BC", PHP_DATE_TIMEZONE_GROUP_ALL_W_BC);
  1440. REGISTER_TIMEZONE_CLASS_CONST_STRING("PER_COUNTRY", PHP_DATE_TIMEZONE_PER_COUNTRY);
  1441. INIT_CLASS_ENTRY(ce_interval, "DateInterval", class_DateInterval_methods);
  1442. ce_interval.create_object = date_object_new_interval;
  1443. date_ce_interval = zend_register_internal_class_ex(&ce_interval, NULL);
  1444. memcpy(&date_object_handlers_interval, &std_object_handlers, sizeof(zend_object_handlers));
  1445. date_object_handlers_interval.offset = XtOffsetOf(php_interval_obj, std);
  1446. date_object_handlers_interval.free_obj = date_object_free_storage_interval;
  1447. date_object_handlers_interval.clone_obj = date_object_clone_interval;
  1448. date_object_handlers_interval.has_property = date_interval_has_property;
  1449. date_object_handlers_interval.read_property = date_interval_read_property;
  1450. date_object_handlers_interval.write_property = date_interval_write_property;
  1451. date_object_handlers_interval.get_properties = date_object_get_properties_interval;
  1452. date_object_handlers_interval.get_property_ptr_ptr = date_interval_get_property_ptr_ptr;
  1453. date_object_handlers_interval.get_gc = date_object_get_gc_interval;
  1454. date_object_handlers_interval.compare = date_interval_compare_objects;
  1455. INIT_CLASS_ENTRY(ce_period, "DatePeriod", class_DatePeriod_methods);
  1456. ce_period.create_object = date_object_new_period;
  1457. date_ce_period = zend_register_internal_class_ex(&ce_period, NULL);
  1458. date_ce_period->get_iterator = date_object_period_get_iterator;
  1459. zend_class_implements(date_ce_period, 1, zend_ce_traversable);
  1460. memcpy(&date_object_handlers_period, &std_object_handlers, sizeof(zend_object_handlers));
  1461. date_object_handlers_period.offset = XtOffsetOf(php_period_obj, std);
  1462. date_object_handlers_period.free_obj = date_object_free_storage_period;
  1463. date_object_handlers_period.clone_obj = date_object_clone_period;
  1464. date_object_handlers_period.get_properties = date_object_get_properties_period;
  1465. date_object_handlers_period.get_property_ptr_ptr = date_period_get_property_ptr_ptr;
  1466. date_object_handlers_period.get_gc = date_object_get_gc_period;
  1467. date_object_handlers_period.read_property = date_period_read_property;
  1468. date_object_handlers_period.write_property = date_period_write_property;
  1469. #define REGISTER_PERIOD_CLASS_CONST_STRING(const_name, value) \
  1470. zend_declare_class_constant_long(date_ce_period, const_name, sizeof(const_name)-1, value);
  1471. REGISTER_PERIOD_CLASS_CONST_STRING("EXCLUDE_START_DATE", PHP_DATE_PERIOD_EXCLUDE_START_DATE);
  1472. } /* }}} */
  1473. static zend_object *date_object_new_date(zend_class_entry *class_type) /* {{{ */
  1474. {
  1475. php_date_obj *intern = zend_object_alloc(sizeof(php_date_obj), class_type);
  1476. zend_object_std_init(&intern->std, class_type);
  1477. object_properties_init(&intern->std, class_type);
  1478. intern->std.handlers = &date_object_handlers_date;
  1479. return &intern->std;
  1480. } /* }}} */
  1481. static zend_object *date_object_clone_date(zend_object *this_ptr) /* {{{ */
  1482. {
  1483. php_date_obj *old_obj = php_date_obj_from_obj(this_ptr);
  1484. php_date_obj *new_obj = php_date_obj_from_obj(date_object_new_date(old_obj->std.ce));
  1485. zend_objects_clone_members(&new_obj->std, &old_obj->std);
  1486. if (!old_obj->time) {
  1487. return &new_obj->std;
  1488. }
  1489. /* this should probably moved to a new `timelib_time *timelime_time_clone(timelib_time *)` */
  1490. new_obj->time = timelib_time_ctor();
  1491. *new_obj->time = *old_obj->time;
  1492. if (old_obj->time->tz_abbr) {
  1493. new_obj->time->tz_abbr = timelib_strdup(old_obj->time->tz_abbr);
  1494. }
  1495. if (old_obj->time->tz_info) {
  1496. new_obj->time->tz_info = old_obj->time->tz_info;
  1497. }
  1498. return &new_obj->std;
  1499. } /* }}} */
  1500. static void date_clone_immutable(zval *object, zval *new_object) /* {{{ */
  1501. {
  1502. ZVAL_OBJ(new_object, date_object_clone_date(Z_OBJ_P(object)));
  1503. } /* }}} */
  1504. static int date_object_compare_date(zval *d1, zval *d2) /* {{{ */
  1505. {
  1506. php_date_obj *o1;
  1507. php_date_obj *o2;
  1508. ZEND_COMPARE_OBJECTS_FALLBACK(d1, d2);
  1509. o1 = Z_PHPDATE_P(d1);
  1510. o2 = Z_PHPDATE_P(d2);
  1511. if (!o1->time || !o2->time) {
  1512. php_error_docref(NULL, E_WARNING, "Trying to compare an incomplete DateTime or DateTimeImmutable object");
  1513. return ZEND_UNCOMPARABLE;
  1514. }
  1515. if (!o1->time->sse_uptodate) {
  1516. timelib_update_ts(o1->time, o1->time->tz_info);
  1517. }
  1518. if (!o2->time->sse_uptodate) {
  1519. timelib_update_ts(o2->time, o2->time->tz_info);
  1520. }
  1521. return timelib_time_compare(o1->time, o2->time);
  1522. } /* }}} */
  1523. static HashTable *date_object_get_gc(zend_object *object, zval **table, int *n) /* {{{ */
  1524. {
  1525. *table = NULL;
  1526. *n = 0;
  1527. return zend_std_get_properties(object);
  1528. } /* }}} */
  1529. static HashTable *date_object_get_gc_timezone(zend_object *object, zval **table, int *n) /* {{{ */
  1530. {
  1531. *table = NULL;
  1532. *n = 0;
  1533. return zend_std_get_properties(object);
  1534. } /* }}} */
  1535. static HashTable *date_object_get_properties_for(zend_object *object, zend_prop_purpose purpose) /* {{{ */
  1536. {
  1537. HashTable *props;
  1538. zval zv;
  1539. php_date_obj *dateobj;
  1540. switch (purpose) {
  1541. case ZEND_PROP_PURPOSE_DEBUG:
  1542. case ZEND_PROP_PURPOSE_SERIALIZE:
  1543. case ZEND_PROP_PURPOSE_VAR_EXPORT:
  1544. case ZEND_PROP_PURPOSE_JSON:
  1545. case ZEND_PROP_PURPOSE_ARRAY_CAST:
  1546. break;
  1547. default:
  1548. return zend_std_get_properties_for(object, purpose);
  1549. }
  1550. dateobj = php_date_obj_from_obj(object);
  1551. props = zend_array_dup(zend_std_get_properties(object));
  1552. if (!dateobj->time) {
  1553. return props;
  1554. }
  1555. /* first we add the date and time in ISO format */
  1556. ZVAL_STR(&zv, date_format("Y-m-d H:i:s.u", sizeof("Y-m-d H:i:s.u")-1, dateobj->time, 1));
  1557. zend_hash_str_update(props, "date", sizeof("date")-1, &zv);
  1558. /* then we add the timezone name (or similar) */
  1559. if (dateobj->time->is_localtime) {
  1560. ZVAL_LONG(&zv, dateobj->time->zone_type);
  1561. zend_hash_str_update(props, "timezone_type", sizeof("timezone_type")-1, &zv);
  1562. switch (dateobj->time->zone_type) {
  1563. case TIMELIB_ZONETYPE_ID:
  1564. ZVAL_STRING(&zv, dateobj->time->tz_info->name);
  1565. break;
  1566. case TIMELIB_ZONETYPE_OFFSET: {
  1567. zend_string *tmpstr = zend_string_alloc(sizeof("UTC+05:00")-1, 0);
  1568. int utc_offset = dateobj->time->z;
  1569. ZSTR_LEN(tmpstr) = snprintf(ZSTR_VAL(tmpstr), sizeof("+05:00"), "%c%02d:%02d",
  1570. utc_offset < 0 ? '-' : '+',
  1571. abs(utc_offset / 3600),
  1572. abs(((utc_offset % 3600) / 60)));
  1573. ZVAL_NEW_STR(&zv, tmpstr);
  1574. }
  1575. break;
  1576. case TIMELIB_ZONETYPE_ABBR:
  1577. ZVAL_STRING(&zv, dateobj->time->tz_abbr);
  1578. break;
  1579. }
  1580. zend_hash_str_update(props, "timezone", sizeof("timezone")-1, &zv);
  1581. }
  1582. return props;
  1583. } /* }}} */
  1584. static zend_object *date_object_new_timezone(zend_class_entry *class_type) /* {{{ */
  1585. {
  1586. php_timezone_obj *intern = zend_object_alloc(sizeof(php_timezone_obj), class_type);
  1587. zend_object_std_init(&intern->std, class_type);
  1588. object_properties_init(&intern->std, class_type);
  1589. intern->std.handlers = &date_object_handlers_timezone;
  1590. return &intern->std;
  1591. } /* }}} */
  1592. static zend_object *date_object_clone_timezone(zend_object *this_ptr) /* {{{ */
  1593. {
  1594. php_timezone_obj *old_obj = php_timezone_obj_from_obj(this_ptr);
  1595. php_timezone_obj *new_obj = php_timezone_obj_from_obj(date_object_new_timezone(old_obj->std.ce));
  1596. zend_objects_clone_members(&new_obj->std, &old_obj->std);
  1597. if (!old_obj->initialized) {
  1598. return &new_obj->std;
  1599. }
  1600. new_obj->type = old_obj->type;
  1601. new_obj->initialized = 1;
  1602. switch (new_obj->type) {
  1603. case TIMELIB_ZONETYPE_ID:
  1604. new_obj->tzi.tz = old_obj->tzi.tz;
  1605. break;
  1606. case TIMELIB_ZONETYPE_OFFSET:
  1607. new_obj->tzi.utc_offset = old_obj->tzi.utc_offset;
  1608. break;
  1609. case TIMELIB_ZONETYPE_ABBR:
  1610. new_obj->tzi.z.utc_offset = old_obj->tzi.z.utc_offset;
  1611. new_obj->tzi.z.dst = old_obj->tzi.z.dst;
  1612. new_obj->tzi.z.abbr = timelib_strdup(old_obj->tzi.z.abbr);
  1613. break;
  1614. }
  1615. return &new_obj->std;
  1616. } /* }}} */
  1617. static int date_object_compare_timezone(zval *tz1, zval *tz2) /* {{{ */
  1618. {
  1619. php_timezone_obj *o1, *o2;
  1620. ZEND_COMPARE_OBJECTS_FALLBACK(tz1, tz2);
  1621. o1 = Z_PHPTIMEZONE_P(tz1);
  1622. o2 = Z_PHPTIMEZONE_P(tz2);
  1623. ZEND_ASSERT(o1->initialized && o2->initialized);
  1624. if (o1->type != o2->type) {
  1625. php_error_docref(NULL, E_WARNING, "Trying to compare different kinds of DateTimeZone objects");
  1626. return ZEND_UNCOMPARABLE;
  1627. }
  1628. switch (o1->type) {
  1629. case TIMELIB_ZONETYPE_OFFSET:
  1630. return o1->tzi.utc_offset == o2->tzi.utc_offset ? 0 : 1;
  1631. case TIMELIB_ZONETYPE_ABBR:
  1632. return strcmp(o1->tzi.z.abbr, o2->tzi.z.abbr) ? 1 : 0;
  1633. case TIMELIB_ZONETYPE_ID:
  1634. return strcmp(o1->tzi.tz->name, o2->tzi.tz->name) ? 1 : 0;
  1635. EMPTY_SWITCH_DEFAULT_CASE();
  1636. }
  1637. } /* }}} */
  1638. static void php_timezone_to_string(php_timezone_obj *tzobj, zval *zv)
  1639. {
  1640. switch (tzobj->type) {
  1641. case TIMELIB_ZONETYPE_ID:
  1642. ZVAL_STRING(zv, tzobj->tzi.tz->name);
  1643. break;
  1644. case TIMELIB_ZONETYPE_OFFSET: {
  1645. zend_string *tmpstr = zend_string_alloc(sizeof("UTC+05:00")-1, 0);
  1646. timelib_sll utc_offset = tzobj->tzi.utc_offset;
  1647. ZSTR_LEN(tmpstr) = snprintf(ZSTR_VAL(tmpstr), sizeof("+05:00"), "%c%02d:%02d",
  1648. utc_offset < 0 ? '-' : '+',
  1649. abs((int)(utc_offset / 3600)),
  1650. abs((int)(utc_offset % 3600) / 60));
  1651. ZVAL_NEW_STR(zv, tmpstr);
  1652. }
  1653. break;
  1654. case TIMELIB_ZONETYPE_ABBR:
  1655. ZVAL_STRING(zv, tzobj->tzi.z.abbr);
  1656. break;
  1657. }
  1658. }
  1659. static HashTable *date_object_get_properties_for_timezone(zend_object *object, zend_prop_purpose purpose) /* {{{ */
  1660. {
  1661. HashTable *props;
  1662. zval zv;
  1663. php_timezone_obj *tzobj;
  1664. switch (purpose) {
  1665. case ZEND_PROP_PURPOSE_DEBUG:
  1666. case ZEND_PROP_PURPOSE_SERIALIZE:
  1667. case ZEND_PROP_PURPOSE_VAR_EXPORT:
  1668. case ZEND_PROP_PURPOSE_JSON:
  1669. case ZEND_PROP_PURPOSE_ARRAY_CAST:
  1670. break;
  1671. default:
  1672. return zend_std_get_properties_for(object, purpose);
  1673. }
  1674. tzobj = php_timezone_obj_from_obj(object);
  1675. props = zend_array_dup(zend_std_get_properties(object));
  1676. if (!tzobj->initialized) {
  1677. return props;
  1678. }
  1679. ZVAL_LONG(&zv, tzobj->type);
  1680. zend_hash_str_update(props, "timezone_type", sizeof("timezone_type")-1, &zv);
  1681. php_timezone_to_string(tzobj, &zv);
  1682. zend_hash_str_update(props, "timezone", sizeof("timezone")-1, &zv);
  1683. return props;
  1684. } /* }}} */
  1685. static HashTable *date_object_get_debug_info_timezone(zend_object *object, int *is_temp) /* {{{ */
  1686. {
  1687. HashTable *ht, *props;
  1688. zval zv;
  1689. php_timezone_obj *tzobj;
  1690. tzobj = php_timezone_obj_from_obj(object);
  1691. props = zend_std_get_properties(object);
  1692. *is_temp = 1;
  1693. ht = zend_array_dup(props);
  1694. ZVAL_LONG(&zv, tzobj->type);
  1695. zend_hash_str_update(ht, "timezone_type", sizeof("timezone_type")-1, &zv);
  1696. php_timezone_to_string(tzobj, &zv);
  1697. zend_hash_str_update(ht, "timezone", sizeof("timezone")-1, &zv);
  1698. return ht;
  1699. } /* }}} */
  1700. static zend_object *date_object_new_interval(zend_class_entry *class_type) /* {{{ */
  1701. {
  1702. php_interval_obj *intern = zend_object_alloc(sizeof(php_interval_obj), class_type);
  1703. zend_object_std_init(&intern->std, class_type);
  1704. object_properties_init(&intern->std, class_type);
  1705. intern->std.handlers = &date_object_handlers_interval;
  1706. return &intern->std;
  1707. } /* }}} */
  1708. static zend_object *date_object_clone_interval(zend_object *this_ptr) /* {{{ */
  1709. {
  1710. php_interval_obj *old_obj = php_interval_obj_from_obj(this_ptr);
  1711. php_interval_obj *new_obj = php_interval_obj_from_obj(date_object_new_interval(old_obj->std.ce));
  1712. zend_objects_clone_members(&new_obj->std, &old_obj->std);
  1713. new_obj->initialized = old_obj->initialized;
  1714. if (old_obj->diff) {
  1715. new_obj->diff = timelib_rel_time_clone(old_obj->diff);
  1716. }
  1717. return &new_obj->std;
  1718. } /* }}} */
  1719. static HashTable *date_object_get_gc_interval(zend_object *object, zval **table, int *n) /* {{{ */
  1720. {
  1721. *table = NULL;
  1722. *n = 0;
  1723. return zend_std_get_properties(object);
  1724. } /* }}} */
  1725. static HashTable *date_object_get_properties_interval(zend_object *object) /* {{{ */
  1726. {
  1727. HashTable *props;
  1728. zval zv;
  1729. php_interval_obj *intervalobj;
  1730. intervalobj = php_interval_obj_from_obj(object);
  1731. props = zend_std_get_properties(object);
  1732. if (!intervalobj->initialized) {
  1733. return props;
  1734. }
  1735. #define PHP_DATE_INTERVAL_ADD_PROPERTY(n,f) \
  1736. ZVAL_LONG(&zv, (zend_long)intervalobj->diff->f); \
  1737. zend_hash_str_update(props, n, sizeof(n)-1, &zv);
  1738. PHP_DATE_INTERVAL_ADD_PROPERTY("y", y);
  1739. PHP_DATE_INTERVAL_ADD_PROPERTY("m", m);
  1740. PHP_DATE_INTERVAL_ADD_PROPERTY("d", d);
  1741. PHP_DATE_INTERVAL_ADD_PROPERTY("h", h);
  1742. PHP_DATE_INTERVAL_ADD_PROPERTY("i", i);
  1743. PHP_DATE_INTERVAL_ADD_PROPERTY("s", s);
  1744. ZVAL_DOUBLE(&zv, (double)intervalobj->diff->us / 1000000.0);
  1745. zend_hash_str_update(props, "f", sizeof("f") - 1, &zv);
  1746. PHP_DATE_INTERVAL_ADD_PROPERTY("weekday", weekday);
  1747. PHP_DATE_INTERVAL_ADD_PROPERTY("weekday_behavior", weekday_behavior);
  1748. PHP_DATE_INTERVAL_ADD_PROPERTY("first_last_day_of", first_last_day_of);
  1749. PHP_DATE_INTERVAL_ADD_PROPERTY("invert", invert);
  1750. if (intervalobj->diff->days != -99999) {
  1751. PHP_DATE_INTERVAL_ADD_PROPERTY("days", days);
  1752. } else {
  1753. ZVAL_FALSE(&zv);
  1754. zend_hash_str_update(props, "days", sizeof("days")-1, &zv);
  1755. }
  1756. PHP_DATE_INTERVAL_ADD_PROPERTY("special_type", special.type);
  1757. PHP_DATE_INTERVAL_ADD_PROPERTY("special_amount", special.amount);
  1758. PHP_DATE_INTERVAL_ADD_PROPERTY("have_weekday_relative", have_weekday_relative);
  1759. PHP_DATE_INTERVAL_ADD_PROPERTY("have_special_relative", have_special_relative);
  1760. return props;
  1761. } /* }}} */
  1762. static zend_object *date_object_new_period(zend_class_entry *class_type) /* {{{ */
  1763. {
  1764. php_period_obj *intern = zend_object_alloc(sizeof(php_period_obj), class_type);
  1765. zend_object_std_init(&intern->std, class_type);
  1766. object_properties_init(&intern->std, class_type);
  1767. intern->std.handlers = &date_object_handlers_period;
  1768. return &intern->std;
  1769. } /* }}} */
  1770. static zend_object *date_object_clone_period(zend_object *this_ptr) /* {{{ */
  1771. {
  1772. php_period_obj *old_obj = php_period_obj_from_obj(this_ptr);
  1773. php_period_obj *new_obj = php_period_obj_from_obj(date_object_new_period(old_obj->std.ce));
  1774. zend_objects_clone_members(&new_obj->std, &old_obj->std);
  1775. new_obj->initialized = old_obj->initialized;
  1776. new_obj->recurrences = old_obj->recurrences;
  1777. new_obj->include_start_date = old_obj->include_start_date;
  1778. new_obj->start_ce = old_obj->start_ce;
  1779. if (old_obj->start) {
  1780. new_obj->start = timelib_time_clone(old_obj->start);
  1781. }
  1782. if (old_obj->current) {
  1783. new_obj->current = timelib_time_clone(old_obj->current);
  1784. }
  1785. if (old_obj->end) {
  1786. new_obj->end = timelib_time_clone(old_obj->end);
  1787. }
  1788. if (old_obj->interval) {
  1789. new_obj->interval = timelib_rel_time_clone(old_obj->interval);
  1790. }
  1791. return &new_obj->std;
  1792. } /* }}} */
  1793. static void date_object_free_storage_date(zend_object *object) /* {{{ */
  1794. {
  1795. php_date_obj *intern = php_date_obj_from_obj(object);
  1796. if (intern->time) {
  1797. timelib_time_dtor(intern->time);
  1798. }
  1799. zend_object_std_dtor(&intern->std);
  1800. } /* }}} */
  1801. static void date_object_free_storage_timezone(zend_object *object) /* {{{ */
  1802. {
  1803. php_timezone_obj *intern = php_timezone_obj_from_obj(object);
  1804. if (intern->type == TIMELIB_ZONETYPE_ABBR) {
  1805. timelib_free(intern->tzi.z.abbr);
  1806. }
  1807. zend_object_std_dtor(&intern->std);
  1808. } /* }}} */
  1809. static void date_object_free_storage_interval(zend_object *object) /* {{{ */
  1810. {
  1811. php_interval_obj *intern = php_interval_obj_from_obj(object);
  1812. timelib_rel_time_dtor(intern->diff);
  1813. zend_object_std_dtor(&intern->std);
  1814. } /* }}} */
  1815. static void date_object_free_storage_period(zend_object *object) /* {{{ */
  1816. {
  1817. php_period_obj *intern = php_period_obj_from_obj(object);
  1818. if (intern->start) {
  1819. timelib_time_dtor(intern->start);
  1820. }
  1821. if (intern->current) {
  1822. timelib_time_dtor(intern->current);
  1823. }
  1824. if (intern->end) {
  1825. timelib_time_dtor(intern->end);
  1826. }
  1827. timelib_rel_time_dtor(intern->interval);
  1828. zend_object_std_dtor(&intern->std);
  1829. } /* }}} */
  1830. /* Advanced Interface */
  1831. PHPAPI zval *php_date_instantiate(zend_class_entry *pce, zval *object) /* {{{ */
  1832. {
  1833. object_init_ex(object, pce);
  1834. return object;
  1835. } /* }}} */
  1836. /* Helper function used to store the latest found warnings and errors while
  1837. * parsing, from either strtotime or parse_from_format. */
  1838. static void update_errors_warnings(timelib_error_container *last_errors) /* {{{ */
  1839. {
  1840. if (DATEG(last_errors)) {
  1841. timelib_error_container_dtor(DATEG(last_errors));
  1842. DATEG(last_errors) = NULL;
  1843. }
  1844. DATEG(last_errors) = last_errors;
  1845. } /* }}} */
  1846. static void php_date_set_time_fraction(timelib_time *time, int microseconds)
  1847. {
  1848. time->us = microseconds;
  1849. }
  1850. static void php_date_get_current_time_with_fraction(time_t *sec, suseconds_t *usec)
  1851. {
  1852. #if HAVE_GETTIMEOFDAY
  1853. struct timeval tp = {0}; /* For setting microseconds */
  1854. gettimeofday(&tp, NULL);
  1855. *sec = tp.tv_sec;
  1856. *usec = tp.tv_usec;
  1857. #else
  1858. *sec = time(NULL);
  1859. *usec = 0;
  1860. #endif
  1861. }
  1862. PHPAPI int php_date_initialize(php_date_obj *dateobj, /*const*/ char *time_str, size_t time_str_len, char *format, zval *timezone_object, int ctor) /* {{{ */
  1863. {
  1864. timelib_time *now;
  1865. timelib_tzinfo *tzi = NULL;
  1866. timelib_error_container *err = NULL;
  1867. int type = TIMELIB_ZONETYPE_ID, new_dst = 0;
  1868. char *new_abbr = NULL;
  1869. timelib_sll new_offset = 0;
  1870. time_t sec;
  1871. suseconds_t usec;
  1872. if (dateobj->time) {
  1873. timelib_time_dtor(dateobj->time);
  1874. }
  1875. if (format) {
  1876. dateobj->time = timelib_parse_from_format(format, time_str_len ? time_str : "", time_str_len ? time_str_len : 0, &err, DATE_TIMEZONEDB, php_date_parse_tzfile_wrapper);
  1877. } else {
  1878. dateobj->time = timelib_strtotime(time_str_len ? time_str : "now", time_str_len ? time_str_len : sizeof("now") -1, &err, DATE_TIMEZONEDB, php_date_parse_tzfile_wrapper);
  1879. }
  1880. /* update last errors and warnings */
  1881. update_errors_warnings(err);
  1882. if (ctor && err && err->error_count) {
  1883. /* spit out the first library error message, at least */
  1884. php_error_docref(NULL, E_WARNING, "Failed to parse time string (%s) at position %d (%c): %s", time_str,
  1885. err->error_messages[0].position, err->error_messages[0].character, err->error_messages[0].message);
  1886. }
  1887. if (err && err->error_count) {
  1888. timelib_time_dtor(dateobj->time);
  1889. dateobj->time = 0;
  1890. return 0;
  1891. }
  1892. if (timezone_object) {
  1893. php_timezone_obj *tzobj;
  1894. tzobj = Z_PHPTIMEZONE_P(timezone_object);
  1895. switch (tzobj->type) {
  1896. case TIMELIB_ZONETYPE_ID:
  1897. tzi = tzobj->tzi.tz;
  1898. break;
  1899. case TIMELIB_ZONETYPE_OFFSET:
  1900. new_offset = tzobj->tzi.utc_offset;
  1901. break;
  1902. case TIMELIB_ZONETYPE_ABBR:
  1903. new_offset = tzobj->tzi.z.utc_offset;
  1904. new_dst = tzobj->tzi.z.dst;
  1905. new_abbr = timelib_strdup(tzobj->tzi.z.abbr);
  1906. break;
  1907. }
  1908. type = tzobj->type;
  1909. } else if (dateobj->time->tz_info) {
  1910. tzi = dateobj->time->tz_info;
  1911. } else {
  1912. tzi = get_timezone_info();
  1913. }
  1914. now = timelib_time_ctor();
  1915. now->zone_type = type;
  1916. switch (type) {
  1917. case TIMELIB_ZONETYPE_ID:
  1918. now->tz_info = tzi;
  1919. break;
  1920. case TIMELIB_ZONETYPE_OFFSET:
  1921. now->z = new_offset;
  1922. break;
  1923. case TIMELIB_ZONETYPE_ABBR:
  1924. now->z = new_offset;
  1925. now->dst = new_dst;
  1926. now->tz_abbr = new_abbr;
  1927. break;
  1928. }
  1929. php_date_get_current_time_with_fraction(&sec, &usec);
  1930. timelib_unixtime2local(now, (timelib_sll) sec);
  1931. php_date_set_time_fraction(now, usec);
  1932. timelib_fill_holes(dateobj->time, now, TIMELIB_NO_CLONE);
  1933. timelib_update_ts(dateobj->time, tzi);
  1934. timelib_update_from_sse(dateobj->time);
  1935. dateobj->time->have_relative = 0;
  1936. timelib_time_dtor(now);
  1937. return 1;
  1938. } /* }}} */
  1939. /* {{{ proto DateTime date_create([string time[, DateTimeZone object]])
  1940. Returns new DateTime object
  1941. */
  1942. PHP_FUNCTION(date_create)
  1943. {
  1944. zval *timezone_object = NULL;
  1945. char *time_str = NULL;
  1946. size_t time_str_len = 0;
  1947. ZEND_PARSE_PARAMETERS_START(0, 2)
  1948. Z_PARAM_OPTIONAL
  1949. Z_PARAM_STRING(time_str, time_str_len)
  1950. Z_PARAM_OBJECT_OF_CLASS_EX(timezone_object, date_ce_timezone, 1, 0)
  1951. ZEND_PARSE_PARAMETERS_END();
  1952. php_date_instantiate(date_ce_date, return_value);
  1953. if (!php_date_initialize(Z_PHPDATE_P(return_value), time_str, time_str_len, NULL, timezone_object, 0)) {
  1954. zval_ptr_dtor(return_value);
  1955. RETURN_FALSE;
  1956. }
  1957. }
  1958. /* }}} */
  1959. /* {{{ proto DateTime date_create_immutable([string time[, DateTimeZone object]])
  1960. Returns new DateTime object
  1961. */
  1962. PHP_FUNCTION(date_create_immutable)
  1963. {
  1964. zval *timezone_object = NULL;
  1965. char *time_str = NULL;
  1966. size_t time_str_len = 0;
  1967. ZEND_PARSE_PARAMETERS_START(0, 2)
  1968. Z_PARAM_OPTIONAL
  1969. Z_PARAM_STRING(time_str, time_str_len)
  1970. Z_PARAM_OBJECT_OF_CLASS_EX(timezone_object, date_ce_timezone, 1, 0)
  1971. ZEND_PARSE_PARAMETERS_END();
  1972. php_date_instantiate(date_ce_immutable, return_value);
  1973. if (!php_date_initialize(Z_PHPDATE_P(return_value), time_str, time_str_len, NULL, timezone_object, 0)) {
  1974. zval_ptr_dtor(return_value);
  1975. RETURN_FALSE;
  1976. }
  1977. }
  1978. /* }}} */
  1979. /* {{{ proto DateTime date_create_from_format(string format, string time[, DateTimeZone object])
  1980. Returns new DateTime object formatted according to the specified format
  1981. */
  1982. PHP_FUNCTION(date_create_from_format)
  1983. {
  1984. zval *timezone_object = NULL;
  1985. char *time_str = NULL, *format_str = NULL;
  1986. size_t time_str_len = 0, format_str_len = 0;
  1987. ZEND_PARSE_PARAMETERS_START(2, 3)
  1988. Z_PARAM_STRING(format_str, format_str_len)
  1989. Z_PARAM_STRING(time_str, time_str_len)
  1990. Z_PARAM_OPTIONAL
  1991. Z_PARAM_OBJECT_OF_CLASS_EX(timezone_object, date_ce_timezone, 1, 0)
  1992. ZEND_PARSE_PARAMETERS_END();
  1993. php_date_instantiate(date_ce_date, return_value);
  1994. if (!php_date_initialize(Z_PHPDATE_P(return_value), time_str, time_str_len, format_str, timezone_object, 0)) {
  1995. zval_ptr_dtor(return_value);
  1996. RETURN_FALSE;
  1997. }
  1998. }
  1999. /* }}} */
  2000. /* {{{ proto DateTime date_create_immutable_from_format(string format, string time[, DateTimeZone object])
  2001. Returns new DateTime object formatted according to the specified format
  2002. */
  2003. PHP_FUNCTION(date_create_immutable_from_format)
  2004. {
  2005. zval *timezone_object = NULL;
  2006. char *time_str = NULL, *format_str = NULL;
  2007. size_t time_str_len = 0, format_str_len = 0;
  2008. ZEND_PARSE_PARAMETERS_START(2, 3)
  2009. Z_PARAM_STRING(format_str, format_str_len)
  2010. Z_PARAM_STRING(time_str, time_str_len)
  2011. Z_PARAM_OPTIONAL
  2012. Z_PARAM_OBJECT_OF_CLASS_EX(timezone_object, date_ce_timezone, 1, 0)
  2013. ZEND_PARSE_PARAMETERS_END();
  2014. php_date_instantiate(date_ce_immutable, return_value);
  2015. if (!php_date_initialize(Z_PHPDATE_P(return_value), time_str, time_str_len, format_str, timezone_object, 0)) {
  2016. zval_ptr_dtor(return_value);
  2017. RETURN_FALSE;
  2018. }
  2019. }
  2020. /* }}} */
  2021. /* {{{ proto DateTime::__construct([string time[, DateTimeZone object]])
  2022. Creates new DateTime object
  2023. */
  2024. PHP_METHOD(DateTime, __construct)
  2025. {
  2026. zval *timezone_object = NULL;
  2027. char *time_str = NULL;
  2028. size_t time_str_len = 0;
  2029. zend_error_handling error_handling;
  2030. ZEND_PARSE_PARAMETERS_START(0, 2)
  2031. Z_PARAM_OPTIONAL
  2032. Z_PARAM_STRING(time_str, time_str_len)
  2033. Z_PARAM_OBJECT_OF_CLASS_EX(timezone_object, date_ce_timezone, 1, 0)
  2034. ZEND_PARSE_PARAMETERS_END();
  2035. zend_replace_error_handling(EH_THROW, NULL, &error_handling);
  2036. php_date_initialize(Z_PHPDATE_P(ZEND_THIS), time_str, time_str_len, NULL, timezone_object, 1);
  2037. zend_restore_error_handling(&error_handling);
  2038. }
  2039. /* }}} */
  2040. /* {{{ proto DateTimeImmutable::__construct([string time[, DateTimeZone object]])
  2041. Creates new DateTimeImmutable object
  2042. */
  2043. PHP_METHOD(DateTimeImmutable, __construct)
  2044. {
  2045. zval *timezone_object = NULL;
  2046. char *time_str = NULL;
  2047. size_t time_str_len = 0;
  2048. zend_error_handling error_handling;
  2049. ZEND_PARSE_PARAMETERS_START(0, 2)
  2050. Z_PARAM_OPTIONAL
  2051. Z_PARAM_STRING(time_str, time_str_len)
  2052. Z_PARAM_OBJECT_OF_CLASS_EX(timezone_object, date_ce_timezone, 1, 0)
  2053. ZEND_PARSE_PARAMETERS_END();
  2054. zend_replace_error_handling(EH_THROW, NULL, &error_handling);
  2055. php_date_initialize(Z_PHPDATE_P(ZEND_THIS), time_str, time_str_len, NULL, timezone_object, 1);
  2056. zend_restore_error_handling(&error_handling);
  2057. }
  2058. /* }}} */
  2059. /* {{{ proto DateTime::createFromImmutable(DateTimeImmutable object)
  2060. Creates new DateTime object from an existing immutable DateTimeImmutable object.
  2061. */
  2062. PHP_METHOD(DateTime, createFromImmutable)
  2063. {
  2064. zval *datetimeimmutable_object = NULL;
  2065. php_date_obj *new_obj = NULL;
  2066. php_date_obj *old_obj = NULL;
  2067. ZEND_PARSE_PARAMETERS_START(1, 1)
  2068. Z_PARAM_OBJECT_OF_CLASS(datetimeimmutable_object, date_ce_immutable)
  2069. ZEND_PARSE_PARAMETERS_END();
  2070. php_date_instantiate(date_ce_date, return_value);
  2071. old_obj = Z_PHPDATE_P(datetimeimmutable_object);
  2072. new_obj = Z_PHPDATE_P(return_value);
  2073. new_obj->time = timelib_time_clone(old_obj->time);
  2074. }
  2075. /* }}} */
  2076. /* {{{ proto DateTime::createFromInterface(DateTimeInterface object)
  2077. Creates new DateTime object from an existing DateTimeInterface object.
  2078. */
  2079. PHP_METHOD(DateTime, createFromInterface)
  2080. {
  2081. zval *datetimeinterface_object = NULL;
  2082. php_date_obj *new_obj = NULL;
  2083. php_date_obj *old_obj = NULL;
  2084. ZEND_PARSE_PARAMETERS_START(1, 1)
  2085. Z_PARAM_OBJECT_OF_CLASS(datetimeinterface_object, date_ce_interface)
  2086. ZEND_PARSE_PARAMETERS_END();
  2087. php_date_instantiate(date_ce_date, return_value);
  2088. old_obj = Z_PHPDATE_P(datetimeinterface_object);
  2089. new_obj = Z_PHPDATE_P(return_value);
  2090. new_obj->time = timelib_time_clone(old_obj->time);
  2091. }
  2092. /* }}} */
  2093. /* {{{ proto DateTimeImmutable::createFromMutable(DateTime object)
  2094. Creates new DateTimeImmutable object from an existing mutable DateTime object.
  2095. */
  2096. PHP_METHOD(DateTimeImmutable, createFromMutable)
  2097. {
  2098. zval *datetime_object = NULL;
  2099. php_date_obj *new_obj = NULL;
  2100. php_date_obj *old_obj = NULL;
  2101. ZEND_PARSE_PARAMETERS_START(1, 1)
  2102. Z_PARAM_OBJECT_OF_CLASS(datetime_object, date_ce_date)
  2103. ZEND_PARSE_PARAMETERS_END();
  2104. php_date_instantiate(date_ce_immutable, return_value);
  2105. old_obj = Z_PHPDATE_P(datetime_object);
  2106. new_obj = Z_PHPDATE_P(return_value);
  2107. new_obj->time = timelib_time_clone(old_obj->time);
  2108. }
  2109. /* }}} */
  2110. /* {{{ proto DateTimeImmutable::createFromInterface(DateTimeInterface object)
  2111. Creates new DateTimeImmutable object from an existing DateTimeInterface object.
  2112. */
  2113. PHP_METHOD(DateTimeImmutable, createFromInterface)
  2114. {
  2115. zval *datetimeinterface_object = NULL;
  2116. php_date_obj *new_obj = NULL;
  2117. php_date_obj *old_obj = NULL;
  2118. ZEND_PARSE_PARAMETERS_START(1, 1)
  2119. Z_PARAM_OBJECT_OF_CLASS(datetimeinterface_object, date_ce_interface)
  2120. ZEND_PARSE_PARAMETERS_END();
  2121. php_date_instantiate(date_ce_immutable, return_value);
  2122. old_obj = Z_PHPDATE_P(datetimeinterface_object);
  2123. new_obj = Z_PHPDATE_P(return_value);
  2124. new_obj->time = timelib_time_clone(old_obj->time);
  2125. }
  2126. /* }}} */
  2127. static int php_date_initialize_from_hash(php_date_obj **dateobj, HashTable *myht)
  2128. {
  2129. zval *z_date;
  2130. zval tmp_obj;
  2131. timelib_tzinfo *tzi;
  2132. z_date = zend_hash_str_find(myht, "date", sizeof("date")-1);
  2133. if (z_date && Z_TYPE_P(z_date) == IS_STRING) {
  2134. zval *z_timezone_type = zend_hash_str_find(myht, "timezone_type", sizeof("timezone_type")-1);
  2135. if (z_timezone_type && Z_TYPE_P(z_timezone_type) == IS_LONG) {
  2136. zval *z_timezone = zend_hash_str_find(myht, "timezone", sizeof("timezone")-1);
  2137. if (z_timezone && Z_TYPE_P(z_timezone) == IS_STRING) {
  2138. switch (Z_LVAL_P(z_timezone_type)) {
  2139. case TIMELIB_ZONETYPE_OFFSET:
  2140. case TIMELIB_ZONETYPE_ABBR: {
  2141. char *tmp = emalloc(Z_STRLEN_P(z_date) + Z_STRLEN_P(z_timezone) + 2);
  2142. int ret;
  2143. snprintf(tmp, Z_STRLEN_P(z_date) + Z_STRLEN_P(z_timezone) + 2, "%s %s", Z_STRVAL_P(z_date), Z_STRVAL_P(z_timezone));
  2144. ret = php_date_initialize(*dateobj, tmp, Z_STRLEN_P(z_date) + Z_STRLEN_P(z_timezone) + 1, NULL, NULL, 0);
  2145. efree(tmp);
  2146. return 1 == ret;
  2147. }
  2148. case TIMELIB_ZONETYPE_ID: {
  2149. int ret;
  2150. php_timezone_obj *tzobj;
  2151. tzi = php_date_parse_tzfile(Z_STRVAL_P(z_timezone), DATE_TIMEZONEDB);
  2152. if (tzi == NULL) {
  2153. return 0;
  2154. }
  2155. tzobj = Z_PHPTIMEZONE_P(php_date_instantiate(date_ce_timezone, &tmp_obj));
  2156. tzobj->type = TIMELIB_ZONETYPE_ID;
  2157. tzobj->tzi.tz = tzi;
  2158. tzobj->initialized = 1;
  2159. ret = php_date_initialize(*dateobj, Z_STRVAL_P(z_date), Z_STRLEN_P(z_date), NULL, &tmp_obj, 0);
  2160. zval_ptr_dtor(&tmp_obj);
  2161. return 1 == ret;
  2162. }
  2163. }
  2164. }
  2165. }
  2166. }
  2167. return 0;
  2168. } /* }}} */
  2169. /* {{{ proto DateTime::__set_state(array array)
  2170. */
  2171. PHP_METHOD(DateTime, __set_state)
  2172. {
  2173. php_date_obj *dateobj;
  2174. zval *array;
  2175. HashTable *myht;
  2176. ZEND_PARSE_PARAMETERS_START(1, 1)
  2177. Z_PARAM_ARRAY(array)
  2178. ZEND_PARSE_PARAMETERS_END();
  2179. myht = Z_ARRVAL_P(array);
  2180. php_date_instantiate(date_ce_date, return_value);
  2181. dateobj = Z_PHPDATE_P(return_value);
  2182. if (!php_date_initialize_from_hash(&dateobj, myht)) {
  2183. zend_throw_error(NULL, "Invalid serialization data for DateTime object");
  2184. }
  2185. }
  2186. /* }}} */
  2187. /* {{{ proto DateTimeImmutable::__set_state(array array)
  2188. */
  2189. PHP_METHOD(DateTimeImmutable, __set_state)
  2190. {
  2191. php_date_obj *dateobj;
  2192. zval *array;
  2193. HashTable *myht;
  2194. ZEND_PARSE_PARAMETERS_START(1, 1)
  2195. Z_PARAM_ARRAY(array)
  2196. ZEND_PARSE_PARAMETERS_END();
  2197. myht = Z_ARRVAL_P(array);
  2198. php_date_instantiate(date_ce_immutable, return_value);
  2199. dateobj = Z_PHPDATE_P(return_value);
  2200. if (!php_date_initialize_from_hash(&dateobj, myht)) {
  2201. zend_throw_error(NULL, "Invalid serialization data for DateTimeImmutable object");
  2202. }
  2203. }
  2204. /* }}} */
  2205. /* {{{ proto DateTime::__wakeup()
  2206. */
  2207. PHP_METHOD(DateTime, __wakeup)
  2208. {
  2209. zval *object = ZEND_THIS;
  2210. php_date_obj *dateobj;
  2211. HashTable *myht;
  2212. ZEND_PARSE_PARAMETERS_NONE();
  2213. dateobj = Z_PHPDATE_P(object);
  2214. myht = Z_OBJPROP_P(object);
  2215. if (!php_date_initialize_from_hash(&dateobj, myht)) {
  2216. zend_throw_error(NULL, "Invalid serialization data for DateTime object");
  2217. }
  2218. }
  2219. /* }}} */
  2220. /* {{{ proto DateTimeImmutable::__wakeup()
  2221. */
  2222. PHP_METHOD(DateTimeImmutable, __wakeup)
  2223. {
  2224. zval *object = ZEND_THIS;
  2225. php_date_obj *dateobj;
  2226. HashTable *myht;
  2227. ZEND_PARSE_PARAMETERS_NONE();
  2228. dateobj = Z_PHPDATE_P(object);
  2229. myht = Z_OBJPROP_P(object);
  2230. if (!php_date_initialize_from_hash(&dateobj, myht)) {
  2231. zend_throw_error(NULL, "Invalid serialization data for DateTimeImmutable object");
  2232. }
  2233. }
  2234. /* }}} */
  2235. /* Helper function used to add an associative array of warnings and errors to a zval */
  2236. static void zval_from_error_container(zval *z, timelib_error_container *error) /* {{{ */
  2237. {
  2238. int i;
  2239. zval element;
  2240. add_assoc_long(z, "warning_count", error->warning_count);
  2241. array_init(&element);
  2242. for (i = 0; i < error->warning_count; i++) {
  2243. add_index_string(&element, error->warning_messages[i].position, error->warning_messages[i].message);
  2244. }
  2245. add_assoc_zval(z, "warnings", &element);
  2246. add_assoc_long(z, "error_count", error->error_count);
  2247. array_init(&element);
  2248. for (i = 0; i < error->error_count; i++) {
  2249. add_index_string(&element, error->error_messages[i].position, error->error_messages[i].message);
  2250. }
  2251. add_assoc_zval(z, "errors", &element);
  2252. } /* }}} */
  2253. /* {{{ proto array date_get_last_errors()
  2254. Returns the warnings and errors found while parsing a date/time string.
  2255. */
  2256. PHP_FUNCTION(date_get_last_errors)
  2257. {
  2258. ZEND_PARSE_PARAMETERS_NONE();
  2259. if (DATEG(last_errors)) {
  2260. array_init(return_value);
  2261. zval_from_error_container(return_value, DATEG(last_errors));
  2262. } else {
  2263. RETURN_FALSE;
  2264. }
  2265. }
  2266. /* }}} */
  2267. void php_date_do_return_parsed_time(INTERNAL_FUNCTION_PARAMETERS, timelib_time *parsed_time, timelib_error_container *error) /* {{{ */
  2268. {
  2269. zval element;
  2270. array_init(return_value);
  2271. #define PHP_DATE_PARSE_DATE_SET_TIME_ELEMENT(name, elem) \
  2272. if (parsed_time->elem == -99999) { \
  2273. add_assoc_bool(return_value, #name, 0); \
  2274. } else { \
  2275. add_assoc_long(return_value, #name, parsed_time->elem); \
  2276. }
  2277. PHP_DATE_PARSE_DATE_SET_TIME_ELEMENT(year, y);
  2278. PHP_DATE_PARSE_DATE_SET_TIME_ELEMENT(month, m);
  2279. PHP_DATE_PARSE_DATE_SET_TIME_ELEMENT(day, d);
  2280. PHP_DATE_PARSE_DATE_SET_TIME_ELEMENT(hour, h);
  2281. PHP_DATE_PARSE_DATE_SET_TIME_ELEMENT(minute, i);
  2282. PHP_DATE_PARSE_DATE_SET_TIME_ELEMENT(second, s);
  2283. if (parsed_time->us == -99999) {
  2284. add_assoc_bool(return_value, "fraction", 0);
  2285. } else {
  2286. add_assoc_double(return_value, "fraction", (double)parsed_time->us / 1000000.0);
  2287. }
  2288. zval_from_error_container(return_value, error);
  2289. timelib_error_container_dtor(error);
  2290. add_assoc_bool(return_value, "is_localtime", parsed_time->is_localtime);
  2291. if (parsed_time->is_localtime) {
  2292. PHP_DATE_PARSE_DATE_SET_TIME_ELEMENT(zone_type, zone_type);
  2293. switch (parsed_time->zone_type) {
  2294. case TIMELIB_ZONETYPE_OFFSET:
  2295. PHP_DATE_PARSE_DATE_SET_TIME_ELEMENT(zone, z);
  2296. add_assoc_bool(return_value, "is_dst", parsed_time->dst);
  2297. break;
  2298. case TIMELIB_ZONETYPE_ID:
  2299. if (parsed_time->tz_abbr) {
  2300. add_assoc_string(return_value, "tz_abbr", parsed_time->tz_abbr);
  2301. }
  2302. if (parsed_time->tz_info) {
  2303. add_assoc_string(return_value, "tz_id", parsed_time->tz_info->name);
  2304. }
  2305. break;
  2306. case TIMELIB_ZONETYPE_ABBR:
  2307. PHP_DATE_PARSE_DATE_SET_TIME_ELEMENT(zone, z);
  2308. add_assoc_bool(return_value, "is_dst", parsed_time->dst);
  2309. add_assoc_string(return_value, "tz_abbr", parsed_time->tz_abbr);
  2310. break;
  2311. }
  2312. }
  2313. if (parsed_time->have_relative) {
  2314. array_init(&element);
  2315. add_assoc_long(&element, "year", parsed_time->relative.y);
  2316. add_assoc_long(&element, "month", parsed_time->relative.m);
  2317. add_assoc_long(&element, "day", parsed_time->relative.d);
  2318. add_assoc_long(&element, "hour", parsed_time->relative.h);
  2319. add_assoc_long(&element, "minute", parsed_time->relative.i);
  2320. add_assoc_long(&element, "second", parsed_time->relative.s);
  2321. if (parsed_time->relative.have_weekday_relative) {
  2322. add_assoc_long(&element, "weekday", parsed_time->relative.weekday);
  2323. }
  2324. if (parsed_time->relative.have_special_relative && (parsed_time->relative.special.type == TIMELIB_SPECIAL_WEEKDAY)) {
  2325. add_assoc_long(&element, "weekdays", parsed_time->relative.special.amount);
  2326. }
  2327. if (parsed_time->relative.first_last_day_of) {
  2328. add_assoc_bool(&element, parsed_time->relative.first_last_day_of == TIMELIB_SPECIAL_FIRST_DAY_OF_MONTH ? "first_day_of_month" : "last_day_of_month", 1);
  2329. }
  2330. add_assoc_zval(return_value, "relative", &element);
  2331. }
  2332. timelib_time_dtor(parsed_time);
  2333. } /* }}} */
  2334. /* {{{ proto array date_parse(string date)
  2335. Returns associative array with detailed info about given date
  2336. */
  2337. PHP_FUNCTION(date_parse)
  2338. {
  2339. zend_string *date;
  2340. timelib_error_container *error;
  2341. timelib_time *parsed_time;
  2342. ZEND_PARSE_PARAMETERS_START(1, 1)
  2343. Z_PARAM_STR(date)
  2344. ZEND_PARSE_PARAMETERS_END();
  2345. parsed_time = timelib_strtotime(ZSTR_VAL(date), ZSTR_LEN(date), &error, DATE_TIMEZONEDB, php_date_parse_tzfile_wrapper);
  2346. php_date_do_return_parsed_time(INTERNAL_FUNCTION_PARAM_PASSTHRU, parsed_time, error);
  2347. }
  2348. /* }}} */
  2349. /* {{{ proto array date_parse_from_format(string format, string date)
  2350. Returns associative array with detailed info about given date
  2351. */
  2352. PHP_FUNCTION(date_parse_from_format)
  2353. {
  2354. zend_string *date, *format;
  2355. timelib_error_container *error;
  2356. timelib_time *parsed_time;
  2357. ZEND_PARSE_PARAMETERS_START(2, 2)
  2358. Z_PARAM_STR(format)
  2359. Z_PARAM_STR(date)
  2360. ZEND_PARSE_PARAMETERS_END();
  2361. parsed_time = timelib_parse_from_format(ZSTR_VAL(format), ZSTR_VAL(date), ZSTR_LEN(date), &error, DATE_TIMEZONEDB, php_date_parse_tzfile_wrapper);
  2362. php_date_do_return_parsed_time(INTERNAL_FUNCTION_PARAM_PASSTHRU, parsed_time, error);
  2363. }
  2364. /* }}} */
  2365. /* {{{ proto string date_format(DateTimeInterface object, string format)
  2366. Returns date formatted according to given format
  2367. */
  2368. PHP_FUNCTION(date_format)
  2369. {
  2370. zval *object;
  2371. php_date_obj *dateobj;
  2372. char *format;
  2373. size_t format_len;
  2374. if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "Os", &object, date_ce_interface, &format, &format_len) == FAILURE) {
  2375. RETURN_THROWS();
  2376. }
  2377. dateobj = Z_PHPDATE_P(object);
  2378. DATE_CHECK_INITIALIZED(dateobj->time, DateTime);
  2379. RETURN_STR(date_format(format, format_len, dateobj->time, dateobj->time->is_localtime));
  2380. }
  2381. /* }}} */
  2382. static int php_date_modify(zval *object, char *modify, size_t modify_len) /* {{{ */
  2383. {
  2384. php_date_obj *dateobj;
  2385. timelib_time *tmp_time;
  2386. timelib_error_container *err = NULL;
  2387. dateobj = Z_PHPDATE_P(object);
  2388. if (!(dateobj->time)) {
  2389. php_error_docref(NULL, E_WARNING, "The DateTime object has not been correctly initialized by its constructor");
  2390. return 0;
  2391. }
  2392. tmp_time = timelib_strtotime(modify, modify_len, &err, DATE_TIMEZONEDB, php_date_parse_tzfile_wrapper);
  2393. /* update last errors and warnings */
  2394. update_errors_warnings(err);
  2395. if (err && err->error_count) {
  2396. /* spit out the first library error message, at least */
  2397. php_error_docref(NULL, E_WARNING, "Failed to parse time string (%s) at position %d (%c): %s", modify,
  2398. err->error_messages[0].position, err->error_messages[0].character, err->error_messages[0].message);
  2399. timelib_time_dtor(tmp_time);
  2400. return 0;
  2401. }
  2402. memcpy(&dateobj->time->relative, &tmp_time->relative, sizeof(timelib_rel_time));
  2403. dateobj->time->have_relative = tmp_time->have_relative;
  2404. dateobj->time->sse_uptodate = 0;
  2405. if (tmp_time->y != -99999) {
  2406. dateobj->time->y = tmp_time->y;
  2407. }
  2408. if (tmp_time->m != -99999) {
  2409. dateobj->time->m = tmp_time->m;
  2410. }
  2411. if (tmp_time->d != -99999) {
  2412. dateobj->time->d = tmp_time->d;
  2413. }
  2414. if (tmp_time->h != -99999) {
  2415. dateobj->time->h = tmp_time->h;
  2416. if (tmp_time->i != -99999) {
  2417. dateobj->time->i = tmp_time->i;
  2418. if (tmp_time->s != -99999) {
  2419. dateobj->time->s = tmp_time->s;
  2420. } else {
  2421. dateobj->time->s = 0;
  2422. }
  2423. } else {
  2424. dateobj->time->i = 0;
  2425. dateobj->time->s = 0;
  2426. }
  2427. }
  2428. if (tmp_time->us != -99999) {
  2429. dateobj->time->us = tmp_time->us;
  2430. }
  2431. timelib_time_dtor(tmp_time);
  2432. timelib_update_ts(dateobj->time, NULL);
  2433. timelib_update_from_sse(dateobj->time);
  2434. dateobj->time->have_relative = 0;
  2435. memset(&dateobj->time->relative, 0, sizeof(dateobj->time->relative));
  2436. return 1;
  2437. } /* }}} */
  2438. /* {{{ proto DateTime date_modify(DateTime object, string modify)
  2439. Alters the timestamp.
  2440. */
  2441. PHP_FUNCTION(date_modify)
  2442. {
  2443. zval *object;
  2444. char *modify;
  2445. size_t modify_len;
  2446. if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "Os", &object, date_ce_date, &modify, &modify_len) == FAILURE) {
  2447. RETURN_THROWS();
  2448. }
  2449. if (!php_date_modify(object, modify, modify_len)) {
  2450. RETURN_FALSE;
  2451. }
  2452. Z_ADDREF_P(object);
  2453. ZVAL_OBJ(return_value, Z_OBJ_P(object));
  2454. }
  2455. /* }}} */
  2456. /* {{{ proto DateTimeImmutable::modify()
  2457. */
  2458. PHP_METHOD(DateTimeImmutable, modify)
  2459. {
  2460. zval *object, new_object;
  2461. char *modify;
  2462. size_t modify_len;
  2463. object = ZEND_THIS;
  2464. if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &modify, &modify_len) == FAILURE) {
  2465. RETURN_THROWS();
  2466. }
  2467. date_clone_immutable(object, &new_object);
  2468. if (!php_date_modify(&new_object, modify, modify_len)) {
  2469. zval_ptr_dtor(&new_object);
  2470. RETURN_FALSE;
  2471. }
  2472. ZVAL_OBJ(return_value, Z_OBJ(new_object));
  2473. }
  2474. /* }}} */
  2475. static void php_date_add(zval *object, zval *interval, zval *return_value) /* {{{ */
  2476. {
  2477. php_date_obj *dateobj;
  2478. php_interval_obj *intobj;
  2479. timelib_time *new_time;
  2480. dateobj = Z_PHPDATE_P(object);
  2481. DATE_CHECK_INITIALIZED(dateobj->time, DateTime);
  2482. intobj = Z_PHPINTERVAL_P(interval);
  2483. DATE_CHECK_INITIALIZED(intobj->initialized, DateInterval);
  2484. new_time = timelib_add(dateobj->time, intobj->diff);
  2485. timelib_time_dtor(dateobj->time);
  2486. dateobj->time = new_time;
  2487. } /* }}} */
  2488. /* {{{ proto DateTime date_add(DateTime object, DateInterval interval)
  2489. Adds an interval to the current date in object.
  2490. */
  2491. PHP_FUNCTION(date_add)
  2492. {
  2493. zval *object, *interval;
  2494. if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "OO", &object, date_ce_date, &interval, date_ce_interval) == FAILURE) {
  2495. RETURN_THROWS();
  2496. }
  2497. php_date_add(object, interval, return_value);
  2498. Z_ADDREF_P(object);
  2499. ZVAL_OBJ(return_value, Z_OBJ_P(object));
  2500. }
  2501. /* }}} */
  2502. /* {{{ proto DateTimeImmutable::add()
  2503. */
  2504. PHP_METHOD(DateTimeImmutable, add)
  2505. {
  2506. zval *object, *interval, new_object;
  2507. object = ZEND_THIS;
  2508. if (zend_parse_parameters(ZEND_NUM_ARGS(), "O", &interval, date_ce_interval) == FAILURE) {
  2509. RETURN_THROWS();
  2510. }
  2511. date_clone_immutable(object, &new_object);
  2512. php_date_add(&new_object, interval, return_value);
  2513. ZVAL_OBJ(return_value, Z_OBJ(new_object));
  2514. }
  2515. /* }}} */
  2516. static void php_date_sub(zval *object, zval *interval, zval *return_value) /* {{{ */
  2517. {
  2518. php_date_obj *dateobj;
  2519. php_interval_obj *intobj;
  2520. timelib_time *new_time;
  2521. dateobj = Z_PHPDATE_P(object);
  2522. DATE_CHECK_INITIALIZED(dateobj->time, DateTime);
  2523. intobj = Z_PHPINTERVAL_P(interval);
  2524. DATE_CHECK_INITIALIZED(intobj->initialized, DateInterval);
  2525. if (intobj->diff->have_special_relative) {
  2526. php_error_docref(NULL, E_WARNING, "Only non-special relative time specifications are supported for subtraction");
  2527. return;
  2528. }
  2529. new_time = timelib_sub(dateobj->time, intobj->diff);
  2530. timelib_time_dtor(dateobj->time);
  2531. dateobj->time = new_time;
  2532. } /* }}} */
  2533. /* {{{ proto DateTime date_sub(DateTime object, DateInterval interval)
  2534. Subtracts an interval to the current date in object.
  2535. */
  2536. PHP_FUNCTION(date_sub)
  2537. {
  2538. zval *object, *interval;
  2539. if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "OO", &object, date_ce_date, &interval, date_ce_interval) == FAILURE) {
  2540. RETURN_THROWS();
  2541. }
  2542. php_date_sub(object, interval, return_value);
  2543. Z_ADDREF_P(object);
  2544. ZVAL_OBJ(return_value, Z_OBJ_P(object));
  2545. }
  2546. /* }}} */
  2547. /* {{{ proto DateTimeImmutable::sub()
  2548. */
  2549. PHP_METHOD(DateTimeImmutable, sub)
  2550. {
  2551. zval *object, *interval, new_object;
  2552. object = ZEND_THIS;
  2553. if (zend_parse_parameters(ZEND_NUM_ARGS(), "O", &interval, date_ce_interval) == FAILURE) {
  2554. RETURN_THROWS();
  2555. }
  2556. date_clone_immutable(object, &new_object);
  2557. php_date_sub(&new_object, interval, return_value);
  2558. ZVAL_OBJ(return_value, Z_OBJ(new_object));
  2559. }
  2560. /* }}} */
  2561. static void set_timezone_from_timelib_time(php_timezone_obj *tzobj, timelib_time *t)
  2562. {
  2563. tzobj->initialized = 1;
  2564. tzobj->type = t->zone_type;
  2565. switch (t->zone_type) {
  2566. case TIMELIB_ZONETYPE_ID:
  2567. tzobj->tzi.tz = t->tz_info;
  2568. break;
  2569. case TIMELIB_ZONETYPE_OFFSET:
  2570. tzobj->tzi.utc_offset = t->z;
  2571. break;
  2572. case TIMELIB_ZONETYPE_ABBR:
  2573. tzobj->tzi.z.utc_offset = t->z;
  2574. tzobj->tzi.z.dst = t->dst;
  2575. tzobj->tzi.z.abbr = timelib_strdup(t->tz_abbr);
  2576. break;
  2577. }
  2578. }
  2579. /* {{{ proto DateTimeZone date_timezone_get(DateTimeInterface object)
  2580. Return new DateTimeZone object relative to give DateTime
  2581. */
  2582. PHP_FUNCTION(date_timezone_get)
  2583. {
  2584. zval *object;
  2585. php_date_obj *dateobj;
  2586. if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O", &object, date_ce_interface) == FAILURE) {
  2587. RETURN_THROWS();
  2588. }
  2589. dateobj = Z_PHPDATE_P(object);
  2590. DATE_CHECK_INITIALIZED(dateobj->time, DateTime);
  2591. if (dateobj->time->is_localtime) {
  2592. php_timezone_obj *tzobj;
  2593. php_date_instantiate(date_ce_timezone, return_value);
  2594. tzobj = Z_PHPTIMEZONE_P(return_value);
  2595. set_timezone_from_timelib_time(tzobj, dateobj->time);
  2596. } else {
  2597. RETURN_FALSE;
  2598. }
  2599. }
  2600. /* }}} */
  2601. static void php_date_timezone_set(zval *object, zval *timezone_object, zval *return_value) /* {{{ */
  2602. {
  2603. php_date_obj *dateobj;
  2604. php_timezone_obj *tzobj;
  2605. dateobj = Z_PHPDATE_P(object);
  2606. DATE_CHECK_INITIALIZED(dateobj->time, DateTime);
  2607. tzobj = Z_PHPTIMEZONE_P(timezone_object);
  2608. switch (tzobj->type) {
  2609. case TIMELIB_ZONETYPE_OFFSET:
  2610. timelib_set_timezone_from_offset(dateobj->time, tzobj->tzi.utc_offset);
  2611. break;
  2612. case TIMELIB_ZONETYPE_ABBR:
  2613. timelib_set_timezone_from_abbr(dateobj->time, tzobj->tzi.z);
  2614. break;
  2615. case TIMELIB_ZONETYPE_ID:
  2616. timelib_set_timezone(dateobj->time, tzobj->tzi.tz);
  2617. break;
  2618. }
  2619. timelib_unixtime2local(dateobj->time, dateobj->time->sse);
  2620. } /* }}} */
  2621. /* {{{ proto DateTime date_timezone_set(DateTime object, DateTimeZone object)
  2622. Sets the timezone for the DateTime object.
  2623. */
  2624. PHP_FUNCTION(date_timezone_set)
  2625. {
  2626. zval *object;
  2627. zval *timezone_object;
  2628. if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "OO", &object, date_ce_date, &timezone_object, date_ce_timezone) == FAILURE) {
  2629. RETURN_THROWS();
  2630. }
  2631. php_date_timezone_set(object, timezone_object, return_value);
  2632. Z_ADDREF_P(object);
  2633. ZVAL_OBJ(return_value, Z_OBJ_P(object));
  2634. }
  2635. /* }}} */
  2636. /* {{{ proto DateTimeImmutable::setTimezone()
  2637. */
  2638. PHP_METHOD(DateTimeImmutable, setTimezone)
  2639. {
  2640. zval *object, new_object;
  2641. zval *timezone_object;
  2642. object = ZEND_THIS;
  2643. if (zend_parse_parameters(ZEND_NUM_ARGS(), "O", &timezone_object, date_ce_timezone) == FAILURE) {
  2644. RETURN_THROWS();
  2645. }
  2646. date_clone_immutable(object, &new_object);
  2647. php_date_timezone_set(&new_object, timezone_object, return_value);
  2648. ZVAL_OBJ(return_value, Z_OBJ(new_object));
  2649. }
  2650. /* }}} */
  2651. /* {{{ proto int date_offset_get(DateTimeInterface object)
  2652. Returns the DST offset.
  2653. */
  2654. PHP_FUNCTION(date_offset_get)
  2655. {
  2656. zval *object;
  2657. php_date_obj *dateobj;
  2658. timelib_time_offset *offset;
  2659. if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O", &object, date_ce_interface) == FAILURE) {
  2660. RETURN_THROWS();
  2661. }
  2662. dateobj = Z_PHPDATE_P(object);
  2663. DATE_CHECK_INITIALIZED(dateobj->time, DateTime);
  2664. if (dateobj->time->is_localtime) {
  2665. switch (dateobj->time->zone_type) {
  2666. case TIMELIB_ZONETYPE_ID:
  2667. offset = timelib_get_time_zone_info(dateobj->time->sse, dateobj->time->tz_info);
  2668. RETVAL_LONG(offset->offset);
  2669. timelib_time_offset_dtor(offset);
  2670. break;
  2671. case TIMELIB_ZONETYPE_OFFSET:
  2672. RETVAL_LONG(dateobj->time->z);
  2673. break;
  2674. case TIMELIB_ZONETYPE_ABBR:
  2675. RETVAL_LONG((dateobj->time->z + (3600 * dateobj->time->dst)));
  2676. break;
  2677. }
  2678. return;
  2679. } else {
  2680. RETURN_LONG(0);
  2681. }
  2682. }
  2683. /* }}} */
  2684. static void php_date_time_set(zval *object, zend_long h, zend_long i, zend_long s, zend_long ms, zval *return_value) /* {{{ */
  2685. {
  2686. php_date_obj *dateobj;
  2687. dateobj = Z_PHPDATE_P(object);
  2688. DATE_CHECK_INITIALIZED(dateobj->time, DateTime);
  2689. dateobj->time->h = h;
  2690. dateobj->time->i = i;
  2691. dateobj->time->s = s;
  2692. dateobj->time->us = ms;
  2693. timelib_update_ts(dateobj->time, NULL);
  2694. timelib_update_from_sse(dateobj->time);
  2695. } /* }}} */
  2696. /* {{{ proto DateTime date_time_set(DateTime object, int hour, int minute[, int second[, int microseconds]])
  2697. Sets the time.
  2698. */
  2699. PHP_FUNCTION(date_time_set)
  2700. {
  2701. zval *object;
  2702. zend_long h, i, s = 0, ms = 0;
  2703. if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "Oll|ll", &object, date_ce_date, &h, &i, &s, &ms) == FAILURE) {
  2704. RETURN_THROWS();
  2705. }
  2706. php_date_time_set(object, h, i, s, ms, return_value);
  2707. Z_ADDREF_P(object);
  2708. ZVAL_OBJ(return_value, Z_OBJ_P(object));
  2709. }
  2710. /* }}} */
  2711. /* {{{ proto DateTimeImmutable::setTime()
  2712. */
  2713. PHP_METHOD(DateTimeImmutable, setTime)
  2714. {
  2715. zval *object, new_object;
  2716. zend_long h, i, s = 0, ms = 0;
  2717. object = ZEND_THIS;
  2718. if (zend_parse_parameters(ZEND_NUM_ARGS(), "ll|ll", &h, &i, &s, &ms) == FAILURE) {
  2719. RETURN_THROWS();
  2720. }
  2721. date_clone_immutable(object, &new_object);
  2722. php_date_time_set(&new_object, h, i, s, ms, return_value);
  2723. ZVAL_OBJ(return_value, Z_OBJ(new_object));
  2724. }
  2725. /* }}} */
  2726. static void php_date_date_set(zval *object, zend_long y, zend_long m, zend_long d, zval *return_value) /* {{{ */
  2727. {
  2728. php_date_obj *dateobj;
  2729. dateobj = Z_PHPDATE_P(object);
  2730. DATE_CHECK_INITIALIZED(dateobj->time, DateTime);
  2731. dateobj->time->y = y;
  2732. dateobj->time->m = m;
  2733. dateobj->time->d = d;
  2734. timelib_update_ts(dateobj->time, NULL);
  2735. } /* }}} */
  2736. /* {{{ proto DateTime date_date_set(DateTime object, int year, int month, int day)
  2737. Sets the date.
  2738. */
  2739. PHP_FUNCTION(date_date_set)
  2740. {
  2741. zval *object;
  2742. zend_long y, m, d;
  2743. if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "Olll", &object, date_ce_date, &y, &m, &d) == FAILURE) {
  2744. RETURN_THROWS();
  2745. }
  2746. php_date_date_set(object, y, m, d, return_value);
  2747. Z_ADDREF_P(object);
  2748. ZVAL_OBJ(return_value, Z_OBJ_P(object));
  2749. }
  2750. /* }}} */
  2751. /* {{{ proto DateTimeImmutable::setDate()
  2752. */
  2753. PHP_METHOD(DateTimeImmutable, setDate)
  2754. {
  2755. zval *object, new_object;
  2756. zend_long y, m, d;
  2757. object = ZEND_THIS;
  2758. if (zend_parse_parameters(ZEND_NUM_ARGS(), "lll", &y, &m, &d) == FAILURE) {
  2759. RETURN_THROWS();
  2760. }
  2761. date_clone_immutable(object, &new_object);
  2762. php_date_date_set(&new_object, y, m, d, return_value);
  2763. ZVAL_OBJ(return_value, Z_OBJ(new_object));
  2764. }
  2765. /* }}} */
  2766. static void php_date_isodate_set(zval *object, zend_long y, zend_long w, zend_long d, zval *return_value) /* {{{ */
  2767. {
  2768. php_date_obj *dateobj;
  2769. dateobj = Z_PHPDATE_P(object);
  2770. DATE_CHECK_INITIALIZED(dateobj->time, DateTime);
  2771. dateobj->time->y = y;
  2772. dateobj->time->m = 1;
  2773. dateobj->time->d = 1;
  2774. memset(&dateobj->time->relative, 0, sizeof(dateobj->time->relative));
  2775. dateobj->time->relative.d = timelib_daynr_from_weeknr(y, w, d);
  2776. dateobj->time->have_relative = 1;
  2777. timelib_update_ts(dateobj->time, NULL);
  2778. } /* }}} */
  2779. /* {{{ proto DateTime date_isodate_set(DateTime object, int year, int week[, int day])
  2780. Sets the ISO date.
  2781. */
  2782. PHP_FUNCTION(date_isodate_set)
  2783. {
  2784. zval *object;
  2785. zend_long y, w, d = 1;
  2786. if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "Oll|l", &object, date_ce_date, &y, &w, &d) == FAILURE) {
  2787. RETURN_THROWS();
  2788. }
  2789. php_date_isodate_set(object, y, w, d, return_value);
  2790. Z_ADDREF_P(object);
  2791. ZVAL_OBJ(return_value, Z_OBJ_P(object));
  2792. }
  2793. /* }}} */
  2794. /* {{{ proto DateTimeImmutable::setISODate()
  2795. */
  2796. PHP_METHOD(DateTimeImmutable, setISODate)
  2797. {
  2798. zval *object, new_object;
  2799. zend_long y, w, d = 1;
  2800. object = ZEND_THIS;
  2801. if (zend_parse_parameters(ZEND_NUM_ARGS(), "ll|l", &y, &w, &d) == FAILURE) {
  2802. RETURN_THROWS();
  2803. }
  2804. date_clone_immutable(object, &new_object);
  2805. php_date_isodate_set(&new_object, y, w, d, return_value);
  2806. ZVAL_OBJ(return_value, Z_OBJ(new_object));
  2807. }
  2808. /* }}} */
  2809. static void php_date_timestamp_set(zval *object, zend_long timestamp, zval *return_value) /* {{{ */
  2810. {
  2811. php_date_obj *dateobj;
  2812. dateobj = Z_PHPDATE_P(object);
  2813. DATE_CHECK_INITIALIZED(dateobj->time, DateTime);
  2814. timelib_unixtime2local(dateobj->time, (timelib_sll)timestamp);
  2815. timelib_update_ts(dateobj->time, NULL);
  2816. php_date_set_time_fraction(dateobj->time, 0);
  2817. } /* }}} */
  2818. /* {{{ proto DateTime date_timestamp_set(DateTime object, int unixTimestamp)
  2819. Sets the date and time based on an Unix timestamp.
  2820. */
  2821. PHP_FUNCTION(date_timestamp_set)
  2822. {
  2823. zval *object;
  2824. zend_long timestamp;
  2825. if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "Ol", &object, date_ce_date, &timestamp) == FAILURE) {
  2826. RETURN_THROWS();
  2827. }
  2828. php_date_timestamp_set(object, timestamp, return_value);
  2829. Z_ADDREF_P(object);
  2830. ZVAL_OBJ(return_value, Z_OBJ_P(object));
  2831. }
  2832. /* }}} */
  2833. /* {{{ proto DateTimeImmutable::setTimestamp()
  2834. */
  2835. PHP_METHOD(DateTimeImmutable, setTimestamp)
  2836. {
  2837. zval *object, new_object;
  2838. zend_long timestamp;
  2839. object = ZEND_THIS;
  2840. if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &timestamp) == FAILURE) {
  2841. RETURN_THROWS();
  2842. }
  2843. date_clone_immutable(object, &new_object);
  2844. php_date_timestamp_set(&new_object, timestamp, return_value);
  2845. ZVAL_OBJ(return_value, Z_OBJ(new_object));
  2846. }
  2847. /* }}} */
  2848. /* {{{ proto int date_timestamp_get(DateTimeInterface object)
  2849. Gets the Unix timestamp.
  2850. */
  2851. PHP_FUNCTION(date_timestamp_get)
  2852. {
  2853. zval *object;
  2854. php_date_obj *dateobj;
  2855. zend_long timestamp;
  2856. int error;
  2857. if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O", &object, date_ce_interface) == FAILURE) {
  2858. RETURN_THROWS();
  2859. }
  2860. dateobj = Z_PHPDATE_P(object);
  2861. DATE_CHECK_INITIALIZED(dateobj->time, DateTime);
  2862. timelib_update_ts(dateobj->time, NULL);
  2863. timestamp = timelib_date_to_int(dateobj->time, &error);
  2864. if (error) {
  2865. RETURN_FALSE;
  2866. } else {
  2867. RETVAL_LONG(timestamp);
  2868. }
  2869. }
  2870. /* }}} */
  2871. /* {{{ proto DateInterval date_diff(DateTime object [, bool absolute])
  2872. Returns the difference between two DateTime objects.
  2873. */
  2874. PHP_FUNCTION(date_diff)
  2875. {
  2876. zval *object1, *object2;
  2877. php_date_obj *dateobj1, *dateobj2;
  2878. php_interval_obj *interval;
  2879. zend_bool absolute = 0;
  2880. if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "OO|b", &object1, date_ce_interface, &object2, date_ce_interface, &absolute) == FAILURE) {
  2881. RETURN_THROWS();
  2882. }
  2883. dateobj1 = Z_PHPDATE_P(object1);
  2884. dateobj2 = Z_PHPDATE_P(object2);
  2885. DATE_CHECK_INITIALIZED(dateobj1->time, DateTimeInterface);
  2886. DATE_CHECK_INITIALIZED(dateobj2->time, DateTimeInterface);
  2887. timelib_update_ts(dateobj1->time, NULL);
  2888. timelib_update_ts(dateobj2->time, NULL);
  2889. php_date_instantiate(date_ce_interval, return_value);
  2890. interval = Z_PHPINTERVAL_P(return_value);
  2891. interval->diff = timelib_diff(dateobj1->time, dateobj2->time);
  2892. if (absolute) {
  2893. interval->diff->invert = 0;
  2894. }
  2895. interval->initialized = 1;
  2896. }
  2897. /* }}} */
  2898. static int timezone_initialize(php_timezone_obj *tzobj, /*const*/ char *tz, size_t tz_len) /* {{{ */
  2899. {
  2900. timelib_time *dummy_t = ecalloc(1, sizeof(timelib_time));
  2901. int dst, not_found;
  2902. char *orig_tz = tz;
  2903. if (strlen(tz) != tz_len) {
  2904. php_error_docref(NULL, E_WARNING, "Timezone must not contain null bytes");
  2905. efree(dummy_t);
  2906. return FAILURE;
  2907. }
  2908. dummy_t->z = timelib_parse_zone(&tz, &dst, dummy_t, &not_found, DATE_TIMEZONEDB, php_date_parse_tzfile_wrapper);
  2909. if (not_found) {
  2910. php_error_docref(NULL, E_WARNING, "Unknown or bad timezone (%s)", orig_tz);
  2911. efree(dummy_t);
  2912. return FAILURE;
  2913. } else {
  2914. set_timezone_from_timelib_time(tzobj, dummy_t);
  2915. timelib_free(dummy_t->tz_abbr);
  2916. efree(dummy_t);
  2917. return SUCCESS;
  2918. }
  2919. } /* }}} */
  2920. /* {{{ proto DateTimeZone timezone_open(string timezone)
  2921. Returns new DateTimeZone object
  2922. */
  2923. PHP_FUNCTION(timezone_open)
  2924. {
  2925. zend_string *tz;
  2926. php_timezone_obj *tzobj;
  2927. ZEND_PARSE_PARAMETERS_START(1, 1)
  2928. Z_PARAM_STR(tz)
  2929. ZEND_PARSE_PARAMETERS_END();
  2930. tzobj = Z_PHPTIMEZONE_P(php_date_instantiate(date_ce_timezone, return_value));
  2931. if (SUCCESS != timezone_initialize(tzobj, ZSTR_VAL(tz), ZSTR_LEN(tz))) {
  2932. zval_ptr_dtor(return_value);
  2933. RETURN_FALSE;
  2934. }
  2935. }
  2936. /* }}} */
  2937. /* {{{ proto DateTimeZone::__construct(string timezone)
  2938. Creates new DateTimeZone object.
  2939. */
  2940. PHP_METHOD(DateTimeZone, __construct)
  2941. {
  2942. zend_string *tz;
  2943. php_timezone_obj *tzobj;
  2944. zend_error_handling error_handling;
  2945. ZEND_PARSE_PARAMETERS_START(1, 1)
  2946. Z_PARAM_STR(tz)
  2947. ZEND_PARSE_PARAMETERS_END();
  2948. zend_replace_error_handling(EH_THROW, NULL, &error_handling);
  2949. tzobj = Z_PHPTIMEZONE_P(ZEND_THIS);
  2950. timezone_initialize(tzobj, ZSTR_VAL(tz), ZSTR_LEN(tz));
  2951. zend_restore_error_handling(&error_handling);
  2952. }
  2953. /* }}} */
  2954. static int php_date_timezone_initialize_from_hash(zval **return_value, php_timezone_obj **tzobj, HashTable *myht) /* {{{ */
  2955. {
  2956. zval *z_timezone_type;
  2957. if ((z_timezone_type = zend_hash_str_find(myht, "timezone_type", sizeof("timezone_type") - 1)) != NULL) {
  2958. zval *z_timezone;
  2959. if ((z_timezone = zend_hash_str_find(myht, "timezone", sizeof("timezone") - 1)) != NULL) {
  2960. if (Z_TYPE_P(z_timezone_type) != IS_LONG) {
  2961. return FAILURE;
  2962. }
  2963. if (Z_TYPE_P(z_timezone) != IS_STRING) {
  2964. return FAILURE;
  2965. }
  2966. if (SUCCESS == timezone_initialize(*tzobj, Z_STRVAL_P(z_timezone), Z_STRLEN_P(z_timezone))) {
  2967. return SUCCESS;
  2968. }
  2969. }
  2970. }
  2971. return FAILURE;
  2972. } /* }}} */
  2973. /* {{{ proto DateTimeZone::__set_state(array array)
  2974. * */
  2975. PHP_METHOD(DateTimeZone, __set_state)
  2976. {
  2977. php_timezone_obj *tzobj;
  2978. zval *array;
  2979. HashTable *myht;
  2980. ZEND_PARSE_PARAMETERS_START(1, 1)
  2981. Z_PARAM_ARRAY(array)
  2982. ZEND_PARSE_PARAMETERS_END();
  2983. myht = Z_ARRVAL_P(array);
  2984. php_date_instantiate(date_ce_timezone, return_value);
  2985. tzobj = Z_PHPTIMEZONE_P(return_value);
  2986. if(php_date_timezone_initialize_from_hash(&return_value, &tzobj, myht) != SUCCESS) {
  2987. zend_throw_error(NULL, "Timezone initialization failed");
  2988. zval_ptr_dtor(return_value);
  2989. }
  2990. }
  2991. /* }}} */
  2992. /* {{{ proto DateTimeZone::__wakeup()
  2993. * */
  2994. PHP_METHOD(DateTimeZone, __wakeup)
  2995. {
  2996. zval *object = ZEND_THIS;
  2997. php_timezone_obj *tzobj;
  2998. HashTable *myht;
  2999. ZEND_PARSE_PARAMETERS_NONE();
  3000. tzobj = Z_PHPTIMEZONE_P(object);
  3001. myht = Z_OBJPROP_P(object);
  3002. if(php_date_timezone_initialize_from_hash(&return_value, &tzobj, myht) != SUCCESS) {
  3003. zend_throw_error(NULL, "Timezone initialization failed");
  3004. }
  3005. }
  3006. /* }}} */
  3007. /* {{{ proto string timezone_name_get(DateTimeZone object)
  3008. Returns the name of the timezone.
  3009. */
  3010. PHP_FUNCTION(timezone_name_get)
  3011. {
  3012. zval *object;
  3013. php_timezone_obj *tzobj;
  3014. if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O", &object, date_ce_timezone) == FAILURE) {
  3015. RETURN_THROWS();
  3016. }
  3017. tzobj = Z_PHPTIMEZONE_P(object);
  3018. DATE_CHECK_INITIALIZED(tzobj->initialized, DateTimeZone);
  3019. php_timezone_to_string(tzobj, return_value);
  3020. }
  3021. /* }}} */
  3022. /* {{{ proto string timezone_name_from_abbr(string abbr[, int gmtOffset[, int isdst]])
  3023. Returns the timezone name from abbreviation
  3024. */
  3025. PHP_FUNCTION(timezone_name_from_abbr)
  3026. {
  3027. zend_string *abbr;
  3028. char *tzid;
  3029. zend_long gmtoffset = -1;
  3030. zend_long isdst = -1;
  3031. ZEND_PARSE_PARAMETERS_START(1, 3)
  3032. Z_PARAM_STR(abbr)
  3033. Z_PARAM_OPTIONAL
  3034. Z_PARAM_LONG(gmtoffset)
  3035. Z_PARAM_LONG(isdst)
  3036. ZEND_PARSE_PARAMETERS_END();
  3037. tzid = timelib_timezone_id_from_abbr(ZSTR_VAL(abbr), gmtoffset, isdst);
  3038. if (tzid) {
  3039. RETURN_STRING(tzid);
  3040. } else {
  3041. RETURN_FALSE;
  3042. }
  3043. }
  3044. /* }}} */
  3045. /* {{{ proto int timezone_offset_get(DateTimeZone object, DateTimeInterface datetime)
  3046. Returns the timezone offset.
  3047. */
  3048. PHP_FUNCTION(timezone_offset_get)
  3049. {
  3050. zval *object, *dateobject;
  3051. php_timezone_obj *tzobj;
  3052. php_date_obj *dateobj;
  3053. timelib_time_offset *offset;
  3054. if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "OO", &object, date_ce_timezone, &dateobject, date_ce_interface) == FAILURE) {
  3055. RETURN_THROWS();
  3056. }
  3057. tzobj = Z_PHPTIMEZONE_P(object);
  3058. DATE_CHECK_INITIALIZED(tzobj->initialized, DateTimeZone);
  3059. dateobj = Z_PHPDATE_P(dateobject);
  3060. DATE_CHECK_INITIALIZED(dateobj->time, DateTimeInterface);
  3061. switch (tzobj->type) {
  3062. case TIMELIB_ZONETYPE_ID:
  3063. offset = timelib_get_time_zone_info(dateobj->time->sse, tzobj->tzi.tz);
  3064. RETVAL_LONG(offset->offset);
  3065. timelib_time_offset_dtor(offset);
  3066. break;
  3067. case TIMELIB_ZONETYPE_OFFSET:
  3068. RETURN_LONG(tzobj->tzi.utc_offset);
  3069. break;
  3070. case TIMELIB_ZONETYPE_ABBR:
  3071. RETURN_LONG(tzobj->tzi.z.utc_offset + (tzobj->tzi.z.dst * 3600));
  3072. break;
  3073. }
  3074. }
  3075. /* }}} */
  3076. /* {{{ proto array timezone_transitions_get(DateTimeZone object [, int timestamp_begin [, int timestamp_end ]])
  3077. Returns numerically indexed array containing associative array for all transitions in the specified range for the timezone.
  3078. */
  3079. PHP_FUNCTION(timezone_transitions_get)
  3080. {
  3081. zval *object, element;
  3082. php_timezone_obj *tzobj;
  3083. unsigned int begin = 0, found;
  3084. zend_long timestamp_begin = ZEND_LONG_MIN, timestamp_end = ZEND_LONG_MAX;
  3085. if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O|ll", &object, date_ce_timezone, &timestamp_begin, &timestamp_end) == FAILURE) {
  3086. RETURN_THROWS();
  3087. }
  3088. tzobj = Z_PHPTIMEZONE_P(object);
  3089. DATE_CHECK_INITIALIZED(tzobj->initialized, DateTimeZone);
  3090. if (tzobj->type != TIMELIB_ZONETYPE_ID) {
  3091. RETURN_FALSE;
  3092. }
  3093. #define add_nominal() \
  3094. array_init(&element); \
  3095. add_assoc_long(&element, "ts", timestamp_begin); \
  3096. add_assoc_str(&element, "time", php_format_date(DATE_FORMAT_ISO8601, 13, timestamp_begin, 0)); \
  3097. add_assoc_long(&element, "offset", tzobj->tzi.tz->type[0].offset); \
  3098. add_assoc_bool(&element, "isdst", tzobj->tzi.tz->type[0].isdst); \
  3099. add_assoc_string(&element, "abbr", &tzobj->tzi.tz->timezone_abbr[tzobj->tzi.tz->type[0].abbr_idx]); \
  3100. add_next_index_zval(return_value, &element);
  3101. #define add(i,ts) \
  3102. array_init(&element); \
  3103. add_assoc_long(&element, "ts", ts); \
  3104. add_assoc_str(&element, "time", php_format_date(DATE_FORMAT_ISO8601, 13, ts, 0)); \
  3105. add_assoc_long(&element, "offset", tzobj->tzi.tz->type[tzobj->tzi.tz->trans_idx[i]].offset); \
  3106. add_assoc_bool(&element, "isdst", tzobj->tzi.tz->type[tzobj->tzi.tz->trans_idx[i]].isdst); \
  3107. add_assoc_string(&element, "abbr", &tzobj->tzi.tz->timezone_abbr[tzobj->tzi.tz->type[tzobj->tzi.tz->trans_idx[i]].abbr_idx]); \
  3108. add_next_index_zval(return_value, &element);
  3109. #define add_last() add(tzobj->tzi.tz->bit64.timecnt - 1, timestamp_begin)
  3110. array_init(return_value);
  3111. if (timestamp_begin == ZEND_LONG_MIN) {
  3112. add_nominal();
  3113. begin = 0;
  3114. found = 1;
  3115. } else {
  3116. begin = 0;
  3117. found = 0;
  3118. if (tzobj->tzi.tz->bit64.timecnt > 0) {
  3119. do {
  3120. if (tzobj->tzi.tz->trans[begin] > timestamp_begin) {
  3121. if (begin > 0) {
  3122. add(begin - 1, timestamp_begin);
  3123. } else {
  3124. add_nominal();
  3125. }
  3126. found = 1;
  3127. break;
  3128. }
  3129. begin++;
  3130. } while (begin < tzobj->tzi.tz->bit64.timecnt);
  3131. }
  3132. }
  3133. if (!found) {
  3134. if (tzobj->tzi.tz->bit64.timecnt > 0) {
  3135. add_last();
  3136. } else {
  3137. add_nominal();
  3138. }
  3139. } else {
  3140. unsigned int i;
  3141. for (i = begin; i < tzobj->tzi.tz->bit64.timecnt; ++i) {
  3142. if (tzobj->tzi.tz->trans[i] < timestamp_end) {
  3143. add(i, tzobj->tzi.tz->trans[i]);
  3144. }
  3145. }
  3146. }
  3147. }
  3148. /* }}} */
  3149. /* {{{ proto array timezone_location_get()
  3150. Returns location information for a timezone, including country code, latitude/longitude and comments
  3151. */
  3152. PHP_FUNCTION(timezone_location_get)
  3153. {
  3154. zval *object;
  3155. php_timezone_obj *tzobj;
  3156. if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O", &object, date_ce_timezone) == FAILURE) {
  3157. RETURN_THROWS();
  3158. }
  3159. tzobj = Z_PHPTIMEZONE_P(object);
  3160. DATE_CHECK_INITIALIZED(tzobj->initialized, DateTimeZone);
  3161. if (tzobj->type != TIMELIB_ZONETYPE_ID) {
  3162. RETURN_FALSE;
  3163. }
  3164. array_init(return_value);
  3165. add_assoc_string(return_value, "country_code", tzobj->tzi.tz->location.country_code);
  3166. add_assoc_double(return_value, "latitude", tzobj->tzi.tz->location.latitude);
  3167. add_assoc_double(return_value, "longitude", tzobj->tzi.tz->location.longitude);
  3168. add_assoc_string(return_value, "comments", tzobj->tzi.tz->location.comments);
  3169. }
  3170. /* }}} */
  3171. static int date_interval_initialize(timelib_rel_time **rt, /*const*/ char *format, size_t format_length) /* {{{ */
  3172. {
  3173. timelib_time *b = NULL, *e = NULL;
  3174. timelib_rel_time *p = NULL;
  3175. int r = 0;
  3176. int retval = 0;
  3177. timelib_error_container *errors;
  3178. timelib_strtointerval(format, format_length, &b, &e, &p, &r, &errors);
  3179. if (errors->error_count > 0) {
  3180. php_error_docref(NULL, E_WARNING, "Unknown or bad format (%s)", format);
  3181. retval = FAILURE;
  3182. if (p) {
  3183. timelib_rel_time_dtor(p);
  3184. }
  3185. } else {
  3186. if(p) {
  3187. *rt = p;
  3188. retval = SUCCESS;
  3189. } else {
  3190. if(b && e) {
  3191. timelib_update_ts(b, NULL);
  3192. timelib_update_ts(e, NULL);
  3193. *rt = timelib_diff(b, e);
  3194. retval = SUCCESS;
  3195. } else {
  3196. php_error_docref(NULL, E_WARNING, "Failed to parse interval (%s)", format);
  3197. retval = FAILURE;
  3198. }
  3199. }
  3200. }
  3201. timelib_error_container_dtor(errors);
  3202. timelib_free(b);
  3203. timelib_free(e);
  3204. return retval;
  3205. } /* }}} */
  3206. static int date_interval_compare_objects(zval *o1, zval *o2) {
  3207. ZEND_COMPARE_OBJECTS_FALLBACK(o1, o2);
  3208. /* There is no well defined way to compare intervals like P1M and P30D, which may compare
  3209. * smaller, equal or greater depending on the point in time at which the interval starts. As
  3210. * such, we treat DateInterval objects are non-comparable and emit a warning. */
  3211. zend_error(E_WARNING, "Cannot compare DateInterval objects");
  3212. return ZEND_UNCOMPARABLE;
  3213. }
  3214. /* {{{ date_interval_read_property */
  3215. static zval *date_interval_read_property(zend_object *object, zend_string *name, int type, void **cache_slot, zval *rv)
  3216. {
  3217. php_interval_obj *obj;
  3218. zval *retval;
  3219. timelib_sll value = -1;
  3220. double fvalue = -1;
  3221. obj = php_interval_obj_from_obj(object);
  3222. if (!obj->initialized) {
  3223. retval = zend_std_read_property(object, name, type, cache_slot, rv);
  3224. return retval;
  3225. }
  3226. #define GET_VALUE_FROM_STRUCT(n,m) \
  3227. if (strcmp(ZSTR_VAL(name), m) == 0) { \
  3228. value = obj->diff->n; \
  3229. break; \
  3230. }
  3231. do {
  3232. GET_VALUE_FROM_STRUCT(y, "y");
  3233. GET_VALUE_FROM_STRUCT(m, "m");
  3234. GET_VALUE_FROM_STRUCT(d, "d");
  3235. GET_VALUE_FROM_STRUCT(h, "h");
  3236. GET_VALUE_FROM_STRUCT(i, "i");
  3237. GET_VALUE_FROM_STRUCT(s, "s");
  3238. if (strcmp(ZSTR_VAL(name), "f") == 0) {
  3239. fvalue = obj->diff->us / 1000000.0;
  3240. break;
  3241. }
  3242. GET_VALUE_FROM_STRUCT(invert, "invert");
  3243. GET_VALUE_FROM_STRUCT(days, "days");
  3244. /* didn't find any */
  3245. retval = zend_std_read_property(object, name, type, cache_slot, rv);
  3246. return retval;
  3247. } while(0);
  3248. retval = rv;
  3249. if (fvalue != -1) {
  3250. ZVAL_DOUBLE(retval, fvalue);
  3251. } else if (value != -99999) {
  3252. ZVAL_LONG(retval, value);
  3253. } else {
  3254. ZVAL_FALSE(retval);
  3255. }
  3256. return retval;
  3257. }
  3258. /* }}} */
  3259. /* {{{ date_interval_write_property */
  3260. static zval *date_interval_write_property(zend_object *object, zend_string *name, zval *value, void **cache_slot)
  3261. {
  3262. php_interval_obj *obj;
  3263. obj = php_interval_obj_from_obj(object);
  3264. if (!obj->initialized) {
  3265. return zend_std_write_property(object, name, value, cache_slot);
  3266. }
  3267. #define SET_VALUE_FROM_STRUCT(n,m) \
  3268. if (strcmp(ZSTR_VAL(name), m) == 0) { \
  3269. obj->diff->n = zval_get_long(value); \
  3270. break; \
  3271. }
  3272. do {
  3273. SET_VALUE_FROM_STRUCT(y, "y");
  3274. SET_VALUE_FROM_STRUCT(m, "m");
  3275. SET_VALUE_FROM_STRUCT(d, "d");
  3276. SET_VALUE_FROM_STRUCT(h, "h");
  3277. SET_VALUE_FROM_STRUCT(i, "i");
  3278. SET_VALUE_FROM_STRUCT(s, "s");
  3279. if (strcmp(ZSTR_VAL(name), "f") == 0) {
  3280. obj->diff->us = zval_get_double(value) * 1000000;
  3281. break;
  3282. }
  3283. SET_VALUE_FROM_STRUCT(invert, "invert");
  3284. /* didn't find any */
  3285. value = zend_std_write_property(object, name, value, cache_slot);
  3286. } while(0);
  3287. return value;
  3288. }
  3289. /* }}} */
  3290. /* {{{ date_interval_get_property_ptr_ptr */
  3291. static zval *date_interval_get_property_ptr_ptr(zend_object *object, zend_string *name, int type, void **cache_slot)
  3292. {
  3293. zval *ret;
  3294. if(zend_binary_strcmp("y", sizeof("y") - 1, ZSTR_VAL(name), ZSTR_LEN(name)) == 0 ||
  3295. zend_binary_strcmp("m", sizeof("m") - 1, ZSTR_VAL(name), ZSTR_LEN(name)) == 0 ||
  3296. zend_binary_strcmp("d", sizeof("d") - 1, ZSTR_VAL(name), ZSTR_LEN(name)) == 0 ||
  3297. zend_binary_strcmp("h", sizeof("h") - 1, ZSTR_VAL(name), ZSTR_LEN(name)) == 0 ||
  3298. zend_binary_strcmp("i", sizeof("i") - 1, ZSTR_VAL(name), ZSTR_LEN(name)) == 0 ||
  3299. zend_binary_strcmp("s", sizeof("s") - 1, ZSTR_VAL(name), ZSTR_LEN(name)) == 0 ||
  3300. zend_binary_strcmp("f", sizeof("f") - 1, ZSTR_VAL(name), ZSTR_LEN(name)) == 0 ||
  3301. zend_binary_strcmp("days", sizeof("days") - 1, ZSTR_VAL(name), ZSTR_LEN(name)) == 0 ||
  3302. zend_binary_strcmp("invert", sizeof("invert") - 1, ZSTR_VAL(name), ZSTR_LEN(name)) == 0) {
  3303. /* Fallback to read_property. */
  3304. ret = NULL;
  3305. } else {
  3306. ret = zend_std_get_property_ptr_ptr(object, name, type, cache_slot);
  3307. }
  3308. return ret;
  3309. }
  3310. /* }}} */
  3311. /* {{{ proto DateInterval::__construct([string interval_spec])
  3312. Creates new DateInterval object.
  3313. */
  3314. PHP_METHOD(DateInterval, __construct)
  3315. {
  3316. zend_string *interval_string = NULL;
  3317. timelib_rel_time *reltime;
  3318. zend_error_handling error_handling;
  3319. ZEND_PARSE_PARAMETERS_START(1, 1)
  3320. Z_PARAM_STR(interval_string)
  3321. ZEND_PARSE_PARAMETERS_END();
  3322. zend_replace_error_handling(EH_THROW, NULL, &error_handling);
  3323. if (date_interval_initialize(&reltime, ZSTR_VAL(interval_string), ZSTR_LEN(interval_string)) == SUCCESS) {
  3324. php_interval_obj *diobj = Z_PHPINTERVAL_P(ZEND_THIS);
  3325. diobj->diff = reltime;
  3326. diobj->initialized = 1;
  3327. }
  3328. zend_restore_error_handling(&error_handling);
  3329. }
  3330. /* }}} */
  3331. static int php_date_interval_initialize_from_hash(zval **return_value, php_interval_obj **intobj, HashTable *myht) /* {{{ */
  3332. {
  3333. (*intobj)->diff = timelib_rel_time_ctor();
  3334. #define PHP_DATE_INTERVAL_READ_PROPERTY(element, member, itype, def) \
  3335. do { \
  3336. zval *z_arg = zend_hash_str_find(myht, element, sizeof(element) - 1); \
  3337. if (z_arg && Z_TYPE_P(z_arg) <= IS_STRING) { \
  3338. (*intobj)->diff->member = (itype)zval_get_long(z_arg); \
  3339. } else { \
  3340. (*intobj)->diff->member = (itype)def; \
  3341. } \
  3342. } while (0);
  3343. #define PHP_DATE_INTERVAL_READ_PROPERTY_I64(element, member) \
  3344. do { \
  3345. zval *z_arg = zend_hash_str_find(myht, element, sizeof(element) - 1); \
  3346. if (z_arg && Z_TYPE_P(z_arg) <= IS_STRING) { \
  3347. zend_string *tmp_str; \
  3348. zend_string *str = zval_get_tmp_string(z_arg, &tmp_str); \
  3349. DATE_A64I((*intobj)->diff->member, ZSTR_VAL(str)); \
  3350. zend_tmp_string_release(tmp_str); \
  3351. } else { \
  3352. (*intobj)->diff->member = -1LL; \
  3353. } \
  3354. } while (0);
  3355. #define PHP_DATE_INTERVAL_READ_PROPERTY_DAYS(member) \
  3356. do { \
  3357. zval *z_arg = zend_hash_str_find(myht, "days", sizeof("days") - 1); \
  3358. if (z_arg && Z_TYPE_P(z_arg) == IS_FALSE) { \
  3359. (*intobj)->diff->member = -99999; \
  3360. } else if (z_arg && Z_TYPE_P(z_arg) <= IS_STRING) { \
  3361. zend_string *str = zval_get_string(z_arg); \
  3362. DATE_A64I((*intobj)->diff->member, ZSTR_VAL(str)); \
  3363. zend_string_release(str); \
  3364. } else { \
  3365. (*intobj)->diff->member = -1LL; \
  3366. } \
  3367. } while (0);
  3368. #define PHP_DATE_INTERVAL_READ_PROPERTY_DOUBLE(element, member, def) \
  3369. do { \
  3370. zval *z_arg = zend_hash_str_find(myht, element, sizeof(element) - 1); \
  3371. if (z_arg) { \
  3372. (*intobj)->diff->member = (double)zval_get_double(z_arg); \
  3373. } else { \
  3374. (*intobj)->diff->member = (double)def; \
  3375. } \
  3376. } while (0);
  3377. PHP_DATE_INTERVAL_READ_PROPERTY("y", y, timelib_sll, -1)
  3378. PHP_DATE_INTERVAL_READ_PROPERTY("m", m, timelib_sll, -1)
  3379. PHP_DATE_INTERVAL_READ_PROPERTY("d", d, timelib_sll, -1)
  3380. PHP_DATE_INTERVAL_READ_PROPERTY("h", h, timelib_sll, -1)
  3381. PHP_DATE_INTERVAL_READ_PROPERTY("i", i, timelib_sll, -1)
  3382. PHP_DATE_INTERVAL_READ_PROPERTY("s", s, timelib_sll, -1)
  3383. {
  3384. zval *z_arg = zend_hash_str_find(myht, "f", sizeof("f") - 1);
  3385. (*intobj)->diff->us = -1000000;
  3386. if (z_arg) {
  3387. double val = zval_get_double(z_arg) * 1000000;
  3388. if (val >= 0 && val < 1000000) {
  3389. (*intobj)->diff->us = val;
  3390. }
  3391. }
  3392. }
  3393. PHP_DATE_INTERVAL_READ_PROPERTY("weekday", weekday, int, -1)
  3394. PHP_DATE_INTERVAL_READ_PROPERTY("weekday_behavior", weekday_behavior, int, -1)
  3395. PHP_DATE_INTERVAL_READ_PROPERTY("first_last_day_of", first_last_day_of, int, -1)
  3396. PHP_DATE_INTERVAL_READ_PROPERTY("invert", invert, int, 0);
  3397. PHP_DATE_INTERVAL_READ_PROPERTY_DAYS(days);
  3398. PHP_DATE_INTERVAL_READ_PROPERTY("special_type", special.type, unsigned int, 0);
  3399. PHP_DATE_INTERVAL_READ_PROPERTY_I64("special_amount", special.amount);
  3400. PHP_DATE_INTERVAL_READ_PROPERTY("have_weekday_relative", have_weekday_relative, unsigned int, 0);
  3401. PHP_DATE_INTERVAL_READ_PROPERTY("have_special_relative", have_special_relative, unsigned int, 0);
  3402. (*intobj)->initialized = 1;
  3403. return 0;
  3404. } /* }}} */
  3405. /* {{{ proto DateInterval::__set_state(array array)
  3406. */
  3407. PHP_METHOD(DateInterval, __set_state)
  3408. {
  3409. php_interval_obj *intobj;
  3410. zval *array;
  3411. HashTable *myht;
  3412. ZEND_PARSE_PARAMETERS_START(1, 1)
  3413. Z_PARAM_ARRAY(array)
  3414. ZEND_PARSE_PARAMETERS_END();
  3415. myht = Z_ARRVAL_P(array);
  3416. php_date_instantiate(date_ce_interval, return_value);
  3417. intobj = Z_PHPINTERVAL_P(return_value);
  3418. php_date_interval_initialize_from_hash(&return_value, &intobj, myht);
  3419. }
  3420. /* }}} */
  3421. /* {{{ proto DateInterval::__wakeup()
  3422. */
  3423. PHP_METHOD(DateInterval, __wakeup)
  3424. {
  3425. zval *object = ZEND_THIS;
  3426. php_interval_obj *intobj;
  3427. HashTable *myht;
  3428. ZEND_PARSE_PARAMETERS_NONE();
  3429. intobj = Z_PHPINTERVAL_P(object);
  3430. myht = Z_OBJPROP_P(object);
  3431. php_date_interval_initialize_from_hash(&return_value, &intobj, myht);
  3432. }
  3433. /* }}} */
  3434. /* {{{ proto DateInterval date_interval_create_from_date_string(string time)
  3435. Uses the normal date parsers and sets up a DateInterval from the relative parts of the parsed string
  3436. */
  3437. PHP_FUNCTION(date_interval_create_from_date_string)
  3438. {
  3439. zend_string *time_str = NULL;
  3440. timelib_time *time;
  3441. timelib_error_container *err = NULL;
  3442. php_interval_obj *diobj;
  3443. ZEND_PARSE_PARAMETERS_START(1, 1)
  3444. Z_PARAM_STR(time_str)
  3445. ZEND_PARSE_PARAMETERS_END();
  3446. time = timelib_strtotime(ZSTR_VAL(time_str), ZSTR_LEN(time_str), &err, DATE_TIMEZONEDB, php_date_parse_tzfile_wrapper);
  3447. if (err->error_count > 0) {
  3448. php_error_docref(NULL, E_WARNING, "Unknown or bad format (%s) at position %d (%c): %s", ZSTR_VAL(time_str),
  3449. err->error_messages[0].position, err->error_messages[0].character ? err->error_messages[0].character : ' ', err->error_messages[0].message);
  3450. RETVAL_FALSE;
  3451. goto cleanup;
  3452. }
  3453. php_date_instantiate(date_ce_interval, return_value);
  3454. diobj = Z_PHPINTERVAL_P(return_value);
  3455. diobj->diff = timelib_rel_time_clone(&time->relative);
  3456. diobj->initialized = 1;
  3457. cleanup:
  3458. timelib_time_dtor(time);
  3459. timelib_error_container_dtor(err);
  3460. }
  3461. /* }}} */
  3462. /* {{{ date_interval_format - */
  3463. static zend_string *date_interval_format(char *format, size_t format_len, timelib_rel_time *t)
  3464. {
  3465. smart_str string = {0};
  3466. size_t i;
  3467. int length, have_format_spec = 0;
  3468. char buffer[33];
  3469. if (!format_len) {
  3470. return ZSTR_EMPTY_ALLOC();
  3471. }
  3472. for (i = 0; i < format_len; i++) {
  3473. if (have_format_spec) {
  3474. switch (format[i]) {
  3475. case 'Y': length = slprintf(buffer, sizeof(buffer), "%02d", (int) t->y); break;
  3476. case 'y': length = slprintf(buffer, sizeof(buffer), "%d", (int) t->y); break;
  3477. case 'M': length = slprintf(buffer, sizeof(buffer), "%02d", (int) t->m); break;
  3478. case 'm': length = slprintf(buffer, sizeof(buffer), "%d", (int) t->m); break;
  3479. case 'D': length = slprintf(buffer, sizeof(buffer), "%02d", (int) t->d); break;
  3480. case 'd': length = slprintf(buffer, sizeof(buffer), "%d", (int) t->d); break;
  3481. case 'H': length = slprintf(buffer, sizeof(buffer), "%02d", (int) t->h); break;
  3482. case 'h': length = slprintf(buffer, sizeof(buffer), "%d", (int) t->h); break;
  3483. case 'I': length = slprintf(buffer, sizeof(buffer), "%02d", (int) t->i); break;
  3484. case 'i': length = slprintf(buffer, sizeof(buffer), "%d", (int) t->i); break;
  3485. case 'S': length = slprintf(buffer, sizeof(buffer), "%02" ZEND_LONG_FMT_SPEC, (zend_long) t->s); break;
  3486. case 's': length = slprintf(buffer, sizeof(buffer), ZEND_LONG_FMT, (zend_long) t->s); break;
  3487. case 'F': length = slprintf(buffer, sizeof(buffer), "%06" ZEND_LONG_FMT_SPEC, (zend_long) t->us); break;
  3488. case 'f': length = slprintf(buffer, sizeof(buffer), ZEND_LONG_FMT, (zend_long) t->us); break;
  3489. case 'a': {
  3490. if ((int) t->days != -99999) {
  3491. length = slprintf(buffer, sizeof(buffer), "%d", (int) t->days);
  3492. } else {
  3493. length = slprintf(buffer, sizeof(buffer), "(unknown)");
  3494. }
  3495. } break;
  3496. case 'r': length = slprintf(buffer, sizeof(buffer), "%s", t->invert ? "-" : ""); break;
  3497. case 'R': length = slprintf(buffer, sizeof(buffer), "%c", t->invert ? '-' : '+'); break;
  3498. case '%': length = slprintf(buffer, sizeof(buffer), "%%"); break;
  3499. default: buffer[0] = '%'; buffer[1] = format[i]; buffer[2] = '\0'; length = 2; break;
  3500. }
  3501. smart_str_appendl(&string, buffer, length);
  3502. have_format_spec = 0;
  3503. } else {
  3504. if (format[i] == '%') {
  3505. have_format_spec = 1;
  3506. } else {
  3507. smart_str_appendc(&string, format[i]);
  3508. }
  3509. }
  3510. }
  3511. smart_str_0(&string);
  3512. if (string.s == NULL) {
  3513. return ZSTR_EMPTY_ALLOC();
  3514. }
  3515. return string.s;
  3516. }
  3517. /* }}} */
  3518. /* {{{ proto string date_interval_format(DateInterval object, string format)
  3519. Formats the interval.
  3520. */
  3521. PHP_FUNCTION(date_interval_format)
  3522. {
  3523. zval *object;
  3524. php_interval_obj *diobj;
  3525. char *format;
  3526. size_t format_len;
  3527. if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "Os", &object, date_ce_interval, &format, &format_len) == FAILURE) {
  3528. RETURN_THROWS();
  3529. }
  3530. diobj = Z_PHPINTERVAL_P(object);
  3531. DATE_CHECK_INITIALIZED(diobj->initialized, DateInterval);
  3532. RETURN_STR(date_interval_format(format, format_len, diobj->diff));
  3533. }
  3534. /* }}} */
  3535. static int date_period_initialize(timelib_time **st, timelib_time **et, timelib_rel_time **d, zend_long *recurrences, /*const*/ char *format, size_t format_length) /* {{{ */
  3536. {
  3537. timelib_time *b = NULL, *e = NULL;
  3538. timelib_rel_time *p = NULL;
  3539. int r = 0;
  3540. int retval = 0;
  3541. timelib_error_container *errors;
  3542. timelib_strtointerval(format, format_length, &b, &e, &p, &r, &errors);
  3543. if (errors->error_count > 0) {
  3544. php_error_docref(NULL, E_WARNING, "Unknown or bad format (%s)", format);
  3545. retval = FAILURE;
  3546. if (p) {
  3547. timelib_rel_time_dtor(p);
  3548. }
  3549. } else {
  3550. *st = b;
  3551. *et = e;
  3552. *d = p;
  3553. *recurrences = r;
  3554. retval = SUCCESS;
  3555. }
  3556. timelib_error_container_dtor(errors);
  3557. return retval;
  3558. } /* }}} */
  3559. /* {{{ proto DatePeriod::__construct(DateTime $start, DateInterval $interval, int recurrences|DateTime $end)
  3560. Creates new DatePeriod object.
  3561. */
  3562. PHP_METHOD(DatePeriod, __construct)
  3563. {
  3564. php_period_obj *dpobj;
  3565. php_date_obj *dateobj;
  3566. zval *start, *end = NULL, *interval;
  3567. zend_long recurrences = 0, options = 0;
  3568. char *isostr = NULL;
  3569. size_t isostr_len = 0;
  3570. timelib_time *clone;
  3571. zend_error_handling error_handling;
  3572. zend_replace_error_handling(EH_THROW, NULL, &error_handling);
  3573. if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS(), "OOl|l", &start, date_ce_interface, &interval, date_ce_interval, &recurrences, &options) == FAILURE) {
  3574. if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS(), "OOO|l", &start, date_ce_interface, &interval, date_ce_interval, &end, date_ce_interface, &options) == FAILURE) {
  3575. if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS(), "s|l", &isostr, &isostr_len, &options) == FAILURE) {
  3576. php_error_docref(NULL, E_WARNING, "This constructor accepts either (DateTimeInterface, DateInterval, int) OR (DateTimeInterface, DateInterval, DateTime) OR (string) as arguments.");
  3577. zend_restore_error_handling(&error_handling);
  3578. return;
  3579. }
  3580. }
  3581. }
  3582. dpobj = Z_PHPPERIOD_P(ZEND_THIS);
  3583. dpobj->current = NULL;
  3584. if (isostr) {
  3585. date_period_initialize(&(dpobj->start), &(dpobj->end), &(dpobj->interval), &recurrences, isostr, isostr_len);
  3586. if (dpobj->start == NULL) {
  3587. php_error_docref(NULL, E_WARNING, "The ISO interval '%s' did not contain a start date.", isostr);
  3588. }
  3589. if (dpobj->interval == NULL) {
  3590. php_error_docref(NULL, E_WARNING, "The ISO interval '%s' did not contain an interval.", isostr);
  3591. }
  3592. if (dpobj->end == NULL && recurrences == 0) {
  3593. php_error_docref(NULL, E_WARNING, "The ISO interval '%s' did not contain an end date or a recurrence count.", isostr);
  3594. }
  3595. if (dpobj->start) {
  3596. timelib_update_ts(dpobj->start, NULL);
  3597. }
  3598. if (dpobj->end) {
  3599. timelib_update_ts(dpobj->end, NULL);
  3600. }
  3601. dpobj->start_ce = date_ce_date;
  3602. } else {
  3603. /* init */
  3604. php_interval_obj *intobj = Z_PHPINTERVAL_P(interval);
  3605. /* start date */
  3606. dateobj = Z_PHPDATE_P(start);
  3607. clone = timelib_time_ctor();
  3608. memcpy(clone, dateobj->time, sizeof(timelib_time));
  3609. if (dateobj->time->tz_abbr) {
  3610. clone->tz_abbr = timelib_strdup(dateobj->time->tz_abbr);
  3611. }
  3612. if (dateobj->time->tz_info) {
  3613. clone->tz_info = dateobj->time->tz_info;
  3614. }
  3615. dpobj->start = clone;
  3616. dpobj->start_ce = Z_OBJCE_P(start);
  3617. /* interval */
  3618. dpobj->interval = timelib_rel_time_clone(intobj->diff);
  3619. /* end date */
  3620. if (end) {
  3621. dateobj = Z_PHPDATE_P(end);
  3622. clone = timelib_time_clone(dateobj->time);
  3623. dpobj->end = clone;
  3624. }
  3625. }
  3626. if (dpobj->end == NULL && recurrences < 1) {
  3627. php_error_docref(NULL, E_WARNING, "The recurrence count '%d' is invalid. Needs to be > 0", (int) recurrences);
  3628. }
  3629. /* options */
  3630. dpobj->include_start_date = !(options & PHP_DATE_PERIOD_EXCLUDE_START_DATE);
  3631. /* recurrrences */
  3632. dpobj->recurrences = recurrences + dpobj->include_start_date;
  3633. dpobj->initialized = 1;
  3634. zend_restore_error_handling(&error_handling);
  3635. }
  3636. /* }}} */
  3637. /* {{{ proto DatePeriod::getStartDate()
  3638. Get start date.
  3639. */
  3640. PHP_METHOD(DatePeriod, getStartDate)
  3641. {
  3642. php_period_obj *dpobj;
  3643. php_date_obj *dateobj;
  3644. ZEND_PARSE_PARAMETERS_NONE();
  3645. dpobj = Z_PHPPERIOD_P(ZEND_THIS);
  3646. php_date_instantiate(dpobj->start_ce, return_value);
  3647. dateobj = Z_PHPDATE_P(return_value);
  3648. dateobj->time = timelib_time_ctor();
  3649. *dateobj->time = *dpobj->start;
  3650. if (dpobj->start->tz_abbr) {
  3651. dateobj->time->tz_abbr = timelib_strdup(dpobj->start->tz_abbr);
  3652. }
  3653. if (dpobj->start->tz_info) {
  3654. dateobj->time->tz_info = dpobj->start->tz_info;
  3655. }
  3656. }
  3657. /* }}} */
  3658. /* {{{ proto DatePeriod::getEndDate()
  3659. Get end date.
  3660. */
  3661. PHP_METHOD(DatePeriod, getEndDate)
  3662. {
  3663. php_period_obj *dpobj;
  3664. php_date_obj *dateobj;
  3665. ZEND_PARSE_PARAMETERS_NONE();
  3666. dpobj = Z_PHPPERIOD_P(ZEND_THIS);
  3667. if (!dpobj->end) {
  3668. return;
  3669. }
  3670. php_date_instantiate(dpobj->start_ce, return_value);
  3671. dateobj = Z_PHPDATE_P(return_value);
  3672. dateobj->time = timelib_time_ctor();
  3673. *dateobj->time = *dpobj->end;
  3674. if (dpobj->end->tz_abbr) {
  3675. dateobj->time->tz_abbr = timelib_strdup(dpobj->end->tz_abbr);
  3676. }
  3677. if (dpobj->end->tz_info) {
  3678. dateobj->time->tz_info = dpobj->end->tz_info;
  3679. }
  3680. }
  3681. /* }}} */
  3682. /* {{{ proto DatePeriod::getDateInterval()
  3683. Get date interval.
  3684. */
  3685. PHP_METHOD(DatePeriod, getDateInterval)
  3686. {
  3687. php_period_obj *dpobj;
  3688. php_interval_obj *diobj;
  3689. ZEND_PARSE_PARAMETERS_NONE();
  3690. dpobj = Z_PHPPERIOD_P(ZEND_THIS);
  3691. php_date_instantiate(date_ce_interval, return_value);
  3692. diobj = Z_PHPINTERVAL_P(return_value);
  3693. diobj->diff = timelib_rel_time_clone(dpobj->interval);
  3694. diobj->initialized = 1;
  3695. }
  3696. /* }}} */
  3697. /* {{{ proto int DatePeriod::getRecurrences()
  3698. Get recurrences.
  3699. */
  3700. PHP_METHOD(DatePeriod, getRecurrences)
  3701. {
  3702. php_period_obj *dpobj;
  3703. ZEND_PARSE_PARAMETERS_NONE();
  3704. dpobj = Z_PHPPERIOD_P(ZEND_THIS);
  3705. if (0 == dpobj->recurrences - dpobj->include_start_date) {
  3706. return;
  3707. }
  3708. RETURN_LONG(dpobj->recurrences - dpobj->include_start_date);
  3709. }
  3710. /* }}} */
  3711. static int check_id_allowed(char *id, zend_long what) /* {{{ */
  3712. {
  3713. if (what & PHP_DATE_TIMEZONE_GROUP_AFRICA && strncasecmp(id, "Africa/", 7) == 0) return 1;
  3714. if (what & PHP_DATE_TIMEZONE_GROUP_AMERICA && strncasecmp(id, "America/", 8) == 0) return 1;
  3715. if (what & PHP_DATE_TIMEZONE_GROUP_ANTARCTICA && strncasecmp(id, "Antarctica/", 11) == 0) return 1;
  3716. if (what & PHP_DATE_TIMEZONE_GROUP_ARCTIC && strncasecmp(id, "Arctic/", 7) == 0) return 1;
  3717. if (what & PHP_DATE_TIMEZONE_GROUP_ASIA && strncasecmp(id, "Asia/", 5) == 0) return 1;
  3718. if (what & PHP_DATE_TIMEZONE_GROUP_ATLANTIC && strncasecmp(id, "Atlantic/", 9) == 0) return 1;
  3719. if (what & PHP_DATE_TIMEZONE_GROUP_AUSTRALIA && strncasecmp(id, "Australia/", 10) == 0) return 1;
  3720. if (what & PHP_DATE_TIMEZONE_GROUP_EUROPE && strncasecmp(id, "Europe/", 7) == 0) return 1;
  3721. if (what & PHP_DATE_TIMEZONE_GROUP_INDIAN && strncasecmp(id, "Indian/", 7) == 0) return 1;
  3722. if (what & PHP_DATE_TIMEZONE_GROUP_PACIFIC && strncasecmp(id, "Pacific/", 8) == 0) return 1;
  3723. if (what & PHP_DATE_TIMEZONE_GROUP_UTC && strncasecmp(id, "UTC", 3) == 0) return 1;
  3724. return 0;
  3725. } /* }}} */
  3726. /* {{{ proto array timezone_identifiers_list([long what[, string country]])
  3727. Returns numerically index array with all timezone identifiers.
  3728. */
  3729. PHP_FUNCTION(timezone_identifiers_list)
  3730. {
  3731. const timelib_tzdb *tzdb;
  3732. const timelib_tzdb_index_entry *table;
  3733. int i, item_count;
  3734. zend_long what = PHP_DATE_TIMEZONE_GROUP_ALL;
  3735. char *option = NULL;
  3736. size_t option_len = 0;
  3737. ZEND_PARSE_PARAMETERS_START(0, 2)
  3738. Z_PARAM_OPTIONAL
  3739. Z_PARAM_LONG(what)
  3740. Z_PARAM_STRING_OR_NULL(option, option_len)
  3741. ZEND_PARSE_PARAMETERS_END();
  3742. /* Extra validation */
  3743. if (what == PHP_DATE_TIMEZONE_PER_COUNTRY && option_len != 2) {
  3744. php_error_docref(NULL, E_NOTICE, "A two-letter ISO 3166-1 compatible country code is expected");
  3745. RETURN_FALSE;
  3746. }
  3747. tzdb = DATE_TIMEZONEDB;
  3748. table = timelib_timezone_identifiers_list((timelib_tzdb*) tzdb, &item_count);
  3749. array_init(return_value);
  3750. for (i = 0; i < item_count; ++i) {
  3751. if (what == PHP_DATE_TIMEZONE_PER_COUNTRY) {
  3752. if (tzdb->data[table[i].pos + 5] == option[0] && tzdb->data[table[i].pos + 6] == option[1]) {
  3753. add_next_index_string(return_value, table[i].id);
  3754. }
  3755. } else if (what == PHP_DATE_TIMEZONE_GROUP_ALL_W_BC || (check_id_allowed(table[i].id, what) && (tzdb->data[table[i].pos + 4] == '\1'))) {
  3756. add_next_index_string(return_value, table[i].id);
  3757. }
  3758. };
  3759. }
  3760. /* }}} */
  3761. /* {{{ proto string timezone_version_get()
  3762. Returns the Olson database version number.
  3763. */
  3764. PHP_FUNCTION(timezone_version_get)
  3765. {
  3766. const timelib_tzdb *tzdb;
  3767. ZEND_PARSE_PARAMETERS_NONE();
  3768. tzdb = DATE_TIMEZONEDB;
  3769. RETURN_STRING(tzdb->version);
  3770. }
  3771. /* }}} */
  3772. /* {{{ proto array timezone_abbreviations_list()
  3773. Returns associative array containing dst, offset and the timezone name
  3774. */
  3775. PHP_FUNCTION(timezone_abbreviations_list)
  3776. {
  3777. const timelib_tz_lookup_table *table, *entry;
  3778. zval element, *abbr_array_p, abbr_array;
  3779. ZEND_PARSE_PARAMETERS_NONE();
  3780. table = timelib_timezone_abbreviations_list();
  3781. array_init(return_value);
  3782. entry = table;
  3783. do {
  3784. array_init(&element);
  3785. add_assoc_bool_ex(&element, "dst", sizeof("dst") -1, entry->type);
  3786. add_assoc_long_ex(&element, "offset", sizeof("offset") - 1, entry->gmtoffset);
  3787. if (entry->full_tz_name) {
  3788. add_assoc_string_ex(&element, "timezone_id", sizeof("timezone_id") - 1, entry->full_tz_name);
  3789. } else {
  3790. add_assoc_null_ex(&element, "timezone_id", sizeof("timezone_id") - 1);
  3791. }
  3792. abbr_array_p = zend_hash_str_find(Z_ARRVAL_P(return_value), entry->name, strlen(entry->name));
  3793. if (!abbr_array_p) {
  3794. array_init(&abbr_array);
  3795. add_assoc_zval(return_value, entry->name, &abbr_array);
  3796. } else {
  3797. ZVAL_COPY_VALUE(&abbr_array, abbr_array_p);
  3798. }
  3799. add_next_index_zval(&abbr_array, &element);
  3800. entry++;
  3801. } while (entry->name);
  3802. }
  3803. /* }}} */
  3804. /* {{{ proto bool date_default_timezone_set(string timezone_identifier)
  3805. Sets the default timezone used by all date/time functions in a script */
  3806. PHP_FUNCTION(date_default_timezone_set)
  3807. {
  3808. char *zone;
  3809. size_t zone_len;
  3810. ZEND_PARSE_PARAMETERS_START(1, 1)
  3811. Z_PARAM_STRING(zone, zone_len)
  3812. ZEND_PARSE_PARAMETERS_END();
  3813. if (!timelib_timezone_id_is_valid(zone, DATE_TIMEZONEDB)) {
  3814. php_error_docref(NULL, E_NOTICE, "Timezone ID '%s' is invalid", zone);
  3815. RETURN_FALSE;
  3816. }
  3817. if (DATEG(timezone)) {
  3818. efree(DATEG(timezone));
  3819. DATEG(timezone) = NULL;
  3820. }
  3821. DATEG(timezone) = estrndup(zone, zone_len);
  3822. RETURN_TRUE;
  3823. }
  3824. /* }}} */
  3825. /* {{{ proto string date_default_timezone_get()
  3826. Gets the default timezone used by all date/time functions in a script */
  3827. PHP_FUNCTION(date_default_timezone_get)
  3828. {
  3829. timelib_tzinfo *default_tz;
  3830. ZEND_PARSE_PARAMETERS_NONE();
  3831. default_tz = get_timezone_info();
  3832. RETVAL_STRING(default_tz->name);
  3833. }
  3834. /* }}} */
  3835. /* {{{ php_do_date_sunrise_sunset
  3836. * Common for date_sunrise() and date_sunset() functions
  3837. */
  3838. static void php_do_date_sunrise_sunset(INTERNAL_FUNCTION_PARAMETERS, int calc_sunset)
  3839. {
  3840. double latitude, longitude, zenith, gmt_offset, altitude;
  3841. zend_bool latitude_is_null = 1, longitude_is_null = 1, zenith_is_null = 1, gmt_offset_is_null = 1;
  3842. double h_rise, h_set, N;
  3843. timelib_sll rise, set, transit;
  3844. zend_long time, retformat = SUNFUNCS_RET_STRING;
  3845. int rs;
  3846. timelib_time *t;
  3847. timelib_tzinfo *tzi;
  3848. zend_string *retstr;
  3849. ZEND_PARSE_PARAMETERS_START(1, 6)
  3850. Z_PARAM_LONG(time)
  3851. Z_PARAM_OPTIONAL
  3852. Z_PARAM_LONG(retformat)
  3853. Z_PARAM_DOUBLE_OR_NULL(latitude, latitude_is_null)
  3854. Z_PARAM_DOUBLE_OR_NULL(longitude, longitude_is_null)
  3855. Z_PARAM_DOUBLE_OR_NULL(zenith, zenith_is_null)
  3856. Z_PARAM_DOUBLE_OR_NULL(gmt_offset, gmt_offset_is_null)
  3857. ZEND_PARSE_PARAMETERS_END();
  3858. if (latitude_is_null) {
  3859. latitude = INI_FLT("date.default_latitude");
  3860. }
  3861. if (longitude_is_null) {
  3862. latitude = INI_FLT("date.default_longitude");
  3863. }
  3864. if (zenith_is_null) {
  3865. if (calc_sunset) {
  3866. zenith = INI_FLT("date.sunset_zenith");
  3867. } else {
  3868. zenith = INI_FLT("date.sunrise_zenith");
  3869. }
  3870. }
  3871. if (retformat != SUNFUNCS_RET_TIMESTAMP &&
  3872. retformat != SUNFUNCS_RET_STRING &&
  3873. retformat != SUNFUNCS_RET_DOUBLE)
  3874. {
  3875. php_error_docref(NULL, E_WARNING, "Wrong return format given, pick one of SUNFUNCS_RET_TIMESTAMP, SUNFUNCS_RET_STRING or SUNFUNCS_RET_DOUBLE");
  3876. RETURN_FALSE;
  3877. }
  3878. altitude = 90 - zenith;
  3879. /* Initialize time struct */
  3880. t = timelib_time_ctor();
  3881. tzi = get_timezone_info();
  3882. t->tz_info = tzi;
  3883. t->zone_type = TIMELIB_ZONETYPE_ID;
  3884. if (gmt_offset_is_null) {
  3885. gmt_offset = timelib_get_current_offset(t) / 3600;
  3886. }
  3887. timelib_unixtime2local(t, time);
  3888. rs = timelib_astro_rise_set_altitude(t, longitude, latitude, altitude, 1, &h_rise, &h_set, &rise, &set, &transit);
  3889. timelib_time_dtor(t);
  3890. if (rs != 0) {
  3891. RETURN_FALSE;
  3892. }
  3893. if (retformat == SUNFUNCS_RET_TIMESTAMP) {
  3894. RETURN_LONG(calc_sunset ? set : rise);
  3895. }
  3896. N = (calc_sunset ? h_set : h_rise) + gmt_offset;
  3897. if (N > 24 || N < 0) {
  3898. N -= floor(N / 24) * 24;
  3899. }
  3900. switch (retformat) {
  3901. case SUNFUNCS_RET_STRING:
  3902. retstr = strpprintf(0, "%02d:%02d", (int) N, (int) (60 * (N - (int) N)));
  3903. RETURN_NEW_STR(retstr);
  3904. break;
  3905. case SUNFUNCS_RET_DOUBLE:
  3906. RETURN_DOUBLE(N);
  3907. break;
  3908. }
  3909. }
  3910. /* }}} */
  3911. /* {{{ proto mixed date_sunrise(mixed time [, int format [, float latitude [, float longitude [, float zenith [, float gmt_offset]]]]])
  3912. Returns time of sunrise for a given day and location */
  3913. PHP_FUNCTION(date_sunrise)
  3914. {
  3915. php_do_date_sunrise_sunset(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
  3916. }
  3917. /* }}} */
  3918. /* {{{ proto mixed date_sunset(mixed time [, int format [, float latitude [, float longitude [, float zenith [, float gmt_offset]]]]])
  3919. Returns time of sunset for a given day and location */
  3920. PHP_FUNCTION(date_sunset)
  3921. {
  3922. php_do_date_sunrise_sunset(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
  3923. }
  3924. /* }}} */
  3925. /* {{{ proto array date_sun_info(int time, float latitude, float longitude)
  3926. Returns an array with information about sun set/rise and twilight begin/end */
  3927. PHP_FUNCTION(date_sun_info)
  3928. {
  3929. zend_long time;
  3930. double latitude, longitude;
  3931. timelib_time *t, *t2;
  3932. timelib_tzinfo *tzi;
  3933. int rs;
  3934. timelib_sll rise, set, transit;
  3935. int dummy;
  3936. double ddummy;
  3937. ZEND_PARSE_PARAMETERS_START(3, 3)
  3938. Z_PARAM_LONG(time)
  3939. Z_PARAM_DOUBLE(latitude)
  3940. Z_PARAM_DOUBLE(longitude)
  3941. ZEND_PARSE_PARAMETERS_END();
  3942. /* Initialize time struct */
  3943. t = timelib_time_ctor();
  3944. tzi = get_timezone_info();
  3945. t->tz_info = tzi;
  3946. t->zone_type = TIMELIB_ZONETYPE_ID;
  3947. timelib_unixtime2local(t, time);
  3948. /* Setup */
  3949. t2 = timelib_time_ctor();
  3950. array_init(return_value);
  3951. /* Get sun up/down and transit */
  3952. rs = timelib_astro_rise_set_altitude(t, longitude, latitude, -50.0/60, 1, &ddummy, &ddummy, &rise, &set, &transit);
  3953. switch (rs) {
  3954. case -1: /* always below */
  3955. add_assoc_bool(return_value, "sunrise", 0);
  3956. add_assoc_bool(return_value, "sunset", 0);
  3957. break;
  3958. case 1: /* always above */
  3959. add_assoc_bool(return_value, "sunrise", 1);
  3960. add_assoc_bool(return_value, "sunset", 1);
  3961. break;
  3962. default:
  3963. t2->sse = rise;
  3964. add_assoc_long(return_value, "sunrise", timelib_date_to_int(t2, &dummy));
  3965. t2->sse = set;
  3966. add_assoc_long(return_value, "sunset", timelib_date_to_int(t2, &dummy));
  3967. }
  3968. t2->sse = transit;
  3969. add_assoc_long(return_value, "transit", timelib_date_to_int(t2, &dummy));
  3970. /* Get civil twilight */
  3971. rs = timelib_astro_rise_set_altitude(t, longitude, latitude, -6.0, 0, &ddummy, &ddummy, &rise, &set, &transit);
  3972. switch (rs) {
  3973. case -1: /* always below */
  3974. add_assoc_bool(return_value, "civil_twilight_begin", 0);
  3975. add_assoc_bool(return_value, "civil_twilight_end", 0);
  3976. break;
  3977. case 1: /* always above */
  3978. add_assoc_bool(return_value, "civil_twilight_begin", 1);
  3979. add_assoc_bool(return_value, "civil_twilight_end", 1);
  3980. break;
  3981. default:
  3982. t2->sse = rise;
  3983. add_assoc_long(return_value, "civil_twilight_begin", timelib_date_to_int(t2, &dummy));
  3984. t2->sse = set;
  3985. add_assoc_long(return_value, "civil_twilight_end", timelib_date_to_int(t2, &dummy));
  3986. }
  3987. /* Get nautical twilight */
  3988. rs = timelib_astro_rise_set_altitude(t, longitude, latitude, -12.0, 0, &ddummy, &ddummy, &rise, &set, &transit);
  3989. switch (rs) {
  3990. case -1: /* always below */
  3991. add_assoc_bool(return_value, "nautical_twilight_begin", 0);
  3992. add_assoc_bool(return_value, "nautical_twilight_end", 0);
  3993. break;
  3994. case 1: /* always above */
  3995. add_assoc_bool(return_value, "nautical_twilight_begin", 1);
  3996. add_assoc_bool(return_value, "nautical_twilight_end", 1);
  3997. break;
  3998. default:
  3999. t2->sse = rise;
  4000. add_assoc_long(return_value, "nautical_twilight_begin", timelib_date_to_int(t2, &dummy));
  4001. t2->sse = set;
  4002. add_assoc_long(return_value, "nautical_twilight_end", timelib_date_to_int(t2, &dummy));
  4003. }
  4004. /* Get astronomical twilight */
  4005. rs = timelib_astro_rise_set_altitude(t, longitude, latitude, -18.0, 0, &ddummy, &ddummy, &rise, &set, &transit);
  4006. switch (rs) {
  4007. case -1: /* always below */
  4008. add_assoc_bool(return_value, "astronomical_twilight_begin", 0);
  4009. add_assoc_bool(return_value, "astronomical_twilight_end", 0);
  4010. break;
  4011. case 1: /* always above */
  4012. add_assoc_bool(return_value, "astronomical_twilight_begin", 1);
  4013. add_assoc_bool(return_value, "astronomical_twilight_end", 1);
  4014. break;
  4015. default:
  4016. t2->sse = rise;
  4017. add_assoc_long(return_value, "astronomical_twilight_begin", timelib_date_to_int(t2, &dummy));
  4018. t2->sse = set;
  4019. add_assoc_long(return_value, "astronomical_twilight_end", timelib_date_to_int(t2, &dummy));
  4020. }
  4021. timelib_time_dtor(t);
  4022. timelib_time_dtor(t2);
  4023. }
  4024. /* }}} */
  4025. static HashTable *date_object_get_gc_period(zend_object *object, zval **table, int *n) /* {{{ */
  4026. {
  4027. *table = NULL;
  4028. *n = 0;
  4029. return zend_std_get_properties(object);
  4030. } /* }}} */
  4031. static HashTable *date_object_get_properties_period(zend_object *object) /* {{{ */
  4032. {
  4033. HashTable *props;
  4034. zval zv;
  4035. php_period_obj *period_obj;
  4036. period_obj = php_period_obj_from_obj(object);
  4037. props = zend_std_get_properties(object);
  4038. if (!period_obj->start) {
  4039. return props;
  4040. }
  4041. if (period_obj->start) {
  4042. php_date_obj *date_obj;
  4043. object_init_ex(&zv, period_obj->start_ce);
  4044. date_obj = Z_PHPDATE_P(&zv);
  4045. date_obj->time = timelib_time_clone(period_obj->start);
  4046. } else {
  4047. ZVAL_NULL(&zv);
  4048. }
  4049. zend_hash_str_update(props, "start", sizeof("start")-1, &zv);
  4050. if (period_obj->current) {
  4051. php_date_obj *date_obj;
  4052. object_init_ex(&zv, period_obj->start_ce);
  4053. date_obj = Z_PHPDATE_P(&zv);
  4054. date_obj->time = timelib_time_clone(period_obj->current);
  4055. } else {
  4056. ZVAL_NULL(&zv);
  4057. }
  4058. zend_hash_str_update(props, "current", sizeof("current")-1, &zv);
  4059. if (period_obj->end) {
  4060. php_date_obj *date_obj;
  4061. object_init_ex(&zv, period_obj->start_ce);
  4062. date_obj = Z_PHPDATE_P(&zv);
  4063. date_obj->time = timelib_time_clone(period_obj->end);
  4064. } else {
  4065. ZVAL_NULL(&zv);
  4066. }
  4067. zend_hash_str_update(props, "end", sizeof("end")-1, &zv);
  4068. if (period_obj->interval) {
  4069. php_interval_obj *interval_obj;
  4070. object_init_ex(&zv, date_ce_interval);
  4071. interval_obj = Z_PHPINTERVAL_P(&zv);
  4072. interval_obj->diff = timelib_rel_time_clone(period_obj->interval);
  4073. interval_obj->initialized = 1;
  4074. } else {
  4075. ZVAL_NULL(&zv);
  4076. }
  4077. zend_hash_str_update(props, "interval", sizeof("interval")-1, &zv);
  4078. /* converted to larger type (int->long); must check when unserializing */
  4079. ZVAL_LONG(&zv, (zend_long) period_obj->recurrences);
  4080. zend_hash_str_update(props, "recurrences", sizeof("recurrences")-1, &zv);
  4081. ZVAL_BOOL(&zv, period_obj->include_start_date);
  4082. zend_hash_str_update(props, "include_start_date", sizeof("include_start_date")-1, &zv);
  4083. return props;
  4084. } /* }}} */
  4085. static int php_date_period_initialize_from_hash(php_period_obj *period_obj, HashTable *myht) /* {{{ */
  4086. {
  4087. zval *ht_entry;
  4088. /* this function does no rollback on error */
  4089. ht_entry = zend_hash_str_find(myht, "start", sizeof("start")-1);
  4090. if (ht_entry) {
  4091. if (Z_TYPE_P(ht_entry) == IS_OBJECT && instanceof_function(Z_OBJCE_P(ht_entry), date_ce_interface)) {
  4092. php_date_obj *date_obj;
  4093. date_obj = Z_PHPDATE_P(ht_entry);
  4094. period_obj->start = timelib_time_clone(date_obj->time);
  4095. period_obj->start_ce = Z_OBJCE_P(ht_entry);
  4096. } else if (Z_TYPE_P(ht_entry) != IS_NULL) {
  4097. return 0;
  4098. }
  4099. } else {
  4100. return 0;
  4101. }
  4102. ht_entry = zend_hash_str_find(myht, "end", sizeof("end")-1);
  4103. if (ht_entry) {
  4104. if (Z_TYPE_P(ht_entry) == IS_OBJECT && instanceof_function(Z_OBJCE_P(ht_entry), date_ce_interface)) {
  4105. php_date_obj *date_obj;
  4106. date_obj = Z_PHPDATE_P(ht_entry);
  4107. period_obj->end = timelib_time_clone(date_obj->time);
  4108. } else if (Z_TYPE_P(ht_entry) != IS_NULL) {
  4109. return 0;
  4110. }
  4111. } else {
  4112. return 0;
  4113. }
  4114. ht_entry = zend_hash_str_find(myht, "current", sizeof("current")-1);
  4115. if (ht_entry) {
  4116. if (Z_TYPE_P(ht_entry) == IS_OBJECT && instanceof_function(Z_OBJCE_P(ht_entry), date_ce_interface)) {
  4117. php_date_obj *date_obj;
  4118. date_obj = Z_PHPDATE_P(ht_entry);
  4119. period_obj->current = timelib_time_clone(date_obj->time);
  4120. } else if (Z_TYPE_P(ht_entry) != IS_NULL) {
  4121. return 0;
  4122. }
  4123. } else {
  4124. return 0;
  4125. }
  4126. ht_entry = zend_hash_str_find(myht, "interval", sizeof("interval")-1);
  4127. if (ht_entry) {
  4128. if (Z_TYPE_P(ht_entry) == IS_OBJECT && Z_OBJCE_P(ht_entry) == date_ce_interval) {
  4129. php_interval_obj *interval_obj;
  4130. interval_obj = Z_PHPINTERVAL_P(ht_entry);
  4131. period_obj->interval = timelib_rel_time_clone(interval_obj->diff);
  4132. } else { /* interval is required */
  4133. return 0;
  4134. }
  4135. } else {
  4136. return 0;
  4137. }
  4138. ht_entry = zend_hash_str_find(myht, "recurrences", sizeof("recurrences")-1);
  4139. if (ht_entry &&
  4140. Z_TYPE_P(ht_entry) == IS_LONG && Z_LVAL_P(ht_entry) >= 0 && Z_LVAL_P(ht_entry) <= INT_MAX) {
  4141. period_obj->recurrences = Z_LVAL_P(ht_entry);
  4142. } else {
  4143. return 0;
  4144. }
  4145. ht_entry = zend_hash_str_find(myht, "include_start_date", sizeof("include_start_date")-1);
  4146. if (ht_entry &&
  4147. (Z_TYPE_P(ht_entry) == IS_FALSE || Z_TYPE_P(ht_entry) == IS_TRUE)) {
  4148. period_obj->include_start_date = (Z_TYPE_P(ht_entry) == IS_TRUE);
  4149. } else {
  4150. return 0;
  4151. }
  4152. period_obj->initialized = 1;
  4153. return 1;
  4154. } /* }}} */
  4155. /* {{{ proto DatePeriod::__set_state(array array)
  4156. */
  4157. PHP_METHOD(DatePeriod, __set_state)
  4158. {
  4159. php_period_obj *period_obj;
  4160. zval *array;
  4161. HashTable *myht;
  4162. ZEND_PARSE_PARAMETERS_START(1, 1)
  4163. Z_PARAM_ARRAY(array)
  4164. ZEND_PARSE_PARAMETERS_END();
  4165. myht = Z_ARRVAL_P(array);
  4166. object_init_ex(return_value, date_ce_period);
  4167. period_obj = Z_PHPPERIOD_P(return_value);
  4168. if (!php_date_period_initialize_from_hash(period_obj, myht)) {
  4169. zend_throw_error(NULL, "Invalid serialization data for DatePeriod object");
  4170. }
  4171. }
  4172. /* }}} */
  4173. /* {{{ proto DatePeriod::__wakeup()
  4174. */
  4175. PHP_METHOD(DatePeriod, __wakeup)
  4176. {
  4177. zval *object = ZEND_THIS;
  4178. php_period_obj *period_obj;
  4179. HashTable *myht;
  4180. ZEND_PARSE_PARAMETERS_NONE();
  4181. period_obj = Z_PHPPERIOD_P(object);
  4182. myht = Z_OBJPROP_P(object);
  4183. if (!php_date_period_initialize_from_hash(period_obj, myht)) {
  4184. zend_throw_error(NULL, "Invalid serialization data for DatePeriod object");
  4185. }
  4186. }
  4187. /* }}} */
  4188. /* {{{ date_period_is_magic_property
  4189. * Common for date_period_read_property() and date_period_write_property() functions
  4190. */
  4191. static int date_period_is_magic_property(zend_string *name)
  4192. {
  4193. if (zend_string_equals_literal(name, "recurrences")
  4194. || zend_string_equals_literal(name, "include_start_date")
  4195. || zend_string_equals_literal(name, "start")
  4196. || zend_string_equals_literal(name, "current")
  4197. || zend_string_equals_literal(name, "end")
  4198. || zend_string_equals_literal(name, "interval")
  4199. ) {
  4200. return 1;
  4201. }
  4202. return 0;
  4203. }
  4204. /* }}} */
  4205. /* {{{ date_period_read_property */
  4206. static zval *date_period_read_property(zend_object *object, zend_string *name, int type, void **cache_slot, zval *rv)
  4207. {
  4208. if (type != BP_VAR_IS && type != BP_VAR_R) {
  4209. if (date_period_is_magic_property(name)) {
  4210. zend_throw_error(NULL, "Retrieval of DatePeriod->%s for modification is unsupported", ZSTR_VAL(name));
  4211. return &EG(uninitialized_zval);
  4212. }
  4213. }
  4214. object->handlers->get_properties(object); /* build properties hash table */
  4215. return zend_std_read_property(object, name, type, cache_slot, rv);
  4216. }
  4217. /* }}} */
  4218. /* {{{ date_period_write_property */
  4219. static zval *date_period_write_property(zend_object *object, zend_string *name, zval *value, void **cache_slot)
  4220. {
  4221. if (date_period_is_magic_property(name)) {
  4222. zend_throw_error(NULL, "Writing to DatePeriod->%s is unsupported", ZSTR_VAL(name));
  4223. return value;
  4224. }
  4225. return zend_std_write_property(object, name, value, cache_slot);
  4226. }
  4227. /* }}} */
  4228. /* {{{ date_period_get_property_ptr_ptr */
  4229. static zval *date_period_get_property_ptr_ptr(zend_object *object, zend_string *name, int type, void **cache_slot)
  4230. {
  4231. if (date_period_is_magic_property(name)) {
  4232. zend_throw_error(NULL, "Retrieval of DatePeriod->%s for modification is unsupported", ZSTR_VAL(name));
  4233. return &EG(error_zval);
  4234. }
  4235. return zend_std_get_property_ptr_ptr(object, name, type, cache_slot);
  4236. }
  4237. /* }}} */