In short: the Show instance for UTCTime doesn't come into scope when Data.Time.Clock
is imported. The haddocks claim that it will. This is confusing.
The haddocks for UTCTime claim the existence of a Show instance: http://hackage.haskell.org/package/time-1.5/docs/Data-Time-Clock.html
And indeed I was using getCurrentTime >>= print
in a piece of code and it was working.
Then the following happened.
$ ghci
GHCi, version 7.8.3: http://www.haskell.org/ghc/ :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer-gmp ... linking ... done.
Loading package base ... linking ... done.
Prelude> import Data.Time.Clock
Prelude Data.Time.Clock> getCurrentTime >>= print
<interactive>:3:20:
No instance for (Show UTCTime) arising from a use of ‘print’
In the second argument of ‘(>>=)’, namely ‘print’
In the expression: getCurrentTime >>= print
In an equation for ‘it’: it = getCurrentTime >>= print
Then I checked my old code which worked. It has a bunch of seemingly unrelated imports. A few minutes of uncommenting the imports one by one, I noticed when I import System.Directory
the Show instance comes into scope.
$ ghci
GHCi, version 7.8.3: http://www.haskell.org/ghc/ :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer-gmp ... linking ... done.
Loading package base ... linking ... done.
Prelude> import Data.Time.Clock
Prelude Data.Time.Clock> import System.Directory ()
Prelude Data.Time.Clock System.Directory> getCurrentTime >>= print
Loading package array-0.5.0.0 ... linking ... done.
Loading package deepseq-1.3.0.2 ... linking ... done.
Loading package old-locale-1.0.0.6 ... linking ... done.
Loading package time-1.4.2 ... linking ... done.
2014-10-22 09:07:14.838338 UTC
It seems this instance is defined in Data.Time.LocalTime.LocalTime
.
Prelude Data.Time.Clock System.Directory> :i UTCTime
data UTCTime
= UTCTime {utctDay :: time-1.4.2:Data.Time.Calendar.Days.Day,
utctDayTime :: DiffTime}
-- Defined in ‘time-1.4.2:Data.Time.Clock.UTC’
instance Eq UTCTime -- Defined in ‘time-1.4.2:Data.Time.Clock.UTC’
instance Ord UTCTime -- Defined in ‘time-1.4.2:Data.Time.Clock.UTC’
instance Read UTCTime
-- Defined in ‘time-1.4.2:Data.Time.Format.Parse’
instance Show UTCTime
-- Defined in ‘time-1.4.2:Data.Time.LocalTime.LocalTime’
The orphan instance can be seen from: http://hackage.haskell.org/package/time-1.5/docs/src/Data-Time-LocalTime-LocalTime.html
Moving the instance back to Data.Time.Clock
probably creates cyclic imports. There must be a solution involving a few internal modules, I suppose.
In any case the haddocks are definitely misleading.
(I now I have used time-1.4.2 above, but the same problem exists in time-1.5 too)