https://github.com/senseyeio/duration
Parse iso8601 duration strings, and use to shift dates/times.
https://github.com/senseyeio/duration
date iso8601 time
Last synced: 7 months ago
JSON representation
Parse iso8601 duration strings, and use to shift dates/times.
- Host: GitHub
- URL: https://github.com/senseyeio/duration
- Owner: senseyeio
- License: other
- Created: 2018-01-18T11:44:24.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2024-03-15T23:57:35.000Z (over 1 year ago)
- Last Synced: 2025-03-01T10:37:19.899Z (7 months ago)
- Topics: date, iso8601, time
- Language: Go
- Size: 24.4 KB
- Stars: 63
- Watchers: 6
- Forks: 18
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
Duration [](https://travis-ci.org/senseyeio/duration) [](https://coveralls.io/github/senseyeio/duration?branch=master) [](https://goreportcard.com/report/senseyeio/duration) [](https://godoc.org/github.com/senseyeio/duration)
=======
Parse ISO8601 duration strings, and use to shift dates/times.Basic Example
-------------```go
package mainimport (
"fmt"
"time""github.com/senseyeio/duration"
)func main() {
d, _ := iso8601.ParseISO8601("P1D")
today := time.Now()
tomorrow := d.Shift(today)
fmt.Println(today.Format("Jan _2"))
fmt.Println(tomorrow.Format("Jan _2"))
}
```Why Does This Package Exist
---------------------------
> Why can't we just use a `time.Duration` and `time.Add`?A very reasonable question.
The code below repeatedly adds 24 hours to a `time.Time`. You might expect the time on that date to stay the same, but [_there are not always 24 hours in a day_](http://infiniteundo.com/post/25326999628/falsehoods-programmers-believe-about-time). When the clocks change in New York, the time will skew by an hour. As you can see from the output, duration.Duration.Shift() can increment the date without shifting the time.
```go
package mainimport (
"fmt"
"time""github.com/senseyeio/duration"
)func main() {
loc, _ := time.LoadLocation("America/New_York")
d, _ := iso8601.ParseISO8601("P1D")
t1, _ := time.ParseInLocation("Jan 2, 2006 at 3:04pm", "Jan 1, 2006 at 3:04pm", loc)
t2 := t1
for i := 0; i < 365; i++ {
t1 = t1.Add(24 * time.Hour)
t2 = d.Shift(t2)
fmt.Printf("time.Add:%d Duration.Shift:%d\n", t1.Hour(), t2.Hour())
}
}// Outputs
// time.Add:15 Duration.Shift:15
// time.Add:15 Duration.Shift:15
// time.Add:15 Duration.Shift:15
// ...
// time.Add:16 Duration.Shift:15
// time.Add:16 Duration.Shift:15
// time.Add:16 Duration.Shift:15
// ...
```-------
Months are tricky. Shifting by months uses `time.AddDate()`, which is great. However, be aware of how differing days in the month are accommodated. Dates will 'roll over' if the month you're shifting to has fewer days. e.g. if you start on Jan 30th and repeat every "P1M", you'll get this:```
Jan 30, 2006
Mar 2, 2006
Apr 2, 2006
May 2, 2006
Jun 2, 2006
Jul 2, 2006
Aug 2, 2006
Sep 2, 2006
Oct 2, 2006
Nov 2, 2006
Dec 2, 2006
Jan 2, 2007
```