Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot derive a Show instance for data containing UTCTime

Tags:

haskell

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.

like image 759
Chry Cheng Avatar asked Jul 11 '10 15:07

Chry Cheng


2 Answers

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.

like image 59
Yitz Avatar answered Oct 24 '22 08:10

Yitz


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.

like image 23
sepp2k Avatar answered Oct 24 '22 08:10

sepp2k