mirror of
git://git.gnupg.org/gnupg.git
synced 2025-07-02 22:46:30 +02:00
common: New functions timegm_u64, isotime2epoch_u64.
* common/mischelp.c (timegm): Move to ... * common/gettime.c (timegm): here. On Windows use timegm_u32. (timegm_u32): New. (isotime2epoch): Factor code out to ... (isotime_make_tm): new helper. (isotime2epoch_u64): New. (_win32_timegm): Remove duplicated code. (parse_timestamp): Use of timegm. (scan_isodatestr): Fallback to isotime2epoch_u64. -- This mainly helps on 32 bit Windows. For Unix we assume everyone is using 64 bit or shall wait until the libc hackers finally provide a time64_t. GnuPG-bug-id: 6736
This commit is contained in:
parent
2c3c049fd8
commit
164c687cb6
4 changed files with 176 additions and 154 deletions
|
@ -126,80 +126,3 @@ same_file_p (const char *name1, const char *name2)
|
|||
}
|
||||
return yes;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
timegm() is a GNU function that might not be available everywhere.
|
||||
It's basically the inverse of gmtime() - you give it a struct tm,
|
||||
and get back a time_t. It differs from mktime() in that it handles
|
||||
the case where the struct tm is UTC and the local environment isn't.
|
||||
|
||||
Note, that this replacement implementation might not be thread-safe!
|
||||
|
||||
Some BSDs don't handle the putenv("foo") case properly, so we use
|
||||
unsetenv if the platform has it to remove environment variables.
|
||||
*/
|
||||
#ifndef HAVE_TIMEGM
|
||||
time_t
|
||||
timegm (struct tm *tm)
|
||||
{
|
||||
#ifdef HAVE_W32_SYSTEM
|
||||
/* This one is thread safe. */
|
||||
SYSTEMTIME st;
|
||||
FILETIME ft;
|
||||
unsigned long long cnsecs;
|
||||
|
||||
st.wYear = tm->tm_year + 1900;
|
||||
st.wMonth = tm->tm_mon + 1;
|
||||
st.wDay = tm->tm_mday;
|
||||
st.wHour = tm->tm_hour;
|
||||
st.wMinute = tm->tm_min;
|
||||
st.wSecond = tm->tm_sec;
|
||||
st.wMilliseconds = 0; /* Not available. */
|
||||
st.wDayOfWeek = 0; /* Ignored. */
|
||||
|
||||
/* System time is UTC thus the conversion is pretty easy. */
|
||||
if (!SystemTimeToFileTime (&st, &ft))
|
||||
{
|
||||
gpg_err_set_errno (EINVAL);
|
||||
return (time_t)(-1);
|
||||
}
|
||||
|
||||
cnsecs = (((unsigned long long)ft.dwHighDateTime << 32)
|
||||
| ft.dwLowDateTime);
|
||||
cnsecs -= 116444736000000000ULL; /* The filetime epoch is 1601-01-01. */
|
||||
return (time_t)(cnsecs / 10000000ULL);
|
||||
|
||||
#else /* (Non thread safe implementation!) */
|
||||
|
||||
time_t answer;
|
||||
char *zone;
|
||||
|
||||
zone=getenv("TZ");
|
||||
putenv("TZ=UTC");
|
||||
tzset();
|
||||
answer=mktime(tm);
|
||||
if(zone)
|
||||
{
|
||||
static char *old_zone;
|
||||
|
||||
if (!old_zone)
|
||||
{
|
||||
old_zone = malloc(3+strlen(zone)+1);
|
||||
if (old_zone)
|
||||
{
|
||||
strcpy(old_zone,"TZ=");
|
||||
strcat(old_zone,zone);
|
||||
}
|
||||
}
|
||||
if (old_zone)
|
||||
putenv (old_zone);
|
||||
}
|
||||
else
|
||||
gnupg_unsetenv("TZ");
|
||||
|
||||
tzset();
|
||||
return answer;
|
||||
#endif
|
||||
}
|
||||
#endif /*!HAVE_TIMEGM*/
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue