2011-11-29 12:11:01 +00:00
|
|
|
/*
|
2014-03-17 09:38:38 +00:00
|
|
|
* Copyright (C) 2011, 2014 Red Hat, Inc.
|
2011-11-29 12:11:01 +00:00
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
2012-09-20 22:30:55 +00:00
|
|
|
* License along with this library. If not, see
|
2012-07-21 10:06:23 +00:00
|
|
|
* <http://www.gnu.org/licenses/>.
|
2011-11-29 12:11:01 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
#include <signal.h>
|
|
|
|
|
|
|
|
#include "testutils.h"
|
2012-12-13 18:21:53 +00:00
|
|
|
#include "virerror.h"
|
2012-12-12 18:06:53 +00:00
|
|
|
#include "viralloc.h"
|
2012-12-12 17:59:27 +00:00
|
|
|
#include "virlog.h"
|
2011-11-29 12:11:01 +00:00
|
|
|
|
|
|
|
#include "virtime.h"
|
|
|
|
|
|
|
|
#define VIR_FROM_THIS VIR_FROM_RPC
|
|
|
|
|
2014-02-28 12:16:17 +00:00
|
|
|
VIR_LOG_INIT("tests.timetest");
|
|
|
|
|
2011-11-29 12:11:01 +00:00
|
|
|
struct testTimeFieldsData {
|
|
|
|
unsigned long long when;
|
|
|
|
struct tm fields;
|
|
|
|
};
|
|
|
|
|
|
|
|
static int testTimeFields(const void *args)
|
|
|
|
{
|
|
|
|
const struct testTimeFieldsData *data = args;
|
|
|
|
struct tm actual;
|
|
|
|
|
2014-07-25 08:13:57 +00:00
|
|
|
virTimeFieldsThen(data->when, &actual);
|
2011-11-29 12:11:01 +00:00
|
|
|
|
2017-11-03 12:09:47 +00:00
|
|
|
#define COMPARE(field) \
|
|
|
|
do { \
|
|
|
|
if (data->fields.field != actual.field) { \
|
|
|
|
VIR_DEBUG("Expect " #field " %d got %d", \
|
|
|
|
data->fields.field, actual.field); \
|
|
|
|
return -1; \
|
|
|
|
} \
|
2011-11-29 12:11:01 +00:00
|
|
|
} while (0)
|
|
|
|
|
|
|
|
/* tm_year value 0 is based off epoch 1900 */
|
|
|
|
actual.tm_year += 1900;
|
|
|
|
/* tm_mon is range 0-11, but we want 1-12 */
|
|
|
|
actual.tm_mon += 1;
|
|
|
|
|
|
|
|
COMPARE(tm_year);
|
|
|
|
COMPARE(tm_mon);
|
|
|
|
COMPARE(tm_mday);
|
|
|
|
COMPARE(tm_hour);
|
|
|
|
COMPARE(tm_min);
|
|
|
|
COMPARE(tm_sec);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
util: new function virTimeLocalOffsetFromUTC
Since there isn't a single libc API to get this value, this patch
supplies one which gets the value by grabbing current time, then
converting that into a struct tm with gmtime_r(), then back to a
time_t using mktime.
The returned value is the difference between UTC and localtime in
seconds. If localtime is ahead of UTC (east) the offset will be a
positive number, and if localtime is behind UTC (west) the offset will
be negative.
This function should be POSIX-compliant, and is threadsafe, but not
async signal safe. If it was ever necessary to know this value in a
child process, we could cache it with a one-time init function when
libvirtd starts, then just supply the cached value, but that
complexity isn't needed for current usage; that would also have the
problem that it might not be accurate after a local daylight savings
boundary.
(If it weren't for DST, we could simply replace this entire function
with "-timezone"; timezone contains the offset of the current timezone
(negated from what we want) but doesn't account for DST. And in spite
of being guaranteed by POSIX, it isn't available on older versions of
mingw.)
Signed-off-by: Eric Blake <eblake@redhat.com>
2014-05-24 14:21:26 +00:00
|
|
|
typedef struct {
|
|
|
|
const char *zone;
|
|
|
|
long offset;
|
|
|
|
} testTimeLocalOffsetData;
|
|
|
|
|
|
|
|
static int
|
|
|
|
testTimeLocalOffset(const void *args)
|
|
|
|
{
|
|
|
|
const testTimeLocalOffsetData *data = args;
|
|
|
|
long actual;
|
|
|
|
|
2019-12-18 17:16:19 +00:00
|
|
|
if (g_setenv("TZ", data->zone, TRUE) == FALSE) {
|
|
|
|
perror("g_setenv");
|
util: new function virTimeLocalOffsetFromUTC
Since there isn't a single libc API to get this value, this patch
supplies one which gets the value by grabbing current time, then
converting that into a struct tm with gmtime_r(), then back to a
time_t using mktime.
The returned value is the difference between UTC and localtime in
seconds. If localtime is ahead of UTC (east) the offset will be a
positive number, and if localtime is behind UTC (west) the offset will
be negative.
This function should be POSIX-compliant, and is threadsafe, but not
async signal safe. If it was ever necessary to know this value in a
child process, we could cache it with a one-time init function when
libvirtd starts, then just supply the cached value, but that
complexity isn't needed for current usage; that would also have the
problem that it might not be accurate after a local daylight savings
boundary.
(If it weren't for DST, we could simply replace this entire function
with "-timezone"; timezone contains the offset of the current timezone
(negated from what we want) but doesn't account for DST. And in spite
of being guaranteed by POSIX, it isn't available on older versions of
mingw.)
Signed-off-by: Eric Blake <eblake@redhat.com>
2014-05-24 14:21:26 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
tzset();
|
|
|
|
|
2014-11-13 14:20:43 +00:00
|
|
|
if (virTimeLocalOffsetFromUTC(&actual) < 0)
|
util: new function virTimeLocalOffsetFromUTC
Since there isn't a single libc API to get this value, this patch
supplies one which gets the value by grabbing current time, then
converting that into a struct tm with gmtime_r(), then back to a
time_t using mktime.
The returned value is the difference between UTC and localtime in
seconds. If localtime is ahead of UTC (east) the offset will be a
positive number, and if localtime is behind UTC (west) the offset will
be negative.
This function should be POSIX-compliant, and is threadsafe, but not
async signal safe. If it was ever necessary to know this value in a
child process, we could cache it with a one-time init function when
libvirtd starts, then just supply the cached value, but that
complexity isn't needed for current usage; that would also have the
problem that it might not be accurate after a local daylight savings
boundary.
(If it weren't for DST, we could simply replace this entire function
with "-timezone"; timezone contains the offset of the current timezone
(negated from what we want) but doesn't account for DST. And in spite
of being guaranteed by POSIX, it isn't available on older versions of
mingw.)
Signed-off-by: Eric Blake <eblake@redhat.com>
2014-05-24 14:21:26 +00:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (data->offset != actual) {
|
2015-10-27 18:14:01 +00:00
|
|
|
VIR_DEBUG("Expect Offset %ld got %ld",
|
util: new function virTimeLocalOffsetFromUTC
Since there isn't a single libc API to get this value, this patch
supplies one which gets the value by grabbing current time, then
converting that into a struct tm with gmtime_r(), then back to a
time_t using mktime.
The returned value is the difference between UTC and localtime in
seconds. If localtime is ahead of UTC (east) the offset will be a
positive number, and if localtime is behind UTC (west) the offset will
be negative.
This function should be POSIX-compliant, and is threadsafe, but not
async signal safe. If it was ever necessary to know this value in a
child process, we could cache it with a one-time init function when
libvirtd starts, then just supply the cached value, but that
complexity isn't needed for current usage; that would also have the
problem that it might not be accurate after a local daylight savings
boundary.
(If it weren't for DST, we could simply replace this entire function
with "-timezone"; timezone contains the offset of the current timezone
(negated from what we want) but doesn't account for DST. And in spite
of being guaranteed by POSIX, it isn't available on older versions of
mingw.)
Signed-off-by: Eric Blake <eblake@redhat.com>
2014-05-24 14:21:26 +00:00
|
|
|
data->offset, actual);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-05-27 13:35:57 +00:00
|
|
|
|
|
|
|
/* return true if the date is Jan 1 or Dec 31 (localtime) */
|
|
|
|
static bool
|
|
|
|
isNearYearEnd(void)
|
|
|
|
{
|
2020-01-09 14:07:15 +00:00
|
|
|
g_autoptr(GDateTime) now = g_date_time_new_now_local();
|
2014-05-27 13:35:57 +00:00
|
|
|
|
2020-01-09 14:07:15 +00:00
|
|
|
return ((g_date_time_get_month(now) == 1 &&
|
|
|
|
g_date_time_get_day_of_month(now) == 1) ||
|
|
|
|
(g_date_time_get_month(now) == 12 &&
|
|
|
|
g_date_time_get_day_of_month(now) == 31));
|
2014-05-27 13:35:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-11-29 12:11:01 +00:00
|
|
|
static int
|
|
|
|
mymain(void)
|
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
|
2020-01-22 17:59:39 +00:00
|
|
|
#ifndef WIN32
|
2011-11-29 12:11:01 +00:00
|
|
|
signal(SIGPIPE, SIG_IGN);
|
2020-01-22 17:59:39 +00:00
|
|
|
#endif /* WIN32 */
|
2011-11-29 12:11:01 +00:00
|
|
|
|
2017-11-03 12:09:47 +00:00
|
|
|
#define TEST_FIELDS(ts, year, mon, day, hour, min, sec) \
|
|
|
|
do { \
|
|
|
|
struct testTimeFieldsData data = { \
|
|
|
|
.when = ts, \
|
|
|
|
.fields = { \
|
|
|
|
.tm_year = year, \
|
|
|
|
.tm_mon = mon, \
|
|
|
|
.tm_mday = day, \
|
|
|
|
.tm_hour = hour, \
|
|
|
|
.tm_min = min, \
|
|
|
|
.tm_sec = sec, \
|
|
|
|
.tm_wday = 0, \
|
|
|
|
.tm_yday = 0, \
|
|
|
|
.tm_isdst = 0, \
|
|
|
|
}, \
|
|
|
|
}; \
|
2016-05-26 15:01:50 +00:00
|
|
|
if (virTestRun("Test fields " #ts " " #year " ", testTimeFields, &data) < 0) \
|
2017-11-03 12:09:47 +00:00
|
|
|
ret = -1; \
|
2011-11-29 12:11:01 +00:00
|
|
|
} while (0)
|
|
|
|
|
2012-10-17 09:23:12 +00:00
|
|
|
TEST_FIELDS(0ull, 1970, 1, 1, 0, 0, 0);
|
|
|
|
TEST_FIELDS(5000ull, 1970, 1, 1, 0, 0, 5);
|
|
|
|
TEST_FIELDS(3605000ull, 1970, 1, 1, 1, 0, 5);
|
|
|
|
TEST_FIELDS(86405000ull, 1970, 1, 2, 0, 0, 5);
|
|
|
|
TEST_FIELDS(31536000000ull, 1971, 1, 1, 0, 0, 0);
|
|
|
|
|
|
|
|
TEST_FIELDS(30866399000ull, 1970, 12, 24, 5, 59, 59);
|
|
|
|
TEST_FIELDS(123465599000ull, 1973, 11, 29, 23, 59, 59);
|
|
|
|
TEST_FIELDS(155001599000ull, 1974, 11, 29, 23, 59, 59);
|
|
|
|
|
|
|
|
TEST_FIELDS(186537599000ull, 1975, 11, 29, 23, 59, 59);
|
|
|
|
TEST_FIELDS(344390399000ull, 1980, 11, 29, 23, 59, 59);
|
2011-11-29 12:11:01 +00:00
|
|
|
TEST_FIELDS(1203161493000ull, 2008, 2, 16, 11, 31, 33);
|
|
|
|
TEST_FIELDS(1234567890000ull, 2009, 2, 13, 23, 31, 30);
|
|
|
|
|
|
|
|
TEST_FIELDS(1322524800000ull, 2011, 11, 29, 0, 0, 0);
|
|
|
|
TEST_FIELDS(1322611199000ull, 2011, 11, 29, 23, 59, 59);
|
|
|
|
|
|
|
|
TEST_FIELDS(2147483648000ull, 2038, 1, 19, 3, 14, 8);
|
|
|
|
|
2017-11-03 12:09:47 +00:00
|
|
|
#define TEST_LOCALOFFSET(tz, off) \
|
|
|
|
do { \
|
|
|
|
testTimeLocalOffsetData data = { \
|
|
|
|
.zone = tz, \
|
|
|
|
.offset = off, \
|
|
|
|
}; \
|
|
|
|
if (virTestRun("Test localtime offset for " #tz, \
|
|
|
|
testTimeLocalOffset, &data) < 0) \
|
|
|
|
ret = -1; \
|
util: new function virTimeLocalOffsetFromUTC
Since there isn't a single libc API to get this value, this patch
supplies one which gets the value by grabbing current time, then
converting that into a struct tm with gmtime_r(), then back to a
time_t using mktime.
The returned value is the difference between UTC and localtime in
seconds. If localtime is ahead of UTC (east) the offset will be a
positive number, and if localtime is behind UTC (west) the offset will
be negative.
This function should be POSIX-compliant, and is threadsafe, but not
async signal safe. If it was ever necessary to know this value in a
child process, we could cache it with a one-time init function when
libvirtd starts, then just supply the cached value, but that
complexity isn't needed for current usage; that would also have the
problem that it might not be accurate after a local daylight savings
boundary.
(If it weren't for DST, we could simply replace this entire function
with "-timezone"; timezone contains the offset of the current timezone
(negated from what we want) but doesn't account for DST. And in spite
of being guaranteed by POSIX, it isn't available on older versions of
mingw.)
Signed-off-by: Eric Blake <eblake@redhat.com>
2014-05-24 14:21:26 +00:00
|
|
|
} while (0)
|
|
|
|
|
|
|
|
TEST_LOCALOFFSET("VIR00:30", -30 * 60);
|
|
|
|
TEST_LOCALOFFSET("VIR01:30", -90 * 60);
|
2014-05-27 13:35:57 +00:00
|
|
|
TEST_LOCALOFFSET("VIR05:00", (-5 * 60) * 60);
|
util: new function virTimeLocalOffsetFromUTC
Since there isn't a single libc API to get this value, this patch
supplies one which gets the value by grabbing current time, then
converting that into a struct tm with gmtime_r(), then back to a
time_t using mktime.
The returned value is the difference between UTC and localtime in
seconds. If localtime is ahead of UTC (east) the offset will be a
positive number, and if localtime is behind UTC (west) the offset will
be negative.
This function should be POSIX-compliant, and is threadsafe, but not
async signal safe. If it was ever necessary to know this value in a
child process, we could cache it with a one-time init function when
libvirtd starts, then just supply the cached value, but that
complexity isn't needed for current usage; that would also have the
problem that it might not be accurate after a local daylight savings
boundary.
(If it weren't for DST, we could simply replace this entire function
with "-timezone"; timezone contains the offset of the current timezone
(negated from what we want) but doesn't account for DST. And in spite
of being guaranteed by POSIX, it isn't available on older versions of
mingw.)
Signed-off-by: Eric Blake <eblake@redhat.com>
2014-05-24 14:21:26 +00:00
|
|
|
TEST_LOCALOFFSET("UTC", 0);
|
|
|
|
TEST_LOCALOFFSET("VIR-00:30", 30 * 60);
|
|
|
|
TEST_LOCALOFFSET("VIR-01:30", 90 * 60);
|
2014-05-27 13:35:57 +00:00
|
|
|
|
util: new function virTimeLocalOffsetFromUTC
Since there isn't a single libc API to get this value, this patch
supplies one which gets the value by grabbing current time, then
converting that into a struct tm with gmtime_r(), then back to a
time_t using mktime.
The returned value is the difference between UTC and localtime in
seconds. If localtime is ahead of UTC (east) the offset will be a
positive number, and if localtime is behind UTC (west) the offset will
be negative.
This function should be POSIX-compliant, and is threadsafe, but not
async signal safe. If it was ever necessary to know this value in a
child process, we could cache it with a one-time init function when
libvirtd starts, then just supply the cached value, but that
complexity isn't needed for current usage; that would also have the
problem that it might not be accurate after a local daylight savings
boundary.
(If it weren't for DST, we could simply replace this entire function
with "-timezone"; timezone contains the offset of the current timezone
(negated from what we want) but doesn't account for DST. And in spite
of being guaranteed by POSIX, it isn't available on older versions of
mingw.)
Signed-off-by: Eric Blake <eblake@redhat.com>
2014-05-24 14:21:26 +00:00
|
|
|
/* test DST processing with timezones that always
|
|
|
|
* have DST in effect; what's more, cover a zone with
|
|
|
|
* with an unusual DST different than a usual one hour
|
2020-01-09 14:07:15 +00:00
|
|
|
*
|
|
|
|
* These tests originally used '0' as the first day,
|
|
|
|
* but changed to '1' due to GLib GTimeZone parsing bug:
|
|
|
|
* https://gitlab.gnome.org/GNOME/glib/issues/1999
|
|
|
|
*
|
|
|
|
* Once we depend on a new enough GLib, we can put then
|
|
|
|
* back to 0 again.
|
util: new function virTimeLocalOffsetFromUTC
Since there isn't a single libc API to get this value, this patch
supplies one which gets the value by grabbing current time, then
converting that into a struct tm with gmtime_r(), then back to a
time_t using mktime.
The returned value is the difference between UTC and localtime in
seconds. If localtime is ahead of UTC (east) the offset will be a
positive number, and if localtime is behind UTC (west) the offset will
be negative.
This function should be POSIX-compliant, and is threadsafe, but not
async signal safe. If it was ever necessary to know this value in a
child process, we could cache it with a one-time init function when
libvirtd starts, then just supply the cached value, but that
complexity isn't needed for current usage; that would also have the
problem that it might not be accurate after a local daylight savings
boundary.
(If it weren't for DST, we could simply replace this entire function
with "-timezone"; timezone contains the offset of the current timezone
(negated from what we want) but doesn't account for DST. And in spite
of being guaranteed by POSIX, it isn't available on older versions of
mingw.)
Signed-off-by: Eric Blake <eblake@redhat.com>
2014-05-24 14:21:26 +00:00
|
|
|
*/
|
2020-01-09 14:07:15 +00:00
|
|
|
TEST_LOCALOFFSET("VIR-00:30VID,1/00:00:00,364/23:59:59",
|
2014-05-27 13:35:57 +00:00
|
|
|
((1 * 60) + 30) * 60);
|
2020-01-09 14:07:15 +00:00
|
|
|
TEST_LOCALOFFSET("VIR-02:30VID,1/00:00:00,364/23:59:59",
|
2014-05-27 13:35:57 +00:00
|
|
|
((3 * 60) + 30) * 60);
|
2020-01-09 14:07:15 +00:00
|
|
|
TEST_LOCALOFFSET("VIR-02:30VID-04:30,1/00:00:00,364/23:59:59",
|
2014-05-27 13:35:57 +00:00
|
|
|
((4 * 60) + 30) * 60);
|
2020-01-09 14:07:15 +00:00
|
|
|
TEST_LOCALOFFSET("VIR-12:00VID-13:00,1/00:00:00,364/23:59:59",
|
2014-05-27 13:35:57 +00:00
|
|
|
((13 * 60) + 0) * 60);
|
|
|
|
|
|
|
|
if (!isNearYearEnd()) {
|
|
|
|
/* experiments have shown that the following tests will fail
|
|
|
|
* during certain hours of Dec 31 or Jan 1 (depending on the
|
|
|
|
* TZ setting in the shell running the test, but in general
|
|
|
|
* for a period that apparently starts at 00:00:00 UTC Jan 1
|
|
|
|
* and continues for 1 - 2 hours). We've determined this is
|
|
|
|
* due to our inability to specify a timezone with DST on/off
|
|
|
|
* settings that make it truly *always* on DST - i.e. it is a
|
|
|
|
* failing of the test data, *not* of the function we are
|
|
|
|
* testing. So to test as much as possible, we still run these
|
|
|
|
* tests, except on Dec 31 and Jan 1.
|
|
|
|
*/
|
|
|
|
|
2020-01-09 14:07:15 +00:00
|
|
|
TEST_LOCALOFFSET("VIR02:45VID00:45,1/00:00:00,364/23:59:59",
|
2014-05-27 13:35:57 +00:00
|
|
|
-45 * 60);
|
2020-01-09 14:07:15 +00:00
|
|
|
TEST_LOCALOFFSET("VIR05:00VID04:00,1/00:00:00,364/23:59:59",
|
2014-05-27 13:35:57 +00:00
|
|
|
((-4 * 60) + 0) * 60);
|
2020-01-09 14:07:15 +00:00
|
|
|
TEST_LOCALOFFSET("VIR11:00VID10:00,1/00:00:00,364/23:59:59",
|
2014-05-27 13:35:57 +00:00
|
|
|
((-10 * 60) + 0) * 60);
|
|
|
|
}
|
util: new function virTimeLocalOffsetFromUTC
Since there isn't a single libc API to get this value, this patch
supplies one which gets the value by grabbing current time, then
converting that into a struct tm with gmtime_r(), then back to a
time_t using mktime.
The returned value is the difference between UTC and localtime in
seconds. If localtime is ahead of UTC (east) the offset will be a
positive number, and if localtime is behind UTC (west) the offset will
be negative.
This function should be POSIX-compliant, and is threadsafe, but not
async signal safe. If it was ever necessary to know this value in a
child process, we could cache it with a one-time init function when
libvirtd starts, then just supply the cached value, but that
complexity isn't needed for current usage; that would also have the
problem that it might not be accurate after a local daylight savings
boundary.
(If it weren't for DST, we could simply replace this entire function
with "-timezone"; timezone contains the offset of the current timezone
(negated from what we want) but doesn't account for DST. And in spite
of being guaranteed by POSIX, it isn't available on older versions of
mingw.)
Signed-off-by: Eric Blake <eblake@redhat.com>
2014-05-24 14:21:26 +00:00
|
|
|
|
2014-03-17 09:38:38 +00:00
|
|
|
return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
|
2011-11-29 12:11:01 +00:00
|
|
|
}
|
|
|
|
|
2017-03-29 14:45:42 +00:00
|
|
|
VIR_TEST_MAIN(mymain)
|