Skip to content

Commit 091c092

Browse files
committed
Upgrade timelib to 2021.03 and fix many date/time issues
1 parent ffb1dd0 commit 091c092

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+52924
-29617
lines changed

NEWS

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,43 @@ PHP NEWS
1010
. Fixed bug #53826 (__callStatic fired in base class through a parent call if
1111
the method is private). (Nikita)
1212

13+
- Date:
14+
. Fixed bug #52480 (Incorrect difference using DateInterval) (Derick)
15+
. Fixed bug #62326 (date_diff() function returns false result) (Derick)
16+
. Fixed bug #64992 (dst not handled past 2038) (Derick)
17+
. Fixed bug #65003 (Wrong date diff) (Derick)
18+
. Fixed bug #66545 (DateTime. diff returns negative values) (Derick)
19+
. Fixed bug #68503 (date_diff on two dates with timezone set localised
20+
returns wrong results) (Derick)
21+
. Fixed bug #69806 (Incorrect date from timestamp) (Derick)
22+
. Fixed bug #71700 (Extra day on diff between begin and end of march 2016)
23+
(Derick)
24+
. Fixed bug #71826 (DateTime::diff confuse on timezone 'Asia/Tokyo') (Derick)
25+
. Fixed bug #73460 (Datetime add not realising it already applied DST
26+
change) (Derick)
27+
. Fixed bug #74173 (DateTimeImmutable::getTimestamp() triggers DST switch in
28+
incorrect time) (Derick)
29+
. Fixed bug #74274 (Handling DST transitions correctly) (Derick)
30+
. Fixed bug #74524 (Date diff is bad calculated, in same time zone) (Derick)
31+
. Fixed bug #75167 (DateTime::add does only care about backward DST
32+
transition, not forward) (Derick)
33+
. Fixed bug #76032 (DateTime->diff having issues with leap days for
34+
timezones ahead of UTC) (Derick)
35+
. Fixed bug #76374 (Date difference varies according day time) (Derick)
36+
. Fixed bug #77571 (DateTime's diff DateInterval incorrect in timezones from
37+
UTC+01:00 to UTC+12:00 (Derick)
38+
. Fixed bug #78452 (diff makes wrong in hour for Asia/Tehran) (Derick)
39+
. Fixed bug #79452 (DateTime::diff() generates months differently between
40+
time zones) (Derick)
41+
. Fixed bug #79698 (timelib mishandles future timestamps (triggered by 'zic
42+
-b slim')) (Derick)
43+
. Fixed bug #79716 (Invalid date time created (with day "00")) (Derick)
44+
. Fixed bug #80610 (DateTime calculate wrong with DateInterval) (Derick)
45+
. Fixed bug #80664 (DateTime objects behave incorrectly around DST
46+
transition) (Derick)
47+
. Fixed bug #80913 (DateTime(Immutable)::sub around DST yield incorrect
48+
time) (Derick)
49+
1350
- FPM:
1451
. Added openmetrics status format. (Cees-Jan Kiewiet)
1552
. Enable process renaming on macOS. (devnexen)

ext/date/config.w32

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
EXTENSION("date", "php_date.c", false, "/Iext/date/lib /DZEND_ENABLE_STATIC_TSRMLS_CACHE=1 /DHAVE_TIMELIB_CONFIG_H=1");
44
PHP_DATE = "yes";
5-
ADD_SOURCES("ext/date/lib", "astro.c timelib.c dow.c parse_date.c parse_tz.c tm2unixtime.c unixtime2tm.c parse_iso_intervals.c interval.c", "date");
5+
ADD_SOURCES("ext/date/lib", "astro.c timelib.c dow.c parse_date.c parse_posix.c parse_tz.c tm2unixtime.c unixtime2tm.c parse_iso_intervals.c interval.c", "date");
66

77
ADD_FLAG('CFLAGS_DATE', "/wd4244");
88

@@ -14,6 +14,7 @@ tl_config.WriteLine("#define timelib_malloc emalloc");
1414
tl_config.WriteLine("#define timelib_realloc erealloc");
1515
tl_config.WriteLine("#define timelib_calloc ecalloc");
1616
tl_config.WriteLine("#define timelib_strdup estrdup");
17+
tl_config.WriteLine("#define timelib_strndup estrndup");
1718
tl_config.WriteLine("#define timelib_free efree");
1819
tl_config.Close();
1920

