#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.
This commit is contained in:
James Turner 2014-01-01 21:03:24 +00:00
parent aea71cf0af
commit df57a23512
2 changed files with 17 additions and 0 deletions

View File

@ -1134,3 +1134,15 @@ char *tzstring (const char* string)
return strncpy (p, string, needed); 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);
}

View File

@ -41,6 +41,11 @@ void show (const char *zone, time_t t, int v);
/* adapted from <time.h> */ /* adapted from <time.h> */
struct tm * fgLocaltime (const time_t *t, const char *tzName); 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. */ /* Prototype for the internal function to get information based on TZ. */
extern struct tm *fgtz_convert (const time_t *t, int use_localtime, extern struct tm *fgtz_convert (const time_t *t, int use_localtime,
struct tm *tp, const char *tzName); struct tm *tp, const char *tzName);