From df57a2351260832774ac0c4bf2fa7a1fe45744ee Mon Sep 17 00:00:00 2001 From: James Turner Date: Wed, 1 Jan 2014 21:03:24 +0000 Subject: [PATCH] #1207: Add helper to get Unix time in GMT On Windows, time() is returning a value which if offset by the selected time-zone. Provide a variant which is always in GMT. --- simgear/timing/lowleveltime.cxx | 12 ++++++++++++ simgear/timing/lowleveltime.h | 5 +++++ 2 files changed, 17 insertions(+) diff --git a/simgear/timing/lowleveltime.cxx b/simgear/timing/lowleveltime.cxx index 9d20655d..36fd1d35 100644 --- a/simgear/timing/lowleveltime.cxx +++ b/simgear/timing/lowleveltime.cxx @@ -1134,3 +1134,15 @@ char *tzstring (const char* string) return strncpy (p, string, needed); } + +time_t sgGMTime() +{ + struct tm now; + time_t now_sec = time(0); +#if defined(SG_WINDOWS) + now = *gmtime(&now_sec); +#else + gmtime_r(&now_sec, &now); +#endif + return mktime(&now); +} \ No newline at end of file diff --git a/simgear/timing/lowleveltime.h b/simgear/timing/lowleveltime.h index 74be3b8e..1be49531 100644 --- a/simgear/timing/lowleveltime.h +++ b/simgear/timing/lowleveltime.h @@ -41,6 +41,11 @@ void show (const char *zone, time_t t, int v); /* adapted from */ struct tm * fgLocaltime (const time_t *t, const char *tzName); +/* version of time() which returns a value in GMT/UTC, without + any timezone adjustment. Necessary on Windows where calling time() + returns a value in the local time-zone. */ +time_t sgGMTime(); + /* Prototype for the internal function to get information based on TZ. */ extern struct tm *fgtz_convert (const time_t *t, int use_localtime, struct tm *tp, const char *tzName); -- 2.39.5