xref: /PHP-5.6/ext/date/lib/timelib_structs.h (revision 11102e26)
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 
220 	int weekday; /* Stores the day in 'next monday' */
221 	int weekday_behavior; /* 0: the current day should *not* be counted when advancing forwards; 1: the current day *should* be counted */
222 
223 	int first_last_day_of;
224 	int invert; /* Whether the difference should be inverted */
225 	timelib_sll days; /* Contains the number of *days*, instead of Y-M-D differences */
226 
227 	timelib_special  special;
228 	unsigned int   have_weekday_relative, have_special_relative;
229 } timelib_rel_time;
230 
231 typedef struct timelib_time_offset {
232 	int32_t      offset;
233 	unsigned int leap_secs;
234 	unsigned int is_dst;
235 	char        *abbr;
236 	timelib_sll  transistion_time;
237 } timelib_time_offset;
238 
239 typedef struct timelib_time {
240 	timelib_sll      y, m, d;     /* Year, Month, Day */
241 	timelib_sll      h, i, s;     /* Hour, mInute, Second */
242 	double           f;           /* Fraction */
243 	int              z;           /* GMT offset in minutes */
244 	char            *tz_abbr;     /* Timezone abbreviation (display only) */
245 	timelib_tzinfo  *tz_info;     /* Timezone structure */
246 	signed int       dst;         /* Flag if we were parsing a DST zone */
247 	timelib_rel_time relative;
248 
249 	timelib_sll      sse;         /* Seconds since epoch */
250 
251 	unsigned int   have_time, have_date, have_zone, have_relative, have_weeknr_day;
252 
253 	unsigned int   sse_uptodate; /* !0 if the sse member is up to date with the date/time members */
254 	unsigned int   tim_uptodate; /* !0 if the date/time members are up to date with the sse member */
255 	unsigned int   is_localtime; /*  1 if the current struct represents localtime, 0 if it is in GMT */
256 	unsigned int   zone_type;    /*  1 time offset,
257 	                              *  3 TimeZone identifier,
258 	                              *  2 TimeZone abbreviation */
259 } timelib_time;
260 
261 typedef struct timelib_abbr_info {
262 	timelib_sll  utc_offset;
263 	char        *abbr;
264 	int          dst;
265 } timelib_abbr_info;
266 
267 typedef struct timelib_error_message {
268 	int         position;
269 	char        character;
270 	char       *message;
271 } timelib_error_message;
272 
273 typedef struct timelib_error_container {
274 	struct timelib_error_message *error_messages;
275 	struct timelib_error_message *warning_messages;
276 	int                           error_count;
277 	int                           warning_count;
278 } timelib_error_container;
279 
280 typedef struct _timelib_tz_lookup_table {
281 	char       *name;
282 	int         type;
283 	float       gmtoffset;
284 	char       *full_tz_name;
285 } timelib_tz_lookup_table;
286 
287 typedef struct _timelib_tzdb_index_entry {
288 	char *id;
289 	unsigned int pos;
290 } timelib_tzdb_index_entry;
291 
292 typedef struct _timelib_tzdb {
293 	char                           *version;
294 	int                             index_size;
295 	const timelib_tzdb_index_entry *index;
296 	const unsigned char            *data;
297 } timelib_tzdb;
298 
299 #define TIMELIB_ZONETYPE_OFFSET 1
300 #define TIMELIB_ZONETYPE_ABBR   2
301 #define TIMELIB_ZONETYPE_ID     3
302 
303 #define SECS_PER_ERA   TIMELIB_LL_CONST(12622780800)
304 #define SECS_PER_DAY   86400
305 #define DAYS_PER_YEAR    365
306 #define DAYS_PER_LYEAR   366
307 /* 400*365 days + 97 leap days */
308 #define DAYS_PER_LYEAR_PERIOD 146097
309 #define YEARS_PER_LYEAR_PERIOD 400
310 
311 #define timelib_is_leap(y) ((y) % 4 == 0 && ((y) % 100 != 0 || (y) % 400 == 0))
312 
313 #define TIMELIB_DEBUG(s)  if (0) { s }
314 
315 #endif
316