Commit 06dac478 authored by Max Kellermann's avatar Max Kellermann

time/Convert: fallback TimeGm() implementation

Move code from Parser.cxx.
parent fdaadc19
......@@ -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
......
......@@ -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.
*/
......
/*
* 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 */
}
/*
* 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
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment