Skip to content

Instantly share code, notes, and snippets.

@pokutuna
Created September 3, 2014 00:34
Show Gist options
  • Save pokutuna/63bb7a881af84d3533f3 to your computer and use it in GitHub Desktop.
Save pokutuna/63bb7a881af84d3533f3 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"time"
)
func intervalIterator(from, to time.Time, interval time.Duration) struct {
hasNext func() bool
get func() time.Time
} {
now := from
return struct {
hasNext func() bool
get func() time.Time
}{
func() bool { return now.Before(to) },
func() time.Time { defer func() { now = now.Add(interval) }(); return now },
}
}
func main() {
now := time.Now()
itr := intervalIterator(
now.Add(-24*time.Hour).Truncate(time.Hour),
now,
time.Hour,
)
for itr.hasNext() {
fmt.Println(itr.get())
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment