How do I express time intervals in Haskell?
I am trying to create a sequence of dates separated at regular intervals, for example, starting at this point, I want to create a list:
[now, now + 10 minutes, now + 20 minutes, now + 30 minutes, ...]
My function will be based on iterate
and has two arguments: start
and interval
, but what types of these arguments should they be? I wandered around the library and wiki with no success. It seems that the recommended way to encode the time is UTCTime
from Data.Time.Clock
, and the recommended way to encode the time difference is NominalDiffTime
, but how can I create a nominal time difference in one day? For example?
The package seems to time-lens
provide ways to do this, but as far as I know lenses are a way to improve expressiveness, I would be surprised if using lenses is the only way to solve this problem.
source to share
In ghci:
Data.Time> now <- getCurrentTime
Data.Time> let tenMinutes = 10*60
Data.Time> mapM_ print . take 10 . iterate (addUTCTime tenMinutes) $ now
2015-06-27 20:14:21.445443 UTC
2015-06-27 20:24:21.445443 UTC
2015-06-27 20:34:21.445443 UTC
2015-06-27 20:44:21.445443 UTC
2015-06-27 20:54:21.445443 UTC
2015-06-27 21:04:21.445443 UTC
2015-06-27 21:14:21.445443 UTC
2015-06-27 21:24:21.445443 UTC
2015-06-27 21:34:21.445443 UTC
2015-06-27 21:44:21.445443 UTC
Note that NominalDiffTime
(as used here) ignores leap seconds; that is, adding 60*60*24
will result in the same time with a different date, even if there are actually 60*60*24+1
seconds between those two UTC times. This is often (but not always) what you want. If you want to be sure to jump, you can use AbsoluteTime
and DiffTime
instead of UTCTime
and NominalDiffTime
.
and of course your function could be just:
import Data.Time.Clock
timeSeq :: UTCTime -> NominalDiffTime -> [UTCTime]
timeSeq start interval = iterate (addUTCTime delta) start
source to share