Class TimeInstance
- Namespace
- Godot
- Assembly
- GodotSharp.dll
The Time singleton allows converting time between various formats and also getting time information from the system.
This class conforms with as many of the ISO 8601 standards as possible. All dates follow the Proleptic Gregorian calendar. As such, the day before 1582-10-15
is 1582-10-14
, not 1582-10-04
. The year before 1 AD (aka 1 BC) is number 0
, with the year before that (2 BC) being -1
, etc.
Conversion methods assume "the same timezone", and do not handle timezone conversions or DST automatically. Leap seconds are also not handled, they must be done manually if desired. Suffixes such as "Z" are not handled, you need to strip them away manually.
When getting time information from the system, the time can either be in the local timezone or UTC depending on the utc
parameter. However, the GetUnixTimeFromSystem() method always uses UTC as it returns the seconds passed since the Unix epoch.
Important: The _from_system
methods use the system clock that the user can manually set. Never use this method for precise time calculation since its results are subject to automatic adjustments by the user or the operating system. Always useGetTicksUsec() or GetTicksMsec() for precise time calculation instead, since they are guaranteed to be monotonic (i.e. never decrease).
[GodotClassName("Time")]
public class TimeInstance : GodotObject, IDisposable
- Inheritance
-
TimeInstance
- Implements
- Inherited Members
Methods
GetDateDictFromSystem(bool)
Returns the current date as a dictionary of keys: year
, month
, day
, and weekday
.
The returned values are in the system's local time when utc
is false
, otherwise they are in UTC.
public Dictionary GetDateDictFromSystem(bool utc = false)
Parameters
utc
bool
Returns
GetDateDictFromUnixTime(long)
Converts the given Unix timestamp to a dictionary of keys: year
, month
, day
, and weekday
.
public Dictionary GetDateDictFromUnixTime(long unixTimeVal)
Parameters
unixTimeVal
long
Returns
GetDateStringFromSystem(bool)
Returns the current date as an ISO 8601 date string (YYYY-MM-DD).
The returned values are in the system's local time when utc
is false
, otherwise they are in UTC.
public string GetDateStringFromSystem(bool utc = false)
Parameters
utc
bool
Returns
GetDateStringFromUnixTime(long)
Converts the given Unix timestamp to an ISO 8601 date string (YYYY-MM-DD).
public string GetDateStringFromUnixTime(long unixTimeVal)
Parameters
unixTimeVal
long
Returns
GetDatetimeDictFromDatetimeString(string, bool)
Converts the given ISO 8601 date and time string (YYYY-MM-DDTHH:MM:SS) to a dictionary of keys: year
, month
, day
, weekday
, hour
, minute
, and second
.
If weekday
is false
, then the weekday
entry is excluded (the calculation is relatively expensive).
Note: Any decimal fraction in the time string will be ignored silently.
public Dictionary GetDatetimeDictFromDatetimeString(string datetime, bool weekday)
Parameters
Returns
GetDatetimeDictFromSystem(bool)
Returns the current date as a dictionary of keys: year
, month
, day
, weekday
, hour
, minute
, second
, and dst
(Daylight Savings Time).
public Dictionary GetDatetimeDictFromSystem(bool utc = false)
Parameters
utc
bool
Returns
GetDatetimeDictFromUnixTime(long)
Converts the given Unix timestamp to a dictionary of keys: year
, month
, day
, weekday
, hour
, minute
, and second
.
The returned Dictionary's values will be the same as the GetDatetimeDictFromSystem(bool) if the Unix timestamp is the current time, with the exception of Daylight Savings Time as it cannot be determined from the epoch.
public Dictionary GetDatetimeDictFromUnixTime(long unixTimeVal)
Parameters
unixTimeVal
long
Returns
GetDatetimeStringFromDatetimeDict(Dictionary, bool)
Converts the given dictionary of keys to an ISO 8601 date and time string (YYYY-MM-DDTHH:MM:SS).
The given dictionary can be populated with the following keys: year
, month
, day
, hour
, minute
, and second
. Any other entries (including dst
) are ignored.
If the dictionary is empty, 0
is returned. If some keys are omitted, they default to the equivalent values for the Unix epoch timestamp 0 (1970-01-01 at 00:00:00).
If useSpace
is true
, the date and time bits are separated by an empty space character instead of the letter T.
public string GetDatetimeStringFromDatetimeDict(Dictionary datetime, bool useSpace)
Parameters
datetime
DictionaryuseSpace
bool
Returns
GetDatetimeStringFromSystem(bool, bool)
Returns the current date and time as an ISO 8601 date and time string (YYYY-MM-DDTHH:MM:SS).
The returned values are in the system's local time when utc
is false
, otherwise they are in UTC.
If useSpace
is true
, the date and time bits are separated by an empty space character instead of the letter T.
public string GetDatetimeStringFromSystem(bool utc = false, bool useSpace = false)
Parameters
Returns
GetDatetimeStringFromUnixTime(long, bool)
Converts the given Unix timestamp to an ISO 8601 date and time string (YYYY-MM-DDTHH:MM:SS).
If useSpace
is true
, the date and time bits are separated by an empty space character instead of the letter T.
public string GetDatetimeStringFromUnixTime(long unixTimeVal, bool useSpace = false)
Parameters
Returns
GetOffsetStringFromOffsetMinutes(long)
Converts the given timezone offset in minutes to a timezone offset string. For example, -480 returns "-08:00", 345 returns "+05:45", and 0 returns "+00:00".
public string GetOffsetStringFromOffsetMinutes(long offsetMinutes)
Parameters
offsetMinutes
long
Returns
GetTicksMsec()
Returns the amount of time passed in milliseconds since the engine started.
Will always be positive or 0 and uses a 64-bit value (it will wrap after roughly 500 million years).
public ulong GetTicksMsec()
Returns
GetTicksUsec()
Returns the amount of time passed in microseconds since the engine started.
Will always be positive or 0 and uses a 64-bit value (it will wrap after roughly half a million years).
public ulong GetTicksUsec()
Returns
GetTimeDictFromSystem(bool)
Returns the current time as a dictionary of keys: hour
, minute
, and second
.
The returned values are in the system's local time when utc
is false
, otherwise they are in UTC.
public Dictionary GetTimeDictFromSystem(bool utc = false)
Parameters
utc
bool
Returns
GetTimeDictFromUnixTime(long)
Converts the given time to a dictionary of keys: hour
, minute
, and second
.
public Dictionary GetTimeDictFromUnixTime(long unixTimeVal)
Parameters
unixTimeVal
long
Returns
GetTimeStringFromSystem(bool)
Returns the current time as an ISO 8601 time string (HH:MM:SS).
The returned values are in the system's local time when utc
is false
, otherwise they are in UTC.
public string GetTimeStringFromSystem(bool utc = false)
Parameters
utc
bool
Returns
GetTimeStringFromUnixTime(long)
Converts the given Unix timestamp to an ISO 8601 time string (HH:MM:SS).
public string GetTimeStringFromUnixTime(long unixTimeVal)
Parameters
unixTimeVal
long
Returns
GetTimeZoneFromSystem()
Returns the current time zone as a dictionary of keys: bias
and name
.
- bias
is the offset from UTC in minutes, since not all time zones are multiples of an hour from UTC.
- name
is the localized name of the time zone, according to the OS locale settings of the current user.
public Dictionary GetTimeZoneFromSystem()
Returns
GetUnixTimeFromDatetimeDict(Dictionary)
Converts a dictionary of time values to a Unix timestamp.
The given dictionary can be populated with the following keys: year
, month
, day
, hour
, minute
, and second
. Any other entries (including dst
) are ignored.
If the dictionary is empty, 0
is returned. If some keys are omitted, they default to the equivalent values for the Unix epoch timestamp 0 (1970-01-01 at 00:00:00).
You can pass the output from GetDatetimeDictFromUnixTime(long) directly into this function and get the same as what was put in.
Note: Unix timestamps are often in UTC. This method does not do any timezone conversion, so the timestamp will be in the same timezone as the given datetime dictionary.
public long GetUnixTimeFromDatetimeDict(Dictionary datetime)
Parameters
datetime
Dictionary
Returns
GetUnixTimeFromDatetimeString(string)
Converts the given ISO 8601 date and/or time string to a Unix timestamp. The string can contain a date only, a time only, or both.
Note: Unix timestamps are often in UTC. This method does not do any timezone conversion, so the timestamp will be in the same timezone as the given datetime string.
Note: Any decimal fraction in the time string will be ignored silently.
public long GetUnixTimeFromDatetimeString(string datetime)
Parameters
datetime
string
Returns
GetUnixTimeFromSystem()
Returns the current Unix timestamp in seconds based on the system time in UTC. This method is implemented by the operating system and always returns the time in UTC. The Unix timestamp is the number of seconds passed since 1970-01-01 at 00:00:00, the Unix epoch.
Note: Unlike other methods that use integer timestamps, this method returns the timestamp as a float for sub-second precision.
public double GetUnixTimeFromSystem()
Returns
HasGodotClassMethod(in godot_string_name)
Check if the type contains a method with the given name. This method is used by Godot to check if a method exists before invoking it. Do not call or override this method.
protected override bool HasGodotClassMethod(in godot_string_name method)
Parameters
method
godot_string_nameName of the method to check for.
Returns
HasGodotClassSignal(in godot_string_name)
Check if the type contains a signal with the given name. This method is used by Godot to check if a signal exists before raising it. Do not call or override this method.
protected override bool HasGodotClassSignal(in godot_string_name signal)
Parameters
signal
godot_string_nameName of the signal to check for.
Returns
InvokeGodotClassMethod(in godot_string_name, NativeVariantPtrArgs, out godot_variant)
Invokes the method with the given name, using the given arguments. This method is used by Godot to invoke methods from the engine side. Do not call or override this method.
protected override bool InvokeGodotClassMethod(in godot_string_name method, NativeVariantPtrArgs args, out godot_variant ret)
Parameters
method
godot_string_nameName of the method to invoke.
args
NativeVariantPtrArgsArguments to use with the invoked method.
ret
godot_variantValue returned by the invoked method.