On Tuesday 23 September 2025 20:21:41 Pali Rohár wrote:
> Function _ftime64 is available since msvcr70.dll. For older msvcrt versions
> provide emulation via 32-bit CRT ftime() function and fill 64-bit time
> value via WinAPI GetSystemTime() function which is available on all Windows
> versions and returns value in SYSTEMTIME format, including milliseconds.
> ---
>  mingw-w64-crt/Makefile.am                  |  2 ++
>  mingw-w64-crt/include/filetime_to_time64.h |  7 ++++
>  mingw-w64-crt/lib-common/msvcrt.def.in     |  2 +-
>  mingw-w64-crt/misc/_ftime64.c              | 39 ++++++++++++++++++++++
>  4 files changed, 49 insertions(+), 1 deletion(-)
>  create mode 100644 mingw-w64-crt/misc/_ftime64.c
> 
> diff --git a/mingw-w64-crt/Makefile.am b/mingw-w64-crt/Makefile.am
> index 91a7864186d5..57b110be331b 100644
> --- a/mingw-w64-crt/Makefile.am
> +++ b/mingw-w64-crt/Makefile.am
> @@ -572,6 +572,7 @@ src_msvcrt32=\
>    misc/_aligned_realloc.c \
>    misc/_create_locale.c \
>    misc/_free_locale.c \
> +  misc/_ftime64.c \
>    misc/_get_current_locale.c \
>    misc/_get_doserrno.c \
>    misc/_get_fmode.c \
> @@ -881,6 +882,7 @@ src_pre_msvcr70=\
>    misc/_aligned_offset_malloc.c \
>    misc/_aligned_offset_realloc.c \
>    misc/_aligned_realloc.c \
> +  misc/_ftime64.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..b0d805ffbfa4 100644
> --- a/mingw-w64-crt/include/filetime_to_time64.h
> +++ b/mingw-w64-crt/include/filetime_to_time64.h
> @@ -11,3 +11,10 @@ static inline __time64_t filetime_to_time64(FILETIME 
> *filetime)
>      /* conversion from unsigned 64-bit FILETIME (1601-01-01 in 
> 100-nanoseconds) to signed 64-bit UNIX timestamp (1970-01-01 in seconds) */
>      return (value - 116444736000000000LL) / 10000000;
>  }
> +
> +static inline unsigned short filetime_to_millitm(FILETIME *filetime)
> +{
> +    unsigned long long value = ((unsigned long long)filetime->dwHighDateTime 
> << 32) | filetime->dwLowDateTime;
> +    /* conversion from unsigned 64-bit FILETIME (1601-01-01 in 
> 100-nanoseconds) to unsigned timeb millitm (milliseconds in last second) */
> +    return (value / 10000ULL) % 1000;
> +}
> diff --git a/mingw-w64-crt/lib-common/msvcrt.def.in 
> b/mingw-w64-crt/lib-common/msvcrt.def.in
> index 236eb591d767..6cbe50c6f60c 100644
> --- a/mingw-w64-crt/lib-common/msvcrt.def.in
> +++ b/mingw-w64-crt/lib-common/msvcrt.def.in
> @@ -1154,7 +1154,7 @@ _ctime64
>  _findfirst64
>  _findnext64
>  F_NON_I386(_fstat64) ; i386 _fstat64 replaced by emu
> -_ftime64
> +F_NON_I386(_ftime64) ; i386 _ftime64 replaced by emu
>  _futime64
>  _gmtime64
>  _localtime64
> diff --git a/mingw-w64-crt/misc/_ftime64.c b/mingw-w64-crt/misc/_ftime64.c
> new file mode 100644
> index 000000000000..2039920445eb
> --- /dev/null
> +++ b/mingw-w64-crt/misc/_ftime64.c
> @@ -0,0 +1,39 @@
> +/**
> + * 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 <sys/timeb.h>
> +
> +#include "filetime_to_time64.h"
> +
> +static void __cdecl emu__ftime64(struct __timeb64 *tb64)
> +{
> +    struct __timeb32 tb32;
> +    SYSTEMTIME systemtime;
> +    FILETIME filetime;
> +
> +    _ftime32(&tb32);
> +    tb64->timezone = tb32.timezone;
> +    tb64->dstflag = tb32.dstflag;
> +
> +    GetSystemTime(&systemtime);
> +    if (SystemTimeToFileTime(&systemtime, &filetime))
> +    {
> +        tb64->time = filetime_to_time64(&filetime);
> +        tb64->millitm = filetime_to_millitm(&filetime);

Now, when I'm looking at this code again, the new function
filetime_to_millitm() is not needed. milliseconds are already stored in
the systemtime.wMilliseconds (filled by GetSystemTime).

I will change it in new version.

> +    }
> +    else
> +    {
> +        tb64->time = (__time64_t)tb32.time; /* truncated */
> +        tb64->millitm = tb32.millitm;
> +    }
> +}
> +
> +#define RETT void
> +#define FUNC _ftime64
> +#define ARGS struct __timeb64 *tb
> +#define CALL tb
> +#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

Reply via email to