| 1 | /* |
| 2 | * SPDX-License-Identifier: MIT |
| 3 | * |
| 4 | * Copyright (C) 2013 JP Ikaheimonen <jp_ikaheimonen@mentor.com> |
| 5 | * Copyright (C) 2016 Michael Jeanson <mjeanson@efficios.com> |
| 6 | */ |
| 7 | |
| 8 | #ifndef _BABELTRACE_INCLUDE_COMPAT_TIME_H |
| 9 | #define _BABELTRACE_INCLUDE_COMPAT_TIME_H |
| 10 | |
| 11 | #include <time.h> |
| 12 | #include <stdlib.h> |
| 13 | |
| 14 | #ifdef __MINGW32__ |
| 15 | |
| 16 | #include <string.h> |
| 17 | |
| 18 | /* |
| 19 | * The Windows version of the time functions use one common tm structure per |
| 20 | * thread which makes them thread-safe. Implement the POSIX _r variants by |
| 21 | * copying this to a user supplied struct. |
| 22 | */ |
| 23 | |
| 24 | static inline |
| 25 | struct tm *bt_gmtime_r(const time_t *timep, struct tm *result) |
| 26 | { |
| 27 | struct tm *local_res; |
| 28 | |
| 29 | if (!result) { |
| 30 | goto error; |
| 31 | } |
| 32 | |
| 33 | local_res = gmtime(timep); |
| 34 | if (!local_res) { |
| 35 | result = NULL; |
| 36 | goto error; |
| 37 | } |
| 38 | |
| 39 | memcpy(result, local_res, sizeof(struct tm)); |
| 40 | |
| 41 | error: |
| 42 | return result; |
| 43 | } |
| 44 | |
| 45 | static inline |
| 46 | struct tm *bt_localtime_r(const time_t *timep, struct tm *result) |
| 47 | { |
| 48 | struct tm *local_res; |
| 49 | |
| 50 | if (!result) { |
| 51 | goto error; |
| 52 | } |
| 53 | |
| 54 | local_res = localtime(timep); |
| 55 | if (!local_res) { |
| 56 | result = NULL; |
| 57 | goto error; |
| 58 | } |
| 59 | |
| 60 | memcpy(result, local_res, sizeof(struct tm)); |
| 61 | |
| 62 | error: |
| 63 | return result; |
| 64 | } |
| 65 | |
| 66 | #else /* __MINGW32__ */ |
| 67 | |
| 68 | static inline |
| 69 | struct tm *bt_gmtime_r(const time_t *timep, struct tm *result) |
| 70 | { |
| 71 | return gmtime_r(timep, result); |
| 72 | } |
| 73 | |
| 74 | static inline |
| 75 | struct tm *bt_localtime_r(const time_t *timep, struct tm *result) |
| 76 | { |
| 77 | return localtime_r(timep, result); |
| 78 | } |
| 79 | |
| 80 | #endif /* __MINGW32__ */ |
| 81 | #endif /* _BABELTRACE_INCLUDE_COMPAT_TIME_H */ |