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