php-internal-docs 8.4.8
Unofficial docs for php/php-src
Loading...
Searching...
No Matches
unixtime2tm.c
Go to the documentation of this file.
1/*
2 * The MIT License (MIT)
3 *
4 * Copyright (c) 2015-2021 Derick Rethans
5 * Copyright (c) 2021 MongoDB
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 * THE SOFTWARE.
24 */
25
26#include "timelib.h"
27#include "timelib_private.h"
28
30{
31 timelib_sll days, era, t;
32 timelib_ull day_of_era, year_of_era, day_of_year, month_portion;
33
34 /* Calculate days since algorithm's epoch (0000-03-01) */
36
37 /* Adjustment for a negative time portion */
38 t = ts % SECS_PER_DAY;
39 days += (t < 0) ? -1 : 0;
40
41 /* Calculate year, month, and day. Algorithm from:
42 * http://howardhinnant.github.io/date_algorithms.html#civil_from_days */
43 era = (days >= 0 ? days : days - DAYS_PER_ERA + 1) / DAYS_PER_ERA;
44 day_of_era = days - era * DAYS_PER_ERA;
45 year_of_era = (day_of_era - day_of_era / 1460 + day_of_era / 36524 - day_of_era / 146096) / DAYS_PER_YEAR;
46 *y = year_of_era + era * YEARS_PER_ERA;
47 day_of_year = day_of_era - (DAYS_PER_YEAR * year_of_era + year_of_era / 4 - year_of_era / 100);
48 month_portion = (5 * day_of_year + 2) / 153;
49 *d = day_of_year - (153 * month_portion + 2) / 5 + 1;
50 *m = month_portion + (month_portion < 10 ? 3 : -9);
51 *y += (*m <= 2);
52
53 TIMELIB_DEBUG(printf("A: ts=%lld, year=%lld, month=%lld, day=%lld,", ts, *y, *m, *d););
54}
55
56/* Converts a Unix timestamp value into broken down time, in GMT */
58{
59 timelib_sll remainder;
60 timelib_sll hours, minutes, seconds;
61
62 timelib_unixtime2date(ts, &tm->y, &tm->m, &tm->d);
63 remainder = ts % SECS_PER_DAY;
64 remainder += (remainder < 0) * SECS_PER_DAY;
65
66 /* That was the date, now we do the time */
67 hours = remainder / 3600;
68 minutes = (remainder - hours * 3600) / 60;
69 seconds = remainder % 60;
70 TIMELIB_DEBUG(printf(" hour=%lld, minute=%lld, second=%lld\n", hours, minutes, seconds););
71
72 tm->h = hours;
73 tm->i = minutes;
74 tm->s = seconds;
75 tm->z = 0;
76 tm->dst = 0;
77 tm->sse = ts;
78 tm->sse_uptodate = 1;
79 tm->tim_uptodate = 1;
80 tm->is_localtime = 0;
81}
82
84{
85 timelib_sll sse;
86 int z = tm->z;
87 signed int dst = tm->dst;
88
89 sse = tm->sse;
90
91 switch (tm->zone_type) {
94 timelib_unixtime2gmt(tm, tm->sse + tm->z + (tm->dst * 3600));
95
96 goto cleanup;
97 }
98
100 int32_t offset = 0;
101
104
105 goto cleanup;
106 }
107
108 default:
109 timelib_unixtime2gmt(tm, tm->sse);
110 goto cleanup;
111 }
112cleanup:
113 tm->sse = sse;
114 tm->is_localtime = 1;
115 tm->have_zone = 1;
116 tm->z = z;
117 tm->dst = dst;
118}
119
121{
122 timelib_time_offset *gmt_offset;
123 timelib_tzinfo *tz = tm->tz_info;
124
125 switch (tm->zone_type) {
128 int z = tm->z;
129 signed int dst = tm->dst;
130
131 timelib_unixtime2gmt(tm, ts + tm->z + (tm->dst * 3600));
132
133 tm->sse = ts;
134 tm->z = z;
135 tm->dst = dst;
136 break;
137 }
138
140 gmt_offset = timelib_get_time_zone_info(ts, tz);
141 timelib_unixtime2gmt(tm, ts + gmt_offset->offset);
142
143 /* we need to reset the sse here as unixtime2gmt modifies it */
144 tm->sse = ts;
145 tm->dst = gmt_offset->is_dst;
146 tm->z = gmt_offset->offset;
147 tm->tz_info = tz;
148
149 timelib_time_tz_abbr_update(tm, gmt_offset->abbr);
150 timelib_time_offset_dtor(gmt_offset);
151 break;
152
153 default:
154 tm->is_localtime = 0;
155 tm->have_zone = 0;
156 return;
157 }
158
159 tm->is_localtime = 1;
160 tm->have_zone = 1;
161}
162
164{
165 if (t->tz_abbr) {
167 }
168 t->tz_abbr = NULL;
169
170 t->z = utc_offset;
171 t->have_zone = 1;
173 t->dst = 0;
174 t->tz_info = NULL;
175}
176
178{
179 if (t->tz_abbr) {
181 }
182 t->tz_abbr = timelib_strdup(abbr_info.abbr);
183
184 t->z = abbr_info.utc_offset;
185 t->have_zone = 1;
187 t->dst = abbr_info.dst;
188 t->tz_info = NULL;
189}
190
192{
193 timelib_time_offset *gmt_offset;
194
195 gmt_offset = timelib_get_time_zone_info(t->sse, tz);
196 t->z = gmt_offset->offset;
197/*
198 if (t->dst != gmt_offset->is_dst) {
199 printf("ERROR (%d, %d)\n", t->dst, gmt_offset->is_dst);
200 exit(1);
201 }
202*/
203 t->dst = gmt_offset->is_dst;
204 t->tz_info = tz;
205 if (t->tz_abbr) {
207 }
208 t->tz_abbr = timelib_strdup(gmt_offset->abbr);
209 timelib_time_offset_dtor(gmt_offset);
210
211 t->have_zone = 1;
213}
214
215/* Converts the time stored in the struct to localtime if localtime = true,
216 * otherwise it converts it to gmttime. This is only done when necessary
217 * of course. */
219{
220 if (localtime) {
221 /* Converting from GMT time to local time */
222 TIMELIB_DEBUG(printf("Converting from GMT time to local time\n"););
223
224 /* Check if TZ is set */
225 if (!t->tz_info) {
226 TIMELIB_DEBUG(printf("E: No timezone configured, can't switch to local time\n"););
227 return -1;
228 }
229
231 } else {
232 /* Converting from local time to GMT time */
233 TIMELIB_DEBUG(printf("Converting from local time to GMT time\n"););
234
236 }
237 return 0;
238}
printf(string $format, mixed ... $values)
#define SECS_PER_DAY
Definition cal_unix.c:24
zend_long offset
#define NULL
Definition gdcache.h:45
timelib_time_offset * timelib_get_time_zone_info(timelib_sll ts, timelib_tzinfo *tz)
Definition parse_tz.c:881
int timelib_get_time_zone_offset_info(timelib_sll ts, timelib_tzinfo *tz, int32_t *offset, timelib_sll *transition_time, unsigned int *is_dst)
Definition parse_tz.c:913
localtime(?int $timestamp=null, bool $associative=false)
timelib_sll utc_offset
Definition timelib.h:274
unsigned int is_dst
Definition timelib.h:246
unsigned int is_localtime
Definition timelib.h:267
timelib_sll s
Definition timelib.h:253
timelib_sll h
Definition timelib.h:253
timelib_sll d
Definition timelib.h:252
timelib_sll sse
Definition timelib.h:261
unsigned int tim_uptodate
Definition timelib.h:266
char * tz_abbr
Definition timelib.h:256
timelib_sll m
Definition timelib.h:252
unsigned int zone_type
Definition timelib.h:268
signed int dst
Definition timelib.h:258
timelib_tzinfo * tz_info
Definition timelib.h:257
unsigned int have_zone
Definition timelib.h:263
timelib_sll y
Definition timelib.h:252
unsigned int sse_uptodate
Definition timelib.h:265
timelib_sll i
Definition timelib.h:253
void timelib_time_offset_dtor(timelib_time_offset *t)
Definition timelib.c:141
void timelib_time_tz_abbr_update(timelib_time *tm, const char *tz_abbr)
Definition timelib.c:121
#define TIMELIB_ZONETYPE_ID
Definition timelib.h:329
unsigned long long timelib_ull
Definition timelib.h:135
#define TIMELIB_ZONETYPE_OFFSET
Definition timelib.h:327
#define timelib_strdup
Definition timelib.h:368
struct _timelib_time timelib_time
signed long long timelib_sll
Definition timelib.h:136
#define timelib_free
Definition timelib.h:369
struct _timelib_time_offset timelib_time_offset
struct _timelib_tzinfo timelib_tzinfo
#define TIMELIB_ZONETYPE_ABBR
Definition timelib.h:328
struct _timelib_abbr_info timelib_abbr_info
#define DAYS_PER_ERA
#define TIMELIB_DEBUG(s)
#define YEARS_PER_ERA
#define HINNANT_EPOCH_SHIFT
#define DAYS_PER_YEAR
void timelib_unixtime2local(timelib_time *tm, timelib_sll ts)
void timelib_unixtime2gmt(timelib_time *tm, timelib_sll ts)
Definition unixtime2tm.c:57
void timelib_set_timezone_from_offset(timelib_time *t, timelib_sll utc_offset)
void timelib_unixtime2date(timelib_sll ts, timelib_sll *y, timelib_sll *m, timelib_sll *d)
Definition unixtime2tm.c:29
void timelib_update_from_sse(timelib_time *tm)
Definition unixtime2tm.c:83
void timelib_set_timezone(timelib_time *t, timelib_tzinfo *tz)
int timelib_apply_localtime(timelib_time *t, unsigned int localtime)
void timelib_set_timezone_from_abbr(timelib_time *t, timelib_abbr_info abbr_info)