ext/date/config0.m4

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ dnl Check for strtoll, atoll
55
AC_CHECK_FUNCS(strtoll atoll)
66

77
PHP_DATE_CFLAGS="-I@ext_builddir@/lib -DZEND_ENABLE_STATIC_TSRMLS_CACHE=1 -DHAVE_TIMELIB_CONFIG_H=1"
8-
timelib_sources="lib/astro.c lib/dow.c lib/parse_date.c lib/parse_tz.c
8+
timelib_sources="lib/astro.c lib/dow.c lib/parse_date.c lib/parse_tz.c lib/parse_posix.c
99
lib/timelib.c lib/tm2unixtime.c lib/unixtime2tm.c lib/parse_iso_intervals.c lib/interval.c"
1010

1111
PHP_NEW_EXTENSION(date, php_date.c $timelib_sources, no,, $PHP_DATE_CFLAGS)
@@ -31,5 +31,6 @@ cat > $ext_builddir/lib/timelib_config.h <<EOF
3131
#define timelib_realloc erealloc
3232
#define timelib_calloc ecalloc
3333
#define timelib_strdup estrdup
34+
#define timelib_strndup estrndup
3435
#define timelib_free efree
3536
EOF

ext/date/lib/LICENSE.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
The MIT License (MIT)
22

3-
Copyright (c) 2015-2019 Derick Rethans
4-
Copyright (c) 2017-2019 MongoDB, Inc.
3+
Copyright (c) 2015-2021 Derick Rethans
4+
Copyright (c) 2017-2019,2021 MongoDB, Inc.
55

66
Permission is hereby granted, free of charge, to any person obtaining a copy
77
of this software and associated documentation files (the "Software"), to deal

ext/date/lib/README.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,8 @@ information.
77

88
It is the library supporting PHP's Date/Time extension and MongoDB's time zone
99
support.
10+
11+
Build Requirements
12+
------------------
13+
14+
On Debian: ``apt install libcpputest-dev``

ext/date/lib/interval.c

Lines changed: 156 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,9 @@ timelib_rel_time *timelib_diff(timelib_time *one, timelib_time *two)
3030
{
3131
timelib_rel_time *rt;
3232
timelib_time *swp;
33-
timelib_sll dst_corr = 0 ,dst_h_corr = 0, dst_m_corr = 0;
34-
timelib_time one_backup, two_backup;
33+
timelib_sll dst_corr = 0, dst_h_corr = 0, dst_m_corr = 0;
34+
timelib_time_offset *trans = NULL;
35+
3536

3637
rt = timelib_rel_time_ctor();
3738
rt->invert = 0;
@@ -45,23 +46,10 @@ timelib_rel_time *timelib_diff(timelib_time *one, timelib_time *two)
4546
rt->invert = 1;
4647
}
4748

48-
/* Calculate correction for DST change over, but only if the TZ type is ID
49-
* and it's the same */
50-
if (one->zone_type == 3 && two->zone_type == 3
51-
&& (strcmp(one->tz_info->name, two->tz_info->name) == 0)
52-
&& (one->z != two->z))
53-
{
54-
dst_corr = two->z - one->z;
55-
dst_h_corr = dst_corr / 3600;
56-
dst_m_corr = (dst_corr % 3600) / 60;
57-
}
58-
59-
/* Save old TZ info */
60-
memcpy(&one_backup, one, sizeof(one_backup));
61-
memcpy(&two_backup, two, sizeof(two_backup));
62-
63-
timelib_apply_localtime(one, 0);
64-
timelib_apply_localtime(two, 0);
49+
/* Calculate correction for UTC offset changes between first and second SSE */
50+
dst_corr = two->z - one->z;
51+
dst_h_corr = dst_corr / 3600;
52+
dst_m_corr = (dst_corr % 3600) / 60;
6553

6654
rt->y = two->y - one->y;
6755
rt->m = two->m - one->m;
@@ -70,29 +58,87 @@ timelib_rel_time *timelib_diff(timelib_time *one, timelib_time *two)
7058
rt->i = two->i - one->i;
7159
rt->s = two->s - one->s;
7260
rt->us = two->us - one->us;
73-
if (one_backup.dst == 0 && two_backup.dst == 1 && two->sse >= one->sse + 86400 - dst_corr) {
74-
rt->h += dst_h_corr;
75-
rt->i += dst_m_corr;
76-
}
7761

