diff options
| author | Ben Straub <bstraub@github.com> | 2012-05-15 15:41:05 -0700 |
|---|---|---|
| committer | Ben Straub <bstraub@github.com> | 2012-05-15 15:41:05 -0700 |
| commit | 1ce4cc0164fbc14fcb7f686483aa581bc946bfdb (patch) | |
| tree | 628f63f6508247275eac89357b8b1b960d68f23b /src/win32/posix_w32.c | |
| parent | 7c22e72ba65fa4ac9c2989d115435aa60788136c (diff) | |
| download | libgit2-1ce4cc0164fbc14fcb7f686483aa581bc946bfdb.tar.gz | |
Fix date.c build in msvc.
Ported the win32 implementations of gmtime_r,
localtime_r, and gettimeofday to be part of the
posix compatibility layer, and fixed
git_signature_now to use them.
Diffstat (limited to 'src/win32/posix_w32.c')
| -rw-r--r-- | src/win32/posix_w32.c | 76 |
1 files changed, 76 insertions, 0 deletions
diff --git a/src/win32/posix_w32.c b/src/win32/posix_w32.c index 10de70da8..092bafed0 100644 --- a/src/win32/posix_w32.c +++ b/src/win32/posix_w32.c @@ -470,3 +470,79 @@ int p_send(GIT_SOCKET socket, const void *buffer, size_t length, int flags) return send(socket, buffer, (int)length, flags); } + +/** + * Borrowed from http://old.nabble.com/Porting-localtime_r-and-gmtime_r-td15282276.html + * On Win32, `gmtime_r` doesn't exist but `gmtime` is threadsafe, so we can use that + */ +struct tm * +localtime_r (const time_t *timer, struct tm *result) +{ + struct tm *local_result; + local_result = localtime (timer); + + if (local_result == NULL || result == NULL) + return NULL; + + memcpy (result, local_result, sizeof (struct tm)); + return result; +} +struct tm * +gmtime_r (const time_t *timer, struct tm *result) +{ + struct tm *local_result; + local_result = gmtime (timer); + + if (local_result == NULL || result == NULL) + return NULL; + + memcpy (result, local_result, sizeof (struct tm)); + return result; +} + +#if defined(_MSC_VER) || defined(_MSC_EXTENSIONS) +#define DELTA_EPOCH_IN_MICROSECS 11644473600000000Ui64 +#else +#define DELTA_EPOCH_IN_MICROSECS 11644473600000000ULL +#endif + +struct timezone +{ + int tz_minuteswest; /* minutes W of Greenwich */ + int tz_dsttime; /* type of dst correction */ +}; + +int gettimeofday(struct timeval *tv, struct timezone *tz) +{ + FILETIME ft; + unsigned __int64 tmpres = 0; + static int tzflag; + + if (NULL != tv) + { + GetSystemTimeAsFileTime(&ft); + + tmpres |= ft.dwHighDateTime; + tmpres <<= 32; + tmpres |= ft.dwLowDateTime; + + /*converting file time to unix epoch*/ + tmpres /= 10; /*convert into microseconds*/ + tmpres -= DELTA_EPOCH_IN_MICROSECS; + tv->tv_sec = (long)(tmpres / 1000000UL); + tv->tv_usec = (long)(tmpres % 1000000UL); + } + + if (NULL != tz) + { + if (!tzflag) + { + _tzset(); + tzflag++; + } + tz->tz_minuteswest = _timezone / 60; + tz->tz_dsttime = _daylight; + } + + return 0; +} |
