Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update and fix Time documentation #54340

Merged
merged 1 commit into from
Oct 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions core/os/os.h
Original file line number Diff line number Diff line change
Expand Up @@ -204,8 +204,8 @@ class OS {
String name;
};

virtual Date get_date(bool local = false) const = 0;
virtual Time get_time(bool local = false) const = 0;
virtual Date get_date(bool p_utc = false) const = 0;
virtual Time get_time(bool p_utc = false) const = 0;
virtual TimeZoneInfo get_time_zone_info() const = 0;
virtual double get_unix_time() const;

Expand Down
13 changes: 7 additions & 6 deletions doc/classes/Time.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
<description>
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 [code]1582-10-15[/code] is [code]1582-10-14[/code], not [code]1582-10-04[/code]. The year before 1 AD (aka 1 BC) is number [code]0[/code], with the year before that (2 BC) being [code]-1[/code], etc.
Conversion methods assume "the same timezone", and do not handle timezone conversions or DST automatically. Unix epoch assumes UTC. 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.
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 [code]utc[/code] parameter. However, the [method get_unix_time_from_system] method always returns the time in UTC.
[b]Important:[/b] The [code]_from_system[/code] methods use the system clock that the user can manually set. [b]Never use[/b] this method for precise time calculation since its results are subject to automatic adjustments by the user or 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).
</description>
<tutorials>
Expand Down Expand Up @@ -82,7 +83,7 @@
<argument index="0" name="utc" type="bool" default="false" />
<argument index="1" name="use_space" type="bool" default="false" />
<description>
Returns the current date and time as a dictionary of keys: [code]year[/code], [code]month[/code], [code]day[/code], [code]weekday[/code], [code]dst[/code] (Daylight Savings Time), [code]hour[/code], [code]minute[/code], and [code]second[/code].
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 [code]utc[/code] is false, otherwise they are in UTC.
If [code]use_space[/code] is true, use a space instead of the letter T in the middle.
</description>
Expand Down Expand Up @@ -152,23 +153,23 @@
<description>
Converts a dictionary of time values to a Unix timestamp.
The given dictionary can be populated with the following keys: [code]year[/code], [code]month[/code], [code]day[/code], [code]hour[/code], [code]minute[/code], and [code]second[/code]. Any other entries (including [code]dst[/code]) are ignored.
If the dictionary is empty, [code]0[/code] 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 UTC).
If the dictionary is empty, [code]0[/code] 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 [method get_datetime_dict_from_unix_time] directly into this function and get the same as what was put in.
[b]Note:[/b] Unix timestamps are usually in UTC, the given datetime dict may not be.
[b]Note:[/b] 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.
</description>
</method>
<method name="get_unix_time_from_datetime_string" qualifiers="const">
<return type="int" />
<argument index="0" name="datetime" type="String" />
<description>
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.
[b]Note:[/b] Unix timestamps are usually in UTC, the given datetime string may not be.
[b]Note:[/b] 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.
</description>
</method>
<method name="get_unix_time_from_system" qualifiers="const">
<return type="float" />
<description>
Returns the current Unix timestamp in seconds based on the system time in UTC.
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.
</description>
</method>
</methods>
Expand Down
8 changes: 4 additions & 4 deletions drivers/unix/os_unix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -160,10 +160,10 @@ double OS_Unix::get_unix_time() const {
return (double)tv_now.tv_sec + double(tv_now.tv_usec) / 1000000;
};

OS::Date OS_Unix::get_date(bool utc) const {
OS::Date OS_Unix::get_date(bool p_utc) const {
time_t t = time(nullptr);
struct tm lt;
if (utc) {
if (p_utc) {
gmtime_r(&t, &lt);
} else {
localtime_r(&t, &lt);
Expand All @@ -181,10 +181,10 @@ OS::Date OS_Unix::get_date(bool utc) const {
return ret;
}

OS::Time OS_Unix::get_time(bool utc) const {
OS::Time OS_Unix::get_time(bool p_utc) const {
time_t t = time(nullptr);
struct tm lt;
if (utc) {
if (p_utc) {
gmtime_r(&t, &lt);
} else {
localtime_r(&t, &lt);
Expand Down
4 changes: 2 additions & 2 deletions drivers/unix/os_unix.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ class OS_Unix : public OS {

virtual String get_name() const override;

virtual Date get_date(bool utc) const override;
virtual Time get_time(bool utc) const override;
virtual Date get_date(bool p_utc) const override;
virtual Time get_time(bool p_utc) const override;
virtual TimeZoneInfo get_time_zone_info() const override;

virtual double get_unix_time() const override;
Expand Down
9 changes: 5 additions & 4 deletions platform/uwp/os_uwp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -441,12 +441,13 @@ String OS_UWP::get_name() const {
return "UWP";
}

OS::Date OS_UWP::get_date(bool utc) const {
OS::Date OS_UWP::get_date(bool p_utc) const {
SYSTEMTIME systemtime;
if (utc)
if (utc) {
GetSystemTime(&systemtime);
else
} else {
GetLocalTime(&systemtime);
}

Date date;
date.day = systemtime.wDay;
Expand All @@ -457,7 +458,7 @@ OS::Date OS_UWP::get_date(bool utc) const {
return date;
}

OS::Time OS_UWP::get_time(bool utc) const {
OS::Time OS_UWP::get_time(bool p_utc) const {
SYSTEMTIME systemtime;
if (utc)
GetSystemTime(&systemtime);
Expand Down
4 changes: 2 additions & 2 deletions platform/uwp/os_uwp.h
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,8 @@ class OS_UWP : public OS {

virtual String get_name() const;

virtual Date get_date(bool utc) const;
virtual Time get_time(bool utc) const;
virtual Date get_date(bool p_utc) const;
virtual Time get_time(bool p_utc) const;
virtual TimeZoneInfo get_time_zone_info() const;
virtual uint64_t get_unix_time() const;

Expand Down
14 changes: 8 additions & 6 deletions platform/windows/os_windows.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -290,12 +290,13 @@ String OS_Windows::get_name() const {
return "Windows";
}

OS::Date OS_Windows::get_date(bool utc) const {
OS::Date OS_Windows::get_date(bool p_utc) const {
SYSTEMTIME systemtime;
if (utc)
if (p_utc) {
GetSystemTime(&systemtime);
else
} else {
GetLocalTime(&systemtime);
}

Date date;
date.day = systemtime.wDay;
Expand All @@ -306,12 +307,13 @@ OS::Date OS_Windows::get_date(bool utc) const {
return date;
}

OS::Time OS_Windows::get_time(bool utc) const {
OS::Time OS_Windows::get_time(bool p_utc) const {
SYSTEMTIME systemtime;
if (utc)
if (p_utc) {
GetSystemTime(&systemtime);
else
} else {
GetLocalTime(&systemtime);
}

Time time;
time.hour = systemtime.wHour;
Expand Down
4 changes: 2 additions & 2 deletions platform/windows/os_windows.h
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,8 @@ class OS_Windows : public OS {

virtual void initialize_joypads() override {}

virtual Date get_date(bool utc) const override;
virtual Time get_time(bool utc) const override;
virtual Date get_date(bool p_utc) const override;
virtual Time get_time(bool p_utc) const override;
virtual TimeZoneInfo get_time_zone_info() const override;
virtual double get_unix_time() const override;

Expand Down