Home | History | Annotate | Line # | Download | only in unit-tests
varmod-localtime.mk revision 1.9
      1 # $NetBSD: varmod-localtime.mk,v 1.9 2023/05/09 08:26:14 rillig Exp $
      2 #
      3 # Tests for the :localtime variable modifier, which formats a timestamp
      4 # using strftime(3) in local time.
      5 #
      6 # See also:
      7 #	varmod-gmtime.mk
      8 
      9 .if ${TZ} != "Europe/Berlin"	# see unit-tests/Makefile
     10 .  error
     11 .endif
     12 
     13 # Test for the default time format, %c.  Since the time always varies, it's
     14 # only possible to check for the general format here.  The names of the
     15 # month and weekday are always in English, independent from the locale.
     16 # Example: Thu Oct 29 18:56:41 2020
     17 .if ${:U:localtime:tW:M??? ??? ?? ??\:??\:?? ????} == ""
     18 .  error
     19 .endif
     20 
     21 
     22 # modifier name too short, falling back to the SysV modifier.
     23 .if ${%Y:L:localtim=1593536400} != "%Y"
     24 .  error
     25 .endif
     26 
     27 
     28 # 2020-07-01T00:00:00Z
     29 .if ${%Y:L:localtime=1593536400} != "2020"
     30 .  error
     31 .endif
     32 
     33 
     34 # modifier name too long, falling back to the SysV modifier.
     35 .if ${%Y:L:localtimer=1593536400} != "%Y"
     36 .  error
     37 .endif
     38 
     39 
     40 # If the modifier name is not matched exactly, fall back to the
     41 # :from=to modifier.
     42 .if ${localtime:L:local%=gm%} != "gmtime"
     43 .  error
     44 .endif
     45 
     46 
     47 # As of 2020-08-16, it is not possible to pass the seconds via a
     48 # variable expression.  This is because parsing of the :localtime
     49 # modifier stops at the '$' and returns to ApplyModifiers.
     50 #
     51 # There, a colon would be skipped but not a dollar.
     52 # Parsing therefore continues at the '$' of the ${:U159...}, looking
     53 # for an ordinary variable modifier.
     54 #
     55 # At this point, the ${:U} is expanded and interpreted as a variable
     56 # modifier, which results in the error message "Unknown modifier '1'".
     57 #
     58 # If ApplyModifier_Localtime were to pass its argument through
     59 # ParseModifierPart, this would work.
     60 .if ${%Y:L:localtime=${:U1593536400}} != "mtime=11593536400}"
     61 .  error
     62 .endif
     63 
     64 
     65 # Before var.c 1.631 from 2020-10-31 21:40:20, it was possible to pass
     66 # negative time stamps to the :localtime modifier, resulting in dates before
     67 # 1970.  Going back 50 years in the past is not a practical use case for
     68 # make.  Therefore, since var.c 1.631, negative time stamps produce a
     69 # parse error.
     70 .if ${:L:localtime=-1} != ""
     71 .  error
     72 .else
     73 .  error
     74 .endif
     75 
     76 
     77 # Spaces were allowed before var.c 1.631 from 2020-10-31 21:40:20, not
     78 # because it would make sense but just as a side-effect from using strtoul.
     79 .if ${:L:localtime= 1} != ""
     80 .  error
     81 .else
     82 .  error
     83 .endif
     84 
     85 
     86 # 0 means now; this differs from GNode.mtime, where a 0 means nonexistent.
     87 # Since "now" constantly changes, the strongest possible test is to match the
     88 # resulting pattern.
     89 .if !${:L:localtime=0:tW:M??? ??? ?? ??\:??\:?? 20??}
     90 .  error
     91 .endif
     92 
     93 
     94 .if ${:L:localtime=1} != "Thu Jan  1 01:00:01 1970"
     95 .  error
     96 .endif
     97 
     98 
     99 # INT32_MAX
    100 .if ${:L:localtime=2147483647} != "Tue Jan 19 04:14:07 2038"
    101 .  error
    102 .endif
    103 
    104 
    105 .if ${:L:localtime=2147483648} == "Tue Jan 19 04:14:08 2038"
    106 # All systems that have unsigned time_t or 64-bit time_t.
    107 .elif ${:L:localtime=2147483648} == "Fri Dec 13 21:45:52 1901"
    108 # FreeBSD-12.0-i386 still has 32-bit signed time_t, see
    109 # sys/x86/include/_types.h, __LP64__.
    110 #
    111 # Linux on 32-bit systems may still have 32-bit signed time_t, see
    112 # sysdeps/unix/sysv/linux/generic/bits/typesizes.h, __TIMESIZE.
    113 .else
    114 .  error
    115 .endif
    116 
    117 
    118 # Integer overflow, at least before var.c 1.631 from 2020-10-31.
    119 # Because this modifier is implemented using strtoul, the parsed time was
    120 # ULONG_MAX, which got converted to -1.  This resulted in a time stamp of
    121 # the second before 1970.
    122 #
    123 # Since var.c 1.631 from 2020-10-31, the overflow is detected and produces a
    124 # parse error.
    125 .if ${:L:localtime=10000000000000000000000000000000} != ""
    126 .  error
    127 .else
    128 .  error
    129 .endif
    130 
    131 # Before var.c 1.631 from 2020-10-31, there was no error handling while
    132 # parsing the :localtime modifier, thus no error message was printed.  Parsing
    133 # stopped after the '=', and the remaining string was parsed for more variable
    134 # modifiers.  Because of the unknown modifier 'e' from the 'error', the whole
    135 # variable value was discarded and thus not printed.
    136 .if ${:L:localtime=error} != ""
    137 .  error
    138 .else
    139 .  error
    140 .endif
    141 
    142 # Before var.c 1.TODO from XXXX-XX-XX, the timestamp could be directly
    143 # followed by the next modifier, without a ':' separator.  This is the same
    144 # bug as for the ':L' and ':P' modifiers.
    145 .if ${%Y:L:localtime=100000S,1970,bad,} != "bad"
    146 .  error
    147 .endif
    148 
    149 all:
    150