0 votes

Hi all,

I might be missing something, but I noticed that commit 'c6de387' removed:

virtual uint64_t get_system_time_msecs() const;

From the OS class without providing alternatives.

The time related functions were later moved to the new Time class but still without any replacement.

Is there still a way to get the current system (or UTC) timestamp in milliseconds in GDScript? and if not why?

Godot version 4.0 Alpha 4
in Engine by (21 points)

2 Answers

+1 vote
Best answer

To answer my own question with the response I received on the Godot Chat.

It was removed in favor of OS.get_unix_time(), which now returns float, so you can get sub-second values from the fractional part, see godot#39189.

Concrete example:

var unix_time: float = Time.get_unix_time_from_system()
var unix_time_int: int = unix_time
var dt: Dictionary = Time.get_datetime_dict_from_unix_time(unix_time)
var ms: int = (unix_time - unix_time_int) * 1000.0
var str := "%s.%s.%s %s:%s:%s:%s" % [dt.year, dt.month, dt.day, dt.hour, dt.minute, dt.second, ms]

Shows 2022.3.15 11:42:11:621.

by (21 points)
edited by

That is nice to know but I still feel like they should have just kept them as shorthand since it was nice to have.

Agreed, but at least it's still possible without having to write a native script.

0 votes

It seems to be there

Another way is:

var sys_time = get_unix_time_from_datetime_string(get_time_string_from_system())
by (98 points)
edited by

Thanks for the answer!

int get_ticks_msec ( ) const

Does not seem to do what I want, as it returns the amount of time passed in milliseconds since the engine started, and not the system time.

I will have to try:

var sys_time = get_unix_time_from_datetime_string(get_time_string_from_system())

But since the documentation for get_time_string_from_system() says it returns the time in a HH:MM:SS, I guess the ms will be omitted.

Yeah. I noticed I read you question wrong and it seems like the best you can get is

systemtimeinseconds - gamestarttimein_msecs to get close to the answer.

The only other way is to use the timer or make a proposal to get that feature added back into godot 4.0 since it still in alpha so it might not be to late to make a suggestion.

I will make a proposal, thanks again. :)

Your welcome and let me know if there is anything I can do to help as one of the projects I am working on also requires get msecs to work properly at this point and I want to move to 4.0 once it is out of alpha.

See above the answer I received on the Godot chat, hope this helps!

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.