189 lines
3.5 KiB
C++
189 lines
3.5 KiB
C++
/**
|
|
* @file include/ActivMiddleware/Misc/CrtHelper.h
|
|
*
|
|
* @brief Workarounds for crt disparities across platforms.
|
|
*/
|
|
|
|
#if (!defined ACTIV_MISC_CRT_HELPER_H)
|
|
#define ACTIV_MISC_CRT_HELPER_H
|
|
|
|
#include "ActivMiddleware/ActivMiddleware.h"
|
|
|
|
#include <time.h>
|
|
|
|
#if (defined ACTIV_PLATFORM_WINDOWS)
|
|
|
|
#if (defined _CRT_NO_TIME_T)
|
|
#include <sys/timeb.h>
|
|
#endif
|
|
#endif
|
|
|
|
#if (defined ACTIV_PLATFORM_OSX)
|
|
#include <sys/time.h>
|
|
#endif
|
|
|
|
//! Activ namespace.
|
|
namespace Activ
|
|
{
|
|
|
|
#if (defined ACTIV_PLATFORM_WINDOWS)
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
/**
|
|
* @brief Convert a time value and correct for the local time zone.
|
|
*
|
|
* @param pTime pointer to time to convert (e.g. from time()).
|
|
* @param pTm pointer to tm structure to receive converted time.
|
|
*
|
|
* @return pTm or NULL on error.
|
|
*/
|
|
inline tm *localtime_r(const time_t * const pTime, tm * const pTm)
|
|
{
|
|
tm * const pRes = ::localtime(pTime);
|
|
|
|
if (ACTIV_UNLIKELY(NULL == pRes))
|
|
return NULL;
|
|
|
|
*pTm = *pRes;
|
|
|
|
return pTm;
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
/**
|
|
* @brief Convert a time value and store as UTC.
|
|
*
|
|
* @param pTime pointer to time to convert (e.g. from time()).
|
|
* @param pTm pointer to tm structure to receive converted time.
|
|
*
|
|
* @return pTm or NULL on error.
|
|
*/
|
|
inline tm *gmtime_r(const time_t * const pTime, tm * const pTm)
|
|
{
|
|
tm * const pRes = ::gmtime(pTime);
|
|
|
|
if (ACTIV_UNLIKELY(NULL == pRes))
|
|
return NULL;
|
|
|
|
*pTm = *pRes;
|
|
|
|
return pTm;
|
|
}
|
|
|
|
#elif (defined ACTIV_PLATFORM_POSIX)
|
|
|
|
using ::localtime_r;
|
|
using ::gmtime_r;
|
|
|
|
#else
|
|
#error Configure for this platform
|
|
#endif
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
// Provide timespec as defined in the C11 standard.
|
|
|
|
#if (defined ACTIV_PLATFORM_POSIX) || (!defined _CRT_NO_TIME_T)
|
|
|
|
using ::timespec;
|
|
|
|
#else
|
|
|
|
struct timespec
|
|
{
|
|
time_t tv_sec; // Seconds - >= 0
|
|
long tv_nsec; // Nanoseconds - [0, 999999999]
|
|
};
|
|
|
|
#endif
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
/**
|
|
* @brief timespec_get. Wrapper if not supported natively.
|
|
*
|
|
* @param ts, timespec to receive the timestamp.
|
|
* @param base, must be TIME_UTC.
|
|
*
|
|
* @return base on success, 0 on error.
|
|
*/
|
|
#if (defined ACTIV_PLATFORM_WINDOWS)
|
|
|
|
#if (!defined _CRT_NO_TIME_T)
|
|
|
|
using ::timespec_get;
|
|
|
|
#else
|
|
|
|
#if (!defined TIME_UTC)
|
|
#define TIME_UTC 1
|
|
#endif
|
|
|
|
inline int timespec_get(timespec *ts, int base)
|
|
{
|
|
assert(base == TIME_UTC);
|
|
|
|
timeb tb;
|
|
::ftime(&tb);
|
|
|
|
ts->tv_sec = tb.time;
|
|
ts->tv_nsec = (tb.millitm * 1000000);
|
|
|
|
return base;
|
|
}
|
|
|
|
#endif
|
|
|
|
#elif (defined ACTIV_PLATFORM_OSX)
|
|
|
|
#if (!defined TIME_UTC)
|
|
#define TIME_UTC 1
|
|
#endif
|
|
|
|
inline int timespec_get(timespec *ts, int base)
|
|
{
|
|
assert(base == TIME_UTC);
|
|
|
|
struct timeval timeVal;
|
|
|
|
(void) ::gettimeofday(&timeVal, NULL);
|
|
|
|
ts->tv_sec = timeVal.tv_sec;
|
|
ts->tv_nsec = (timeVal.tv_usec * 1000);
|
|
|
|
return base;
|
|
}
|
|
|
|
#elif (defined ACTIV_PLATFORM_POSIX)
|
|
|
|
#if (defined __USE_ISOC11) || (defined ACTIV_PLATFORM_SOLARIS)
|
|
|
|
using ::timespec_get;
|
|
|
|
#else
|
|
|
|
#if (!defined TIME_UTC)
|
|
#define TIME_UTC 1
|
|
#endif
|
|
|
|
inline int timespec_get(timespec *ts, int base)
|
|
{
|
|
assert(base == TIME_UTC);
|
|
|
|
(void) ::clock_gettime(CLOCK_REALTIME, ts);
|
|
|
|
return base;
|
|
}
|
|
|
|
#endif
|
|
|
|
#else
|
|
#error Need to add for this platform
|
|
#endif
|
|
|
|
} // namespace Activ
|
|
|
|
#endif // (!defined ACTIV_MISC_CRT_HELPER_H)
|