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