xref: /illumos-gate/usr/src/test/tz-tests/tests/basic_tzs.ksh (revision 5debf020cfb76b4873f9e7726caf914aebe00dc8)
1#!/usr/bin/ksh
2#
3# This file and its contents are supplied under the terms of the
4# Common Development and Distribution License ("CDDL"), version 1.0.
5# You may only use this file in accordance with the terms of version
6# 1.0 of the CDDL.
7#
8# A full copy of the text of the CDDL should have accompanied this
9# source.  A copy of the CDDL is also available via the Internet at
10# http://www.illumos.org/license/CDDL.
11#
12
13#
14# Copyright 2025 Oxide Computer Company
15#
16
17#
18# This takes a few historical transition points in US time zones and verifies
19# that we still see the expected date formats for a given POSIX time.  In
20# particular we look at DST transitions before and after the Energy Policy Act
21# of 2005 which changed the range of time that DST was in effect for.
22#
23# In addition, we use a few different time zones with the same time_t value to
24# confirm that basic time zone functionality works. We also use some POSIX time
25# zone strings. Note, strings that begin with a ':' according to POSIX use an
26# implementation defined scheme, which for us is the same as the normal zoneinfo
27# database.
28#
29
30unalias -a
31set -o pipefail
32export LANG=C.UTF-8
33
34#
35# Program paths that we allow someone to interpose on.
36#
37DATE=${DATE:-"/usr/bin/date"}
38
39btz_exit=0
40
41typeset -A btz_tzs=(
42	["CHI (0)"]=(zone="America/Chicago" ts="834971400"
43	    res="Sun Jun 16 19:30:00 CDT 1996")
44	["CHI (1)"]=(zone="America/Chicago" ts="866253600"
45	    res="Fri Jun 13 21:00:00 CDT 1997")
46	["CHI (2)"]=(zone="America/Chicago" ts="897876000"
47	    res="Sun Jun 14 21:00:00 CDT 1998")
48	["CHI (3)"]=(zone="CST6CDT,M3.2.0/2:00:00,M11.1.0/2:00:00"
49	    ts="834971400" res="Sun Jun 16 19:30:00 CDT 1996")
50	["CHI (4)"]=(zone="CST6CDT,M3.2.0/2:00:00,M11.1.0/2:00:00"
51	    ts="866253600" res="Fri Jun 13 21:00:00 CDT 1997")
52	["CHI (5)"]=(zone="CST6CDT,M3.2.0/2:00:00,M11.1.0/2:00:00"
53	    ts="866253600" res="Fri Jun 13 21:00:00 CDT 1997")
54	["PST (0)"]=(zone="US/Pacific" ts="1080738123"
55	    res="Wed Mar 31 05:02:03 PST 2004")
56	["PST (1)"]=(zone="US/Pacific" ts="1082728984"
57	    res="Fri Apr 23 07:03:04 PDT 2004")
58	["PST (2)"]=(zone="US/Pacific" ts="1680267723"
59	    res="Fri Mar 31 06:02:03 PDT 2023")
60	["PST (3)"]=(zone="US/Pacific" ts="1682258584"
61	    res="Sun Apr 23 07:03:04 PDT 2023")
62	["PST (4)"]=(zone="US/Pacific" ts="969650527"
63	    res="Fri Sep 22 12:22:07 PDT 2000")
64	["PST (5)"]=(zone="US/Pacific" ts="1095880927"
65	    res="Wed Sep 22 12:22:07 PDT 2004")
66	#
67	# These variants hard code the transition points which means that they
68	# should differ from the PST examples above which follow Olson. Note the
69	# way to read M3.2.0 is the first day (Sunday) of the second week of the
70	# 3rd month (March).
71	#
72	["PST/POSIX (0)"]=(zone="PST8PDT,M3.2.0/2:00:00,M11.1.0/2:00:00" ts="1080738123"
73	    res="Wed Mar 31 06:02:03 PDT 2004")
74	["PST/POSIX (1)"]=(zone="PST8PDT,M3.2.0/2:00:00,M11.1.0/2:00:00" ts="1079254984"
75	    res="Sun Mar 14 01:03:04 PST 2004")
76	["PST/POSIX (2)"]=(zone="PST8PDT,M3.2.0/2:00:00,M11.1.0/2:00:00" ts="1079258584"
77	    res="Sun Mar 14 03:03:04 PDT 2004")
78	["PST/POSIX (3)"]=(zone="PST8PDT,M3.2.0/2:00:00,M11.1.0/2:00:00" ts="941965199"
79	    res="Sun Nov  7 01:59:59 PDT 1999")
80	["PST/POSIX (4)"]=(zone="PST8PDT,M3.2.0/2:00:00,M11.1.0/2:00:00" ts="941968799"
81	    res="Sun Nov  7 01:59:59 PST 1999")
82	["World (Auckland)"]=(zone="Pacific/Auckland" ts="946684800"
83	    res="Sat Jan  1 13:00:00 NZDT 2000")
84	["World (Belize)"]=(zone="America/Belize" ts="946684800"
85	    res="Fri Dec 31 18:00:00 CST 1999")
86	["World (Brisbane)"]=(zone="Australia/Brisbane" ts="946684800"
87	    res="Sat Jan  1 10:00:00 AEST 2000")
88	["World (Brisbane, ':')"]=(zone=":Australia/Brisbane" ts="946684800"
89	    res="Sat Jan  1 10:00:00 AEST 2000")
90	["World (Casablanca)"]=(zone="Africa/Casablanca" ts="946684800"
91	    res="Sat Jan  1 00:00:00 +01 2000")
92	["World (Damascus)"]=(zone="Asia/Damascus" ts="946684800"
93	    res="Sat Jan  1 02:00:00 +03 2000")
94	["World (Egypt)"]=(zone="Egypt" ts="946684800"
95	    res="Sat Jan  1 02:00:00 EET 2000")
96	["World (GMT)"]=(zone="GMT" ts="946684800"
97	    res="Sat Jan  1 00:00:00 GMT 2000")
98	["World (Guam)"]=(zone="Pacific/Guam" ts="946684800"
99	    res="Sat Jan  1 10:00:00 ChST 2000")
100	["World (Hong Kong)"]=(zone="Hongkong" ts="946684800"
101	    res="Sat Jan  1 08:00:00 HKT 2000")
102	["World (Japan)"]=(zone="Japan" ts="946684800"
103	    res="Sat Jan  1 09:00:00 JST 2000")
104	["World (Japan/POSIX)"]=(zone="JST-9" ts="946684800"
105	    res="Sat Jan  1 09:00:00 JST 2000")
106	["World (London)"]=(zone="Europe/London" ts="946684800"
107	    res="Sat Jan  1 00:00:00 GMT 2000")
108	["World (London/POSIX)"]=(zone="BST0GMT,M3.2.0/2:00:00,M11.1.0/2:00:00"
109	    ts="946684800" res="Sat Jan  1 00:00:00 BST 2000")
110	["World (Longyearbyen)"]=(zone="Arctic/Longyearbyen" ts="946684800"
111	    res="Sat Jan  1 01:00:00 CET 2000")
112	["World (Manila)"]=(zone="Asia/Manila" ts="946684800"
113	    res="Sat Jan  1 08:00:00 PST 2000")
114	["World (McMurdo)"]=(zone="Antarctica/McMurdo" ts="946684800"
115	    res="Sat Jan  1 13:00:00 NZDT 2000")
116	["World (Rome)"]=(zone="Europe/Rome" ts="946684800"
117	    res="Sat Jan  1 01:00:00 CET 2000")
118	["World (Singapore)"]=(zone="Singapore" ts="946684800"
119	    res="Sat Jan  1 08:00:00 +08 2000")
120	["World (St. Thomas)"]=(zone="America/St_Thomas" ts="946684800"
121	    res="Fri Dec 31 20:00:00 AST 1999")
122	["World (Tunis)"]=(zone="Africa/Tunis" ts="946684800"
123	    res="Sat Jan  1 01:00:00 CET 2000")
124	["World (Tunis, ':')"]=(zone=":Africa/Tunis" ts="946684800"
125	    res="Sat Jan  1 01:00:00 CET 2000")
126	["World (UTC)"]=(zone="UTC" ts="946684800"
127	    res="Sat Jan  1 00:00:00 UTC 2000")
128)
129
130test_one()
131{
132	typeset key="$1"
133	typeset out=
134	typeset -n data=btz_tzs[$key]
135
136	out=$(TZ=${data.zone} $DATE -r ${data.ts})
137	if [[ "$out" != "${data.res}" ]]; then
138		printf "TEST FAILED: %s: expected %s, found %s\n" "$key" \
139		    "${data.res}" "$out" >&2
140		btz_exit=1
141	else
142		printf "TEST PASSED: %s: %s\n" "$key" "$out"
143	fi
144}
145
146for i in "${!btz_tzs[@]}"; do
147	test_one "$i"
148done
149
150if (( btz_exit == 0 )); then
151	printf "All tests passed successfully!\n"
152fi
153
154exit $btz_exit
155