On Thursday 09 October 2025 16:00:13 Martin Storsjö wrote:
> On Tue, 23 Sep 2025, Pali Rohár wrote:
> 
> > 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/lib-common/msvcrt.def.in |  2 +-
> > mingw-w64-crt/misc/_gmtime64.c         | 46 ++++++++++++++++++++++++++
> > 3 files changed, 49 insertions(+), 1 deletion(-)
> > create mode 100644 mingw-w64-crt/misc/_gmtime64.c
> 
> > +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;
> > +    unsigned long long value;
> > +    FILETIME filetime;
> > +    SYSTEMTIME systemtime;
> > +
> > +    value = (*timeptr * 10000000) + 116444736000000000LL;
> > +    filetime.dwLowDateTime = value & 0xffffffff;
> > +    filetime.dwHighDateTime = value >> 32;
> > +    if (!FileTimeToSystemTime(&filetime, &systemtime))
> > +        return NULL;
> > +
> > +    /* retrieve thread local storage for gmtime tm */
> > +    tmptr = _gmtime32(&(__time32_t){0});
> > +    if (!tmptr)
> > +        return NULL;
> > +
> > +    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;
> 
> Some spaces around the operator would make this more readable
> 
> > +    tmptr->tm_wday = systemtime.wDayOfWeek;
> > +    tmptr->tm_yday = days_in_year[tmptr->tm_mon] + systemtime.wDay + 
> > ((systemtime.wMonth >= 3 && ((systemtime.wYear % 4 == 0 && systemtime.wYear 
> > % 100 != 0) || systemtime.wYear % 400 == 0)) ? 1 : 0);
> 
> This expression is quite unwieldy; it would be much nicer with two
> intermediate variables; leap_year and leap_day I think.
> 
> // Martin

Ok, I can change the code in this way.


_______________________________________________
Mingw-w64-public mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/mingw-w64-public

Reply via email to