7862
rt->days = fabs(floor((one->sse - two->sse - (dst_h_corr * 3600) - (dst_m_corr * 60)) / 86400));
7963

64+
/* Fall Back: Cater for transition period, where rt->invert is 0, but there are negative numbers */
65+
if (one->dst == 1 && two->dst == 0) {
66+
/* First for two "Type 3" times */
67+
if (one->zone_type == 3 && two->zone_type == 3) {
68+
trans = timelib_get_time_zone_info(two->sse, two->tz_info);
69+
if (trans) {
70+
if (one->sse >= trans->transition_time + dst_corr && one->sse < trans->transition_time) {
71+
timelib_sll flipped = SECS_PER_HOUR + (rt->i * 60) + (rt->s);
72+
rt->h = flipped / SECS_PER_HOUR;
73+
rt->i = (flipped - rt->h * SECS_PER_HOUR) / 60;
74+
rt->s = flipped % 60;
75+
}
76+
timelib_time_offset_dtor(trans);
77+
trans = NULL;
78+
}
79+
} else if (rt->h == 0 && (rt->i < 0 || rt->s < 0)) {
80+
/* Then for all the others */
81+
timelib_sll flipped = SECS_PER_HOUR + (rt->i * 60) + (rt->s);
82+
rt->h = flipped / SECS_PER_HOUR;
83+
rt->i = (flipped - rt->h * SECS_PER_HOUR) / 60;
84+
rt->s = flipped % 60;
85+
dst_corr += SECS_PER_HOUR;
86+
dst_h_corr++;
87+
}
88+
}
89+
8090
timelib_do_rel_normalize(rt->invert ? one : two, rt);
8191

