Skip to content

Commit d7bec3b

Browse files
romanmichalvasko
authored andcommitted
compat UPDATE add timegm to compat
1 parent d67abae commit d7bec3b

3 files changed

Lines changed: 41 additions & 0 deletions

File tree

CMakeModules/UseCompat.cmake

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ macro(USE_COMPAT)
6262

6363
check_symbol_exists(get_current_dir_name "unistd.h" HAVE_GET_CURRENT_DIR_NAME)
6464

65+
check_function_exists(timegm HAVE_TIMEGM)
66+
6567
# crypt
6668
check_include_file("crypt.h" HAVE_CRYPT_H)
6769

compat/compat.c

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,3 +391,37 @@ crypt_r(const char *phrase, const char *setting, struct crypt_data *data)
391391
}
392392

393393
#endif
394+
395+
#ifndef HAVE_TIMEGM
396+
time_t
397+
timegm(struct tm *tm)
398+
{
399+
pthread_mutex_t tz_lock = PTHREAD_MUTEX_INITIALIZER;
400+
time_t ret;
401+
char *tz;
402+
403+
pthread_mutex_lock(&tz_lock);
404+
405+
tz = getenv("TZ");
406+
if (tz) {
407+
tz = strdup(tz);
408+
}
409+
setenv("TZ", "", 1);
410+
tzset();
411+
412+
ret = mktime(tm);
413+
414+
if (tz) {
415+
setenv("TZ", tz, 1);
416+
free(tz);
417+
} else {
418+
unsetenv("TZ");
419+
}
420+
tzset();
421+
422+
pthread_mutex_unlock(&tz_lock);
423+
424+
return ret;
425+
}
426+
427+
#endif

compat/compat.h.in

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@
7777
#cmakedefine HAVE_STRCHRNUL
7878
#cmakedefine HAVE_GET_CURRENT_DIR_NAME
7979
#cmakedefine HAVE_CRYPT_R
80+
#cmakedefine HAVE_TIMEGM
8081

8182
#ifndef bswap64
8283
#define bswap64(val) \
@@ -222,4 +223,8 @@ struct crypt_data {
222223
char *crypt_r(const char *phrase, const char *setting, struct crypt_data *data);
223224
#endif
224225

226+
#ifndef HAVE_TIMEGM
227+
time_t timegm(struct tm *tm);
228+
#endif
229+
225230
#endif /* _COMPAT_H_ */

0 commit comments

Comments
 (0)