xref: /php-src/ext/date/lib/timelib_private.h (revision 06d4c70e)
1 /*
2  * The MIT License (MIT)
3  *
4  * Copyright (c) 2015-2019 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_PRIVATE_H__
26 #define __TIMELIB_PRIVATE_H__
27 
28 #ifdef HAVE_TIMELIB_CONFIG_H
29 # include "timelib_config.h"
30 #endif
31 
32 #ifdef HAVE_SYS_TIME_H
33 # include <sys/time.h>
34 #endif
35 
36 #ifdef _WIN32
37 # ifdef HAVE_WINSOCK2_H
38 #  include <winsock2.h>
39 # endif
40 #endif
41 
42 #include <string.h>
43 
44 #ifdef HAVE_SYS_TYPES_H
45 #include <sys/types.h>
46 #endif
47 
48 #if defined(HAVE_STDINT_H)
49 # include <stdint.h>
50 #endif
51 
52 #if HAVE_UNISTD_H
53 # include <unistd.h>
54 #endif
55 
56 #if HAVE_IO_H
57 # include <io.h>
58 #endif
59 
60 #if HAVE_DIRENT_H
61 # include <dirent.h>
62 #endif
63 
64 #include <stdio.h>
65 #include <limits.h>
66 
67 #define TIMELIB_SECOND   1
68 #define TIMELIB_MINUTE   2
69 #define TIMELIB_HOUR     3
70 #define TIMELIB_DAY      4
71 #define TIMELIB_MONTH    5
72 #define TIMELIB_YEAR     6
73 #define TIMELIB_WEEKDAY  7
74 #define TIMELIB_SPECIAL  8
75 #define TIMELIB_MICROSEC 9
76 
77 #define TIMELIB_SPECIAL_WEEKDAY                   0x01
78 #define TIMELIB_SPECIAL_DAY_OF_WEEK_IN_MONTH      0x02
79 #define TIMELIB_SPECIAL_LAST_DAY_OF_WEEK_IN_MONTH 0x03
80 
81 #define TIMELIB_SPECIAL_FIRST_DAY_OF_MONTH        0x01
82 #define TIMELIB_SPECIAL_LAST_DAY_OF_MONTH         0x02
83 
84 #define TIMELIB_TIME_PART_DONT_KEEP               0x00
85 #define TIMELIB_TIME_PART_KEEP                    0x01
86 
87 #define MINS_PER_HOUR     60
88 #define SECS_PER_ERA   TIMELIB_LL_CONST(12622780800)
89 #define SECS_PER_DAY   86400
90 #define SECS_PER_HOUR   3600
91 #define USECS_PER_HOUR TIMELIB_LL_CONST(3600000000)
92 
93 #define DAYS_PER_WEEK      7
94 #define DAYS_PER_YEAR    365
95 #define DAYS_PER_LYEAR   366
96 #define MONTHS_PER_YEAR   12
97 /* 400*365 days + 97 leap days */
98 #define DAYS_PER_ERA  146097
99 #define YEARS_PER_ERA    400
100 #define HINNANT_EPOCH_SHIFT 719468 /* 0000-03-01 instead of 1970-01-01 */
101 
102 #define TIMELIB_TZINFO_PHP       0x01
103 #define TIMELIB_TZINFO_ZONEINFO  0x02
104 
105 #define timelib_is_leap(y) ((y) % 4 == 0 && ((y) % 100 != 0 || (y) % 400 == 0))
106 
107 #define TIMELIB_DEBUG(s)  if (0) { s }
108 
109 #define TIMELIB_TIME_FREE(m)    \
110 	if (m) {        \
111 		timelib_free(m);    \
112 		m = NULL;   \
113 	}
114 
115 #if defined (__GNUC__)
116 # define TIMELIB_GNUC_CHECK_VERSION(major, minor) \
117       ((__GNUC__ > (major)) ||                   \
118       ((__GNUC__ == (major)) && (__GNUC_MINOR__ >= (minor))))
119 #else
120 # define TIMELIB_GNUC_CHECK_VERSION(major, minor) 0
121 #endif
122 
123 #if TIMELIB_GNUC_CHECK_VERSION(7, 0)
124 # define TIMELIB_BREAK_INTENTIONALLY_MISSING __attribute__ ((fallthrough));
125 #else
126 # define TIMELIB_BREAK_INTENTIONALLY_MISSING
127 #endif
128 
129 struct _ttinfo
130 {
131 	int32_t      offset;
132 	int          isdst;
133 	unsigned int abbr_idx;
134 
135 	unsigned int isstdcnt;
136 	unsigned int isgmtcnt;
137 };
138 
139 struct _tlinfo
140 {
141 	int64_t  trans;
142 	int32_t  offset;
143 };
144 
145 
146 #ifndef LONG_MAX
147 #define LONG_MAX 2147483647L
148 #endif
149 
150 #ifndef LONG_MIN
151 #define LONG_MIN (- LONG_MAX - 1)
152 #endif
153 
154 #ifdef __cplusplus
155 extern "C" {
156 #endif
157 
158 /* From unixtime2tm.c */
159 int timelib_apply_localtime(timelib_time *t, unsigned int localtime);
160 
161 /* From parse_posix.c */
162 timelib_sll timelib_ts_at_start_of_year(timelib_sll year);
163 ttinfo* timelib_fetch_posix_timezone_offset(timelib_tzinfo *tz, timelib_sll ts, timelib_sll *transition_time);
164 
165 /* From parse_tz.c */
166 void timelib_time_tz_abbr_update(timelib_time* tm, const char* tz_abbr);
167 ttinfo* timelib_fetch_timezone_offset(timelib_tzinfo *tz, timelib_sll ts, timelib_sll *transition_time);
168 
169 /* From timelib.c */
170 int timelib_strcasecmp(const char *s1, const char *s2);
171 int timelib_strncasecmp(const char *s1, const char *s2, size_t n);
172 
173 #ifdef __cplusplus
174 } /* extern "C" */
175 #endif
176 
177 #endif
178