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

Add methods to convert an ISO 8601 timestamp to a date dictionary and vice versa #2114

Closed
Calinou opened this issue Jan 13, 2021 · 1 comment · Fixed by godotengine/godot#49123
Milestone

Comments

@Calinou
Copy link
Member

Calinou commented Jan 13, 2021

Describe the project you are working on

The Godot editor 🙂

Describe the problem or limitation you are having in your project

There's no built-in method to convert ISO 8601 timestamps to other date formats supported by Godot (date dictionaries and UNIX timestamps).

Describe the feature / enhancement and how it helps to overcome the problem or limitation

Add OS.get_iso8601_time_from_datetime() and OS.get_datetime_from_iso8601_time() methods.

We already have similar methods to convert a date dictionary to an UNIX timestamp and vice versa.

Describe how your proposal will work, with code, pseudo-code, mock-ups, and/or diagrams

These methods should be added in the OS singleton next to the other date conversion methods.

If this enhancement will not be used often, can it be worked around with a few lines of script?

It can be implemented easily using a script:

## Parses an ISO-8601 date string to a datetime dictionary that can be parsed by Godot.
func parse_date(iso_date: String) -> Dictionary:
	var date := iso_date.split("T")[0].split("-")
	var time := iso_date.split("T")[1].trim_suffix("Z").split(":")

	return {
		year = date[0],
		month = date[1],
		day = date[2],
		hour = time[0],
		minute = time[1],
		second = time[2],
	}

func _ready() -> void:
    print(parse_date("2021-01-11T15:00:00Z"))

However, given that most people will need this when they interact with a REST API, I'd argue this should be present in core.

Is there a reason why this should be core and not an add-on in the asset library?

Other programming languages such as JavaScript already feature this in their standard library.

@aaronfranke
Copy link
Member

Related: The existence of more time APIs would be another argument in favor of moving time and date methods out of OS.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants