summaryrefslogtreecommitdiff
path: root/missing_d
diff options
context:
space:
mode:
authorAndrew J. Schorr <aschorr@telemetry-investments.com>2018-12-12 14:08:15 -0500
committerAndrew J. Schorr <aschorr@telemetry-investments.com>2018-12-12 14:08:15 -0500
commitd5d60a503f6de8866be843b40fe6de7c20a0f3a0 (patch)
tree90a217238b5b5c08536829c177ccaf339b1a13c3 /missing_d
parentc856f5c96f88cc8a5aacf6ee90e92ed80bd8c3ba (diff)
downloadgawk-d5d60a503f6de8866be843b40fe6de7c20a0f3a0.tar.gz
Speed up UTC mktime by using library timegm if available instead of our slow implementation.
Diffstat (limited to 'missing_d')
-rw-r--r--missing_d/ChangeLog4
-rw-r--r--missing_d/timegm.c29
2 files changed, 33 insertions, 0 deletions
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;
+}