duration

package module
v0.0.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 26, 2026 License: MIT Imports: 5 Imported by: 10 modules, 11 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

⌚ duration

Build status Go Report Card GoDoc License MIT

Parse an ISO 8601 duration string, as described in Appendix A of RFC 3339, into time.Duration

In addition to the strict grammar below, the following are accepted:

  • fractions in any component, using . or , as the decimal separator
  • weeks combined with other components, such as P1WT2H
  • a leading - for negative durations

Results are rounded to the nearest nanosecond, with halves rounded away from zero. Fractional digits after the 18th are ignored. Strings longer than 256 bytes are rejected.

There are probably a few unsupported edge cases still to be fixed, please help me find them :)

The following constants are used to do the calculations for longer durations:

HoursPerDay = 24.0
HoursPerWeek = 168.0
HoursPerMonth = 730.4841667
HoursPerYear = 8765.81

Look in the test for examples of both valid and invalid duration strings.

Installation

go get github.com/peterhellberg/duration@latest

Feel free to copy this package into your own codebase.

Usage

package main

import (
	"fmt"

	"github.com/peterhellberg/duration"
)

func main() {
	if d, err := duration.Parse("P1DT30H4S"); err == nil {
		fmt.Println(d) // Output: 54h0m4s
	}
}

RFC3339 grammar for durations

   dur-second        = 1*DIGIT "S"
   dur-minute        = 1*DIGIT "M" [dur-second]
   dur-hour          = 1*DIGIT "H" [dur-minute]
   dur-time          = "T" (dur-hour / dur-minute / dur-second)
   dur-day           = 1*DIGIT "D"
   dur-week          = 1*DIGIT "W"
   dur-month         = 1*DIGIT "M" [dur-day]
   dur-year          = 1*DIGIT "Y" [dur-month]
   dur-date          = (dur-day / dur-month / dur-year) [dur-time]

   duration          = "P" (dur-date / dur-time / dur-week)

Documentation

Overview

Package duration parses ISO 8601 duration strings into time.Duration

The format is the one described in Appendix A of RFC 3339, for example "P1Y2M3DT4H5M6.7S". In addition to the strict grammar, the following are accepted:

  • fractions in any component, using "." or "," as the decimal separator
  • weeks combined with other components, such as "P1WT2H"
  • a leading "-" for negative durations

Years, months, weeks and days are converted using the HoursPer* constants. Results are rounded to the nearest nanosecond, with halves rounded away from zero. Fractional digits after the 18th are ignored. Strings longer than 256 bytes are rejected.

Installation

Just go get the package:

go get github.com/peterhellberg/duration@latest

Usage

A small usage example

package main

import (
	"fmt"

	"github.com/peterhellberg/duration"
)

func main() {
	if d, err := duration.Parse("P1DT30H4S"); err == nil {
		fmt.Println(d) // Output: 54h0m4s
	}
}

Index

Examples

Constants

View Source
const (
	// HoursPerDay is the number of hours per day according to Google
	HoursPerDay = 24.0

	// HoursPerWeek is the number of hours per week according to Google
	HoursPerWeek = 168.0

	// HoursPerMonth is the number of hours per month according to Google
	HoursPerMonth = 730.4841667

	// HoursPerYear is the number of hours per year according to Google
	HoursPerYear = 8765.81
)

Variables

View Source
var (
	// ErrInvalidString is returned when passed an invalid string
	ErrInvalidString = errors.New("invalid duration string")

	// ErrUnsupportedFormat is returned when parsing fails
	ErrUnsupportedFormat = errors.New("unsupported duration string format")
)

Functions

func Parse

func Parse(s string) (time.Duration, error)

Parse parses an ISO 8601 duration string into a time.Duration.

It returns ErrInvalidString if s contains no components or ends with "T", and ErrUnsupportedFormat if s is longer than 256 bytes, does not match the format, or the result does not fit in a time.Duration.

Example
package main

import (
	"fmt"

	"github.com/peterhellberg/duration"
)

func main() {
	if d, err := duration.Parse("PT1M65S"); err == nil {
		fmt.Println(d)
	}

}
Output:
2m5s

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL