Skip to content

Commit 2e3a421

Browse files
committed
tests/ansi: add test case for TZ environment variable behaviour
1 parent 8d96d92 commit 2e3a421

File tree

3 files changed

+63
-0
lines changed

3 files changed

+63
-0
lines changed

tests/ansi/tz.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#include <stdio.h>
2+
#include <time.h>
3+
4+
extern int daylight;
5+
extern long timezone;
6+
extern char *tzname[2];
7+
8+
int main() {
9+
tzset();
10+
printf("%s %s %ld %d\n", tzname[0], tzname[1], timezone, daylight);
11+
return 0;
12+
}

tests/ansi/tz.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import subprocess
2+
import sys
3+
import os
4+
from pyexpect import expect
5+
6+
wrapper = os.getenv("MESON_EXE_WRAPPER")
7+
wrapper = [x for x in (wrapper,) if x]
8+
9+
def check_tz(tz, expected):
10+
output = subprocess.check_output(
11+
wrapper + [sys.argv[1]],
12+
stderr=subprocess.STDOUT,
13+
env={"TZ": tz},
14+
)
15+
16+
expect(expected).to_equal(output)
17+
18+
check_tz("UTC0", b"UTC UTC 0 0\n")
19+
check_tz("GMT0", b"GMT GMT 0 0\n")
20+
check_tz(":UTC0", b"UTC UTC 0 0\n")
21+
check_tz("EST+5", b"EST EST 18000 0\n")
22+
check_tz("EET-2", b"EET EET -7200 0\n")
23+
check_tz("<+03>-03", b"+03 +03 -10800 0\n")
24+
check_tz("NPT-05:45", b"NPT NPT -20700 0\n")
25+
check_tz("NST+03:30NDT", b"NST NDT 12600 1\n")
26+
check_tz("NZST-12NZDT-13", b"NZST NZDT -43200 1\n")
27+
check_tz("<PST-8>+8<PDT-7>+7,M3.2.0,M11.1.0", b"PST-8 PDT-7 28800 1\n")
28+
29+
# These depend on tzdata, which may or may not exist.
30+
# Useful to check every now and then, but these also present subtle differences
31+
# between the host libc and mlibc, so we can't expect these to always hold up.
32+
# E.g. glibc will set `tzname` from the TZ environment variable even if it
33+
# cannot find the tzinfo file, however mlibc will use the name of the timezone
34+
# as read from the tzinfo file or the timezone that it defaults to (UTC).
35+
36+
# Would be b"Universal 0 0\n" on glibc.
37+
# check_tz("", b"UTC UTC 0 0\n")
38+
39+
# Would be b"UTC 0 0\n" on glibc.
40+
# check_tz("UTC", b"UTC UTC 0 0\n")
41+
# check_tz(":UTC", b"UTC UTC 0 0\n")
42+
43+
# Would be b"Universal 0 0\n" on glibc.
44+
# check_tz("Universal", b"UTC UTC 0 0\n")
45+
# check_tz(":Universal", b"UTC UTC 0 0\n")
46+
47+
# Would be b"ATimeZoneThatShouldHopefullyNeverBeInTzdata 0 0\n" on glibc.
48+
# check_tz("ATimeZoneThatShouldHopefullyNeverBeInTzdata", b"UTC UTC 0 0\n")
49+
# check_tz(":ATimeZoneThatShouldHopefullyNeverBeInTzdata", b"UTC UTC 0 0\n")

tests/meson.build

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ all_test_cases = [
1818
'ansi/wcsrtombs',
1919
'ansi/wmemcmp',
2020
'ansi/timegm',
21+
'ansi/tz',
2122
'ansi/ungetc',
2223
'ansi/wcsdup',
2324
'ansi/wcsncasecmp',
@@ -130,6 +131,7 @@ fail_test_cases = [
130131
]
131132

132133
wrapped_test_cases = [
134+
'ansi/tz',
133135
'glibc/error',
134136
'glibc/error_at_line',
135137
]

0 commit comments

Comments
 (0)