diff options
author | Max Kellermann <max@musicpd.org> | 2019-08-19 22:15:56 +0200 |
---|---|---|
committer | Max Kellermann <max@musicpd.org> | 2019-08-19 22:22:07 +0200 |
commit | 06dac4783f993614750650b12117a3764407afd1 (patch) | |
tree | 90e72cf48fbb9a74a4447ead5d6816493f4151e7 /src/time | |
parent | fdaadc19cb176d28cbef7684f84e88b8000e6180 (diff) |
time/Convert: fallback TimeGm() implementation
Move code from Parser.cxx.
Diffstat (limited to 'src/time')
-rw-r--r-- | src/time/Convert.cxx | 35 | ||||
-rw-r--r-- | src/time/Convert.hxx | 4 | ||||
-rw-r--r-- | src/time/Parser.cxx | 28 | ||||
-rw-r--r-- | src/time/Parser.hxx | 2 |
4 files changed, 33 insertions, 36 deletions
diff --git a/src/time/Convert.cxx b/src/time/Convert.cxx index aff35620b..2351f4b52 100644 --- a/src/time/Convert.cxx +++ b/src/time/Convert.cxx @@ -67,15 +67,42 @@ LocalTime(std::chrono::system_clock::time_point tp) return *tm; } -#ifdef __GLIBC__ +#ifndef __GLIBC__ + +/** + * Determine the time zone offset in a portable way. + */ +gcc_const +static time_t +GetTimeZoneOffset() noexcept +{ + time_t t = 1234567890; + struct tm tm; + tm.tm_isdst = 0; +#ifdef _WIN32 + struct tm *p = gmtime(&t); +#else + struct tm *p = &tm; + gmtime_r(&t, p); +#endif + return t - mktime(&tm); +} + +#endif /* !__GLIBC__ */ std::chrono::system_clock::time_point TimeGm(struct tm &tm) noexcept { - return std::chrono::system_clock::from_time_t(timegm(&tm)); -} +#ifdef __GLIBC__ + /* timegm() is a GNU extension */ + const auto t = timegm(&tm); +#else + tm.tm_isdst = 0; + const auto t = mktime(&tm) + GetTimeZoneOffset(); +#endif /* !__GLIBC__ */ -#endif + return std::chrono::system_clock::from_time_t(t); +} std::chrono::system_clock::time_point MakeTime(struct tm &tm) noexcept diff --git a/src/time/Convert.hxx b/src/time/Convert.hxx index 7e1330fcf..2f6f3367a 100644 --- a/src/time/Convert.hxx +++ b/src/time/Convert.hxx @@ -53,8 +53,6 @@ GmTime(std::chrono::system_clock::time_point tp); struct tm LocalTime(std::chrono::system_clock::time_point tp); -#ifdef __GLIBC__ - /** * Convert a UTC-based "struct tm" to a UTC-based time point. */ @@ -62,8 +60,6 @@ gcc_pure std::chrono::system_clock::time_point TimeGm(struct tm &tm) noexcept; -#endif - /** * Convert a local "struct tm" to a UTC-based time point. */ diff --git a/src/time/Parser.cxx b/src/time/Parser.cxx index c4d7288b6..016a0e3d5 100644 --- a/src/time/Parser.cxx +++ b/src/time/Parser.cxx @@ -1,5 +1,5 @@ /* - * Copyright (C) 2014-2017 Max Kellermann <max.kellermann@gmail.com> + * Copyright 2014-2019 Max Kellermann <max.kellermann@gmail.com> * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions @@ -29,31 +29,12 @@ #include "Parser.hxx" #include "Convert.hxx" -#include "util/Compiler.h" #include <stdexcept> #include <assert.h> #include <time.h> -#if !defined(__GLIBC__) && !defined(_WIN32) - -/** - * Determine the time zone offset in a portable way. - */ -gcc_const -static time_t -GetTimeZoneOffset() noexcept -{ - time_t t = 1234567890; - struct tm tm; - tm.tm_isdst = 0; - gmtime_r(&t, &tm); - return t - mktime(&tm); -} - -#endif - std::chrono::system_clock::time_point ParseTimePoint(const char *s, const char *format) { @@ -71,13 +52,6 @@ ParseTimePoint(const char *s, const char *format) if (end == nullptr || *end != 0) throw std::runtime_error("Failed to parse time stamp"); -#ifdef __GLIBC__ - /* timegm() is a GNU extension */ return TimeGm(tm); -#else - tm.tm_isdst = 0; - const auto t = mktime(&tm) + GetTimeZoneOffset(); - return std::chrono::system_clock::from_time_t(t); -#endif /* !__GLIBC__ */ #endif /* !_WIN32 */ } diff --git a/src/time/Parser.hxx b/src/time/Parser.hxx index 97aa0c016..bf92afa72 100644 --- a/src/time/Parser.hxx +++ b/src/time/Parser.hxx @@ -1,5 +1,5 @@ /* - * Copyright (C) 2014-2017 Max Kellermann <max.kellermann@gmail.com> + * Copyright 2014-2019 Max Kellermann <max.kellermann@gmail.com> * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions |