https://github.com/boolproof/p6y
Duration string parser for GO
https://github.com/boolproof/p6y
duration duration-parsing golang iso-8601
Last synced: 9 months ago
JSON representation
Duration string parser for GO
- Host: GitHub
- URL: https://github.com/boolproof/p6y
- Owner: boolproof
- License: mit
- Created: 2022-03-31T19:11:46.000Z (about 4 years ago)
- Default Branch: main
- Last Pushed: 2022-04-02T19:56:44.000Z (about 4 years ago)
- Last Synced: 2025-03-02T01:43:59.011Z (over 1 year ago)
- Topics: duration, duration-parsing, golang, iso-8601
- Language: Go
- Homepage:
- Size: 10.7 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# p6y - duration string parsing in GO
Package provides parsing of duration information encoded in string compliant with format described by **ISO 8601** (https://en.wikipedia.org/wiki/ISO_8601#Durations).
## Current limitations
Only `PnYnMnDTnHnMnS` and `PnW` formats are parsed by the package (no suppport for both `PYYYYMMDDThhmmss` and `P[YYYY]-[MM]-[DD]T[hh]:[mm]:[ss]`).
Package can not parse values with fractions (only integer values).
If parsing is successfull (no error), use following result struct methods to access intrger values of duration components:
- `Years()`
- `Months()`
- `Days()`
- `Weeks()`
- `Hours()`
- `Minutes()`
- `Seconds()`
## Example usage
```go
package main
import (
"fmt"
"github.com/boolproof/p6y"
)
func main() {
d, err := p6y.NewDuration("P7Y5DT12H")
if err == nil {
fmt.Printf("This will take %d years, %d months,"+
"%d days and %d hours\n",
d.Years(), d.Months(), d.Days(), d.Hours())
//outputs: "This will take 7 years, 0 months, 5 days and 12 hours"
} else {
fmt.Println(err.Error())
}
}
```