2 * Copyright (C) 2011, 2014 Red Hat, Inc.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library. If not, see
16 * <http://www.gnu.org/licenses/>.
23 #include "testutils.h"
28 #define VIR_FROM_THIS VIR_FROM_RPC
30 VIR_LOG_INIT("tests.timetest");
32 struct testTimeFieldsData
{
33 unsigned long long when
;
37 static int testTimeFields(const void *args
)
39 const struct testTimeFieldsData
*data
= args
;
42 virTimeFieldsThen(data
->when
, &actual
);
44 #define COMPARE(field) \
46 if (data->fields.field != actual.field) { \
47 VIR_DEBUG("Expect " #field " %d got %d", \
48 data->fields.field, actual.field); \
53 /* tm_year value 0 is based off epoch 1900 */
54 actual
.tm_year
+= 1900;
55 /* tm_mon is range 0-11, but we want 1-12 */
72 } testTimeLocalOffsetData
;
75 testTimeLocalOffset(const void *args
)
77 const testTimeLocalOffsetData
*data
= args
;
80 if (g_setenv("TZ", data
->zone
, TRUE
) == FALSE
) {
86 if (virTimeLocalOffsetFromUTC(&actual
) < 0)
89 if (data
->offset
!= actual
) {
90 VIR_DEBUG("Expect Offset %ld got %ld",
91 data
->offset
, actual
);
98 /* return true if the date is Jan 1 or Dec 31 (localtime) */
102 g_autoptr(GDateTime
) now
= g_date_time_new_now_local();
104 return ((g_date_time_get_month(now
) == 1 &&
105 g_date_time_get_day_of_month(now
) == 1) ||
106 (g_date_time_get_month(now
) == 12 &&
107 g_date_time_get_day_of_month(now
) == 31));
117 signal(SIGPIPE
, SIG_IGN
);
120 #define TEST_FIELDS(ts, year, mon, day, hour, min, sec) \
122 struct testTimeFieldsData data = { \
136 if (virTestRun("Test fields " #ts " " #year " ", testTimeFields, &data) < 0) \
140 TEST_FIELDS(0ull, 1970, 1, 1, 0, 0, 0);
141 TEST_FIELDS(5000ull, 1970, 1, 1, 0, 0, 5);
142 TEST_FIELDS(3605000ull, 1970, 1, 1, 1, 0, 5);
143 TEST_FIELDS(86405000ull, 1970, 1, 2, 0, 0, 5);
144 TEST_FIELDS(31536000000ull, 1971, 1, 1, 0, 0, 0);
146 TEST_FIELDS(30866399000ull, 1970, 12, 24, 5, 59, 59);
147 TEST_FIELDS(123465599000ull, 1973, 11, 29, 23, 59, 59);
148 TEST_FIELDS(155001599000ull, 1974, 11, 29, 23, 59, 59);
150 TEST_FIELDS(186537599000ull, 1975, 11, 29, 23, 59, 59);
151 TEST_FIELDS(344390399000ull, 1980, 11, 29, 23, 59, 59);
152 TEST_FIELDS(1203161493000ull, 2008, 2, 16, 11, 31, 33);
153 TEST_FIELDS(1234567890000ull, 2009, 2, 13, 23, 31, 30);
155 TEST_FIELDS(1322524800000ull, 2011, 11, 29, 0, 0, 0);
156 TEST_FIELDS(1322611199000ull, 2011, 11, 29, 23, 59, 59);
158 TEST_FIELDS(2147483648000ull, 2038, 1, 19, 3, 14, 8);
160 #define TEST_LOCALOFFSET(tz, off) \
162 testTimeLocalOffsetData data = { \
166 if (virTestRun("Test localtime offset for " #tz, \
167 testTimeLocalOffset, &data) < 0) \
171 TEST_LOCALOFFSET("VIR00:30", -30 * 60);
172 TEST_LOCALOFFSET("VIR01:30", -90 * 60);
173 TEST_LOCALOFFSET("VIR05:00", (-5 * 60) * 60);
174 TEST_LOCALOFFSET("UTC", 0);
175 TEST_LOCALOFFSET("VIR-00:30", 30 * 60);
176 TEST_LOCALOFFSET("VIR-01:30", 90 * 60);
178 /* experiments have shown that the following tests will fail
179 * during certain hours of Dec 31 or Jan 1 (depending on the
180 * TZ setting in the shell running the test, but in general
181 * for a period that apparently starts at 00:00:00 UTC Jan 1
182 * and continues for 1 - 2 hours). We've determined this is
183 * due to our inability to specify a timezone with DST on/off
184 * settings that make it truly *always* on DST - i.e. it is a
185 * failing of the test data, *not* of the function we are
186 * testing. So to test as much as possible, we still run these
187 * tests, except on Dec 31 and Jan 1.
189 if (!isNearYearEnd()) {
190 /* test DST processing with timezones that always
191 * have DST in effect; what's more, cover a zone with
192 * with an unusual DST different than a usual one hour
194 * These tests originally used '0' as the first day,
195 * but changed to '1' due to GLib GTimeZone parsing bug:
196 * https://gitlab.gnome.org/GNOME/glib/issues/1999
198 * Once we depend on a new enough GLib (>= 2.63.4), we
199 * can put them back to 0 again.
201 TEST_LOCALOFFSET("VIR-00:30VID,1/00:00:00,364/23:59:59",
202 ((1 * 60) + 30) * 60);
203 TEST_LOCALOFFSET("VIR-02:30VID,1/00:00:00,364/23:59:59",
204 ((3 * 60) + 30) * 60);
205 TEST_LOCALOFFSET("VIR-02:30VID-04:30,1/00:00:00,364/23:59:59",
206 ((4 * 60) + 30) * 60);
207 TEST_LOCALOFFSET("VIR-12:00VID-13:00,1/00:00:00,364/23:59:59",
208 ((13 * 60) + 0) * 60);
209 TEST_LOCALOFFSET("VIR02:45VID00:45,1/00:00:00,364/23:59:59",
211 TEST_LOCALOFFSET("VIR05:00VID04:00,1/00:00:00,364/23:59:59",
212 ((-4 * 60) + 0) * 60);
213 TEST_LOCALOFFSET("VIR11:00VID10:00,1/00:00:00,364/23:59:59",
214 ((-10 * 60) + 0) * 60);
217 return ret
== 0 ? EXIT_SUCCESS
: EXIT_FAILURE
;
220 VIR_TEST_MAIN(mymain
)