Skip to content

Instantly share code, notes, and snippets.

@fenix-hub
Last active November 19, 2022 18:46

Revisions

  1. fenix-hub revised this gist Oct 26, 2022. 2 changed files with 9 additions and 2 deletions.
    5 changes: 3 additions & 2 deletions iso8601_parser.gd
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    extends Resource

    var regex_str: String = "(?<repeat>R[0-9]\/)?P(?<years>[0-9]+Y)?(?<months>[0-9]+M)?([0-9]+W)?([0-9]+D)?T(?<hours>[0-9]+H)?(?<minutes>[0-9]+M)?(?<seconds>[0-9]+(\.?[0-9]+)?S)?"
    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()

    @@ -19,9 +19,10 @@ func parse_iso_to_dict(iso_string: String) -> Dictionary:
    if not result:
    return {}
    return {
    repeat = result.get_string("repeat") as float,
    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
    6 changes: 6 additions & 0 deletions tests.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,6 @@
    PT2H20M10.5S
    P1Y3M20D
    P1Y3M20DT2H20M10.5S
    R3/PT2H20M10.5S
    R3/P1Y3M20D
    R3/P1Y3M20DT2H20M10.5S
  2. fenix-hub revised this gist Oct 26, 2022. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions iso8601_parser.gd
    Original file line number Diff line number Diff line change
    @@ -16,6 +16,8 @@ func parse_iso(iso_string: String) -> RegExMatch:

    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") as float,
    years = result.get_string("years") as float,
  3. fenix-hub created this gist Oct 26, 2022.
    29 changes: 29 additions & 0 deletions iso8601_parser.gd
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,29 @@
    extends Resource

    var regex_str: String = "(?<repeat>R[0-9]\/)?P(?<years>[0-9]+Y)?(?<months>[0-9]+M)?([0-9]+W)?([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)
    return {
    repeat = result.get_string("repeat") as float,
    years = result.get_string("years") as float,
    months = result.get_string("months") as float,
    days = result.get_string("days") as float,
    minutes = result.get_string("minutes") as float,
    seconds = result.get_string("seconds") as float
    }