#Parsing ISO 8601
11 messages · Page 1 of 1 (latest)
i dont think you can parse timespans in go
@atomic violet as far i know ISO 8601 format is '2022-09-25T01:43:59+00:00' or '2022-09-25T01:43:59Z' and currently support in format RFC339.
Can you share the value?
PT1M1S is a ISO8601 duration of 1 minute 1 second
there's nothing in the stdlib for this
but I found this
https://stackoverflow.com/a/64671275
Go time formats require you to use the exact same numbers for day, year, hour, second, etc. as the ones used in the time package. https://pkg.go.dev/time#pkg-constants
2 or 02 for day
06 or 2006 for year
etc
I would recommend creating a method to adapt the ISO 8601 format duration to be compatible with this: https://pkg.go.dev/time#ParseDuration
Basically you need to split all of the individual units, then convert the units larger than hours into hours, and use the correct unit suffix from the unit map in the stdlib src:
var unitMap = map[string]uint64{
"ns": uint64(Nanosecond),
"us": uint64(Microsecond),
"µs": uint64(Microsecond), // U+00B5 = micro symbol
"μs": uint64(Microsecond), // U+03BC = Greek letter mu
"ms": uint64(Millisecond),
"s": uint64(Second),
"m": uint64(Minute),
"h": uint64(Hour),
}
PT1M1S would become 1m1s
PTD1H2S3 (assuming D is days and H is hours): 24h2h3s
Here's a regex pattern I used in one of my FOSS projects: (?P<Q>\d+)(?P<U>[^\d\s]+), you can use that with FindAllStringSubmatch to find all qty / unit pairs most likely, then you just have to worry about the possible prefixes