23
23
#include <winbase.h>
24
24
#include <winnt.h>
25
25
26
- #define UNIX_EPOCH_IN_TICKS 116444736000000000ull /* difference between 1970 and 1601 */
27
- #define TICKS_PER_MS 10000ull /* 1 tick is 100 nanoseconds */
26
+ #define UNIX_EPOCH_IN_TICKS 116444736000000000LL /* difference between 1970 and 1601 */
27
+ #define TICKS_PER_MS 10000LL /* 1 tick is 100 nanoseconds */
28
28
29
29
/*
30
30
* If you take the limit of SYSTEMTIME (last millisecond in 30827) then you end up with
44
44
* https://support.microsoft.com/en-us/help/167296/how-to-convert-a-unix-time-t-to-a-win32-filetime-or-systemtime
45
45
*/
46
46
static void
47
- unix_time_to_filetime (double t , LPFILETIME ft_p )
47
+ unix_time_to_filetime (LONGLONG t , LPFILETIME ft_p )
48
48
{
49
- LONGLONG ll = (LONGLONG ) t * TICKS_PER_MS + UNIX_EPOCH_IN_TICKS ;
50
-
51
- /* FILETIME values before the epoch are invalid. */
52
- if (ll < 0 )
53
- {
54
- ll = 0 ;
55
- }
49
+ LONGLONG ll = t * TICKS_PER_MS + UNIX_EPOCH_IN_TICKS ;
56
50
57
51
ft_p -> dwLowDateTime = (DWORD ) ll ;
58
52
ft_p -> dwHighDateTime = (DWORD ) (ll >> 32 );
@@ -63,13 +57,15 @@ unix_time_to_filetime (double t, LPFILETIME ft_p)
63
57
*
64
58
* @return unix time
65
59
*/
66
- static double
60
+ static LONGLONG
67
61
filetime_to_unix_time (LPFILETIME ft_p )
68
62
{
69
63
ULARGE_INTEGER date ;
64
+ LONGLONG ll ;
70
65
date .HighPart = ft_p -> dwHighDateTime ;
71
66
date .LowPart = ft_p -> dwLowDateTime ;
72
- return (double ) (((LONGLONG ) date .QuadPart - UNIX_EPOCH_IN_TICKS ) / TICKS_PER_MS );
67
+ ll = date .QuadPart - UNIX_EPOCH_IN_TICKS ;
68
+ return ll / TICKS_PER_MS ;
73
69
} /* filetime_to_unix_time */
74
70
75
71
/**
@@ -85,6 +81,7 @@ jerry_port_local_tza (double unix_ms)
85
81
FILETIME local ;
86
82
SYSTEMTIME utc_sys ;
87
83
SYSTEMTIME local_sys ;
84
+ LONGLONG t = (LONGLONG ) (unix_ms );
88
85
89
86
/*
90
87
* If the time is earlier than the date 1601-01-02, then always using date 1601-01-02 to
@@ -93,23 +90,23 @@ jerry_port_local_tza (double unix_ms)
93
90
* after converting between local time and utc time, the time may be earlier than 1601-01-01
94
91
* in UTC time, that exceeds the FILETIME representation range.
95
92
*/
96
- if (unix_ms < ( double ) UNIX_EPOCH_DATE_1601_01_02 )
93
+ if (t < UNIX_EPOCH_DATE_1601_01_02 )
97
94
{
98
- unix_ms = ( double ) UNIX_EPOCH_DATE_1601_01_02 ;
95
+ t = UNIX_EPOCH_DATE_1601_01_02 ;
99
96
}
100
97
101
98
/* Like above, do not use the last supported day */
102
- if (unix_ms > ( double ) UNIX_EPOCH_DATE_30827_12_29 )
99
+ if (t > UNIX_EPOCH_DATE_30827_12_29 )
103
100
{
104
- unix_ms = ( double ) UNIX_EPOCH_DATE_30827_12_29 ;
101
+ t = UNIX_EPOCH_DATE_30827_12_29 ;
105
102
}
106
- unix_time_to_filetime (unix_ms , & utc );
103
+ unix_time_to_filetime (t , & utc );
107
104
108
105
if (FileTimeToSystemTime (& utc , & utc_sys ) && SystemTimeToTzSpecificLocalTime (NULL , & utc_sys , & local_sys )
109
106
&& SystemTimeToFileTime (& local_sys , & local ))
110
107
{
111
- double unix_local = filetime_to_unix_time (& local );
112
- return (int32_t ) (unix_local - unix_ms );
108
+ LONGLONG unix_local = filetime_to_unix_time (& local );
109
+ return (int32_t ) (unix_local - t );
113
110
}
114
111
115
112
return 0 ;
@@ -125,7 +122,7 @@ jerry_port_current_time (void)
125
122
{
126
123
FILETIME ft ;
127
124
GetSystemTimeAsFileTime (& ft );
128
- return filetime_to_unix_time (& ft );
125
+ return ( double ) filetime_to_unix_time (& ft );
129
126
} /* jerry_port_current_time */
130
127
131
128
#endif /* defined(_WIN32) */
0 commit comments