Created
September 3, 2014 00:34
-
-
Save pokutuna/63bb7a881af84d3533f3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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