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