Last active
November 19, 2022 18:46
-
-
Save fenix-hub/0571f49458d476d9192eeede8dcc10c8 to your computer and use it in GitHub Desktop.
Parse an ISO8601 Time Period definition to a GDScript Dictionary
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extends Resource | |
var regex_str: String = "((?<repeat>R[0-9]+)\/)?P((?<years>[0-9]+)Y)?((?<months>[0-9]+)M)?((?<weeks>[0-9]+)W)?((?<days>[0-9]+)D)?(T((?<hours>[0-9]+)H)?((?<minutes>[0-9]+)M)?((?<seconds>[0-9]+\.?[0-9]+)?S)?)?" | |
var regex: RegEx.new() | |
class _init() -> void: | |
regex.compile(regex_str) | |
func parse_iso(iso_string: String) -> RegExMatch: | |
var result: RegExMatch = regex.search(iso_string) | |
if not result: | |
printerr("Could not evaluate provided string, since it doesn't follow ISO8691!") | |
return null | |
return result | |
func parse_iso_to_dict(iso_string: String) -> Dictionary: | |
var result: RegExMatch = parse_iso(iso_string) | |
if not result: | |
return {} | |
return { | |
repeat = result.get_string("repeat").rstrip("/") as int, | |
years = result.get_string("years") as float, | |
months = result.get_string("months") as float, | |
weeks = result.get_string("weeks") as float, | |
days = result.get_string("days") as float, | |
minutes = result.get_string("minutes") as float, | |
seconds = result.get_string("seconds") as float | |
} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
PT2H20M10.5S | |
P1Y3M20D | |
P1Y3M20DT2H20M10.5S | |
R3/PT2H20M10.5S | |
R3/P1Y3M20D | |
R3/P1Y3M20DT2H20M10.5S |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment