varmod-gmtime.mk revision 1.16 1 # $NetBSD: varmod-gmtime.mk,v 1.16 2023/08/19 08:19:25 rillig Exp $
2 #
3 # Tests for the :gmtime variable modifier, which formats a timestamp
4 # using strftime(3) in UTC.
5 #
6 # See also:
7 # varmod-localtime.mk
8
9 .if ${TZ:Uundefined} != "undefined" # see unit-tests/Makefile
10 . error
11 .endif
12
13 # Test for the default time format, %c. Since the time always varies, it's
14 # only possible to check for the general format here. The names of the
15 # month and weekday are always in English, independent from the locale.
16 # Example: Thu Oct 29 18:56:41 2020
17 .if ${:U:gmtime:tW:M??? ??? ?? ??\:??\:?? ????} == ""
18 . error
19 .endif
20
21
22 # modifier name too short, falling back to the SysV modifier.
23 .if ${%Y:L:gmtim=1593536400} != "%Y"
24 . error
25 .endif
26
27
28 # 2020-07-01T00:00:00Z
29 .if ${%Y:L:gmtime=1593536400} != "2020"
30 . error
31 .endif
32
33
34 # modifier name too long, falling back to the SysV modifier.
35 .if ${%Y:L:gmtimer=1593536400} != "%Y"
36 . error
37 .endif
38
39
40 # If the modifier name is not matched exactly, fall back to the
41 # :from=to modifier.
42 .if ${gmtime:L:gm%=local%} != "localtime"
43 . error
44 .endif
45
46
47 # Before var.c 1.1050 from 2023-05-09, it was not possible to pass the
48 # seconds via a variable expression.
49 .if ${%Y:L:gmtime=${:U1593536400}} != "2020"
50 . error
51 .endif
52
53
54 # Before var.c 1.631 from 2020-10-31 21:40:20, it was possible to pass
55 # negative time stamps to the :gmtime modifier, resulting in dates before
56 # 1970. Going back 50 years in the past is not a practical use case for
57 # make. Therefore, since var.c 1.631, negative time stamps produce a
58 # parse error.
59 # expect+2: Invalid time value "-1"
60 # expect+1: Malformed conditional (${:L:gmtime=-1} != "")
61 .if ${:L:gmtime=-1} != ""
62 . error
63 .else
64 . error
65 .endif
66
67
68 # Spaces were allowed before var.c 1.631 from 2020-10-31 21:40:20, not
69 # because it would make sense but just as a side-effect from using strtoul.
70 # expect+2: Invalid time value " 1"
71 # expect+1: Malformed conditional (${:L:gmtime= 1} != "")
72 .if ${:L:gmtime= 1} != ""
73 . error
74 .else
75 . error
76 .endif
77
78
79 # 0 means now; this differs from GNode.mtime, where a 0 means nonexistent.
80 # Since "now" constantly changes, the strongest possible test is to match the
81 # resulting pattern.
82 .if !${:L:gmtime=0:tW:M??? ??? ?? ??\:??\:?? 20??}
83 . error
84 .endif
85
86
87 .if ${:L:gmtime=1} != "Thu Jan 1 00:00:01 1970"
88 . error
89 .endif
90
91
92 # INT32_MAX
93 .if ${:L:gmtime=2147483647} != "Tue Jan 19 03:14:07 2038"
94 . error
95 .endif
96
97
98 .if ${:L:gmtime=2147483648} == "Tue Jan 19 03:14:08 2038"
99 # All systems that have unsigned time_t or 64-bit time_t.
100 .elif ${:L:gmtime=2147483648} == "Fri Dec 13 20:45:52 1901"
101 # FreeBSD-12.0-i386 still has 32-bit signed time_t, see
102 # sys/x86/include/_types.h, __LP64__.
103 #
104 # Linux on 32-bit systems may still have 32-bit signed time_t, see
105 # sysdeps/unix/sysv/linux/generic/bits/typesizes.h, __TIMESIZE.
106 .else
107 . error
108 .endif
109
110
111 # Integer overflow, at least before var.c 1.631 from 2020-10-31.
112 # Because this modifier is implemented using strtoul, the parsed time was
113 # ULONG_MAX, which got converted to -1. This resulted in a time stamp of
114 # the second before 1970.
115 #
116 # Since var.c 1.631 from 2020-10-31, the overflow is detected and produces a
117 # parse error.
118 # expect+2: Invalid time value "10000000000000000000000000000000"
119 # expect+1: Malformed conditional (${:L:gmtime=10000000000000000000000000000000} != "")
120 .if ${:L:gmtime=10000000000000000000000000000000} != ""
121 . error
122 .else
123 . error
124 .endif
125
126 # Before var.c 1.631 from 2020-10-31, there was no error handling while
127 # parsing the :gmtime modifier, thus no error message was printed. Parsing
128 # stopped after the '=', and the remaining string was parsed for more variable
129 # modifiers. Because of the unknown modifier 'e' from the 'error', the whole
130 # variable value was discarded and thus not printed.
131 # expect+2: Invalid time value "error"
132 # expect+1: Malformed conditional (${:L:gmtime=error} != "")
133 .if ${:L:gmtime=error} != ""
134 . error
135 .else
136 . error
137 .endif
138
139 # Before var.c 1.1050 from 2023-05-09, the timestamp could be directly
140 # followed by the next modifier, without a ':' separator. This was the same
141 # bug as for the ':L' and ':P' modifiers.
142 # expect+2: Invalid time value "100000S,1970,bad,"
143 # expect+1: Malformed conditional (${%Y:L:gmtime=100000S,1970,bad,} != "bad")
144 .if ${%Y:L:gmtime=100000S,1970,bad,} != "bad"
145 . error
146 .endif
147
148
149 # As of 2023-08-19, ':gmtime' but not ':localtime' reports wrong values for
150 # '%s', depending on the operating system and the timezone, as demonstrated by
151 # the following test program:
152 #
153 # for mod in gmtime localtime; do
154 # for tz in UTC Europe/Berlin America/Los_Angeles; do
155 # TZ=$tz bmake -r -v "\${%F %T %z %s $mod $tz:L:$mod}"
156 # done
157 # done
158 #
159 # Cygwin:
160 # 2023-08-19 07:34:06 +0000 1692430446 gmtime UTC
161 # 2023-08-19 07:34:06 +0000 1692430446 gmtime Europe/Berlin
162 # 2023-08-19 07:34:06 +0000 1692430446 gmtime America/Los_Angeles
163 # 2023-08-19 07:34:06 +0000 1692430446 localtime UTC
164 # 2023-08-19 09:34:07 +0200 1692430447 localtime Europe/Berlin
165 # 2023-08-19 00:34:07 -0700 1692430447 localtime America/Los_Angeles
166 #
167 # Looks good:
168 # * ':gmtime' consistently reports timezone offset '+0000'.
169 # * '%s' is independent of the timezone.
170 #
171 # NetBSD 10.99:
172 # 2023-08-19 07:34:37 +0000 1692430477 gmtime UTC
173 # 2023-08-19 07:34:37 +0100 1692426877 gmtime Europe/Berlin
174 # 2023-08-19 07:34:37 -0800 1692459277 gmtime America/Los_Angeles
175 # 2023-08-19 07:34:37 +0000 1692430477 localtime UTC
176 # 2023-08-19 09:34:37 +0200 1692430477 localtime Europe/Berlin
177 # 2023-08-19 00:34:37 -0700 1692430477 localtime America/Los_Angeles
178 #
179 # Looks bad:
180 # * ':gmtime' reports different timezone offsets.
181 # * ':gmtime' reports different seconds since the Epoch.
182 # * ':gmtime' reports the timezone offset '+0100' for Europe/Berlin,
183 # even though at 2023-08-19, DST with offset '+0200' was in place.
184 #
185 # Debian:
186 # 2023-08-19 07:29:10 +0000 1692430150 gmtime UTC
187 # 2023-08-19 07:29:10 +0000 1692426550 gmtime Europe/Berlin
188 # 2023-08-19 07:29:10 +0000 1692458950 gmtime America/Los_Angeles
189 # 2023-08-19 07:29:10 +0000 1692430150 localtime UTC
190 # 2023-08-19 09:29:10 +0200 1692430150 localtime Europe/Berlin
191 # 2023-08-19 00:29:10 -0700 1692430150 localtime America/Los_Angeles
192 #
193 # Looks mixed:
194 # * ':gmtime' reports the correct timezone offset '+0000'.
195 # * ':gmtime' reports different seconds since the Epoch, and the '%s'
196 # value cannot be derived from the '%F %T %z' values.
197
198 all:
199