#Parsing ISO 8601

11 messages · Page 1 of 1 (latest)

atomic violet
#

How can I parse an ISO 8601 format? The string being returned is "PT1M1S" for 1 minute and 1 second. I tried parsing it using the RFC339 layout but it didnt work.

worthy fern
#

i dont think you can parse timespans in go

strange rain
#

@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?

flint ether
# atomic violet How can I parse an ISO 8601 format? The string being returned is "PT1M1S" for 1 ...

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

mellow turtle
#

2 or 02 for day

#

06 or 2006 for year

#

etc

scenic nimbus
#

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