sosodev/duration

ISO 8601 duration parsing in Go

Go

39

24 commits

updated May 29, 2026

See the code

README

duration

Go Reference

It's a Go module for parsing ISO 8601 durations and converting them to the often much more useful time.Duration.

why?

ISO 8601 is a pretty common standard and sometimes these durations show up in the wild.

installation

go get github.com/sosodev/duration

usage

package main

import (
	"fmt"
	"time"
	"github.com/sosodev/duration"
)

func main() {
	d, err := duration.Parse("P3Y6M4DT12H30M5.5S")
	if err != nil {
		panic(err)
	}
	
	fmt.Println(d.Years) // 3
	fmt.Println(d.Months) // 6
	fmt.Println(d.Days) // 4
	fmt.Println(d.Hours) // 12
	fmt.Println(d.Minutes) // 30
	fmt.Println(d.Seconds) // 5.5
	
	d, err = duration.Parse("PT33.3S")
	if err != nil {
		panic(err)
	}
	
	fmt.Println(d.ToTimeDuration() == time.Second*33+time.Millisecond*300) // true
}

correctness

This module aims to implement the ISO 8601 duration specification correctly. It properly supports fractional units and has unit tests that assert the correctness of it's parsing and conversion to a time.Duration.

With that said durations with months or years specified will be converted to time.Duration with a little fuzziness. Since I couldn't find a standard value, and they obviously vary, for those I used 2.628e+15 nanoseconds for a month and 3.154e+16 nanoseconds for a year.

sosodev/duration

ISO 8601 duration parsing in Go

Go

39

24 commits

updated May 29, 2026

See the code

README

duration

Go Reference

It's a Go module for parsing ISO 8601 durations and converting them to the often much more useful time.Duration.

why?

ISO 8601 is a pretty common standard and sometimes these durations show up in the wild.

installation

go get github.com/sosodev/duration

usage

package main

import (
	"fmt"
	"time"
	"github.com/sosodev/duration"
)

func main() {
	d, err := duration.Parse("P3Y6M4DT12H30M5.5S")
	if err != nil {
		panic(err)
	}
	
	fmt.Println(d.Years) // 3
	fmt.Println(d.Months) // 6
	fmt.Println(d.Days) // 4
	fmt.Println(d.Hours) // 12
	fmt.Println(d.Minutes) // 30
	fmt.Println(d.Seconds) // 5.5
	
	d, err = duration.Parse("PT33.3S")
	if err != nil {
		panic(err)
	}
	
	fmt.Println(d.ToTimeDuration() == time.Second*33+time.Millisecond*300) // true
}

correctness

This module aims to implement the ISO 8601 duration specification correctly. It properly supports fractional units and has unit tests that assert the correctness of it's parsing and conversion to a time.Duration.

With that said durations with months or years specified will be converted to time.Duration with a little fuzziness. Since I couldn't find a standard value, and they obviously vary, for those I used 2.628e+15 nanoseconds for a month and 3.154e+16 nanoseconds for a year.

Project health

Overall

3.5/10

Maintained

0/10

Code review

4/10
OpenSSF Scorecard

Latest release

v1.4.0 · Feb 22, 2026

Releases this year

1

Significant stargazers

Vincent Bernardoff

325 followers · starred Apr 2023

Elan Hasson

74 followers · starred Jun 2023