How to use the Haskell `parseTimeM` function?

Haskell has a function parseTimeMthat replaces an obsolete function parseTime. I assume that the reason the first preference over the last is because it has more flexibility in parsing errors. Instead, Maybehe can call failon the attached monad.

So I tried. Firstly, the deprecated function:

> parseTime defaultTimeLocale  "%Y" "2015" :: Maybe UTCTime
Just 2015-01-01 00:00:00 UTC

> parseTime defaultTimeLocale  "%Y" "201x" :: Maybe UTCTime
Nothing

Great. As expected. Then, the monad Identity:

> runIdentity $ parseTimeM  False defaultTimeLocale  "%Y" "2015" :: UTCTime
2015-01-01 00:00:00 UTC

> runIdentity $ parseTimeM  False defaultTimeLocale  "%Y" "201x" :: UTCTime
*** Exception: parseTimeM: no parse of "201x"

It is also expected since the monad Identitydoes not have an elegant failure mode. But I was expecting something else with a monad Exceptwhose purpose is to gracefully catch errors.

> runExcept $ parseTimeM  False defaultTimeLocale  "%Y" "2015" :: Either () UTCTime
Right 2015-01-01 00:00:00 UTC

> runExcept $ parseTimeM  False defaultTimeLocale  "%Y" "201x" :: Either () UTCTime
*** Exception: parseTimeM: no parse of "201y"

? Left ()? , , , , , , ?

+4
1

Except mtl, . , (ExceptT ) :

type Except e = ExceptT e Identity

, , , , fail Except Identity .

Except e , fail e? e, e, , fail .

parseTimeM, fail Monad.

+3

All Articles