xref: /PHP-8.0/ext/date/php_date.h (revision 26b1572d)
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 
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 	int             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 struct _php_interval_obj {
70 	timelib_rel_time *diff;
71 	int               initialized;
72 	zend_object       std;
73 };
74 
php_interval_obj_from_obj(zend_object * obj)75 static inline php_interval_obj *php_interval_obj_from_obj(zend_object *obj) {
76 	return (php_interval_obj*)((char*)(obj) - XtOffsetOf(php_interval_obj, std));
77 }
78 
79 #define Z_PHPINTERVAL_P(zv)  php_interval_obj_from_obj(Z_OBJ_P((zv)))
80 
81 struct _php_period_obj {
82 	timelib_time     *start;
83 	zend_class_entry *start_ce;
84 	timelib_time     *current;
85 	timelib_time     *end;
86 	timelib_rel_time *interval;
87 	int               recurrences;
88 	int               initialized;
89 	int               include_start_date;
90 	zend_object       std;
91 };
92 
php_period_obj_from_obj(zend_object * obj)93 static inline php_period_obj *php_period_obj_from_obj(zend_object *obj) {
94 	return (php_period_obj*)((char*)(obj) - XtOffsetOf(php_period_obj, std));
95 }
96 
97 #define Z_PHPPERIOD_P(zv)  php_period_obj_from_obj(Z_OBJ_P((zv)))
98 
99 ZEND_BEGIN_MODULE_GLOBALS(date)
100 	char                    *default_timezone;
101 	char                    *timezone;
102 	HashTable               *tzcache;
103 	timelib_error_container *last_errors;
104 	int                     timezone_valid;
105 ZEND_END_MODULE_GLOBALS(date)
106 
107 #define DATEG(v) ZEND_MODULE_GLOBALS_ACCESSOR(date, v)
108 
109 PHPAPI time_t php_time(void);
110 
111 /* Backwards compatibility wrapper */
112 PHPAPI zend_long php_parse_date(const char *string, zend_long *now);
113 PHPAPI void php_mktime(INTERNAL_FUNCTION_PARAMETERS, int gmt);
114 PHPAPI int php_idate(char format, time_t ts, int localtime);
115 
116 #define _php_strftime php_strftime
117 
118 PHPAPI void php_strftime(INTERNAL_FUNCTION_PARAMETERS, int gm);
119 PHPAPI zend_string *php_format_date(const char *format, size_t format_len, time_t ts, int localtime);
120 
121 /* Mechanism to set new TZ database */
122 PHPAPI void php_date_set_tzdb(timelib_tzdb *tzdb);
123 PHPAPI timelib_tzinfo *get_timezone_info(void);
124 
125 /* Grabbing CE's so that other exts can use the date objects too */
126 PHPAPI zend_class_entry *php_date_get_date_ce(void);
127 PHPAPI zend_class_entry *php_date_get_immutable_ce(void);
128 PHPAPI zend_class_entry *php_date_get_interface_ce(void);
129 PHPAPI zend_class_entry *php_date_get_timezone_ce(void);
130 PHPAPI zend_class_entry *php_date_get_interval_ce(void);
131 PHPAPI zend_class_entry *php_date_get_period_ce(void);
132 
133 /* Functions for creating DateTime objects, and initializing them from a string */
134 #define PHP_DATE_INIT_CTOR   0x01
135 #define PHP_DATE_INIT_FORMAT 0x02
136 
137 PHPAPI zval *php_date_instantiate(zend_class_entry *pce, zval *object);
138 PHPAPI int php_date_initialize(php_date_obj *dateobj, const char *time_str, size_t time_str_len, const char *format, zval *timezone_object, int flags);
139 
140 
141 #endif /* PHP_DATE_H */
142