Instead of:
Just (defaultLocalTime, _) = strptime "" ""
You can do:
defaultLocalTime =
case strptime "" "" of
Just (x, _) -> x
Nothing -> error "No default local time"
Or some other error message.
Instead of:
Just (defaultLocalTime, _) = strptime "" ""
You can do:
defaultLocalTime =
case strptime "" "" of
Just (x, _) -> x
Nothing -> error "No default local time"
Or some other error message.
Thank you, @jaror. I just wondered (sorry for not being clear), is there is an alternative way of getting the default local time, without using strptime
and just relying on Data.Time.LocalTime
(or another submodule of Data.Time
)
I think it should always return 1970-01-01 00:00:00 +0000, so you could write that manually as:
defaultLocalTime = LocalTime (YearDay 1970 1) (TimeOfDay 0 0 0)
Thank you, but the error reads:
• Illegal term-level use of the type constructor ‘LocalTime’
imported from ‘Data.Time.LocalTime’
Looking at the definition:
-- | Bidirectional abstract constructor for ISO 8601 Ordinal Date format.
-- Invalid day numbers will be clipped to the correct range (1 to 365 or 366).
pattern YearDay :: Year -> DayOfYear -> Day
pattern YearDay y d <-
(toOrdinalDate -> (y, d))
where
YearDay y d = fromOrdinalDate y d
Do you understand what’s that pattern
keyword there ? I’ve never came across this. Is that a pattern synonym ?
You should still import the LocalTime
term constructor.
That’s part of the PatternSynonyms
extension.
Here’s a fully working program with explicit imports:
{-# LANGUAGE PatternSynonyms #-}
import Data.Time.Calendar.OrdinalDate (pattern YearDay)
import Data.Time.LocalTime (LocalTime (LocalTime), TimeOfDay (TimeOfDay))
defaultLocalTime = LocalTime (YearDay 1970 1) (TimeOfDay 0 0 0)
Alternatively you can avoid PatternSynonyms
by using fromOrdinalDate
which does the same thing as YearDay
:
import Data.Time.Calendar.OrdinalDate (fromOrdinalDate)
import Data.Time.LocalTime (LocalTime (LocalTime), TimeOfDay (TimeOfDay))
defaultLocalTime = LocalTime (fromOrdinalDate 1970 1) (TimeOfDay 0 0 0)
I agree with this way of proceeding. My motivation is a bit different: long term maintenance of existing packages results in a more stable, reliable and economic Haskell ecosystem.