From 84d060c768c10145374d48ebd2b3f00430c73638 Mon Sep 17 00:00:00 2001 From: volzhs Date: Thu, 20 Dec 2018 06:18:52 +0900 Subject: [PATCH] Added OS.get_system_time_msecs() --- core/bind/core_bind.cpp | 5 +++++ core/bind/core_bind.h | 1 + core/os/os.cpp | 3 +++ core/os/os.h | 1 + doc/classes/OS.xml | 7 +++++++ drivers/unix/os_unix.cpp | 6 ++++++ drivers/unix/os_unix.h | 1 + platform/windows/os_windows.cpp | 11 ++++++++--- platform/windows/os_windows.h | 1 + 9 files changed, 33 insertions(+), 3 deletions(-) diff --git a/core/bind/core_bind.cpp b/core/bind/core_bind.cpp index 8641af84d97..6f4561d8e6f 100644 --- a/core/bind/core_bind.cpp +++ b/core/bind/core_bind.cpp @@ -827,6 +827,10 @@ uint64_t _OS::get_system_time_secs() const { return OS::get_singleton()->get_system_time_secs(); } +uint64_t _OS::get_system_time_msecs() const { + return OS::get_singleton()->get_system_time_msecs(); +} + void _OS::delay_usec(uint32_t p_usec) const { OS::get_singleton()->delay_usec(p_usec); @@ -1182,6 +1186,7 @@ void _OS::_bind_methods() { ClassDB::bind_method(D_METHOD("get_datetime_from_unix_time", "unix_time_val"), &_OS::get_datetime_from_unix_time); ClassDB::bind_method(D_METHOD("get_unix_time_from_datetime", "datetime"), &_OS::get_unix_time_from_datetime); ClassDB::bind_method(D_METHOD("get_system_time_secs"), &_OS::get_system_time_secs); + ClassDB::bind_method(D_METHOD("get_system_time_msecs"), &_OS::get_system_time_msecs); ClassDB::bind_method(D_METHOD("set_icon", "icon"), &_OS::set_icon); diff --git a/core/bind/core_bind.h b/core/bind/core_bind.h index 4cdf09d5228..39704286b63 100644 --- a/core/bind/core_bind.h +++ b/core/bind/core_bind.h @@ -281,6 +281,7 @@ public: Dictionary get_time_zone_info() const; uint64_t get_unix_time() const; uint64_t get_system_time_secs() const; + uint64_t get_system_time_msecs() const; int get_static_memory_usage() const; int get_static_memory_peak_usage() const; diff --git a/core/os/os.cpp b/core/os/os.cpp index 7547b6a0425..ea60a033732 100644 --- a/core/os/os.cpp +++ b/core/os/os.cpp @@ -61,6 +61,9 @@ uint64_t OS::get_unix_time() const { uint64_t OS::get_system_time_secs() const { return 0; } +uint64_t OS::get_system_time_msecs() const { + return 0; +} void OS::debug_break(){ // something diff --git a/core/os/os.h b/core/os/os.h index 05ec3ac4249..046686ff51a 100644 --- a/core/os/os.h +++ b/core/os/os.h @@ -328,6 +328,7 @@ public: virtual TimeZoneInfo get_time_zone_info() const = 0; virtual uint64_t get_unix_time() const; virtual uint64_t get_system_time_secs() const; + virtual uint64_t get_system_time_msecs() const; virtual void delay_usec(uint32_t p_usec) const = 0; virtual uint64_t get_ticks_usec() const = 0; diff --git a/doc/classes/OS.xml b/doc/classes/OS.xml index ac865de3cd0..82f5928074c 100644 --- a/doc/classes/OS.xml +++ b/doc/classes/OS.xml @@ -381,6 +381,13 @@ Returns the epoch time of the operating system in seconds. + + + + + Returns the epoch time of the operating system in milliseconds. + + diff --git a/drivers/unix/os_unix.cpp b/drivers/unix/os_unix.cpp index 279274734fe..12a12630421 100644 --- a/drivers/unix/os_unix.cpp +++ b/drivers/unix/os_unix.cpp @@ -202,6 +202,12 @@ uint64_t OS_Unix::get_system_time_secs() const { return uint64_t(tv_now.tv_sec); } +uint64_t OS_Unix::get_system_time_msecs() const { + struct timeval tv_now; + gettimeofday(&tv_now, NULL); + return uint64_t(tv_now.tv_sec * 1000 + tv_now.tv_usec / 1000); +} + OS::Date OS_Unix::get_date(bool utc) const { time_t t = time(NULL); diff --git a/drivers/unix/os_unix.h b/drivers/unix/os_unix.h index b7024546031..94a8d040cd1 100644 --- a/drivers/unix/os_unix.h +++ b/drivers/unix/os_unix.h @@ -84,6 +84,7 @@ public: virtual uint64_t get_unix_time() const; virtual uint64_t get_system_time_secs() const; + virtual uint64_t get_system_time_msecs() const; virtual void delay_usec(uint32_t p_usec) const; virtual uint64_t get_ticks_usec() const; diff --git a/platform/windows/os_windows.cpp b/platform/windows/os_windows.cpp index e1cbbcc66b9..dff87b2c5d2 100644 --- a/platform/windows/os_windows.cpp +++ b/platform/windows/os_windows.cpp @@ -2142,8 +2142,13 @@ uint64_t OS_Windows::get_unix_time() const { uint64_t OS_Windows::get_system_time_secs() const { - const uint64_t WINDOWS_TICK = 10000000; - const uint64_t SEC_TO_UNIX_EPOCH = 11644473600LL; + return get_system_time_msecs() / 1000; +} + +uint64_t OS_Windows::get_system_time_msecs() const { + + const uint64_t WINDOWS_TICK = 10000; + const uint64_t MSEC_TO_UNIX_EPOCH = 11644473600000LL; SYSTEMTIME st; GetSystemTime(&st); @@ -2154,7 +2159,7 @@ uint64_t OS_Windows::get_system_time_secs() const { ret <<= 32; ret |= ft.dwLowDateTime; - return (uint64_t)(ret / WINDOWS_TICK - SEC_TO_UNIX_EPOCH); + return (uint64_t)(ret / WINDOWS_TICK - MSEC_TO_UNIX_EPOCH); } void OS_Windows::delay_usec(uint32_t p_usec) const { diff --git a/platform/windows/os_windows.h b/platform/windows/os_windows.h index d09ade4daab..e12e465dd39 100644 --- a/platform/windows/os_windows.h +++ b/platform/windows/os_windows.h @@ -254,6 +254,7 @@ public: virtual TimeZoneInfo get_time_zone_info() const; virtual uint64_t get_unix_time() const; virtual uint64_t get_system_time_secs() const; + virtual uint64_t get_system_time_msecs() const; virtual bool can_draw() const; virtual Error set_cwd(const String &p_cwd);