Merge pull request #2796 from jimklimov/fix-docs-markup
[networkupstools.git] / include / timehead.h
blob0eeea4bf9b5652c047bfb8e03aabb250717b027f
1 /* timehead.h - from the autoconf docs: sanely include the right time headers everywhere
3 Copyright (C) 2001 Russell Kroll <rkroll@exploits.org>
4 2005 Arnaud Quette <arnaud.quette@free.fr>
5 2020-2024 Jim Klimov <jimklimov+nut@gmail.com>
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #ifndef NUT_TIMEHEAD_H_SEEN
23 #define NUT_TIMEHEAD_H_SEEN 1
25 #ifdef __cplusplus
26 /* *INDENT-OFF* */
27 extern "C" {
28 /* *INDENT-ON* */
29 #endif
31 #ifdef TIME_WITH_SYS_TIME
32 # include <sys/time.h>
33 # include <time.h>
34 #else
35 # ifdef HAVE_SYS_TIME_H
36 # include <sys/time.h>
37 # else
38 # include <time.h>
39 # endif
40 #endif
42 #ifndef HAVE_STRPTIME
43 /* Use fallback implementation provided in e.g. libcommon(client).la: */
44 char * strptime(const char *buf, const char *fmt, struct tm *tm);
45 #endif
47 #if !(defined HAVE_LOCALTIME_R && HAVE_LOCALTIME_R) && !(defined HAVE_DECL_LOCALTIME_R && HAVE_DECL_LOCALTIME_R)
48 # if (defined HAVE_LOCALTIME_S && HAVE_LOCALTIME_S) || (defined HAVE_DECL_LOCALTIME_S && HAVE_DECL_LOCALTIME_S)
49 /* A bit of a silly trick, but should help on MSYS2 builds it seems
50 * errno_t localtime_s(struct tm *_Tm, const time_t *_Time)
52 # define localtime_r(timer, buf) (localtime_s(buf, timer) ? NULL : buf)
53 # else
54 # include <string.h> /* memcpy */
55 static inline struct tm *localtime_r( const time_t *timer, struct tm *buf ) {
56 /* Note: not thread-safe per se! */
57 struct tm *tmp = localtime (timer);
58 memcpy(buf, tmp, sizeof(struct tm));
59 return buf;
61 # endif
62 #endif
64 #if !(defined HAVE_GMTIME_R && HAVE_GMTIME_R) && !(defined HAVE_DECL_GMTIME_R && HAVE_DECL_GMTIME_R)
65 # if (defined HAVE_GMTIME_S && HAVE_GMTIME_S) || (defined HAVE_DECL_GMTIME_S && HAVE_DECL_GMTIME_S)
66 /* See comment above */
67 # define gmtime_r(timer, buf) (gmtime_s(buf, timer) ? NULL : buf)
68 # else
69 # include <string.h> /* memcpy */
70 static inline struct tm *gmtime_r( const time_t *timer, struct tm *buf ) {
71 /* Note: not thread-safe per se! */
72 struct tm *tmp = gmtime (timer);
73 memcpy(buf, tmp, sizeof(struct tm));
74 return buf;
76 # endif
77 #endif
79 #if !(defined HAVE_TIMEGM && HAVE_TIMEGM) && !(defined HAVE_DECL_TIMEGM && HAVE_DECL_TIMEGM)
80 # if (defined HAVE__MKGMTIME && HAVE__MKGMTIME) || (defined HAVE_DECL__MKGMTIME && HAVE_DECL__MKGMTIME)
81 # define timegm(tm) _mkgmtime(tm)
82 # else
83 # ifdef WANT_TIMEGM_FALLBACK
84 /* use an implementation from fallbacks in NUT codebase */
85 # define timegm(tm) timegm_fallback(tm)
86 # else
87 # error "No fallback implementation for timegm"
88 # endif
89 # endif
90 #endif
92 #ifdef __cplusplus
93 /* *INDENT-OFF* */
95 /* *INDENT-ON* */
96 #endif
98 #endif /* NUT_TIMEHEAD_H_SEEN */