1425bb815Sopenharmony_ci/* Copyright JS Foundation and other contributors, http://js.foundation 2425bb815Sopenharmony_ci * 3425bb815Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4425bb815Sopenharmony_ci * you may not use this file except in compliance with the License. 5425bb815Sopenharmony_ci * You may obtain a copy of the License at 6425bb815Sopenharmony_ci * 7425bb815Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8425bb815Sopenharmony_ci * 9425bb815Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10425bb815Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS 11425bb815Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12425bb815Sopenharmony_ci * See the License for the specific language governing permissions and 13425bb815Sopenharmony_ci * limitations under the License. 14425bb815Sopenharmony_ci */ 15425bb815Sopenharmony_ci 16425bb815Sopenharmony_ci#ifdef HAVE_TM_GMTOFF 17425bb815Sopenharmony_ci#include <time.h> 18425bb815Sopenharmony_ci#endif /* HAVE_TM_GMTOFF */ 19425bb815Sopenharmony_ci 20425bb815Sopenharmony_ci#ifdef _WIN32 21425bb815Sopenharmony_ci#include <windows.h> 22425bb815Sopenharmony_ci#include <winbase.h> 23425bb815Sopenharmony_ci#include <winnt.h> 24425bb815Sopenharmony_ci#include <time.h> 25425bb815Sopenharmony_ci#endif /* _WIN32 */ 26425bb815Sopenharmony_ci 27425bb815Sopenharmony_ci#ifdef __GNUC__ 28425bb815Sopenharmony_ci#include <sys/time.h> 29425bb815Sopenharmony_ci#endif /* __GNUC__ */ 30425bb815Sopenharmony_ci 31425bb815Sopenharmony_ci#include "jerryscript-port.h" 32425bb815Sopenharmony_ci#include "jerryscript-port-default.h" 33425bb815Sopenharmony_ci 34425bb815Sopenharmony_ci#ifdef JERRY_FOR_IAR_CONFIG 35425bb815Sopenharmony_ci#define _BSD_SOURCE 36425bb815Sopenharmony_ci/* the "sys/time.h" should be put ahead of "time.h" so that the right implementation for gettimeofday can be found. */ 37425bb815Sopenharmony_ci#include "sys/time.h" 38425bb815Sopenharmony_ci#include "time.h" 39425bb815Sopenharmony_ci#include "config-gt.h" 40425bb815Sopenharmony_ci#include "config-jupiter.h" 41425bb815Sopenharmony_ci#endif /* JERRY_FOR_IAR_CONFIG */ 42425bb815Sopenharmony_ci 43425bb815Sopenharmony_ci#ifdef _WIN32 44425bb815Sopenharmony_ci/* https://support.microsoft.com/en-us/help/167296/how-to-convert-a-unix-time-t-to-a-win32-filetime-or-systemtime */ 45425bb815Sopenharmony_cistatic void UnixTimeToFileTime (LONGLONG t, LPFILETIME pft) 46425bb815Sopenharmony_ci{ 47425bb815Sopenharmony_ci LONGLONG ll = t * 10000000 + 116444736000000000; 48425bb815Sopenharmony_ci pft->dwLowDateTime = (DWORD) ll; 49425bb815Sopenharmony_ci pft->dwHighDateTime = (DWORD) (ll >> 32); 50425bb815Sopenharmony_ci} /* UnixTimeToFileTime */ 51425bb815Sopenharmony_ci#endif /* _WIN32 */ 52425bb815Sopenharmony_ci 53425bb815Sopenharmony_ci/** 54425bb815Sopenharmony_ci * Default implementation of jerry_port_get_local_time_zone_adjustment. Uses the 'tm_gmtoff' field 55425bb815Sopenharmony_ci * of 'struct tm' (a GNU extension) filled by 'localtime_r' if available on the 56425bb815Sopenharmony_ci * system, does nothing otherwise. 57425bb815Sopenharmony_ci * 58425bb815Sopenharmony_ci * @return offset between UTC and local time at the given unix timestamp, if 59425bb815Sopenharmony_ci * available. Otherwise, returns 0, assuming UTC time. 60425bb815Sopenharmony_ci */ 61425bb815Sopenharmony_cidouble jerry_port_get_local_time_zone_adjustment (double unix_ms, /**< ms since unix epoch */ 62425bb815Sopenharmony_ci bool is_utc) /**< is the time above in UTC? */ 63425bb815Sopenharmony_ci{ 64425bb815Sopenharmony_ci#ifdef HAVE_TM_GMTOFF 65425bb815Sopenharmony_ci struct tm tm; 66425bb815Sopenharmony_ci time_t now = (time_t) (unix_ms / 1000); 67425bb815Sopenharmony_ci localtime_r (&now, &tm); 68425bb815Sopenharmony_ci if (!is_utc) 69425bb815Sopenharmony_ci { 70425bb815Sopenharmony_ci now -= tm.tm_gmtoff; 71425bb815Sopenharmony_ci localtime_r (&now, &tm); 72425bb815Sopenharmony_ci } 73425bb815Sopenharmony_ci return ((double) tm.tm_gmtoff) * 1000; 74425bb815Sopenharmony_ci#else /* !HAVE_TM_GMTOFF */ 75425bb815Sopenharmony_ci#ifdef JERRY_IAR_JUPITER 76425bb815Sopenharmony_ci struct tm tm; 77425bb815Sopenharmony_ci time_t now = (time_t) (unix_ms / 1000); 78425bb815Sopenharmony_ci localtime_r (&now, &tm); 79425bb815Sopenharmony_ci if (!is_utc) 80425bb815Sopenharmony_ci { 81425bb815Sopenharmony_ci now -= tm.tm_gmtoff; 82425bb815Sopenharmony_ci localtime_r (&now, &tm); 83425bb815Sopenharmony_ci } 84425bb815Sopenharmony_ci return ((double) tm.tm_gmtoff) * 1000; 85425bb815Sopenharmony_ci#else /* !JERRY_IAR_JUPITER */ 86425bb815Sopenharmony_ci (void) unix_ms; 87425bb815Sopenharmony_ci (void) is_utc; 88425bb815Sopenharmony_ci#ifdef _WIN32 89425bb815Sopenharmony_ci FILETIME fileTime, localFileTime; 90425bb815Sopenharmony_ci SYSTEMTIME systemTime, localSystemTime; 91425bb815Sopenharmony_ci ULARGE_INTEGER time, localTime; 92425bb815Sopenharmony_ci 93425bb815Sopenharmony_ci _tzset (); 94425bb815Sopenharmony_ci UnixTimeToFileTime ((LONGLONG) (unix_ms / 1000), &fileTime); 95425bb815Sopenharmony_ci 96425bb815Sopenharmony_ci if (FileTimeToSystemTime (&fileTime, &systemTime) 97425bb815Sopenharmony_ci && SystemTimeToTzSpecificLocalTime (0, &systemTime, &localSystemTime) 98425bb815Sopenharmony_ci && SystemTimeToFileTime (&localSystemTime, &localFileTime)) 99425bb815Sopenharmony_ci { 100425bb815Sopenharmony_ci time.LowPart = fileTime.dwLowDateTime; 101425bb815Sopenharmony_ci time.HighPart = fileTime.dwHighDateTime; 102425bb815Sopenharmony_ci localTime.LowPart = localFileTime.dwLowDateTime; 103425bb815Sopenharmony_ci localTime.HighPart = localFileTime.dwHighDateTime; 104425bb815Sopenharmony_ci return (double)(((LONGLONG) localTime.QuadPart - (LONGLONG) time.QuadPart) / 10000); 105425bb815Sopenharmony_ci } 106425bb815Sopenharmony_ci#endif /* _WIN32 */ 107425bb815Sopenharmony_ci#endif 108425bb815Sopenharmony_ci return 0.0; 109425bb815Sopenharmony_ci#endif /* HAVE_TM_GMTOFF */ 110425bb815Sopenharmony_ci} /* jerry_port_get_local_time_zone_adjustment */ 111425bb815Sopenharmony_ci 112425bb815Sopenharmony_ci/** 113425bb815Sopenharmony_ci * Default implementation of jerry_port_get_current_time. Uses 'gettimeofday' if 114425bb815Sopenharmony_ci * available on the system, does nothing otherwise. 115425bb815Sopenharmony_ci * 116425bb815Sopenharmony_ci * @return milliseconds since Unix epoch - if 'gettimeofday' is available and 117425bb815Sopenharmony_ci * executed successfully, 118425bb815Sopenharmony_ci * 0 - otherwise. 119425bb815Sopenharmony_ci */ 120425bb815Sopenharmony_cidouble jerry_port_get_current_time (void) 121425bb815Sopenharmony_ci{ 122425bb815Sopenharmony_ci#if defined (__GNUC__) || defined (JERRY_IAR_JUPITER) 123425bb815Sopenharmony_ci struct timeval tv = {0}; 124425bb815Sopenharmony_ci 125425bb815Sopenharmony_ci if (gettimeofday (&tv, NULL) == 0) 126425bb815Sopenharmony_ci { 127425bb815Sopenharmony_ci return ((double) tv.tv_sec) * 1000.0 + ((double) tv.tv_usec) / 1000.0; 128425bb815Sopenharmony_ci } 129425bb815Sopenharmony_ci#endif /* __GNUC__ || JERRY_IAR_JUPITER */ 130425bb815Sopenharmony_ci 131425bb815Sopenharmony_ci#ifdef _WIN32 132425bb815Sopenharmony_ci time_t ltime; 133425bb815Sopenharmony_ci time (<ime); 134425bb815Sopenharmony_ci return (double) (ltime * 1000); 135425bb815Sopenharmony_ci#endif /* _WIN32 */ 136425bb815Sopenharmony_ci 137425bb815Sopenharmony_ci return 0.0; 138425bb815Sopenharmony_ci} /* jerry_port_get_current_time */ 139