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/interval.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
#include <math.h>
28
29
static void swap_times(timelib_time **one, timelib_time **two, timelib_rel_time *rt)
30
0
{
31
0
  timelib_time *swp;
32
33
0
  swp = *two;
34
0
  *two = *one;
35
0
  *one = swp;
36
0
  rt->invert = 1;
37
0
}
38
39
static void sort_old_to_new(timelib_time **one, timelib_time **two, timelib_rel_time *rt)
40
10
{
41
  /* Check whether date/times need to be inverted. If both times are
42
   * TIMELIB_ZONETYPE_ID times with the same TZID, we use the y-s + us fields. */
43
10
  if (
44
10
    (*one)->zone_type == TIMELIB_ZONETYPE_ID &&
45
10
    (*two)->zone_type == TIMELIB_ZONETYPE_ID &&
46
10
    (strcmp((*one)->tz_info->name, (*two)->tz_info->name) == 0)
47
10
  ) {
48
10
    if (
49
10
      ((*one)->y > (*two)->y) ||
50
10
      ((*one)->y == (*two)->y && (*one)->m > (*two)->m) ||
51
10
      ((*one)->y == (*two)->y && (*one)->m == (*two)->m && (*one)->d > (*two)->d) ||
52
10
      ((*one)->y == (*two)->y && (*one)->m == (*two)->m && (*one)->d == (*two)->d && (*one)->h > (*two)->h) ||
53
10
      ((*one)->y == (*two)->y && (*one)->m == (*two)->m && (*one)->d == (*two)->d && (*one)->h == (*two)->h && (*one)->i > (*two)->i) ||
54
10
      ((*one)->y == (*two)->y && (*one)->m == (*two)->m && (*one)->d == (*two)->d && (*one)->h == (*two)->h && (*one)->i == (*two)->i && (*one)->s > (*two)->s) ||
55
10
      ((*one)->y == (*two)->y && (*one)->m == (*two)->m && (*one)->d == (*two)->d && (*one)->h == (*two)->h && (*one)->i == (*two)->i && (*one)->s == (*two)->s && (*one)->us > (*two)->us)
56
10
    ) {
57
0
      swap_times(one, two, rt);
58
0
    }
59
10
    return;
60
10
  }
61
62
  /* Fall back to using the SSE instead to rearrange */
63
0
  if (
64
0
    ((*one)->sse > (*two)->sse) ||
65
0
    ((*one)->sse == (*two)->sse && (*one)->us > (*two)->us)
66
0
  ) {
67
0
    swap_times(one, two, rt);
68
0
  }
69
0
}
70
71
static timelib_rel_time *timelib_diff_with_tzid(timelib_time *one, timelib_time *two)
72
10
{
73
10
  timelib_rel_time *rt;
74
10
  timelib_sll       dst_corr = 0, dst_h_corr = 0, dst_m_corr = 0;
75
10
  int32_t           trans_offset;
76
10
  timelib_sll       trans_transition_time;
77
78
10
  rt = timelib_rel_time_ctor();
79
10
  rt->invert = 0;
80
81
10
  sort_old_to_new(&one, &two, rt);
82
83
  /* Calculate correction for UTC offset changes between first and second SSE */
84
10
  dst_corr = two->z - one->z;
85
10
  dst_h_corr = dst_corr / 3600;
86
10
  dst_m_corr = (dst_corr % 3600) / 60;
87
88
10
  rt->y = two->y - one->y;
89
10
  rt->m = two->m - one->m;
90
10
  rt->d = two->d - one->d;
91
10
  rt->h = two->h - one->h;
92
10
  rt->i = two->i - one->i;
93
10
  rt->s = two->s - one->s;
94
10
  rt->us = two->us - one->us;
95
96
10
  rt->days = timelib_diff_days(one, two);
97
98
  /* Fall Back: Cater for transition period, where rt->invert is 0, but there are negative numbers */
99
10
  if (two->sse < one->sse) {
100
0
    timelib_sll flipped = llabs((rt->i * 60) + (rt->s) - dst_corr);
101
0
    rt->h = flipped / SECS_PER_HOUR;
102
0
    rt->i = (flipped - rt->h * SECS_PER_HOUR) / 60;
103
0
    rt->s = flipped % 60;
104
105
0
    rt->invert = 1 - rt->invert;
106
0
  }
107
108
10
  timelib_do_rel_normalize(rt->invert ? one : two, rt);
109
110
10
  if (one->dst == 1 && two->dst == 0) { /* Fall Back */
111
0
    if (two->tz_info) {
112
0
      if ((two->sse - one->sse + dst_corr) < SECS_PER_DAY) {
113
0
        rt->h -= dst_h_corr;
114
0
        rt->i -= dst_m_corr;
115
0
      }
116
0
    }
117
10
  } else if (one->dst == 0 && two->dst == 1) { /* Spring Forward */
118
0
    if (two->tz_info) {
119
0
      int success = timelib_get_time_zone_offset_info(two->sse, two->tz_info, &trans_offset, &trans_transition_time, NULL);
120
121
0
      if (
122
0
        success &&
123
0
        !((one->sse + SECS_PER_DAY > trans_transition_time) && (one->sse + SECS_PER_DAY <= (trans_transition_time + dst_corr))) &&
124
0
        two->sse >= trans_transition_time &&
125
0
        ((two->sse - one->sse + dst_corr) % SECS_PER_DAY) > (two->sse - trans_transition_time)
126
0
      ) {
127
0
        rt->h -= dst_h_corr;
128
0
        rt->i -= dst_m_corr;
129
0
      }
130
0
    }
131
10
  } else if (two->sse - one->sse >= SECS_PER_DAY) {
132
    /* Check whether we're in the period to the next transition time */
133
0
    if (timelib_get_time_zone_offset_info(two->sse - two->z, two->tz_info, &trans_offset, &trans_transition_time, NULL)) {
134
0
      dst_corr = one->z - trans_offset;
135
136
0
      if (two->sse >= trans_transition_time - dst_corr && two->sse < trans_transition_time) {
137
0
        rt->d--;
138
0
        rt->h = 24;
139
0
      }
140
0
    }
141
0
  }
142
143
10
  return rt;
144
10
}
145
146
timelib_rel_time *timelib_diff(timelib_time *one, timelib_time *two)
147
10
{
148
10
  timelib_rel_time *rt;
149
150
10
  if (one->zone_type == TIMELIB_ZONETYPE_ID && two->zone_type == TIMELIB_ZONETYPE_ID && strcmp(one->tz_info->name, two->tz_info->name) == 0) {
151
10
    return timelib_diff_with_tzid(one, two);
152
10
  }
153
154
0
  rt = timelib_rel_time_ctor();
155
0
  rt->invert = 0;
156
157
0
  sort_old_to_new(&one, &two, rt);
158
159
0
  rt->y = two->y - one->y;
160
0
  rt->m = two->m - one->m;
161
0
  rt->d = two->d - one->d;
162
0
  rt->h = two->h - one->h;
163
0
  if (one->zone_type != TIMELIB_ZONETYPE_ID) {
164
0
    rt->h = rt->h + one->dst;
165
0
  }
166
0
  if (two->zone_type != TIMELIB_ZONETYPE_ID) {
167
0
    rt->h = rt->h - two->dst;
168
0
  }
169
0
  rt->i = two->i - one->i;
170
0
  rt->s = two->s - one->s - two->z + one->z;
171
0
  rt->us = two->us - one->us;
172
173
0
  rt->days = timelib_diff_days(one, two);
174
175
0
  timelib_do_rel_normalize(rt->invert ? one : two, rt);
176
177
0
  return rt;
178
10
}
179
180
181
int timelib_diff_days(timelib_time *one, timelib_time *two)
182
10
{
183
10
  int days = 0;
184
185
10
  if (timelib_same_timezone(one, two)) {
186
10
    timelib_time *earliest, *latest;
187
10
    double earliest_time, latest_time;
188
189
10
    if (timelib_time_compare(one, two) < 0) {
190
10
      earliest = one;
191
10
      latest = two;
192
10
    } else {
193
0
      earliest = two;
194
0
      latest = one;
195
0
    }
196
10
    timelib_hmsf_to_decimal_hour(earliest->h, earliest->i, earliest->s, earliest->us, &earliest_time);
197
10
    timelib_hmsf_to_decimal_hour(latest->h, latest->i, latest->s, latest->us, &latest_time);
198
199
10
    days = llabs(timelib_epoch_days_from_time(one) - timelib_epoch_days_from_time(two));
200
10
    if (latest_time < earliest_time && days > 0) {
201
0
      days--;
202
0
    }
203
10
  } else {
204
    /* FIXME: This truncates the range of days to INT_MAX to avoid an
205
     * overflow later on. Ideally, all APIs have better error handling. */
206
0
    double ddays = fabs(floor(one->sse - two->sse) / 86400);
207
0
    days = ddays <= INT_MAX ? ddays : INT_MAX;
208
0
  }
209
210
10
  return days;
211
10
}
212
213
214
timelib_time *timelib_add(timelib_time *old_time, timelib_rel_time *interval)
215
0
{
216
0
  int bias = 1;
217
0
  timelib_time *t = timelib_time_clone(old_time);
218
219
0
  if (interval->have_weekday_relative || interval->have_special_relative) {
220
0
    memcpy(&t->relative, interval, sizeof(timelib_rel_time));
221
0
  } else {
222
0
    if (interval->invert) {
223
0
      bias = -1;
224
0
    }
225
0
    memset(&t->relative, 0, sizeof(timelib_rel_time));
226
0
    t->relative.y = interval->y * bias;
227
0
    t->relative.m = interval->m * bias;
228
0
    t->relative.d = interval->d * bias;
229
0
    t->relative.h = interval->h * bias;
230
0
    t->relative.i = interval->i * bias;
231
0
    t->relative.s = interval->s * bias;
232
0
    t->relative.us = interval->us * bias;
233
0
  }
234
0
  t->have_relative = 1;
235
0
  t->sse_uptodate = 0;
236
237
0
  timelib_update_ts(t, NULL);
238
239
0
  timelib_update_from_sse(t);
240
0
  t->have_relative = 0;
241
242
0
  return t;
243
0
}
244
245
timelib_time *timelib_sub(timelib_time *old_time, timelib_rel_time *interval)
246
0
{
247
0
  int bias = 1;
248
0
  timelib_time *t = timelib_time_clone(old_time);
249
250
0
  if (interval->invert) {
251
0
    bias = -1;
252
0
  }
253
254
0
  memset(&t->relative, 0, sizeof(timelib_rel_time));
255
0
  t->relative.y = 0 - (interval->y * bias);
256
0
  t->relative.m = 0 - (interval->m * bias);
257
0
  t->relative.d = 0 - (interval->d * bias);
258
0
  t->relative.h = 0 - (interval->h * bias);
259
0
  t->relative.i = 0 - (interval->i * bias);
260
0
  t->relative.s = 0 - (interval->s * bias);
261
0
  t->relative.us = 0 - (interval->us * bias);
262
0
  t->have_relative = 1;
263
0
  t->sse_uptodate = 0;
264
265
0
  timelib_update_ts(t, NULL);
266
267
0
  timelib_update_from_sse(t);
268
269
0
  t->have_relative = 0;
270
271
0
  return t;
272
0
}
273
274
static void do_range_limit(timelib_sll start, timelib_sll end, timelib_sll adj, timelib_sll *a, timelib_sll *b)
275
0
{
276
0
  if (*a < start) {
277
0
    *b -= (start - *a - 1) / adj + 1;
278
0
    *a += adj * ((start - *a - 1) / adj + 1);
279
0
  }
280
0
  if (*a >= end) {
281
0
    *b += *a / adj;
282
0
    *a -= adj * (*a / adj);
283
0
  }
284
0
}
285
286
287
timelib_time *timelib_add_wall(timelib_time *old_time, timelib_rel_time *interval)
288
0
{
289
0
  int bias = 1;
290
0
  timelib_time *t = timelib_time_clone(old_time);
291
292
0
  t->have_relative = 1;
293
0
  t->sse_uptodate = 0;
294
295
0
  if (interval->have_weekday_relative || interval->have_special_relative) {
296
0
    memcpy(&t->relative, interval, sizeof(timelib_rel_time));
297
298
0
    timelib_update_ts(t, NULL);
299
300
0
    timelib_update_from_sse(t);
301
0
  } else {
302
0
    if (interval->invert) {
303
0
      bias = -1;
304
0
    }
305
0
    memset(&t->relative, 0, sizeof(timelib_rel_time));
306
0
    t->relative.y = interval->y * bias;
307
0
    t->relative.m = interval->m * bias;
308
0
    t->relative.d = interval->d * bias;
309
310
0
    if (t->relative.y || t->relative.m || t->relative.d) {
311
0
      timelib_update_ts(t, NULL);
312
0
    }
313
314
0
    if (interval->us == 0) {
315
0
      t->sse += bias * timelib_hms_to_seconds(interval->h, interval->i, interval->s);
316
0
      timelib_update_from_sse(t);
317
0
    } else {
318
0
      timelib_rel_time *temp_interval = timelib_rel_time_clone(interval);
319
320
0
      do_range_limit(0, 1000000, 1000000, &temp_interval->us, &temp_interval->s);
321
0
      t->sse += bias * timelib_hms_to_seconds(temp_interval->h, temp_interval->i, temp_interval->s);
322
0
      timelib_update_from_sse(t);
323
0
      t->us += temp_interval->us * bias;
324
325
0
      timelib_do_normalize(t);
326
0
      timelib_update_ts(t, NULL);
327
328
0
      timelib_rel_time_dtor(temp_interval);
329
0
    }
330
0
    timelib_do_normalize(t);
331
0
  }
332
333
0
  if (t->zone_type == TIMELIB_ZONETYPE_ID) {
334
0
    timelib_set_timezone(t, t->tz_info);
335
0
  }
336
0
  t->have_relative = 0;
337
338
0
  return t;
339
0
}
340
341
timelib_time *timelib_sub_wall(timelib_time *old_time, timelib_rel_time *interval)
342
0
{
343
0
  int bias = 1;
344
0
  timelib_time *t = timelib_time_clone(old_time);
345
346
0
  t->have_relative = 1;
347
0
  t->sse_uptodate = 0;
348
349
0
  if (interval->have_weekday_relative || interval->have_special_relative) {
350
0
    memcpy(&t->relative, interval, sizeof(timelib_rel_time));
351
352
0
    timelib_update_ts(t, NULL);
353
0
    timelib_update_from_sse(t);
354
0
  } else {
355
0
    if (interval->invert) {
356
0
      bias = -1;
357
0
    }
358
0
    memset(&t->relative, 0, sizeof(timelib_rel_time));
359
0
    t->relative.y = 0 - (interval->y * bias);
360
0
    t->relative.m = 0 - (interval->m * bias);
361
0
    t->relative.d = 0 - (interval->d * bias);
362
363
0
    if (t->relative.y || t->relative.m || t->relative.d) {
364
0
      timelib_update_ts(t, NULL);
365
0
    }
366
367
0
    if (interval->us == 0) {
368
0
      t->sse -= bias * timelib_hms_to_seconds(interval->h, interval->i, interval->s);
369
0
      timelib_update_from_sse(t);
370
0
    } else {
371
0
      timelib_rel_time *temp_interval = timelib_rel_time_clone(interval);
372
373
0
      do_range_limit(0, 1000000, 1000000, &temp_interval->us, &temp_interval->s);
374
0
      t->sse -= bias * timelib_hms_to_seconds(temp_interval->h, temp_interval->i, temp_interval->s);
375
0
      timelib_update_from_sse(t);
376
0
      t->us -= temp_interval->us * bias;
377
378
0
      timelib_do_normalize(t);
379
0
      timelib_update_ts(t, NULL);
380
381
0
      timelib_rel_time_dtor(temp_interval);
382
0
    }
383
0
    timelib_do_normalize(t);
384
0
  }
385
386
0
  if (t->zone_type == TIMELIB_ZONETYPE_ID) {
387
0
    timelib_set_timezone(t, t->tz_info);
388
0
  }
389
0
  t->have_relative = 0;
390
391
0
  return t;
392
0
}