Skip to content

Instantly share code, notes, and snippets.

@mightybyte
Created December 10, 2013 19:04
Show Gist options
  • Save mightybyte/7896191 to your computer and use it in GitHub Desktop.
Save mightybyte/7896191 to your computer and use it in GitHub Desktop.
A collection of core date/time formlets for digestive-functors.
utcTimeFormlet :: Monad m
=> String
-- ^ Date format string
-> String
-- ^ Time format string
-> TimeZone
-> Formlet Text m UTCTime
utcTimeFormlet dFmt tFmt tz d =
localTimeToUTC tz <$> localTimeFormlet dFmt tFmt (utcToLocalTime tz <$> d)
localTimeFormlet :: Monad m
=> String
-- ^ Date format string
-> String
-- ^ Time format string
-> Formlet Text m LocalTime
localTimeFormlet dFmt tFmt d = LocalTime
<$> "date" .: dateFormlet dFmt (localDay <$> d)
<*> "time" .: timeFormlet tFmt (localTimeOfDay <$> d)
dateFormlet :: Monad m
=> String
-- ^ Format string
-> Formlet Text m Day
dateFormlet fmt d =
validate vFunc (string $ formatTime defaultTimeLocale fmt <$> d)
where
vFunc = maybe (Error "invalid date") Success .
parseTime defaultTimeLocale fmt
timeFormlet :: Monad m
=> String
-- ^ Format string
-> Formlet Text m TimeOfDay
timeFormlet fmt d =
validate vFunc (string $ formatTime defaultTimeLocale fmt <$> d)
where
vFunc = maybe (Error "invalid time") Success .
parseTime defaultTimeLocale fmt
@lukehoersten
Copy link

https://gist.github.com/LukeHoersten/7896241 for my ZonedTime implementation w/ optional forms.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment