logo

utils-std

Collection of commonly available Unix tools git clone https://anongit.hacktivis.me/git/utils-std.git/
commit: d32d759c54b2657970b62eba39f5707dee4bb8b9
parent 7f10d23fd7b1ab10be7a36ad26ec02bdc942b4b8
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date:   Sun, 11 Jan 2026 13:19:12 +0100

libutils/datetime_parse: split utils_timegm to it's own file

Due to BSD visibility annoyances, better put the non-POSIX functions out.

Diffstat:

Mcmd/date.c10+++++++---
Mlibutils/datetime_parse.c13-------------
Mlibutils/datetime_parse.h3+++
Alibutils/utils_timegm.c22++++++++++++++++++++++
4 files changed, 32 insertions(+), 16 deletions(-)

diff --git a/cmd/date.c b/cmd/date.c @@ -2,7 +2,7 @@ // SPDX-FileCopyrightText: 2017 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me> // SPDX-License-Identifier: MPL-2.0 -#define _DEFAULT_SOURCE // tm_gmtoff/tm_zone (POSIX.1-2024), timegm (mktime() future directions) +#define _DEFAULT_SOURCE // tm_gmtoff/tm_zone (POSIX.1-2024) #define _POSIX_C_SOURCE 200809L #define _XOPEN_SOURCE 700 // strptime is in XSI @@ -305,6 +305,10 @@ main(int argc, char *argv[]) if(argc > 0 && input_format != NULL) { struct tm input_tm; + + time_t timez = 0; + gmtime_r(&timez, &input_tm); // init + char *res = strptime(argv[0], input_format, &input_tm); if(res == NULL) @@ -318,7 +322,7 @@ main(int argc, char *argv[]) } /* TODO: %N (nanoseconds) support */ - tp.tv_sec = timegm(&input_tm); + tp.tv_sec = utils_timegm(&input_tm); tp.tv_nsec = 0; if(tp.tv_sec == (time_t)-1) { @@ -389,7 +393,7 @@ main(int argc, char *argv[]) } /* TODO: %N (nanoseconds) support */ - tp.tv_sec = timegm(&arg_tm); + tp.tv_sec = utils_timegm(&arg_tm); tp.tv_nsec = 0; if(tp.tv_sec == (time_t)-1) { diff --git a/libutils/datetime_parse.c b/libutils/datetime_parse.c @@ -71,19 +71,6 @@ tzoffset_parse(char *s, struct tm *time, const char **errstr) return s; } -// Wrap timegm() to still get portable behavior while setting tm_gmtoff directly -static time_t -utils_timegm(struct tm *tm) -{ - long gmtoff = tm->tm_gmtoff; - tm->tm_isdst = 0; - tm->tm_gmtoff = 0; - tm->tm_zone = "UTC"; - - time_t ret = timegm(tm); - return ret - gmtoff; -} - // Sets *errstr to NULL when it isn't an email date-time // // Check if it could be Email / Internet Message Format datetime diff --git a/libutils/datetime_parse.h b/libutils/datetime_parse.h @@ -6,3 +6,6 @@ // Sets errstr on failure extern char *datetime_parse(char *arg, time_t *epoch, long *nsec, const char **errstr); + +// libutils/utils_timegm.c +extern time_t utils_timegm(struct tm *tm); diff --git a/libutils/utils_timegm.c b/libutils/utils_timegm.c @@ -0,0 +1,22 @@ +// utils-std: Collection of commonly available Unix tools +// SPDX-FileCopyrightText: 2017 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me> +// SPDX-License-Identifier: MPL-2.0 + +#define _DEFAULT_SOURCE // tm_gmtoff/tm_zone, timegm (POSIX.1-2024 mktime() future directions) +#include "./datetime_parse.h" + +#include <time.h> + +// Wrap timegm() to still get portable behavior while setting tm_gmtoff directly +// Effectively the opposite of gmtime() +time_t +utils_timegm(struct tm *tm) +{ + long gmtoff = tm->tm_gmtoff; + tm->tm_isdst = 0; + tm->tm_gmtoff = 0; + tm->tm_zone = "UTC"; + + time_t ret = timegm(tm); + return ret - gmtoff; +}