6 changed files with 162 additions and 42 deletions
@ -0,0 +1,98 @@
|
||||
/*
|
||||
* Copyright (C) 2011-2013 Daniel Scharrer |
||||
* |
||||
* This software is provided 'as-is', without any express or implied |
||||
* warranty. In no event will the author(s) be held liable for any damages |
||||
* arising from the use of this software. |
||||
* |
||||
* Permission is granted to anyone to use this software for any purpose, |
||||
* including commercial applications, and to alter it and redistribute it |
||||
* freely, subject to the following restrictions: |
||||
* |
||||
* 1. The origin of this software must not be misrepresented; you must not |
||||
* claim that you wrote the original software. If you use this software |
||||
* in a product, an acknowledgment in the product documentation would be |
||||
* appreciated but is not required. |
||||
* 2. Altered source versions must be plainly marked as such, and must not be |
||||
* misrepresented as being the original software. |
||||
* 3. This notice may not be removed or altered from any source distribution. |
||||
*/ |
||||
|
||||
#include "util/time.hpp" |
||||
|
||||
#include "configure.hpp" |
||||
|
||||
#if INNOEXTRACT_HAVE_TIMEGM || INNOEXTRACT_HAVE_MKGMTIME \ |
||||
|| INNOEXTRACT_HAVE_GMTIME_R || INNOEXTRACT_HAVE_GMTIME_S |
||||
#include <time.h> |
||||
#endif |
||||
|
||||
#if !INNOEXTRACT_HAVE_TIMEGM |
||||
#include <stdlib.h> |
||||
#endif |
||||
|
||||
namespace util { |
||||
|
||||
std::time_t parse_time(std::tm tm) { |
||||
|
||||
#if INNOEXTRACT_HAVE_TIMEGM |
||||
|
||||
return timegm(&tm); |
||||
|
||||
#elif INNOEXTRACT_HAVE_MKGMTIME |
||||
|
||||
return _mkgmtime(&tm); |
||||
|
||||
#else |
||||
|
||||
char * tz = getenv("TZ"); |
||||
setenv("TZ", "UTC", 1); |
||||
tzset(); |
||||
|
||||
std::time_t ret = std::mktime(&tm); |
||||
|
||||
if(tz) { |
||||
setenv("TZ", tz, 1); |
||||
} else { |
||||
unsetenv("TZ"); |
||||
} |
||||
tzset(); |
||||
|
||||
return ret; |
||||
|
||||
#endif |
||||
|
||||
} |
||||
|
||||
std::tm format_time(time_t t) { |
||||
|
||||
std::tm ret; |
||||
|
||||
#if INNOEXTRACT_HAVE_GMTIME_R |
||||
|
||||
gmtime_r(&t, &ret); |
||||
|
||||
#elif INNOEXTRACT_HAVE_GMTIME_S |
||||
|
||||
_gmtime_s(&ret, &t); |
||||
|
||||
#else |
||||
|
||||
// Hope that this is threadsafe...
|
||||
ret = *gmtime(&t); |
||||
|
||||
#endif |
||||
|
||||
return ret; |
||||
} |
||||
|
||||
time_t to_local_time(time_t t) { |
||||
|
||||
// Format time as UTC ...
|
||||
std::tm time = format_time(t); |
||||
|
||||
//... and interpret it as local time
|
||||
return std::mktime(&time); |
||||
} |
||||
|
||||
} // namespace util
|
||||
@ -0,0 +1,48 @@
|
||||
/*
|
||||
* Copyright (C) 2011-2013 Daniel Scharrer |
||||
* |
||||
* This software is provided 'as-is', without any express or implied |
||||
* warranty. In no event will the author(s) be held liable for any damages |
||||
* arising from the use of this software. |
||||
* |
||||
* Permission is granted to anyone to use this software for any purpose, |
||||
* including commercial applications, and to alter it and redistribute it |
||||
* freely, subject to the following restrictions: |
||||
* |
||||
* 1. The origin of this software must not be misrepresented; you must not |
||||
* claim that you wrote the original software. If you use this software |
||||
* in a product, an acknowledgment in the product documentation would be |
||||
* appreciated but is not required. |
||||
* 2. Altered source versions must be plainly marked as such, and must not be |
||||
* misrepresented as being the original software. |
||||
* 3. This notice may not be removed or altered from any source distribution. |
||||
*/ |
||||
|
||||
#ifndef INNOEXTRACT_UTIL_TIME_HPP |
||||
#define INNOEXTRACT_UTIL_TIME_HPP |
||||
|
||||
#include <ctime> |
||||
|
||||
namespace util { |
||||
|
||||
/*!
|
||||
* Convert UTC clock time to a timestamp |
||||
* Note: may not be threadsafe on all systems! |
||||
*/ |
||||
std::time_t parse_time(std::tm tm); |
||||
|
||||
/*!
|
||||
* Convert a timestamp to UTC clock time |
||||
* Note: may not be threadsafe on all systems! |
||||
*/ |
||||
std::tm format_time(std::time_t t); |
||||
|
||||
/*!
|
||||
* Convert a timestamp to local time |
||||
* Note: may not be threadsafe on all systems! |
||||
*/ |
||||
std::time_t to_local_time(std::time_t t); |
||||
|
||||
} // namespace util
|
||||
|
||||
#endif // INNOEXTRACT_UTIL_TIME_HPP
|
||||
Loading…
Reference in new issue