Browse Source

convert to GMT, not localtime in GNUNET_TIME_year_to_time

Christian Grothoff 3 years ago
parent
commit
4769344a7a
2 changed files with 32 additions and 2 deletions
  1. 1 1
      configure.ac
  2. 31 1
      src/util/time.c

+ 1 - 1
configure.ac

@@ -1538,7 +1538,7 @@ AC_FUNC_VPRINTF
 AC_HEADER_SYS_WAIT
 AC_TYPE_OFF_T
 AC_TYPE_UID_T
-AC_CHECK_FUNCS([atoll stat64 strnlen mremap getrlimit setrlimit sysconf initgroups strndup gethostbyname2 getpeerucred getpeereid setresuid $funcstocheck getifaddrs freeifaddrs getresgid mallinfo malloc_size malloc_usable_size getrusage random srandom stat statfs statvfs wait4])
+AC_CHECK_FUNCS([atoll stat64 strnlen mremap getrlimit setrlimit sysconf initgroups strndup gethostbyname2 getpeerucred getpeereid setresuid $funcstocheck getifaddrs freeifaddrs getresgid mallinfo malloc_size malloc_usable_size getrusage random srandom stat statfs statvfs wait4 timegm])
 
 # restore LIBS
 LIBS=$SAVE_LIBS

+ 31 - 1
src/util/time.c

@@ -716,6 +716,32 @@ GNUNET_TIME_time_to_year (struct GNUNET_TIME_Absolute at)
 }
 
 
+#ifndef HAVE_TIMEGM
+/**
+ * As suggested in the timegm() man page.
+ */
+static time_t
+my_timegm (struct tm *tm)
+{
+  time_t ret;
+  char *tz;
+
+  tz = getenv ("TZ");
+  setenv ("TZ", "", 1);
+  tzset ();
+  ret = mktime (tm);
+  if (tz)
+    setenv ("TZ", tz, 1);
+  else
+    unsetenv ("TZ");
+  tzset ();
+  return ret;
+}
+
+
+#endif
+
+
 /**
  * Convert a year to an expiration time of January 1st of that year.
  *
@@ -740,7 +766,11 @@ GNUNET_TIME_year_to_time (unsigned int year)
   t.tm_mon = 0;
   t.tm_wday = 1;
   t.tm_yday = 1;
-  tp = mktime (&t);
+#ifndef HAVE_TIMEGM
+  tp = my_timegm (&t);
+#else
+  tp = timegm (&t);
+#endif
   GNUNET_break (tp != (time_t) -1);
   ret.abs_value_us = tp * 1000LL * 1000LL; /* seconds to microseconds */
   return ret;