From 1ca8b4d9e81bfe7bed51fa6b858d59b721143054 Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Thu, 13 Feb 2020 17:08:23 -0500 Subject: time: Split out time-related functions from util --- util.c | 86 ------------------------------------------------------------------ 1 file changed, 86 deletions(-) (limited to 'util.c') diff --git a/util.c b/util.c index 6af7d27..ec019ce 100644 --- a/util.c +++ b/util.c @@ -163,92 +163,6 @@ char *xregerror(int err, const regex_t *regex) { return str; } -int xlocaltime(const time_t *timep, struct tm *result) { - // Should be called before localtime_r() according to POSIX.1-2004 - tzset(); - - if (localtime_r(timep, result)) { - return 0; - } else { - return -1; - } -} - -int xgmtime(const time_t *timep, struct tm *result) { - // Should be called before gmtime_r() according to POSIX.1-2004 - tzset(); - - if (gmtime_r(timep, result)) { - return 0; - } else { - return -1; - } -} - -int xmktime(struct tm *tm, time_t *timep) { - *timep = mktime(tm); - - if (*timep == -1) { - int error = errno; - - struct tm tmp; - if (xlocaltime(timep, &tmp) != 0) { - return -1; - } - - if (tm->tm_year != tmp.tm_year || tm->tm_yday != tmp.tm_yday - || tm->tm_hour != tmp.tm_hour || tm->tm_min != tmp.tm_min || tm->tm_sec != tmp.tm_sec) { - errno = error; - return -1; - } - } - - return 0; -} - -int xtimegm(struct tm *tm, time_t *timep) { - // Some man pages for timegm() recommend this as a portable approach - int ret = -1; - int error; - - char *old_tz = getenv("TZ"); - if (old_tz) { - old_tz = strdup(old_tz); - if (!old_tz) { - error = errno; - goto fail; - } - } - - if (setenv("TZ", "UTC0", true) != 0) { - error = errno; - goto fail; - } - - ret = xmktime(tm, timep); - error = errno; - - if (old_tz) { - if (setenv("TZ", old_tz, true) != 0) { - ret = -1; - error = errno; - goto fail; - } - } else { - if (unsetenv("TZ") != 0) { - ret = -1; - error = errno; - goto fail; - } - } - - tzset(); -fail: - free(old_tz); - errno = error; - return ret; -} - void format_mode(mode_t mode, char str[11]) { strcpy(str, "----------"); -- cgit v1.2.3