SDDS ToolKit Programs and Libraries for C and Python
Loading...
Searching...
No Matches
time_utils.c
1#include "mdb.h"
2#include "mdb_thread.h"
3#include "time_utils.h"
4
5#if !(defined(_MSC_VER) || defined(LINUX) || defined(SOLARIS) || defined(__APPLE__) || defined(__MACH__) || defined(__unix__) || defined(__CYGWIN__))
6static MDB_THREAD_LOCK localtime_lock = MDB_THREAD_LOCK_INITIALIZER;
7#endif
8
9int mdb_localtime_copy(const time_t *when, struct tm *result) {
10 if (!when || !result)
11 return 0;
12
13#if defined(_MSC_VER)
14 return localtime_s(result, when) == 0;
15#elif defined(LINUX) || defined(SOLARIS) || defined(__APPLE__) || defined(__MACH__) || defined(__unix__) || defined(__CYGWIN__)
16 return localtime_r(when, result) != NULL;
17#else
18 {
19 struct tm *temporary;
20 int status;
21
22 /* Last resort for platforms without reentrant localtime APIs. */
23 mdb_thread_lock(&localtime_lock);
24 temporary = localtime(when);
25 status = temporary != NULL;
26 if (status)
27 *result = *temporary;
28 mdb_thread_unlock(&localtime_lock);
29 return status;
30 }
31#endif
32}