xref: /PHP-7.1/ext/date/lib/timelib_structs.h (revision 7121b8d0)
1 /*
2  * The MIT License (MIT)
3  *
4  * Copyright (c) 2015 Derick Rethans
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22  * THE SOFTWARE.
23  */
24 
25 #ifndef __TIMELIB_STRUCTS_H__
26 #define __TIMELIB_STRUCTS_H__
27 
28 #ifdef HAVE_TIMELIB_CONFIG_H
29 # include "timelib_config.h"
30 #endif
31 
32 #ifndef TIMELIB_OMIT_STDINT
33 
34 #ifdef HAVE_SYS_TYPES_H
35 #include <sys/types.h>
36 #endif
37 
38 #if defined(HAVE_INTTYPES_H)
39 #include <inttypes.h>
40 #elif defined(HAVE_STDINT_H)
41 #include <stdint.h>
42 #endif
43 
44 # ifndef HAVE_INT32_T
45 #  if SIZEOF_INT == 4
46 typedef int int32_t;
47 #  elif SIZEOF_LONG == 4
48 typedef long int int32_t;
49 #  endif
50 # endif
51 
52 # ifndef HAVE_UINT32_T
53 #  if SIZEOF_INT == 4
54 typedef unsigned int uint32_t;
55 #  elif SIZEOF_LONG == 4
56 typedef unsigned long int uint32_t;
57 #  endif
58 # endif
59 
60 #ifdef _WIN32
61 # if _MSC_VER >= 1600
62 # include <stdint.h>
63 # endif
64 # ifndef SIZEOF_INT
65 #  define SIZEOF_INT 4
66 # endif
67 # ifndef SIZEOF_LONG
68 #  define SIZEOF_LONG 4
69 # endif
70 # ifndef int32_t
71 typedef __int32           int32_t;
72 # endif
73 # ifndef uint32_t
74 typedef unsigned __int32  uint32_t;
75 # endif
76 # ifndef int64_t
77 typedef __int64           int64_t;
78 # endif
79 # ifndef uint64_t
80 typedef unsigned __int64  uint64_t;
81 # endif
82 # ifndef PRId32
83 #  define PRId32       "I32d"
84 # endif
85 # ifndef PRIu32
86 #  define PRIu32       "I32u"
87 # endif
88 # ifndef PRId64
89 #  define PRId64       "I64d"
90 # endif
91 # ifndef PRIu64
92 #  define PRIu64       "I64u"
93 # endif
94 # ifndef INT32_MAX
95 #define INT32_MAX    _I32_MAX
96 # endif
97 # ifndef INT32_MIN
98 #define INT32_MIN    ((int32_t)_I32_MIN)
99 # endif
100 # ifndef UINT32_MAX
101 #define UINT32_MAX   _UI32_MAX
102 # endif
103 # ifndef INT64_MIN
104 #define INT64_MIN    ((int64_t)_I64_MIN)
105 # endif
106 # ifndef INT64_MAX
107 #define INT64_MAX    _I64_MAX
108 # endif
109 # ifndef UINT64_MAX
110 #define UINT64_MAX   _UI64_MAX
111 # endif
112 #endif
113 
114 #endif /* TIMELIB_OMIT_STDINT */
115 
116 #include <stdio.h>
117 
118 #ifdef HAVE_STDLIB_H
119 #include <stdlib.h>
120 #endif
121 
122 #ifdef HAVE_STRING_H
123 #include <string.h>
124 #else
125 #include <strings.h>
126 #endif
127 
128 #if (defined(__x86_64__) || defined(__LP64__) || defined(_LP64) || defined(_WIN64)) && !defined(TIMELIB_FORCE_LONG32)
129 typedef int64_t timelib_long;
130 typedef uint64_t timelib_ulong;
131 # define TIMELIB_LONG_MAX INT64_MAX
132 # define TIMELIB_LONG_MIN INT64_MIN
133 # define TIMELIB_ULONG_MAX UINT64_MAX
134 # define TIMELIB_LONG_FMT "%" PRId64
135 # define TIMELIB_ULONG_FMT "%" PRIu64
136 #else
137 typedef int32_t timelib_long;
138 typedef uint32_t timelib_ulong;
139 # define TIMELIB_LONG_MAX INT32_MAX
140 # define TIMELIB_LONG_MIN INT32_MIN
141 # define TIMELIB_ULONG_MAX UINT32_MAX
142 # define TIMELIB_LONG_FMT "%" PRId32
143 # define TIMELIB_ULONG_FMT "%" PRIu32
144 #endif
145 
146 #if defined(_MSC_VER)
147 typedef uint64_t timelib_ull;
148 typedef int64_t timelib_sll;
149 # define TIMELIB_LL_CONST(n) n ## i64
150 #else
151 typedef unsigned long long timelib_ull;
152 typedef signed long long timelib_sll;
153 # define TIMELIB_LL_CONST(n) n ## ll
154 #endif
155 
156 typedef struct ttinfo
157 {
158 	int32_t      offset;
159 	int          isdst;
160 	unsigned int abbr_idx;
161 
162 	unsigned int isstdcnt;
163 	unsigned int isgmtcnt;
164 } ttinfo;
165 
166 typedef struct tlinfo
167 {
168 	int32_t  trans;
169 	int32_t  offset;
170 } tlinfo;
171 
172 typedef struct tlocinfo
173 {
174 	char country_code[3];
175 	double latitude;
176 	double longitude;
177 	char *comments;
178 } tlocinfo;
179 
180 typedef struct timelib_tzinfo
181 {
182 	char    *name;
183 	struct {
184 		uint32_t ttisgmtcnt;
185 		uint32_t ttisstdcnt;
186 		uint32_t leapcnt;
187 		uint32_t timecnt;
188 		uint32_t typecnt;
189 		uint32_t charcnt;
190 	} bit32;
191 	struct {
192 		uint64_t ttisgmtcnt;
193 		uint64_t ttisstdcnt;
194 		uint64_t leapcnt;
195 		uint64_t timecnt;
196 		uint64_t typecnt;
197 		uint64_t charcnt;
198 	} bit64;
199 
200 	int32_t *trans;
201 	unsigned char *trans_idx;
202 
203 	ttinfo  *type;
204 	char    *timezone_abbr;
205 
206 	tlinfo  *leap_times;
207 	unsigned char bc;
208 	tlocinfo location;
209 } timelib_tzinfo;
210 
211 typedef struct timelib_special {
212 	unsigned int type;
213 	timelib_sll amount;
214 } timelib_special;
215 
216 typedef struct timelib_rel_time {
217 	timelib_sll y, m, d; /* Years, Months and Days */
218 	timelib_sll h, i, s; /* Hours, mInutes and Seconds */
219 	double      f;       /* Fraction */
220 
221 	int weekday; /* Stores the day in 'next monday' */
222 	int weekday_behavior; /* 0: the current day should *not* be counted when advancing forwards; 1: the current day *should* be counted */
223 
224 	int first_last_day_of;
225 	int invert; /* Whether the difference should be inverted */
226 	timelib_sll days; /* Contains the number of *days*, instead of Y-M-D differences */
227 
228 	timelib_special  special;
229 	unsigned int   have_weekday_relative, have_special_relative;
230 } timelib_rel_time;
231 
232 typedef struct timelib_time_offset {
233 	int32_t      offset;
234 	unsigned int leap_secs;
235 	unsigned int is_dst;
236 	char        *abbr;
237 	timelib_sll  transistion_time;
238 } timelib_time_offset;
239 
240 typedef struct timelib_time {
241 	timelib_sll      y, m, d;     /* Year, Month, Day */
242 	timelib_sll      h, i, s;     /* Hour, mInute, Second */
243 	double           f;           /* Fraction */
244 	int              z;           /* GMT offset in minutes */
245 	char            *tz_abbr;     /* Timezone abbreviation (display only) */
246 	timelib_tzinfo  *tz_info;     /* Timezone structure */
247 	signed int       dst;         /* Flag if we were parsing a DST zone */
248 	timelib_rel_time relative;
249 
250 	timelib_sll      sse;         /* Seconds since epoch */
251 
252 	unsigned int   have_time, have_date, have_zone, have_relative, have_weeknr_day;
253 
254 	unsigned int   sse_uptodate; /* !0 if the sse member is up to date with the date/time members */
255 	unsigned int   tim_uptodate; /* !0 if the date/time members are up to date with the sse member */
256 	unsigned int   is_localtime; /*  1 if the current struct represents localtime, 0 if it is in GMT */
257 	unsigned int   zone_type;    /*  1 time offset,
258 	                              *  3 TimeZone identifier,
259 	                              *  2 TimeZone abbreviation */
260 } timelib_time;
261 
262 typedef struct timelib_abbr_info {
263 	timelib_sll  utc_offset;
264 	char        *abbr;
265 	int          dst;
266 } timelib_abbr_info;
267 
268 typedef struct timelib_error_message {
269 	int         position;
270 	char        character;
271 	char       *message;
272 } timelib_error_message;
273 
274 typedef struct timelib_error_container {
275 	struct timelib_error_message *error_messages;
276 	struct timelib_error_message *warning_messages;
277 	int                           error_count;
278 	int                           warning_count;
279 } timelib_error_container;
280 
281 typedef struct _timelib_tz_lookup_table {
282 	char       *name;
283 	int         type;
284 	float       gmtoffset;
285 	char       *full_tz_name;
286 } timelib_tz_lookup_table;
287 
288 typedef struct _timelib_tzdb_index_entry {
289 	char *id;
290 	unsigned int pos;
291 } timelib_tzdb_index_entry;
292 
293 typedef struct _timelib_tzdb {
294 	char                           *version;
295 	int                             index_size;
296 	const timelib_tzdb_index_entry *index;
297 	const unsigned char            *data;
298 } timelib_tzdb;
299 
300 #define TIMELIB_ZONETYPE_OFFSET 1
301 #define TIMELIB_ZONETYPE_ABBR   2
302 #define TIMELIB_ZONETYPE_ID     3
303 
304 #define SECS_PER_ERA   TIMELIB_LL_CONST(12622780800)
305 #define SECS_PER_DAY   86400
306 #define DAYS_PER_YEAR    365
307 #define DAYS_PER_LYEAR   366
308 /* 400*365 days + 97 leap days */
309 #define DAYS_PER_LYEAR_PERIOD 146097
310 #define YEARS_PER_LYEAR_PERIOD 400
311 
312 #define timelib_is_leap(y) ((y) % 4 == 0 && ((y) % 100 != 0 || (y) % 400 == 0))
313 
314 #define TIMELIB_DEBUG(s)  if (0) { s }
315 
316 #endif
317