Haskell:
Comparing two dates
How to:
Haskell, quietly known for its purity, needs you to talk date-talk with the right libraries. Let’s use Data.Time
.
import Data.Time
-- Define two dates
date1 :: UTCTime
date1 = UTCTime (fromGregorian 2023 4 1) (secondsToDiffTime 0)
date2 :: UTCTime
date2 = UTCTime (fromGregorian 2024 4 2) (secondsToDiffTime 3600)
-- Compare the dates
compareDates :: UTCTime -> UTCTime -> Ordering
compareDates = compare
main :: IO ()
main = do
print $ date1 `compareDates` date2 -- Output will be LT
print $ date2 `compareDates` date1 -- Output will be GT
print $ date1 `compareDates` date1 -- Output will be EQ
Straightforward, right? LT
for less than, GT
for greater than, and EQ
for equal.
Deep Dive
Back in the day, Haskell’s time handling wasn’t as slick. We owe our current comforts to the Data.Time
library progression over the years. It gives us UTCTime
, a happily unambiguous point in time.
Alternatives? Sure. You might find Data.Time.Calendar
and Data.Time.Clock
useful for specific scenarios. There’s also the old time
library for those feeling nostalgic or stuck with legacy code.
Now, the nitty-gritty: Comparing dates in Haskell hinges on UTCTime
which pairs a day (Day
) and a time (DiffTime
or NominalDiffTime
). It’s the compare
function doing the heavy lifting, a neat member of the Ord
class, letting us use >, <, ==
and more. Just remember Haskell loves its type safety. Ensure you’re always comparing apples with apples, or in our case, UTCTime
with UTCTime
.
See Also
Dive deeper or find help with these: