Skip to content

Commit

Permalink
Fix Time.get_unix_time_from_system() not including msecs
Browse files Browse the repository at this point in the history
  • Loading branch information
timothyqiu committed Aug 8, 2022
1 parent 5b4e7a0 commit 1be078e
Show file tree
Hide file tree
Showing 10 changed files with 49 additions and 1 deletion.
3 changes: 3 additions & 0 deletions core/os/os.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ uint64_t OS::get_system_time_secs() const {
uint64_t OS::get_system_time_msecs() const {
return 0;
}
double OS::get_subsecond_unix_time() const {
return 0.0;
}
void OS::debug_break(){

// something
Expand Down
1 change: 1 addition & 0 deletions core/os/os.h
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,7 @@ class OS {
virtual uint64_t get_unix_time() const;
virtual uint64_t get_system_time_secs() const;
virtual uint64_t get_system_time_msecs() const;
virtual double get_subsecond_unix_time() const; // For use in Time::get_unix_time().

virtual void delay_usec(uint32_t p_usec) const = 0;
virtual void add_frame_delay(bool p_can_draw);
Expand Down
2 changes: 1 addition & 1 deletion core/os/time.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@ Dictionary Time::get_time_zone_from_system() const {
}

double Time::get_unix_time_from_system() const {
return OS::get_singleton()->get_unix_time();
return OS::get_singleton()->get_subsecond_unix_time();
}

uint64_t Time::get_ticks_msec() const {
Expand Down
1 change: 1 addition & 0 deletions doc/classes/OS.xml
Original file line number Diff line number Diff line change
Expand Up @@ -552,6 +552,7 @@
<description>
Returns the current UNIX epoch timestamp in seconds.
[b]Important:[/b] This is the system clock that the user can manually set. [b]Never use[/b] this method for precise time calculation since its results are also subject to automatic adjustments by the operating system. [b]Always use[/b] [method get_ticks_usec] or [method get_ticks_msec] for precise time calculation instead, since they are guaranteed to be monotonic (i.e. never decrease).
[b]Note:[/b] To get a floating point timestamp with sub-second precision, use [method Time.get_unix_time_from_system].
</description>
</method>
<method name="get_unix_time_from_datetime" qualifiers="const">
Expand Down
6 changes: 6 additions & 0 deletions drivers/unix/os_unix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,12 @@ uint64_t OS_Unix::get_system_time_msecs() const {
return uint64_t(tv_now.tv_sec) * 1000 + uint64_t(tv_now.tv_usec) / 1000;
}

double OS_Unix::get_subsecond_unix_time() const {
struct timeval tv_now;
gettimeofday(&tv_now, nullptr);
return (double)tv_now.tv_sec + double(tv_now.tv_usec) / 1000000;
}

OS::Date OS_Unix::get_date(bool utc) const {
time_t t = time(nullptr);
struct tm lt;
Expand Down
1 change: 1 addition & 0 deletions drivers/unix/os_unix.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ class OS_Unix : public OS {
virtual uint64_t get_unix_time() const;
virtual uint64_t get_system_time_secs() const;
virtual uint64_t get_system_time_msecs() const;
virtual double get_subsecond_unix_time() const;

virtual void delay_usec(uint32_t p_usec) const;
virtual uint64_t get_ticks_usec() const;
Expand Down
17 changes: 17 additions & 0 deletions platform/uwp/os_uwp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,23 @@ uint64_t OS_UWP::get_unix_time() const {
return (*(uint64_t *)&ft - *(uint64_t *)&fep) / 10000000;
};

double OS_UWP::get_subsecond_unix_time() const {
// 1 Windows tick is 100ns
const uint64_t WINDOWS_TICKS_PER_SECOND = 10000000;
const uint64_t TICKS_TO_UNIX_EPOCH = 116444736000000000LL;

SYSTEMTIME st;
GetSystemTime(&st);
FILETIME ft;
SystemTimeToFileTime(&st, &ft);
uint64_t ticks_time;
ticks_time = ft.dwHighDateTime;
ticks_time <<= 32;
ticks_time |= ft.dwLowDateTime;

return (double)(ticks_time - TICKS_TO_UNIX_EPOCH) / WINDOWS_TICKS_PER_SECOND;
}

void OS_UWP::delay_usec(uint32_t p_usec) const {
int msec = p_usec < 1000 ? 1 : p_usec / 1000;

Expand Down
1 change: 1 addition & 0 deletions platform/uwp/os_uwp.h
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ class OS_UWP : public OS {
virtual Time get_time(bool utc) const;
virtual TimeZoneInfo get_time_zone_info() const;
virtual uint64_t get_unix_time() const;
virtual double get_subsecond_unix_time() const;

virtual bool can_draw() const;
virtual Error set_cwd(const String &p_cwd);
Expand Down
17 changes: 17 additions & 0 deletions platform/windows/os_windows.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2579,6 +2579,23 @@ uint64_t OS_Windows::get_system_time_msecs() const {
return (uint64_t)(ret / WINDOWS_TICK - MSEC_TO_UNIX_EPOCH);
}

double OS_Windows::get_subsecond_unix_time() const {
// 1 Windows tick is 100ns
const uint64_t WINDOWS_TICKS_PER_SECOND = 10000000;
const uint64_t TICKS_TO_UNIX_EPOCH = 116444736000000000LL;

SYSTEMTIME st;
GetSystemTime(&st);
FILETIME ft;
SystemTimeToFileTime(&st, &ft);
uint64_t ticks_time;
ticks_time = ft.dwHighDateTime;
ticks_time <<= 32;
ticks_time |= ft.dwLowDateTime;

return (double)(ticks_time - TICKS_TO_UNIX_EPOCH) / WINDOWS_TICKS_PER_SECOND;
}

void OS_Windows::delay_usec(uint32_t p_usec) const {
if (p_usec < 1000)
Sleep(1);
Expand Down
1 change: 1 addition & 0 deletions platform/windows/os_windows.h
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,7 @@ class OS_Windows : public OS {
virtual uint64_t get_unix_time() const;
virtual uint64_t get_system_time_secs() const;
virtual uint64_t get_system_time_msecs() const;
virtual double get_subsecond_unix_time() const;

virtual bool can_draw() const;
virtual Error set_cwd(const String &p_cwd);
Expand Down

0 comments on commit 1be078e

Please sign in to comment.