xref: /PHP-7.3/ext/date/php_date.h (revision 599b94ff)
1 /*
2    +----------------------------------------------------------------------+
3    | PHP Version 7                                                        |
4    +----------------------------------------------------------------------+
5    | Copyright (c) 1997-2018 The PHP Group                                |
6    +----------------------------------------------------------------------+
7    | This source file is subject to version 3.01 of the PHP license,      |
8    | that is bundled with this package in the file LICENSE, and is        |
9    | available through the world-wide-web at the following url:           |
10    | http://www.php.net/license/3_01.txt                                  |
11    | If you did not receive a copy of the PHP license and are unable to   |
12    | obtain it through the world-wide-web, please send a note to          |
13    | license@php.net so we can mail you a copy immediately.               |
14    +----------------------------------------------------------------------+
15    | Authors: Derick Rethans <derick@derickrethans.nl>                    |
16    +----------------------------------------------------------------------+
17 */
18 
19 #ifndef PHP_DATE_H
20 #define PHP_DATE_H
21 
22 #include "lib/timelib.h"
23 #include "Zend/zend_hash.h"
24 
25 #include "php_version.h"
26 #define PHP_DATE_VERSION PHP_VERSION
27 
28 extern zend_module_entry date_module_entry;
29 #define phpext_date_ptr &date_module_entry
30 
31 PHP_FUNCTION(date);
32 PHP_FUNCTION(idate);
33 PHP_FUNCTION(gmdate);
34 PHP_FUNCTION(strtotime);
35 
36 PHP_FUNCTION(mktime);
37 PHP_FUNCTION(gmmktime);
38 
39 PHP_FUNCTION(checkdate);
40 
41 #ifdef HAVE_STRFTIME
42 PHP_FUNCTION(strftime);
43 PHP_FUNCTION(gmstrftime);
44 #endif
45 
46 PHP_FUNCTION(time);
47 PHP_FUNCTION(localtime);
48 PHP_FUNCTION(getdate);
49 
50 /* Advanced Interface */
51 PHP_METHOD(DateTime, __construct);
52 PHP_METHOD(DateTime, __wakeup);
53 PHP_METHOD(DateTime, __set_state);
54 PHP_METHOD(DateTime, createFromImmutable);
55 PHP_FUNCTION(date_create);
56 PHP_FUNCTION(date_create_immutable);
57 PHP_FUNCTION(date_create_from_format);
58 PHP_FUNCTION(date_create_immutable_from_format);
59 PHP_FUNCTION(date_parse);
60 PHP_FUNCTION(date_parse_from_format);
61 PHP_FUNCTION(date_get_last_errors);
62 PHP_FUNCTION(date_format);
63 PHP_FUNCTION(date_modify);
64 PHP_FUNCTION(date_add);
65 PHP_FUNCTION(date_sub);
66 PHP_FUNCTION(date_timezone_get);
67 PHP_FUNCTION(date_timezone_set);
68 PHP_FUNCTION(date_offset_get);
69 PHP_FUNCTION(date_diff);
70 
71 PHP_FUNCTION(date_time_set);
72 PHP_FUNCTION(date_date_set);
73 PHP_FUNCTION(date_isodate_set);
74 PHP_FUNCTION(date_timestamp_set);
75 PHP_FUNCTION(date_timestamp_get);
76 
77 PHP_METHOD(DateTimeImmutable, __construct);
78 PHP_METHOD(DateTimeImmutable, __set_state);
79 PHP_METHOD(DateTimeImmutable, modify);
80 PHP_METHOD(DateTimeImmutable, add);
81 PHP_METHOD(DateTimeImmutable, sub);
82 PHP_METHOD(DateTimeImmutable, setTimezone);
83 PHP_METHOD(DateTimeImmutable, setTime);
84 PHP_METHOD(DateTimeImmutable, setDate);
85 PHP_METHOD(DateTimeImmutable, setISODate);
86 PHP_METHOD(DateTimeImmutable, setTimestamp);
87 PHP_METHOD(DateTimeImmutable, createFromMutable);
88 
89 PHP_METHOD(DateTimeZone, __construct);
90 PHP_METHOD(DateTimeZone, __wakeup);
91 PHP_METHOD(DateTimeZone, __set_state);
92 PHP_FUNCTION(timezone_open);
93 PHP_FUNCTION(timezone_name_get);
94 PHP_FUNCTION(timezone_name_from_abbr);
95 PHP_FUNCTION(timezone_offset_get);
96 PHP_FUNCTION(timezone_transitions_get);
97 PHP_FUNCTION(timezone_location_get);
98 PHP_FUNCTION(timezone_identifiers_list);
99 PHP_FUNCTION(timezone_abbreviations_list);
100 PHP_FUNCTION(timezone_version_get);
101 
102 PHP_METHOD(DateInterval, __construct);
103 PHP_METHOD(DateInterval, __wakeup);
104 PHP_METHOD(DateInterval, __set_state);
105 PHP_FUNCTION(date_interval_format);
106 PHP_FUNCTION(date_interval_create_from_date_string);
107 
108 PHP_METHOD(DatePeriod, __construct);
109 PHP_METHOD(DatePeriod, __wakeup);
110 PHP_METHOD(DatePeriod, __set_state);
111 PHP_METHOD(DatePeriod, getStartDate);
112 PHP_METHOD(DatePeriod, getEndDate);
113 PHP_METHOD(DatePeriod, getDateInterval);
114 PHP_METHOD(DatePeriod, getRecurrences);
115 
116 /* Options and Configuration */
117 PHP_FUNCTION(date_default_timezone_set);
118 PHP_FUNCTION(date_default_timezone_get);
119 
120 /* Astro functions */
121 PHP_FUNCTION(date_sunrise);
122 PHP_FUNCTION(date_sunset);
123 PHP_FUNCTION(date_sun_info);
124 
125 PHP_RINIT_FUNCTION(date);
126 PHP_RSHUTDOWN_FUNCTION(date);
127 PHP_MINIT_FUNCTION(date);
128 PHP_MSHUTDOWN_FUNCTION(date);
129 PHP_MINFO_FUNCTION(date);
130 
131 typedef struct _php_date_obj php_date_obj;
132 typedef struct _php_timezone_obj php_timezone_obj;
133 typedef struct _php_interval_obj php_interval_obj;
134 typedef struct _php_period_obj php_period_obj;
135 
136 struct _php_date_obj {
137 	timelib_time *time;
138 	HashTable    *props;
139 	zend_object   std;
140 };
141 
php_date_obj_from_obj(zend_object * obj)142 static inline php_date_obj *php_date_obj_from_obj(zend_object *obj) {
143 	return (php_date_obj*)((char*)(obj) - XtOffsetOf(php_date_obj, std));
144 }
145 
146 #define Z_PHPDATE_P(zv)  php_date_obj_from_obj(Z_OBJ_P((zv)))
147 
148 struct _php_timezone_obj {
149 	int             initialized;
150 	int             type;
151 	union {
152 		timelib_tzinfo   *tz;         /* TIMELIB_ZONETYPE_ID */
153 		timelib_sll       utc_offset; /* TIMELIB_ZONETYPE_OFFSET */
154 		timelib_abbr_info z;          /* TIMELIB_ZONETYPE_ABBR */
155 	} tzi;
156 	HashTable *props;
157 	zend_object std;
158 };
159 
php_timezone_obj_from_obj(zend_object * obj)160 static inline php_timezone_obj *php_timezone_obj_from_obj(zend_object *obj) {
161 	return (php_timezone_obj*)((char*)(obj) - XtOffsetOf(php_timezone_obj, std));
162 }
163 
164 #define Z_PHPTIMEZONE_P(zv)  php_timezone_obj_from_obj(Z_OBJ_P((zv)))
165 
166 struct _php_interval_obj {
167 	timelib_rel_time *diff;
168 	HashTable        *props;
169 	int               initialized;
170 	zend_object       std;
171 };
172 
php_interval_obj_from_obj(zend_object * obj)173 static inline php_interval_obj *php_interval_obj_from_obj(zend_object *obj) {
174 	return (php_interval_obj*)((char*)(obj) - XtOffsetOf(php_interval_obj, std));
175 }
176 
177 #define Z_PHPINTERVAL_P(zv)  php_interval_obj_from_obj(Z_OBJ_P((zv)))
178 
179 struct _php_period_obj {
180 	timelib_time     *start;
181 	zend_class_entry *start_ce;
182 	timelib_time     *current;
183 	timelib_time     *end;
184 	timelib_rel_time *interval;
185 	int               recurrences;
186 	int               initialized;
187 	int               include_start_date;
188 	zend_object       std;
189 };
190 
php_period_obj_from_obj(zend_object * obj)191 static inline php_period_obj *php_period_obj_from_obj(zend_object *obj) {
192 	return (php_period_obj*)((char*)(obj) - XtOffsetOf(php_period_obj, std));
193 }
194 
195 #define Z_PHPPERIOD_P(zv)  php_period_obj_from_obj(Z_OBJ_P((zv)))
196 
197 ZEND_BEGIN_MODULE_GLOBALS(date)
198 	char                    *default_timezone;
199 	char                    *timezone;
200 	HashTable               *tzcache;
201 	timelib_error_container *last_errors;
202 	int                     timezone_valid;
203 ZEND_END_MODULE_GLOBALS(date)
204 
205 #define DATEG(v) ZEND_MODULE_GLOBALS_ACCESSOR(date, v)
206 
207 PHPAPI time_t php_time();
208 
209 /* Backwards compatibility wrapper */
210 PHPAPI zend_long php_parse_date(char *string, zend_long *now);
211 PHPAPI void php_mktime(INTERNAL_FUNCTION_PARAMETERS, int gmt);
212 PHPAPI int php_idate(char format, time_t ts, int localtime);
213 #if HAVE_STRFTIME
214 #define _php_strftime php_strftime
215 PHPAPI void php_strftime(INTERNAL_FUNCTION_PARAMETERS, int gm);
216 #endif
217 PHPAPI zend_string *php_format_date(char *format, size_t format_len, time_t ts, int localtime);
218 
219 /* Mechanism to set new TZ database */
220 PHPAPI void php_date_set_tzdb(timelib_tzdb *tzdb);
221 PHPAPI timelib_tzinfo *get_timezone_info(void);
222 
223 /* Grabbing CE's so that other exts can use the date objects too */
224 PHPAPI zend_class_entry *php_date_get_date_ce(void);
225 PHPAPI zend_class_entry *php_date_get_immutable_ce(void);
226 PHPAPI zend_class_entry *php_date_get_interface_ce(void);
227 PHPAPI zend_class_entry *php_date_get_timezone_ce(void);
228 PHPAPI zend_class_entry *php_date_get_interval_ce(void);
229 PHPAPI zend_class_entry *php_date_get_period_ce(void);
230 
231 /* Functions for creating DateTime objects, and initializing them from a string */
232 PHPAPI zval *php_date_instantiate(zend_class_entry *pce, zval *object);
233 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);
234 
235 
236 #endif /* PHP_DATE_H */
237