Home | History | Annotate | Line # | Download | only in unit-tests
varmod-gmtime.mk revision 1.27
      1 # $NetBSD: varmod-gmtime.mk,v 1.27 2025/01/11 20:54:45 rillig Exp $
      2 #
      3 # Tests for the :gmtime variable modifier, which formats a timestamp
      4 # using strftime(3) in UTC.
      5 #
      6 # See also:
      7 #	varmod-localtime.mk
      8 
      9 .if ${TZ:Uundefined} != "undefined"	# 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:gmtime:tW:M??? ??? ?? ??\:??\:?? ????} == ""
     18 .  error
     19 .endif
     20 
     21 
     22 # modifier name too short, falling back to the SysV modifier.
     23 .if ${%Y:L:gmtim=1593536400} != "%Y"
     24 .  error
     25 .endif
     26 
     27 
     28 # 2020-07-01T00:00:00Z
     29 .if ${%Y:L:gmtime=1593536400} != "2020"
     30 .  error
     31 .endif
     32 
     33 
     34 # modifier name too long, falling back to the SysV modifier.
     35 .if ${%Y:L:gmtimer=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 ${gmtime:L:gm%=local%} != "localtime"
     43 .  error
     44 .endif
     45 
     46 
     47 # Before var.c 1.1050 from 2023-05-09, it was not possible to pass the
     48 # seconds via an expression.
     49 .if ${%Y:L:gmtime=${:U1593536400}} != "2020"
     50 .  error
     51 .endif
     52 
     53 
     54 # Before var.c 1.631 from 2020-10-31 21:40:20, it was possible to pass
     55 # negative time stamps to the :gmtime modifier, resulting in dates before
     56 # 1970.  Going back 50 years in the past is not a practical use case for
     57 # make.  Therefore, since var.c 1.631, negative time stamps produce a
     58 # parse error.
     59 # expect+1: Invalid time value "-1"
     60 .if ${:L:gmtime=-1} != ""
     61 .  error
     62 .else
     63 .  error
     64 .endif
     65 
     66 
     67 # Spaces were allowed before var.c 1.631 from 2020-10-31 21:40:20, not
     68 # because it would make sense but just as a side-effect from using strtoul.
     69 # expect+1: Invalid time value " 1"
     70 .if ${:L:gmtime= 1} != ""
     71 .  error
     72 .else
     73 .  error
     74 .endif
     75 
     76 
     77 # 0 means now; this differs from GNode.mtime, where a 0 means nonexistent.
     78 # Since "now" constantly changes, the strongest possible test is to match the
     79 # resulting pattern.
     80 .if !${:L:gmtime=0:tW:M??? ??? ?? ??\:??\:?? 20??}
     81 .  error
     82 .endif
     83 
     84 
     85 .if ${:L:gmtime=1} != "Thu Jan  1 00:00:01 1970"
     86 .  error
     87 .endif
     88 
     89 
     90 # INT32_MAX
     91 .if ${:L:gmtime=2147483647} != "Tue Jan 19 03:14:07 2038"
     92 .  error
     93 .endif
     94 
     95 
     96 .if ${:L:gmtime=2147483648} == "Tue Jan 19 03:14:08 2038"
     97 # All systems that have unsigned time_t or 64-bit time_t.
     98 .elif ${:L:gmtime=2147483648} == "Fri Dec 13 20:45:52 1901"
     99 # FreeBSD-12.0-i386 still has 32-bit signed time_t, see
    100 # sys/x86/include/_types.h, __LP64__.
    101 #
    102 # Linux on 32-bit systems may still have 32-bit signed time_t, see
    103 # sysdeps/unix/sysv/linux/generic/bits/typesizes.h, __TIMESIZE.
    104 .else
    105 .  error
    106 .endif
    107 
    108 
    109 # Integer overflow, at least before var.c 1.631 from 2020-10-31.
    110 # Because this modifier is implemented using strtoul, the parsed time was
    111 # ULONG_MAX, which got converted to -1.  This resulted in a time stamp of
    112 # the second before 1970.
    113 #
    114 # Since var.c 1.631 from 2020-10-31, the overflow is detected and produces a
    115 # parse error.
    116 # expect+1: Invalid time value "10000000000000000000000000000000"
    117 .if ${:L:gmtime=10000000000000000000000000000000} != ""
    118 .  error
    119 .else
    120 .  error
    121 .endif
    122 
    123 # Before var.c 1.631 from 2020-10-31, there was no error handling while
    124 # parsing the :gmtime modifier, thus no error message was printed.  Parsing
    125 # stopped after the '=', and the remaining string was parsed for more variable
    126 # modifiers.  Because of the unknown modifier 'e' from the 'error', the whole
    127 # variable value was discarded and thus not printed.
    128 # expect+1: Invalid time value "error"
    129 .if ${:L:gmtime=error} != ""
    130 .  error
    131 .else
    132 .  error
    133 .endif
    134 
    135 # Before var.c 1.1050 from 2023-05-09, the timestamp could be directly
    136 # followed by the next modifier, without a ':' separator.  This was the same
    137 # bug as for the ':L' and ':P' modifiers.
    138 # expect+1: Invalid time value "100000S,1970,bad,"
    139 .if ${%Y:L:gmtime=100000S,1970,bad,} != "bad"
    140 .  error
    141 .endif
    142 
    143 
    144 # Before var.c 1.1062 from 2023-08-19, ':gmtime' but not ':localtime' reported
    145 # wrong values for '%s', depending on the operating system and the timezone.
    146 export TZ=UTC
    147 .for t in ${%s:L:gmtime} ${%s:L:localtime}
    148 TIMESTAMPS+= $t
    149 .endfor
    150 export TZ=Europe/Berlin
    151 .for t in ${%s:L:gmtime} ${%s:L:localtime}
    152 TIMESTAMPS+= $t
    153 .endfor
    154 export TZ=UTC
    155 .for t in ${%s:L:gmtime} ${%s:L:localtime}
    156 TIMESTAMPS+= $t
    157 .endfor
    158 export TZ=America/Los_Angeles
    159 .for t in ${%s:L:gmtime} ${%s:L:localtime}
    160 TIMESTAMPS+= $t
    161 .endfor
    162 export TZ=UTC
    163 .for t in ${%s:L:gmtime} ${%s:L:localtime}
    164 TIMESTAMPS+= $t
    165 .endfor
    166 .for a b in ${TIMESTAMPS:[1]} ${TIMESTAMPS:@t@$t $t@} ${TIMESTAMPS:[-1]}
    167 .  if $a > $b
    168 .    warning timestamp $a > $b
    169 .  endif
    170 .endfor
    171 
    172 
    173 .if ${year=%Y month=%m day=%d:L:gmtime=1459494000} != "year=2016 month=04 day=01"
    174 .  error
    175 .endif
    176 # Slightly contorted syntax to convert a UTC timestamp from an expression to a
    177 # formatted timestamp.
    178 .if ${%Y%m%d:L:${gmtime=${:U1459494000}:L}} != "20160401"
    179 .  error
    180 .endif
    181 
    182 
    183 all:
    184