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