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