Chry Cheng
Chry Cheng

Reputation: 3468

Cannot derive a Show instance for data containing UTCTime

I have data defined like the ff.:

import Data.Time.Clock

data D = D { ...,
             someDate :: UTCTime,
             ... }
         deriving (Eq, Show)

When I compile it, I get the ff. error:

No instance for (Show UTCTime)
  arising from the 'deriving' clause of a data type declaration
               at ...

I already have the time-1.1.3 package which should already have this instance according to documentation. My GHC version is 6.8.2.

Upvotes: 14

Views: 1554

Answers (2)

Yitz
Yitz

Reputation: 5117

Don't import each piece of the Data.Time suite separately. It's designed for you to import just Data.Time, and that will pull in just about everything that is commonly used. Including the Show instance for UTCTime.

If you don't want that much namespace clutter, import just the symbols you need:

import Data.Time (UTCTime, getCurrentTime)

or whatever else. That's anyway a good idea - it protects you against name clashes later on if the library gets updated and starts using a name that you have already defined.

Upvotes: 28

sepp2k
sepp2k

Reputation: 370417

The documentation lies. If you look at the source for Data.Time.Clock.UTC, there simply is no Show instance for UTCTime.

Edit:

As newacct helpfully pointed out, there's an orphaned instance for Show UTCTime in Data.Time.LocalTime.LocalTime, so if you import that, your code will work.

Upvotes: 3

Related Questions