82-
/* We need to do this after normalisation otherwise we can't get "24H" */
83-
if (one_backup.dst == 1 && two_backup.dst == 0 && two->sse >= one->sse + 86400) {
84-
if (two->sse < one->sse + 86400 - dst_corr) {
85-
rt->d--;
86-
rt->h = 24;
87-
} else {
88-
rt->h += dst_h_corr;
89-
rt->i += dst_m_corr;
92+
/* Do corrections for "Type 3" times */
93+
if (one->zone_type == 3 && two->zone_type == 3) {
94+
if (one->dst == 1 && two->dst == 0) { /* Fall Back */
95+
if (two->tz_info) {
96+
trans = timelib_get_time_zone_info(two->sse, two->tz_info);
97+
98+
if (
99+
trans &&
100+
two->sse >= trans->transition_time &&
101+
((two->sse - one->sse + dst_corr) % SECS_PER_DAY) > (two->sse - trans->transition_time)
102+
) {
103+
rt->h -= dst_h_corr;
104+
rt->i -= dst_m_corr;
105+
}
106+
}
107+
} else if (one->dst == 0 && two->dst == 1) { /* Spring Forward */
108+
if (two->tz_info) {
109+
trans = timelib_get_time_zone_info(two->sse, two->tz_info);
110+
111+
if (
112+
trans &&
113+
!((one->sse + SECS_PER_DAY > trans->transition_time) && (one->sse + SECS_PER_DAY <= (trans->transition_time + dst_corr))) &&
114+
two->sse >= trans->transition_time &&
115+
((two->sse - one->sse + dst_corr) % SECS_PER_DAY) > (two->sse - trans->transition_time)
116+
) {
117+
rt->h -= dst_h_corr;
118+
rt->i -= dst_m_corr;
119+
}
120+
}
121+
} else if (two->sse - one->sse >= SECS_PER_DAY) {
122+
/* Check whether we're in the period to the next transition time */
123+
trans = timelib_get_time_zone_info(two->sse - two->z, two->tz_info);
124+
dst_corr = one->z - trans->offset;
125+
126+
if (two->sse >= trans->transition_time - dst_corr && two->sse < trans->transition_time) {
127+
rt->d--;
128+
rt->h = 24;
129+
}
90130
}
131+
} else {
132+
/* Then for all the others */
133+
rt->h -= dst_h_corr + (two->dst - one->dst);
134+
rt->i -= dst_m_corr;
135+
136+
timelib_do_rel_normalize(rt->invert ? one : two, rt);
91137
}
92138

93-
/* Restore old TZ info */
94-
memcpy(one, &one_backup, sizeof(one_backup));
95-
memcpy(two, &two_backup, sizeof(two_backup));
139+
if (trans) {
140+
timelib_time_offset_dtor(trans);
141+
}
96142

97143
return rt;
98144
}
@@ -122,13 +168,6 @@ timelib_time *timelib_add(timelib_time *old_time, timelib_rel_time *interval)
122168

123169
timelib_update_ts(t, NULL);
124170

125-
// printf("%lld %lld %d\n", old_time->dst, t->dst, (t->sse - old_time->sse));
126-
/* Adjust for backwards DST changeover */
127-
if (old_time->dst == 1 && t->dst == 0 && !interval->y && !interval->m && !interval->d) {
128-
t->sse -= old_time->z;
129-
t->sse += t->z;
130-
}
131-
132171
timelib_update_from_sse(t);
133172
t->have_relative = 0;
134173

@@ -157,19 +196,86 @@ timelib_time *timelib_sub(timelib_time *old_time, timelib_rel_time *interval)
157196

158197
timelib_update_ts(t, NULL);
159198

160-
/* Adjust for backwards DST changeover */
161-
if (old_time->dst == 1 && t->dst == 0 && !interval->y && !interval->m && !interval->d) {
162-
t->sse -= old_time->z;
163-
t->sse += t->z;
164-
}
165-
/* Adjust for forwards DST changeover */
166-
if (old_time->dst == 0 && t->dst == 1 && !interval->y && !interval->m && !interval->d ) {
167-
t->sse -= old_time->z;
168-
t->sse += t->z;
199+
timelib_update_from_sse(t);
200+
201+
t->have_relative = 0;
202+
203+
return t;
204+
}
205+
206+
timelib_time *timelib_add_wall(timelib_time *old_time, timelib_rel_time *interval)
207+
{
208+
int bias = 1;
209+
timelib_time *t = timelib_time_clone(old_time);
210+
211+
t->have_relative = 1;
212+
t->sse_uptodate = 0;
213+
214+
if (interval->have_weekday_relative || interval->have_special_relative) {
215+
memcpy(&t->relative, interval, sizeof(timelib_rel_time));
216+
217+
timelib_update_ts(t, NULL);
218+
} else {
219+
if (interval->invert) {
220+
bias = -1;
221+
}
222+
memset(&t->relative, 0, sizeof(timelib_rel_time));
223+
t->relative.y = interval->y * bias;
224+
t->relative.m = interval->m * bias;
225+
t->relative.d = interval->d * bias;
226+
227+
if (t->relative.y || t->relative.m || t->relative.d) {
228+
timelib_update_ts(t, NULL);
229+
}
230+
231+
t->sse += bias * timelib_hms_to_seconds(interval->h, interval->i, interval->s);
232+
t->us += interval->us * bias;
233+
timelib_do_normalize(t);
169234
}
170235

171236
timelib_update_from_sse(t);
237+
if (t->zone_type == TIMELIB_ZONETYPE_ID) {
238+
timelib_set_timezone(t, t->tz_info);
239+
}
240+
t->have_relative = 0;
241+
242+
return t;
243+
}
244+
245+
timelib_time *timelib_sub_wall(timelib_time *old_time, timelib_rel_time *interval)
246+
{
247+
int bias = 1;
248+
timelib_time *t = timelib_time_clone(old_time);
172249

250+
t->have_relative = 1;
251+
t->sse_uptodate = 0;
252+
253+
if (interval->have_weekday_relative || interval->have_special_relative) {
254+
memcpy(&t->relative, interval, sizeof(timelib_rel_time));
255+
256+
timelib_update_ts(t, NULL);
257+
} else {
258+
if (interval->invert) {
259+
bias = -1;
260+
}
261+
memset(&t->relative, 0, sizeof(timelib_rel_time));
262+
t->relative.y = 0 - (interval->y * bias);
263+
t->relative.m = 0 - (interval->m * bias);
264+
t->relative.d = 0 - (interval->d * bias);
265+
266+
if (t->relative.y || t->relative.m || t->relative.d) {
267+
timelib_update_ts(t, NULL);
268+
}
269+
270+
t->sse -= bias * timelib_hms_to_seconds(interval->h, interval->i, interval->s);
271+
t->us -= interval->us * bias;
272+
timelib_do_normalize(t);
273+
}
274+
275+
timelib_update_from_sse(t);
276+
if (t->zone_type == TIMELIB_ZONETYPE_ID) {
277+
timelib_set_timezone(t, t->tz_info);
278+
}
173279
t->have_relative = 0;
174280

175281
return t;

0 commit comments

Comments
 (0)