Function _gmtime64 is available since msvcr70.dll. For older msvcrt versions
provide emulation via WinAPI FileTimeToSystemTime() function which is
available on all Windows versions and takes 64-bit time value. To retrieve
thread local storage for gmtime return value (struct tm), use the
_gmtime32() function with dummy value.
---
mingw-w64-crt/Makefile.am | 2 +
mingw-w64-crt/include/filetime_to_time64.h | 8 ++++
mingw-w64-crt/lib-common/msvcrt.def.in | 2 +-
mingw-w64-crt/misc/_gmtime64.c | 50 ++++++++++++++++++++++
4 files changed, 61 insertions(+), 1 deletion(-)
create mode 100644 mingw-w64-crt/misc/_gmtime64.c
diff --git a/mingw-w64-crt/Makefile.am b/mingw-w64-crt/Makefile.am
index 4482a5c1b117..8ca36efd67cc 100644
--- a/mingw-w64-crt/Makefile.am
+++ b/mingw-w64-crt/Makefile.am
@@ -570,6 +570,7 @@ src_msvcrt32=\
misc/_get_current_locale.c \
misc/_get_doserrno.c \
misc/_get_fmode.c \
+ misc/_gmtime64.c \
misc/_initterm_e.c \
misc/_set_doserrno.c \
misc/_set_fmode.c \
@@ -879,6 +880,7 @@ src_pre_msvcr70=\
misc/_aligned_offset_realloc.c \
misc/_aligned_realloc.c \
misc/_ftime64.c \
+ misc/_gmtime64.c \
misc/_time64.c \
misc/strtoimax.c \
misc/strtoumax.c \
diff --git a/mingw-w64-crt/include/filetime_to_time64.h
b/mingw-w64-crt/include/filetime_to_time64.h
index 2d8459844a5f..afaa2b925f27 100644
--- a/mingw-w64-crt/include/filetime_to_time64.h
+++ b/mingw-w64-crt/include/filetime_to_time64.h
@@ -4,6 +4,14 @@
* No warranty is given; refer to the file DISCLAIMER.PD within this package.
*/
+static inline void time64_to_filetime(__time64_t time64, FILETIME *filetime)
+{
+ /* conversion from signed 64-bit UNIX timestamp (1970-01-01 in seconds) to
unsigned 64-bit FILETIME (1601-01-01 in 100-nanoseconds) */
+ unsigned long long value = (time64 * 10000000) + 116444736000000000LL;
+ filetime->dwLowDateTime = value & 0xffffffff;
+ filetime->dwHighDateTime = value >> 32;
+}
+
static inline __time64_t filetime_to_time64(FILETIME *filetime)
{
unsigned long long value = ((unsigned long long)filetime->dwHighDateTime
<< 32) | filetime->dwLowDateTime;
diff --git a/mingw-w64-crt/lib-common/msvcrt.def.in
b/mingw-w64-crt/lib-common/msvcrt.def.in
index 6bc43bb24bb0..02c7b64d604d 100644
--- a/mingw-w64-crt/lib-common/msvcrt.def.in
+++ b/mingw-w64-crt/lib-common/msvcrt.def.in
@@ -1156,7 +1156,7 @@ _findnext64
F_NON_I386(_fstat64) ; i386 _fstat64 replaced by emu
F_NON_I386(_ftime64) ; i386 _ftime64 replaced by emu
_futime64
-_gmtime64
+F_NON_I386(_gmtime64) ; i386 _gmtime64 replaced by emu
_localtime64
_mktime64
F_X86_ANY(_osplatform DATA)
diff --git a/mingw-w64-crt/misc/_gmtime64.c b/mingw-w64-crt/misc/_gmtime64.c
new file mode 100644
index 000000000000..ebcd10d1a4e2
--- /dev/null
+++ b/mingw-w64-crt/misc/_gmtime64.c
@@ -0,0 +1,50 @@
+/**
+ * This file has no copyright assigned and is placed in the Public Domain.
+ * This file is part of the mingw-w64 runtime package.
+ * No warranty is given; refer to the file DISCLAIMER.PD within this package.
+ */
+
+#include <windows.h>
+#include <time.h>
+
+#include "filetime_to_time64.h"
+
+static const short int days_in_year[] = { -1, 30, 58, 89, 119, 150, 180, 211,
242, 272, 303, 333, 364 };
+
+static struct tm *__cdecl emu__gmtime64(const __time64_t *timeptr)
+{
+ struct tm *tmptr;
+ FILETIME filetime;
+ SYSTEMTIME systemtime;
+ int is_leap_year;
+ int has_leap_day;
+
+ time64_to_filetime(*timeptr, &filetime);
+ if (!FileTimeToSystemTime(&filetime, &systemtime))
+ return NULL;
+
+ /* retrieve thread local storage for gmtime tm */
+ tmptr = _gmtime32(&(__time32_t){0});
+ if (!tmptr)
+ return NULL;
+
+ is_leap_year = ((systemtime.wYear % 4 == 0 && systemtime.wYear % 100 != 0)
|| systemtime.wYear % 400 == 0);
+ has_leap_day = (systemtime.wMonth >= 3 && is_leap_year);
+
+ tmptr->tm_sec = systemtime.wSecond;
+ tmptr->tm_min = systemtime.wMinute;
+ tmptr->tm_hour = systemtime.wHour;
+ tmptr->tm_mday = systemtime.wDay;
+ tmptr->tm_mon = systemtime.wMonth - 1;
+ tmptr->tm_year = systemtime.wYear - 1900;
+ tmptr->tm_wday = systemtime.wDayOfWeek;
+ tmptr->tm_yday = days_in_year[tmptr->tm_mon] + systemtime.wDay +
(has_leap_day ? 1 : 0);
+ tmptr->tm_isdst = 0;
+ return tmptr;
+}
+
+#define RETT struct tm *
+#define FUNC _gmtime64
+#define ARGS const __time64_t *timeptr
+#define CALL timeptr
+#include "msvcrt_or_emu_glue.h"
--
2.20.1
_______________________________________________
Mingw-w64-public mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/mingw-w64-public