1 /* Copyright JS Foundation and other contributors, http://js.foundation
2 *
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #ifdef HAVE_TM_GMTOFF
17 #include <time.h>
18 #endif /* HAVE_TM_GMTOFF */
19
20 #ifdef _WIN32
21 #include <windows.h>
22 #include <winbase.h>
23 #include <winnt.h>
24 #include <time.h>
25 #endif /* _WIN32 */
26
27 #ifdef __GNUC__
28 #include <sys/time.h>
29 #endif /* __GNUC__ */
30
31 #include "jerryscript-port.h"
32 #include "jerryscript-port-default.h"
33
34 #ifdef JERRY_FOR_IAR_CONFIG
35 #define _BSD_SOURCE
36 /* the "sys/time.h" should be put ahead of "time.h" so that the right implementation for gettimeofday can be found. */
37 #include "sys/time.h"
38 #include "time.h"
39 #include "config-gt.h"
40 #include "config-jupiter.h"
41 #endif /* JERRY_FOR_IAR_CONFIG */
42
43 #ifdef _WIN32
44 /* https://support.microsoft.com/en-us/help/167296/how-to-convert-a-unix-time-t-to-a-win32-filetime-or-systemtime */
UnixTimeToFileTime(LONGLONG t,LPFILETIME pft)45 static void UnixTimeToFileTime (LONGLONG t, LPFILETIME pft)
46 {
47 LONGLONG ll = t * 10000000 + 116444736000000000;
48 pft->dwLowDateTime = (DWORD) ll;
49 pft->dwHighDateTime = (DWORD) (ll >> 32);
50 } /* UnixTimeToFileTime */
51 #endif /* _WIN32 */
52
53 /**
54 * Default implementation of jerry_port_get_local_time_zone_adjustment. Uses the 'tm_gmtoff' field
55 * of 'struct tm' (a GNU extension) filled by 'localtime_r' if available on the
56 * system, does nothing otherwise.
57 *
58 * @return offset between UTC and local time at the given unix timestamp, if
59 * available. Otherwise, returns 0, assuming UTC time.
60 */
jerry_port_get_local_time_zone_adjustment(double unix_ms,bool is_utc)61 double jerry_port_get_local_time_zone_adjustment (double unix_ms, /**< ms since unix epoch */
62 bool is_utc) /**< is the time above in UTC? */
63 {
64 #ifdef HAVE_TM_GMTOFF
65 struct tm tm;
66 time_t now = (time_t) (unix_ms / 1000);
67 localtime_r (&now, &tm);
68 if (!is_utc)
69 {
70 now -= tm.tm_gmtoff;
71 localtime_r (&now, &tm);
72 }
73 return ((double) tm.tm_gmtoff) * 1000;
74 #else /* !HAVE_TM_GMTOFF */
75 #ifdef JERRY_IAR_JUPITER
76 struct tm tm;
77 time_t now = (time_t) (unix_ms / 1000);
78 localtime_r (&now, &tm);
79 if (!is_utc)
80 {
81 now -= tm.tm_gmtoff;
82 localtime_r (&now, &tm);
83 }
84 return ((double) tm.tm_gmtoff) * 1000;
85 #else /* !JERRY_IAR_JUPITER */
86 (void) unix_ms;
87 (void) is_utc;
88 #ifdef _WIN32
89 FILETIME fileTime, localFileTime;
90 SYSTEMTIME systemTime, localSystemTime;
91 ULARGE_INTEGER time, localTime;
92
93 _tzset ();
94 UnixTimeToFileTime ((LONGLONG) (unix_ms / 1000), &fileTime);
95
96 if (FileTimeToSystemTime (&fileTime, &systemTime)
97 && SystemTimeToTzSpecificLocalTime (0, &systemTime, &localSystemTime)
98 && SystemTimeToFileTime (&localSystemTime, &localFileTime))
99 {
100 time.LowPart = fileTime.dwLowDateTime;
101 time.HighPart = fileTime.dwHighDateTime;
102 localTime.LowPart = localFileTime.dwLowDateTime;
103 localTime.HighPart = localFileTime.dwHighDateTime;
104 return (double)(((LONGLONG) localTime.QuadPart - (LONGLONG) time.QuadPart) / 10000);
105 }
106 #endif /* _WIN32 */
107 #endif
108 return 0.0;
109 #endif /* HAVE_TM_GMTOFF */
110 } /* jerry_port_get_local_time_zone_adjustment */
111
112 /**
113 * Default implementation of jerry_port_get_current_time. Uses 'gettimeofday' if
114 * available on the system, does nothing otherwise.
115 *
116 * @return milliseconds since Unix epoch - if 'gettimeofday' is available and
117 * executed successfully,
118 * 0 - otherwise.
119 */
jerry_port_get_current_time(void)120 double jerry_port_get_current_time (void)
121 {
122 #if defined (__GNUC__) || defined (JERRY_IAR_JUPITER)
123 struct timeval tv = {0};
124
125 if (gettimeofday (&tv, NULL) == 0)
126 {
127 return ((double) tv.tv_sec) * 1000.0 + ((double) tv.tv_usec) / 1000.0;
128 }
129 #endif /* __GNUC__ || JERRY_IAR_JUPITER */
130
131 #ifdef _WIN32
132 time_t ltime;
133 time (<ime);
134 return (double) (ltime * 1000);
135 #endif /* _WIN32 */
136
137 return 0.0;
138 } /* jerry_port_get_current_time */
139