Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/peterhellberg/duration
Parse a RFC 3339 duration string into time.Duration
https://github.com/peterhellberg/duration
duration-string go parser rfc-3339
Last synced: about 1 month ago
JSON representation
Parse a RFC 3339 duration string into time.Duration
- Host: GitHub
- URL: https://github.com/peterhellberg/duration
- Owner: peterhellberg
- License: mit
- Created: 2015-06-13T13:12:42.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2022-11-29T17:07:15.000Z (about 2 years ago)
- Last Synced: 2024-11-06T07:39:01.097Z (about 2 months ago)
- Topics: duration-string, go, parser, rfc-3339
- Language: Go
- Homepage: https://tools.ietf.org/html/rfc3339
- Size: 19.5 KB
- Stars: 29
- Watchers: 4
- Forks: 2
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# :watch: duration
[![Build status](https://github.com/peterhellberg/duration/actions/workflows/test.yml/badge.svg)](https://github.com/peterhellberg/duration/actions/workflows/test.yml)
[![Go Report Card](https://goreportcard.com/badge/peterhellberg/duration)](https://goreportcard.com/report/peterhellberg/duration)
[![GoDoc](https://img.shields.io/badge/godoc-reference-blue.svg?style=flat)](https://pkg.go.dev/github.com/peterhellberg/duration)
[![License MIT](https://img.shields.io/badge/license-MIT-lightgrey.svg?style=flat)](https://github.com/peterhellberg/duration/blob/master/LICENSE)Parse a [RFC3339](https://www.ietf.org/rfc/rfc3339.txt) duration string into `time.Duration`
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 -u github.com/peterhellberg/duration
Feel free to copy this package into your own codebase.
## Usage
```go
package mainimport (
"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)
```