diff options
author | Lennart Poettering <lennart@poettering.net> | 2017-11-20 10:52:20 +0100 |
---|---|---|
committer | Lennart Poettering <lennart@poettering.net> | 2017-11-20 10:53:26 +0100 |
commit | 9a9a4f10e96f602cb7c19bbf95f7aa8ce632dad3 (patch) | |
tree | 444ae72ef566507d81e929be678db95032f899fe /src | |
parent | calendarspec: rework destructor to return its own type (diff) | |
download | systemd-9a9a4f10e96f602cb7c19bbf95f7aa8ce632dad3.tar.xz systemd-9a9a4f10e96f602cb7c19bbf95f7aa8ce632dad3.zip |
util: add new helper in_utc_timezone()
As the name suggests it checks whether we are running in an UTC
timezone.
Diffstat (limited to 'src')
-rw-r--r-- | src/basic/time-util.c | 6 | ||||
-rw-r--r-- | src/basic/time-util.h | 2 | ||||
-rw-r--r-- | src/test/test-time-util.c | 17 |
3 files changed, 25 insertions, 0 deletions
diff --git a/src/basic/time-util.c b/src/basic/time-util.c index 6bf3c8db1d..f5af3281a1 100644 --- a/src/basic/time-util.c +++ b/src/basic/time-util.c @@ -1457,3 +1457,9 @@ usec_t usec_shift_clock(usec_t x, clockid_t from, clockid_t to) { /* x lies in the past */ return usec_sub_unsigned(b, usec_sub_unsigned(a, x)); } + +bool in_utc_timezone(void) { + tzset(); + + return timezone == 0 && daylight == 0; +} diff --git a/src/basic/time-util.h b/src/basic/time-util.h index f600bc3cbb..dc4a159310 100644 --- a/src/basic/time-util.h +++ b/src/basic/time-util.h @@ -156,6 +156,8 @@ struct tm *localtime_or_gmtime_r(const time_t *t, struct tm *tm, bool utc); unsigned long usec_to_jiffies(usec_t usec); +bool in_utc_timezone(void); + static inline usec_t usec_add(usec_t a, usec_t b) { usec_t c; diff --git a/src/test/test-time-util.c b/src/test/test-time-util.c index e639ded48c..ebf85fcc7c 100644 --- a/src/test/test-time-util.c +++ b/src/test/test-time-util.c @@ -383,6 +383,22 @@ static void test_usec_shift_clock(void) { } } +static void test_in_utc_timezone(void) { + assert_se(setenv("TZ", ":UTC", 1) >= 0); + assert_se(in_utc_timezone()); + assert_se(streq(tzname[0], "UTC")); + assert_se(streq(tzname[1], "UTC")); + assert_se(timezone == 0); + assert_se(daylight == 0); + + assert_se(setenv("TZ", "Europe/Berlin", 1) >= 0); + assert_se(!in_utc_timezone()); + assert_se(streq(tzname[0], "CET")); + assert_se(streq(tzname[1], "CEST")); + + assert_se(unsetenv("TZ") >= 0); +} + int main(int argc, char *argv[]) { uintmax_t x; @@ -409,6 +425,7 @@ int main(int argc, char *argv[]) { test_format_timestamp_utc(); test_dual_timestamp_deserialize(); test_usec_shift_clock(); + test_in_utc_timezone(); /* Ensure time_t is signed */ assert_cc((time_t) -1 < (time_t) 1); |