time/Convert: fallback TimeGm() implementation
Move code from Parser.cxx.
This commit is contained in:
parent
fdaadc19cb
commit
06dac4783f
@ -67,15 +67,42 @@ LocalTime(std::chrono::system_clock::time_point tp)
|
|||||||
return *tm;
|
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
|
std::chrono::system_clock::time_point
|
||||||
TimeGm(struct tm &tm) noexcept
|
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
|
std::chrono::system_clock::time_point
|
||||||
MakeTime(struct tm &tm) noexcept
|
MakeTime(struct tm &tm) noexcept
|
||||||
|
@ -53,8 +53,6 @@ GmTime(std::chrono::system_clock::time_point tp);
|
|||||||
struct tm
|
struct tm
|
||||||
LocalTime(std::chrono::system_clock::time_point tp);
|
LocalTime(std::chrono::system_clock::time_point tp);
|
||||||
|
|
||||||
#ifdef __GLIBC__
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Convert a UTC-based "struct tm" to a UTC-based time point.
|
* Convert a UTC-based "struct tm" to a UTC-based time point.
|
||||||
*/
|
*/
|
||||||
@ -62,8 +60,6 @@ gcc_pure
|
|||||||
std::chrono::system_clock::time_point
|
std::chrono::system_clock::time_point
|
||||||
TimeGm(struct tm &tm) noexcept;
|
TimeGm(struct tm &tm) noexcept;
|
||||||
|
|
||||||
#endif
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Convert a local "struct tm" to a UTC-based time point.
|
* Convert a local "struct tm" to a UTC-based time point.
|
||||||
*/
|
*/
|
||||||
|
@ -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
|
* Redistribution and use in source and binary forms, with or without
|
||||||
* modification, are permitted provided that the following conditions
|
* modification, are permitted provided that the following conditions
|
||||||
@ -29,31 +29,12 @@
|
|||||||
|
|
||||||
#include "Parser.hxx"
|
#include "Parser.hxx"
|
||||||
#include "Convert.hxx"
|
#include "Convert.hxx"
|
||||||
#include "util/Compiler.h"
|
|
||||||
|
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
|
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
#include <time.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
|
std::chrono::system_clock::time_point
|
||||||
ParseTimePoint(const char *s, const char *format)
|
ParseTimePoint(const char *s, const char *format)
|
||||||
{
|
{
|
||||||
@ -71,13 +52,6 @@ ParseTimePoint(const char *s, const char *format)
|
|||||||
if (end == nullptr || *end != 0)
|
if (end == nullptr || *end != 0)
|
||||||
throw std::runtime_error("Failed to parse time stamp");
|
throw std::runtime_error("Failed to parse time stamp");
|
||||||
|
|
||||||
#ifdef __GLIBC__
|
|
||||||
/* timegm() is a GNU extension */
|
|
||||||
return TimeGm(tm);
|
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 */
|
#endif /* !_WIN32 */
|
||||||
}
|
}
|
||||||
|
@ -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
|
* Redistribution and use in source and binary forms, with or without
|
||||||
* modification, are permitted provided that the following conditions
|
* modification, are permitted provided that the following conditions
|
||||||
|
Loading…
Reference in New Issue
Block a user