Browse Source

Add sub-ms Windows date provider

pull/1664/head
Sami Vaarala 7 years ago
parent
commit
a2b08ef563
  1. 32
      src-input/duk_bi_date_windows.c
  2. 3
      src-input/duk_bi_protos.h

32
src-input/duk_bi_date_windows.c

@ -22,6 +22,12 @@ DUK_LOCAL void duk__convert_systime_to_ularge(const SYSTEMTIME *st, ULARGE_INTEG
res->HighPart = ft.dwHighDateTime;
}
}
DUK_LOCAL void duk__convert_filetime_to_ularge(const FILETIME *ft, ULARGE_INTEGER *res) {
res->LowPart = ft->dwLowDateTime;
res->HighPart = ft->dwHighDateTime;
}
DUK_LOCAL void duk__set_systime_jan1970(SYSTEMTIME *st) {
DUK_MEMZERO((void *) st, sizeof(*st));
st->wYear = 1970;
@ -52,12 +58,36 @@ DUK_INTERNAL duk_double_t duk_bi_date_get_now_windows(duk_hthread *thr) {
duk__convert_systime_to_ularge((const SYSTEMTIME *) &st2, &tmp2);
/* Difference is in 100ns units, convert to milliseconds, keeping
* fractions since Duktape 2.2.0.
* fractions since Duktape 2.2.0. This is only theoretical because
* SYSTEMTIME is limited to milliseconds.
*/
return (duk_double_t) ((LONGLONG) tmp1.QuadPart - (LONGLONG) tmp2.QuadPart) / 10000.0;
}
#endif /* DUK_USE_DATE_NOW_WINDOWS */
#if defined(DUK_USE_DATE_NOW_WINDOWS_SUBMS)
DUK_INTERNAL duk_double_t duk_bi_date_get_now_windows_subms(duk_hthread *thr) {
/* Variant of the basic algorithm using GetSystemTimePreciseAsFileTime()
* for more accuracy.
*/
FILETIME ft1;
SYSTEMTIME st2;
ULARGE_INTEGER tmp1, tmp2;
DUK_UNREF(thr);
GetSystemTimePreciseAsFileTime(&ft1);
duk__convert_filetime_to_ularge((const FILETIME *) &ft1, &tmp1);
duk__set_systime_jan1970(&st2);
duk__convert_systime_to_ularge((const SYSTEMTIME *) &st2, &tmp2);
/* Difference is in 100ns units, convert to milliseconds, keeping
* fractions since Duktape 2.2.0.
*/
return (duk_double_t) ((LONGLONG) tmp1.QuadPart - (LONGLONG) tmp2.QuadPart) / 10000.0;
}
#endif /* DUK_USE_DATE_NOW_WINDOWS */
#if defined(DUK_USE_DATE_TZO_WINDOWS)
DUK_INTERNAL duk_int_t duk_bi_date_get_local_tzoffset_windows(duk_double_t d) {

3
src-input/duk_bi_protos.h

@ -31,6 +31,9 @@ DUK_INTERNAL_DECL duk_double_t duk_bi_date_get_now_time(duk_hthread *thr);
#if defined(DUK_USE_DATE_NOW_WINDOWS)
DUK_INTERNAL_DECL duk_double_t duk_bi_date_get_now_windows(duk_hthread *thr);
#endif
#if defined(DUK_USE_DATE_NOW_WINDOWS_SUBMS)
DUK_INTERNAL_DECL duk_double_t duk_bi_date_get_now_windows_subms(duk_hthread *thr);
#endif
#if defined(DUK_USE_DATE_TZO_GMTIME_R) || defined(DUK_USE_DATE_TZO_GMTIME_S) || defined(DUK_USE_DATE_TZO_GMTIME)
DUK_INTERNAL_DECL duk_int_t duk_bi_date_get_local_tzoffset_gmtime(duk_double_t d);
#endif

Loading…
Cancel
Save