Coverage Report

Created: 2026-09-14 06:25

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/php-src/ext/date/lib/tm2unixtime.c
Line
Count
Source
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
#include "timelib.h"
26
#include "timelib_private.h"
27
28
/*                                    dec  jan  feb  mrt  apr  may  jun  jul  aug  sep  oct  nov  dec */
29
static int days_in_month_leap[13] = {  31,  31,  29,  31,  30,  31,  30,  31,  31,  30,  31,  30,  31 };
30
static int days_in_month[13]      = {  31,  31,  28,  31,  30,  31,  30,  31,  31,  30,  31,  30,  31 };
31
32
static void do_range_limit(timelib_sll start, timelib_sll end, timelib_sll adj, timelib_sll *a, timelib_sll *b)
33
12.0k
{
34
12.0k
  if (*a < start) {
35
    /* We calculate 'a + 1' first as 'start - *a - 1' causes an int64_t overflows if *a is
36
     * LONG_MIN. 'start' is 0 in this context, and '0 - LONG_MIN > LONG_MAX'. */
37
142
    timelib_sll a_plus_1 = *a + 1;
38
39
142
    *b -= (start - a_plus_1) / adj + 1;
40
41
    /* This code add the extra 'adj' separately, as otherwise this can overflow int64_t in
42
     * situations where *b is near LONG_MIN. */
43
142
    *a += adj * ((start - a_plus_1) / adj);
44
142
    *a += adj;
45
142
  }
46
12.0k
  if (*a >= end) {
47
2.00k
    *b += *a / adj;
48
2.00k
    *a -= adj * (*a / adj);
49
2.00k
  }
50
12.0k
}
51
52
static void inc_month(timelib_sll *y, timelib_sll *m)
53
0
{
54
0
  (*m)++;
55
0
  if (*m > 12) {
56
0
    *m -= 12;
57
0
    (*y)++;
58
0
  }
59
0
}
60
61
static void dec_month(timelib_sll *y, timelib_sll *m)
62
0
{
63
0
  (*m)--;
64
0
  if (*m < 1) {
65
0
    *m += 12;
66
0
    (*y)--;
67
0
  }
68
0
}
69
70
static void do_range_limit_days_relative(timelib_sll *base_y, timelib_sll *base_m, timelib_sll *y, timelib_sll *m, timelib_sll *d, timelib_sll invert)
71
10
{
72
10
  timelib_sll leapyear;
73
10
  timelib_sll month, year;
74
10
  timelib_sll days;
75
76
10
  do_range_limit(1, 13, 12, base_m, base_y);
77
78
10
  year = *base_y;
79
10
  month = *base_m;
80
81
/*
82
  printf( "S: Y%d M%d   %d %d %d   %d\n", year, month, *y, *m, *d, days);
83
*/
84
10
  if (!invert) {
85
10
    while (*d < 0) {
86
0
      dec_month(&year, &month);
87
0
      leapyear = timelib_is_leap(year);
88
0
      days = leapyear ? days_in_month_leap[month] : days_in_month[month];
89
90
      /* printf( "I  Y%d M%d   %d %d %d   %d\n", year, month, *y, *m, *d, days); */
91
92
0
      *d += days;
93
0
      (*m)--;
94
0
    }
95
10
  } else {
96
0
    while (*d < 0) {
97
0
      leapyear = timelib_is_leap(year);
98
0
      days = leapyear ? days_in_month_leap[month] : days_in_month[month];
99
100
      /* printf( "I  Y%d M%d   %d %d %d   %d\n", year, month, *y, *m, *d, days); */
101
102
0
      *d += days;
103
0
      (*m)--;
104
0
      inc_month(&year, &month);
105
0
    }
106
0
  }
107
  /*
108
  printf( "E: Y%d M%d   %d %d %d   %d\n", year, month, *y, *m, *d, days);
109
  */
110
10
}
111
112
static int do_range_limit_days(timelib_sll *y, timelib_sll *m, timelib_sll *d)
113
3.45k
{
114
3.45k
  timelib_sll leapyear;
115
3.45k
  timelib_sll previous_month, previous_year;
116
3.45k
  timelib_sll days_in_previous_month;
117
3.45k
  int retval = 0;
118
3.45k
  int *days_per_month_current_year;
119
120
  /* can jump an entire leap year period quickly */
121
3.45k
  if (*d >= DAYS_PER_ERA || *d <= -DAYS_PER_ERA) {
122
10
    *y += YEARS_PER_ERA * (*d / DAYS_PER_ERA);
123
10
    *d -= DAYS_PER_ERA * (*d / DAYS_PER_ERA);
124
10
  }
125
126
3.45k
  do_range_limit(1, 13, 12, m, y);
127
128
3.45k
  leapyear = timelib_is_leap(*y);
129
3.45k
  days_per_month_current_year = leapyear ? days_in_month_leap : days_in_month;
130
131
4.36k
  while (*d <= 0 && *m > 0) {
132
909
    previous_month = (*m) - 1;
133
909
    if (previous_month < 1) {
134
79
      previous_month += 12;
135
79
      previous_year = (*y) - 1;
136
830
    } else {
137
830
      previous_year = (*y);
138
830
    }
139
909
    leapyear = timelib_is_leap(previous_year);
140
909
    days_in_previous_month = leapyear ? days_in_month_leap[previous_month] : days_in_month[previous_month];
141
142
909
    *d += days_in_previous_month;
143
909
    (*m)--;
144
909
    retval = 1;
145
909
  }
146
27.3k
  while (*d > 0 && *m <= 12 && *d > days_per_month_current_year[*m]) {
147
23.8k
    *d -=  days_per_month_current_year[*m];
148
23.8k
    (*m)++;
149
23.8k
    retval = 1;
150
23.8k
  }
151
3.45k
  return retval;
152
3.45k
}
153
154
static void do_adjust_for_weekday(timelib_time* time)
155
16
{
156
16
  timelib_sll current_dow, difference;
157
158
16
  current_dow = timelib_day_of_week(time->y, time->m, time->d);
159
16
  if (time->relative.weekday_behavior == 2)
160
0
  {
161
    /* To make "this week" work, where the current DOW is a "sunday" */
162
0
    if (current_dow == 0 && time->relative.weekday != 0) {
163
0
      time->relative.weekday -= 7;
164
0
    }
165
166
    /* To make "sunday this week" work, where the current DOW is not a
167
     * "sunday" */
168
0
    if (time->relative.weekday == 0 && current_dow != 0) {
169
0
      time->relative.weekday = 7;
170
0
    }
171
172
0
    time->d -= current_dow;
173
0
    time->d += time->relative.weekday;
174
0
    return;
175
0
  }
176
16
  difference = time->relative.weekday - current_dow;
177
16
  if ((time->relative.d < 0 && difference < 0) || (time->relative.d >= 0 && difference <= -time->relative.weekday_behavior)) {
178
15
    difference += 7;
179
15
  }
180
16
  if (time->relative.weekday >= 0) {
181
16
    time->d += difference;
182
16
  } else {
183
0
    time->d -= (7 - (abs(time->relative.weekday) - current_dow));
184
0
  }
185
16
  time->relative.have_weekday_relative = 0;
186
16
}
187
188
void timelib_do_rel_normalize(timelib_time *base, timelib_rel_time *rt)
189
10
{
190
10
  do_range_limit(0, 1000000, 1000000, &rt->us, &rt->s);
191
10
  do_range_limit(0, 60, 60, &rt->s, &rt->i);
192
10
  do_range_limit(0, 60, 60, &rt->i, &rt->h);
193
10
  do_range_limit(0, 24, 24, &rt->h, &rt->d);
194
10
  do_range_limit(0, 12, 12, &rt->m, &rt->y);
195
196
10
  do_range_limit_days_relative(&base->y, &base->m, &rt->y, &rt->m, &rt->d, rt->invert);
197
10
  do_range_limit(0, 12, 12, &rt->m, &rt->y);
198
10
}
199
200
void timelib_do_normalize(timelib_time* time)
201
1.44k
{
202
1.44k
  if (time->us != TIMELIB_UNSET) do_range_limit(0, 1000000, 1000000, &time->us, &time->s);
203
1.44k
  if (time->s != TIMELIB_UNSET) do_range_limit(0, 60, 60, &time->s, &time->i);
204
1.44k
  if (time->s != TIMELIB_UNSET) do_range_limit(0, 60, 60, &time->i, &time->h);
205
1.44k
  if (time->s != TIMELIB_UNSET) do_range_limit(0, 24, 24, &time->h, &time->d);
206
1.44k
  do_range_limit(1, 13, 12, &time->m, &time->y);
207
208
  /* Short cut if we're doing things against the Epoch */
209
1.44k
  if (time->y == 1970 && time->m == 1) {
210
95
    timelib_date_from_epoch_days(time->d - 1, &time->y, &time->m, &time->d);
211
95
    return;
212
95
  }
213
214
3.45k
  do {} while (do_range_limit_days(&time->y, &time->m, &time->d));
215
1.34k
  do_range_limit(1, 13, 12, &time->m, &time->y);
216
1.34k
}
217
218
static void do_adjust_relative(timelib_time* time)
219
361
{
220
361
  if (time->relative.have_weekday_relative) {
221
16
    do_adjust_for_weekday(time);
222
16
  }
223
361
  timelib_do_normalize(time);
224
225
361
  if (time->have_relative) {
226
45
    time->us += time->relative.us;
227
228
45
    time->s += time->relative.s;
229
45
    time->i += time->relative.i;
230
45
    time->h += time->relative.h;
231
232
45
    time->d += time->relative.d;
233
45
    time->m += time->relative.m;
234
45
    time->y += time->relative.y;
235
45
  }
236
237
361
  switch (time->relative.first_last_day_of) {
238
0
    case TIMELIB_SPECIAL_FIRST_DAY_OF_MONTH: /* first */
239
0
      time->d = 1;
240
0
      break;
241
0
    case TIMELIB_SPECIAL_LAST_DAY_OF_MONTH: /* last */
242
0
      time->d = 0;
243
0
      time->m++;
244
0
      break;
245
361
  }
246
247
361
  timelib_do_normalize(time);
248
361
}
249
250
static void do_adjust_special_weekday(timelib_time* time)
251
0
{
252
0
  timelib_sll count, dow, rem;
253
254
0
  count = time->relative.special.amount;
255
0
  dow = timelib_day_of_week(time->y, time->m, time->d);
256
257
  /* Add increments of 5 weekdays as a week, leaving the DOW unchanged. */
258
0
  time->d += (count / 5) * 7;
259
260
  /* Deal with the remainder. */
261
0
  rem = (count % 5);
262
263
0
  if (count > 0) {
264
0
    if (rem == 0) {
265
      /* Head back to Friday if we stop on the weekend. */
266
0
      if (dow == 0) {
267
0
        time->d -= 2;
268
0
      } else if (dow == 6) {
269
0
        time->d -= 1;
270
0
      }
271
0
    } else if (dow == 6) {
272
      /* We ended up on Saturday, but there's still work to do, so move
273
       * to Sunday and continue from there. */
274
0
      time->d += 1;
275
0
    } else if (dow + rem > 5) {
276
      /* We're on a weekday, but we're going past Friday, so skip right
277
       * over the weekend. */
278
0
      time->d += 2;
279
0
    }
280
0
  } else {
281
    /* Completely mirror the forward direction. This also covers the 0
282
     * case, since if we start on the weekend, we want to move forward as
283
     * if we stopped there while going backwards. */
284
0
    if (rem == 0) {
285
0
      if (dow == 6) {
286
0
        time->d += 2;
287
0
      } else if (dow == 0) {
288
0
        time->d += 1;
289
0
      }
290
0
    } else if (dow == 0) {
291
0
      time->d -= 1;
292
0
    } else if (dow + rem < 1) {
293
0
      time->d -= 2;
294
0
    }
295
0
  }
296
297
0
  time->d += rem;
298
0
}
299
300
static void do_adjust_special(timelib_time* time)
301
361
{
302
361
  if (time->relative.have_special_relative) {
303
0
    switch (time->relative.special.type) {
304
0
      case TIMELIB_SPECIAL_WEEKDAY:
305
0
        do_adjust_special_weekday(time);
306
0
        break;
307
0
    }
308
0
  }
309
361
  timelib_do_normalize(time);
310
361
  memset(&(time->relative.special), 0, sizeof(time->relative.special));
311
361
}
312
313
static void do_adjust_special_early(timelib_time* time)
314
361
{
315
361
  if (time->relative.have_special_relative) {
316
0
    switch (time->relative.special.type) {
317
0
      case TIMELIB_SPECIAL_DAY_OF_WEEK_IN_MONTH:
318
0
        time->d = 1;
319
0
        time->m += time->relative.m;
320
0
        time->relative.m = 0;
321
0
        break;
322
0
      case TIMELIB_SPECIAL_LAST_DAY_OF_WEEK_IN_MONTH:
323
0
        time->d = 1;
324
0
        time->m += time->relative.m + 1;
325
0
        time->relative.m = 0;
326
0
        break;
327
0
    }
328
0
  }
329
361
  switch (time->relative.first_last_day_of) {
330
0
    case TIMELIB_SPECIAL_FIRST_DAY_OF_MONTH: /* first */
331
0
      time->d = 1;
332
0
      break;
333
0
    case TIMELIB_SPECIAL_LAST_DAY_OF_MONTH: /* last */
334
0
      time->d = 0;
335
0
      time->m++;
336
0
      break;
337
361
  }
338
361
  timelib_do_normalize(time);
339
361
}
340
341
static void do_adjust_timezone(timelib_time *tz, timelib_tzinfo *tzi)
342
361
{
343
361
  switch (tz->zone_type) {
344
58
    case TIMELIB_ZONETYPE_OFFSET:
345
346
58
      tz->is_localtime = 1;
347
58
      tz->sse += -tz->z;
348
58
      return;
349
350
14
    case TIMELIB_ZONETYPE_ABBR: {
351
352
14
      tz->is_localtime = 1;
353
14
      tz->sse += (-tz->z - tz->dst * SECS_PER_HOUR);
354
14
      return;
355
0
    }
356
357
289
    case TIMELIB_ZONETYPE_ID:
358
289
      tzi = tz->tz_info;
359
289
      TIMELIB_BREAK_INTENTIONALLY_MISSING
360
361
289
    default: {
362
      /* No timezone in struct, fallback to reference if possible */
363
289
      int32_t              current_offset = 0;
364
289
      timelib_sll          current_transition_time = 0;
365
289
      unsigned int         current_is_dst = 0;
366
289
      int32_t              after_offset = 0;
367
289
      timelib_sll          after_transition_time = 0;
368
289
      timelib_sll          adjustment;
369
289
      int                  in_transition;
370
289
      int32_t              actual_offset;
371
289
      timelib_sll          actual_transition_time;
372
373
289
      if (!tzi) {
374
0
        return;
375
0
      }
376
377
289
      timelib_get_time_zone_offset_info(tz->sse, tzi, &current_offset, &current_transition_time, &current_is_dst);
378
289
      timelib_get_time_zone_offset_info(tz->sse - current_offset, tzi, &after_offset, &after_transition_time, NULL);
379
289
      actual_offset = after_offset;
380
289
      actual_transition_time = after_transition_time;
381
289
      if (current_offset == after_offset && tz->have_zone) {
382
        /* Make sure we're not missing a DST change because we don't know the actual offset yet */
383
17
        if (current_offset >= 0 && tz->dst && !current_is_dst) {
384
            /* Timezone or its DST at or east of UTC, so the local time, interpreted as UTC, leaves DST (as defined in the actual timezone) before the actual local time */
385
5
            int32_t              earlier_offset;
386
5
            timelib_sll          earlier_transition_time;
387
5
            timelib_get_time_zone_offset_info(tz->sse - current_offset - 7200, tzi, &earlier_offset, &earlier_transition_time, NULL);
388
5
            if ((earlier_offset != after_offset) && (tz->sse - earlier_offset < after_transition_time)) {
389
                /* Looking behind a bit clarified the actual offset to use */
390
0
                actual_offset = earlier_offset;
391
0
                actual_transition_time = earlier_transition_time;
392
0
            }
393
12
        } else if (current_offset <= 0 && current_is_dst && !tz->dst) {
394
            /* Timezone west of UTC, so the local time, interpreted as UTC, leaves DST (as defined in the actual timezone) after the actual local time */
395
0
            int32_t              later_offset;
396
0
            timelib_sll          later_transition_time;
397
0
            timelib_get_time_zone_offset_info(tz->sse - current_offset + 7200, tzi, &later_offset, &later_transition_time, NULL);
398
0
            if ((later_offset != after_offset) && (tz->sse - later_offset >= later_transition_time)) {
399
                /* Looking ahead a bit clarified the actual offset to use */
400
0
                actual_offset = later_offset;
401
0
                actual_transition_time = later_transition_time;
402
0
            }
403
0
        }
404
17
      }
405
406
289
      tz->is_localtime = 1;
407
408
289
      in_transition = (
409
289
        actual_transition_time != INT64_MIN &&
410
6
        ((tz->sse - actual_offset) >= (actual_transition_time + (current_offset - actual_offset))) &&
411
6
        ((tz->sse - actual_offset) < actual_transition_time)
412
289
      );
413
414
289
      if ((current_offset != actual_offset) && !in_transition) {
415
0
        adjustment = -actual_offset;
416
289
      } else {
417
289
        adjustment = -current_offset;
418
289
      }
419
420
289
      tz->sse += adjustment;
421
289
      timelib_set_timezone(tz, tzi);
422
289
      return;
423
289
    }
424
361
  }
425
0
  return;
426
361
}
427
428
timelib_sll timelib_epoch_days_from_time(timelib_time *time)
429
742
{
430
742
  timelib_sll y = time->y; // Make copy, as we don't want to change the original one
431
742
  timelib_sll era, year_of_era, day_of_year, day_of_era;
432
433
742
  y -= time->m <= 2;
434
742
  era = (y >= 0 ? y : y - 399) / YEARS_PER_ERA;
435
742
  year_of_era = y - era * YEARS_PER_ERA;                                                        // [0, 399]
436
742
  day_of_year = (153 * (time->m + (time->m > 2 ? -3 : 9)) + 2)/5 + time->d - 1;                 // [0, 365]
437
742
  day_of_era = year_of_era * DAYS_PER_YEAR + year_of_era / 4 - year_of_era / 100 + day_of_year; // [0, 146096]
438
439
742
  return era * DAYS_PER_ERA + day_of_era - HINNANT_EPOCH_SHIFT;
440
742
}
441
442
void timelib_update_ts(timelib_time* time, timelib_tzinfo* tzi)
443
361
{
444
361
  do_adjust_special_early(time);
445
361
  do_adjust_relative(time);
446
361
  do_adjust_special(time);
447
448
  /* You might be wondering, why this code does this in two steps. This is because
449
   * timelib_epoch_days_from_time(time) * SECS_PER_DAY with the lowest limit of
450
   * timelib_epoch_days_from_time() is less than the range of an int64_t. This then overflows. In
451
   * order to be able to still allow for any time in that day that only halfly fits in the int64_t
452
   * range, we add the time element first, which is always positive, and then twice half the value
453
   * of the earliest day as expressed as unix timestamp. */
454
361
  time->sse = timelib_hms_to_seconds(time->h, time->i, time->s);
455
361
  time->sse += timelib_epoch_days_from_time(time) * (SECS_PER_DAY / 2);
456
361
  time->sse += timelib_epoch_days_from_time(time) * (SECS_PER_DAY / 2);
457
458
  // This modifies time->sse, if needed
459
361
  do_adjust_timezone(time, tzi);
460
461
361
  time->sse_uptodate = 1;
462
361
  time->have_relative = time->relative.have_weekday_relative = time->relative.have_special_relative = time->relative.first_last_day_of = 0;
463
361
}
464
465
#if 0
466
int main(void)
467
{
468
  timelib_sll res;
469
  timelib_time time;
470
471
  time = timelib_strtotime("10 Feb 2005 06:07:03 PM CET"); /* 1108055223 */
472
  printf ("%04d-%02d-%02d %02d:%02d:%02d.%-5d %+04d %1d",
473
    time.y, time.m, time.d, time.h, time.i, time.s, time.f, time.z, time.dst);
474
  if (time.have_relative) {
475
    printf ("%3dY %3dM %3dD / %3dH %3dM %3dS",
476
      time.relative.y, time.relative.m, time.relative.d, time.relative.h, time.relative.i, time.relative.s);
477
  }
478
  if (time.have_weekday_relative) {
479
    printf (" / %d", time.relative.weekday);
480
  }
481
  res = time2unixtime(&time);
482
  printf("%Ld\n", res);
483
484
  return 0;
485
}
486
#endif