diff options
author | Andrew J. Schorr <aschorr@telemetry-investments.com> | 2018-12-12 14:08:15 -0500 |
---|---|---|
committer | Andrew J. Schorr <aschorr@telemetry-investments.com> | 2018-12-12 14:08:15 -0500 |
commit | d5d60a503f6de8866be843b40fe6de7c20a0f3a0 (patch) | |
tree | 90a217238b5b5c08536829c177ccaf339b1a13c3 | |
parent | c856f5c96f88cc8a5aacf6ee90e92ed80bd8c3ba (diff) | |
download | egawk-d5d60a503f6de8866be843b40fe6de7c20a0f3a0.tar.gz egawk-d5d60a503f6de8866be843b40fe6de7c20a0f3a0.tar.bz2 egawk-d5d60a503f6de8866be843b40fe6de7c20a0f3a0.zip |
Speed up UTC mktime by using library timegm if available instead of our slow implementation.
-rwxr-xr-x | ChangeLog | 8 | ||||
-rw-r--r-- | builtin.c | 30 | ||||
-rw-r--r-- | configh.in | 3 | ||||
-rwxr-xr-x | configure | 2 | ||||
-rw-r--r-- | configure.ac | 2 | ||||
-rw-r--r-- | missing_d/ChangeLog | 4 | ||||
-rw-r--r-- | missing_d/timegm.c | 29 | ||||
-rw-r--r-- | protos.h | 4 | ||||
-rw-r--r-- | replace.c | 4 |
9 files changed, 55 insertions, 31 deletions
@@ -1,3 +1,11 @@ +2018-12-12 Andrew J. Schorr <aschorr@telemetry-investments.com> + + * configure.ac (AC_CHECK_FUNCS): Check for timegm. + * builtin.c (mktime_tz): Remove function; we will use timegm instead. + (do_mktime): Replace 'mktime_tz(& then, "UTC+0")' with 'timegm(& then)'. + * protos.h (timegm): Add timegm proto on systems lacking it. + * replace.c (timegm): Include missing_d/timegm.c if needed. + 2018-12-06 Arnold D. Robbins <arnold@skeeve.com> * configure.ac: Add -ggdb3 to CFLAGS if developing and remove @@ -2096,34 +2096,6 @@ do_systime(int nargs ATTRIBUTE_UNUSED) return make_number((AWKNUM) lclock); } -/* mktime_tz --- based on Linux timegm man page */ - -static time_t -mktime_tz(struct tm *tm, const char *tzreq) -{ - time_t ret; - char *tz = getenv("TZ"); - - if (tz) - tz = estrdup(tz, strlen(tz)); - if (setenv("TZ", tzreq, 1) < 0) { - warning(_("setenv(TZ, %s) failed (%s)"), tzreq, strerror(errno)); - return -1; - } - tzset(); - ret = mktime(tm); - if (tz) { - if (setenv("TZ", tz, 1) < 0) - fatal(_("setenv(TZ, %s) restoration failed (%s)"), tz, strerror(errno)); - free(tz); - } else { - if (unsetenv("TZ") < 0) - fatal(_("unsetenv(TZ) failed (%s)"), strerror(errno)); - } - tzset(); - return ret; -} - /* do_mktime --- turn a time string into a timestamp */ NODE * @@ -2184,7 +2156,7 @@ do_mktime(int nargs) then.tm_year = year - 1900; then.tm_isdst = dst; - then_stamp = (do_gmt ? mktime_tz(& then, "UTC+0") : mktime(& then)); + then_stamp = (do_gmt ? timegm(& then) : mktime(& then)); return make_number((AWKNUM) then_stamp); } @@ -279,6 +279,9 @@ /* Define to 1 if you have the <termios.h> header file. */ #undef HAVE_TERMIOS_H +/* Define to 1 if you have the `timegm' function. */ +#undef HAVE_TIMEGM + /* Define to 1 if you have the `tmpfile' function. */ #undef HAVE_TMPFILE @@ -9982,7 +9982,7 @@ for ac_func in __etoa_l atexit btowc fmod gai_strerror \ memset_ulong mkstemp posix_openpt setenv setlocale setsid sigprocmask \ snprintf strchr \ strerror strftime strcasecmp strncasecmp strcoll strtod strtoul \ - system tmpfile towlower towupper tzset usleep waitpid wcrtomb \ + system timegm tmpfile towlower towupper tzset usleep waitpid wcrtomb \ wcscoll wctype do : as_ac_var=`$as_echo "ac_cv_func_$ac_func" | $as_tr_sh` diff --git a/configure.ac b/configure.ac index c6c7e501..bbc87b9e 100644 --- a/configure.ac +++ b/configure.ac @@ -312,7 +312,7 @@ AC_CHECK_FUNCS(__etoa_l atexit btowc fmod gai_strerror \ memset_ulong mkstemp posix_openpt setenv setlocale setsid sigprocmask \ snprintf strchr \ strerror strftime strcasecmp strncasecmp strcoll strtod strtoul \ - system tmpfile towlower towupper tzset usleep waitpid wcrtomb \ + system timegm tmpfile towlower towupper tzset usleep waitpid wcrtomb \ wcscoll wctype) dnl this check is for both mbrtowc and the mbstate_t type, which is good AC_FUNC_MBRTOWC diff --git a/missing_d/ChangeLog b/missing_d/ChangeLog index bc54838f..2d69087a 100644 --- a/missing_d/ChangeLog +++ b/missing_d/ChangeLog @@ -1,3 +1,7 @@ +2018-12-12 Andrew J. Schorr <aschorr@telemetry-investments.com> + + * timegm.c: New file implementing timegm. + 2018-10-03 Arnold D. Robbins <arnold@skeeve.com> * intprops.h, verify.h: Removed. The copy in ../support is diff --git a/missing_d/timegm.c b/missing_d/timegm.c new file mode 100644 index 00000000..f7b780de --- /dev/null +++ b/missing_d/timegm.c @@ -0,0 +1,29 @@ +#include <time.h> +#include <stdlib.h> + +static time_t +timegm(struct tm *tm) +{ + time_t ret; + char *tz = getenv("TZ"); + const char *tzreq = "UTC+0"; /* more portable than ""? */ + + if (tz) + tz = estrdup(tz, strlen(tz)); + if (setenv("TZ", tzreq, 1) < 0) { + warning(_("setenv(TZ, %s) failed (%s)"), tzreq, strerror(errno)); + return -1; + } + tzset(); + ret = mktime(tm); + if (tz) { + if (setenv("TZ", tz, 1) < 0) + fatal(_("setenv(TZ, %s) restoration failed (%s)"), tz, strerror(errno)); + free(tz); + } else { + if (unsetenv("TZ") < 0) + fatal(_("unsetenv(TZ) failed (%s)"), strerror(errno)); + } + tzset(); + return ret; +} @@ -129,6 +129,10 @@ extern void tzset(); extern time_t mktime(struct tm *tp); #endif +#ifndef HAVE_TIMEGM +extern time_t timegm(struct tm *); +#endif + #ifndef HAVE_SNPRINTF extern int snprintf(char *restrict buf, size_t len, const char *restrict fmt, ...); #endif @@ -91,6 +91,10 @@ #include "missing_d/mktime.c" #endif /* HAVE_MKTIME */ +#ifndef HAVE_TIMEGM +#include "missing_d/timegm.c" +#endif /* HAVE_TIMEGM */ + #ifndef HAVE_SNPRINTF #include "missing_d/snprintf.c" #endif |