xref: /freebsd/contrib/bmake/unit-tests/varmod-gmtime.mk (revision 36d6566e5985030fd2f1100bd9c1387bbe0bd290)
1# $NetBSD: varmod-gmtime.mk,v 1.6 2020/10/31 20:30:06 rillig Exp $
2#
3# Tests for the :gmtime variable modifier, which formats a timestamp
4# using strftime(3) in UTC.
5
6all:	mod-gmtime
7all:	mod-gmtime-indirect
8all:	parse-errors
9
10# Test for the default time format, %c.  Since the time always varies, it's
11# only possible to check for the general format here.  The names of the
12# month and weekday are always in English, independent from the locale.
13# Example: Thu Oct 29 18:56:41 2020
14.if ${:U:gmtime:tW:M??? ??? ?? ??\:??\:?? ????} == ""
15.  error
16.endif
17
18mod-gmtime:
19	@echo $@:
20
21	# modifier name too short
22	@echo ${%Y:L:gmtim=1593536400}
23
24	# 2020-07-01T00:00:00Z
25	@echo ${%Y:L:gmtime=1593536400}
26
27	# modifier name too long
28	@echo ${%Y:L:gmtimer=1593536400}
29
30	# If the modifier name is not matched exactly, fall back to the
31	# :from=to modifier.
32	@echo ${gmtime:L:gm%=local%} == localtime
33
34mod-gmtime-indirect:
35	@echo $@:
36
37	# As of 2020-08-16, it is not possible to pass the seconds via a
38	# variable expression.  This is because parsing of the :gmtime
39	# modifier stops at the '$' and returns to ApplyModifiers.
40	#
41	# There, a colon would be skipped but not a dollar.
42	# Parsing therefore continues at the '$' of the ${:U159...}, looking
43	# for an ordinary variable modifier.
44	#
45	# At this point, the ${:U} is expanded and interpreted as a variable
46	# modifier, which results in the error message "Unknown modifier '1'".
47	#
48	# If ApplyModifier_Gmtime were to pass its argument through
49	# ParseModifierPart, this would work.
50	@echo ${%Y:L:gmtime=${:U1593536400}}
51
52parse-errors:
53	@echo $@:
54
55	# As of 2020-10-31, it is possible to pass negative time stamps
56	# to the :gmtime modifier, resulting in dates before 1970.
57	# Going back 50 years in the past is not a practical use case for
58	# make.
59	: -1 becomes ${:L:gmtime=-1}.
60
61	# Spaces are allowed, not because it would make sense but just as
62	# a side-effect from using strtoul.
63	: space 1 becomes ${:L:gmtime= 1}.
64
65	# 0 means now; to get consistent test results, the actual value has
66	# to be normalized.
67	: 0 becomes ${:L:gmtime=0:C,^... ... .. ..:..:.. 20..$,ok,W}.
68
69	: 1 becomes ${:L:gmtime=1}.
70
71	: INT32_MAX becomes ${:L:gmtime=2147483647}.
72
73	# This may be different if time_t is still a 32-bit signed integer.
74	: INT32_MAX + 1 becomes ${:L:gmtime=2147483648}.
75
76	# Integer overflow.
77	# Because this modifier is implemented using strtoul, the parsed
78	# time is ULONG_MAX, which gets converted to -1.  This results
79	# in a time stamp of the second before 1970.
80	: overflow becomes ${:L:gmtime=10000000000000000000000000000000}.
81
82	# As of 2020-10-31, there is no error handling while parsing the
83	# :gmtime modifier, thus no error message is printed.  Parsing
84	# stops after the '=', and the remaining string is parsed for
85	# more variable modifiers.  Because of the unknown modifier 'e',
86	# the whole variable value is discarded and thus not printed.
87	: letter becomes ${:L:gmtime=error}.
88