xref: /php-src/ext/date/php_date.h (revision 9f586f6c)
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    | https://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 
17 #ifndef PHP_DATE_H
18 #define PHP_DATE_H
19 
20 #include "lib/timelib.h"
21 #include "Zend/zend_hash.h"
22 
23 /* Same as SIZEOF_ZEND_LONG but using TIMELIB_LONG_MAX/MIN */
24 #if TIMELIB_LONG_MAX == INT32_MAX
25 # define PHP_DATE_SIZEOF_LONG 4
26 #elif TIMELIB_LONG_MAX == INT64_MAX
27 # define PHP_DATE_SIZEOF_LONG 8
28 #else
29 # error "Unknown TIMELIB LONG SIZE"
30 #endif
31 
32 /* Same as ZEND_DOUBLE_FITS_LONG but using TIMELIB_LONG_MAX/MIN */
33 #if PHP_DATE_SIZEOF_LONG == 4
34 # define PHP_DATE_DOUBLE_FITS_LONG(d) (!((d) > (double)TIMELIB_LONG_MAX || (d) < (double)TIMELIB_LONG_MIN))
35 #elif PHP_DATE_SIZEOF_LONG == 8
36   /* >= as (double)TIMELIB_LONG_MAX is outside signed range */
37 # define PHP_DATE_DOUBLE_FITS_LONG(d) (!((d) >= (double)TIMELIB_LONG_MAX || (d) < (double)TIMELIB_LONG_MIN))
38 #endif
39 
40 #include "php_version.h"
41 #define PHP_DATE_VERSION PHP_VERSION
42 
43 extern zend_module_entry date_module_entry;
44 #define phpext_date_ptr &date_module_entry
45 
46 PHP_RINIT_FUNCTION(date);
47 PHP_RSHUTDOWN_FUNCTION(date);
48 PHP_MINIT_FUNCTION(date);
49 PHP_MSHUTDOWN_FUNCTION(date);
50 PHP_MINFO_FUNCTION(date);
51 ZEND_MODULE_POST_ZEND_DEACTIVATE_D(date);
52 
53 typedef struct _php_date_obj php_date_obj;
54 typedef struct _php_timezone_obj php_timezone_obj;
55 typedef struct _php_interval_obj php_interval_obj;
56 typedef struct _php_period_obj php_period_obj;
57 
58 struct _php_date_obj {
59 	timelib_time *time;
60 	zend_object   std;
61 };
62 
php_date_obj_from_obj(zend_object * obj)63 static inline php_date_obj *php_date_obj_from_obj(zend_object *obj) {
64 	return (php_date_obj*)((char*)(obj) - XtOffsetOf(php_date_obj, std));
65 }
66 
67 #define Z_PHPDATE_P(zv)  php_date_obj_from_obj(Z_OBJ_P((zv)))
68 
69 struct _php_timezone_obj {
70 	bool            initialized;
71 	int             type;
72 	union {
73 		timelib_tzinfo   *tz;         /* TIMELIB_ZONETYPE_ID */
74 		timelib_sll       utc_offset; /* TIMELIB_ZONETYPE_OFFSET */
75 		timelib_abbr_info z;          /* TIMELIB_ZONETYPE_ABBR */
76 	} tzi;
77 	zend_object std;
78 };
79 
php_timezone_obj_from_obj(zend_object * obj)80 static inline php_timezone_obj *php_timezone_obj_from_obj(zend_object *obj) {
81 	return (php_timezone_obj*)((char*)(obj) - XtOffsetOf(php_timezone_obj, std));
82 }
83 
84 #define Z_PHPTIMEZONE_P(zv)  php_timezone_obj_from_obj(Z_OBJ_P((zv)))
85 
86 #define PHP_DATE_CIVIL   1
87 #define PHP_DATE_WALL    2
88 
89 struct _php_interval_obj {
90 	timelib_rel_time *diff;
91 	int               civil_or_wall;
92 	bool              from_string;
93 	zend_string      *date_string;
94 	bool              initialized;
95 	zend_object       std;
96 };
97 
php_interval_obj_from_obj(zend_object * obj)98 static inline php_interval_obj *php_interval_obj_from_obj(zend_object *obj) {
99 	return (php_interval_obj*)((char*)(obj) - XtOffsetOf(php_interval_obj, std));
100 }
101 
102 #define Z_PHPINTERVAL_P(zv)  php_interval_obj_from_obj(Z_OBJ_P((zv)))
103 
104 struct _php_period_obj {
105 	timelib_time     *start;
106 	zend_class_entry *start_ce;
107 	timelib_time     *current;
108 	timelib_time     *end;
109 	timelib_rel_time *interval;
110 	int               recurrences;
111 	bool              initialized;
112 	bool              include_start_date;
113 	bool              include_end_date;
114 	zend_object       std;
115 };
116 
php_period_obj_from_obj(zend_object * obj)117 static inline php_period_obj *php_period_obj_from_obj(zend_object *obj) {
118 	return (php_period_obj*)((char*)(obj) - XtOffsetOf(php_period_obj, std));
119 }
120 
121 #define Z_PHPPERIOD_P(zv)  php_period_obj_from_obj(Z_OBJ_P((zv)))
122 
123 ZEND_BEGIN_MODULE_GLOBALS(date)
124 	char                    *default_timezone;
125 	char                    *timezone;
126 	HashTable               *tzcache;
127 	timelib_error_container *last_errors;
128 ZEND_END_MODULE_GLOBALS(date)
129 
130 #define DATEG(v) ZEND_MODULE_GLOBALS_ACCESSOR(date, v)
131 
132 PHPAPI time_t php_time(void);
133 
134 /* Backwards compatibility wrapper */
135 PHPAPI zend_long php_parse_date(const char *string, zend_long *now);
136 PHPAPI void php_mktime(INTERNAL_FUNCTION_PARAMETERS, bool gmt);
137 PHPAPI int php_idate(char format, time_t ts, bool localtime);
138 
139 #define _php_strftime php_strftime
140 
141 PHPAPI void php_strftime(INTERNAL_FUNCTION_PARAMETERS, bool gm);
142 PHPAPI zend_string *php_format_date(const char *format, size_t format_len, time_t ts, bool localtime);
143 PHPAPI zend_string *php_format_date_obj(const char *format, size_t format_len, php_date_obj *date_obj);
144 
145 /* Mechanism to set new TZ database */
146 PHPAPI void php_date_set_tzdb(timelib_tzdb *tzdb);
147 PHPAPI timelib_tzinfo *get_timezone_info(void);
148 
149 /* Grabbing CE's so that other exts can use the date objects too */
150 PHPAPI zend_class_entry *php_date_get_date_ce(void);
151 PHPAPI zend_class_entry *php_date_get_immutable_ce(void);
152 PHPAPI zend_class_entry *php_date_get_interface_ce(void);
153 PHPAPI zend_class_entry *php_date_get_timezone_ce(void);
154 PHPAPI zend_class_entry *php_date_get_interval_ce(void);
155 PHPAPI zend_class_entry *php_date_get_period_ce(void);
156 
157 /* Functions for creating DateTime objects, and initializing them from a string */
158 #define PHP_DATE_INIT_CTOR   0x01
159 #define PHP_DATE_INIT_FORMAT 0x02
160 
161 PHPAPI zval *php_date_instantiate(zend_class_entry *pce, zval *object);
162 PHPAPI bool php_date_initialize(php_date_obj *dateobj, const char *time_str, size_t time_str_len, const char *format, zval *timezone_object, int flags);
163 PHPAPI void php_date_initialize_from_ts_long(php_date_obj *dateobj, zend_long sec, int usec);
164 PHPAPI bool php_date_initialize_from_ts_double(php_date_obj *dateobj, double ts);
165 
166 #endif /* PHP_DATE_H */
167