[Haskell-cafe] Re: Sillyness in the standard libs.

Simon Marlow simonmarhaskell at gmail.com
Thu Nov 29 08:44:33 EST 2007

Brandon S. Allbery KF8NH wrote:
> On Nov 19, 2007, at 16:06 , Arthur van Leeuwen wrote:
>> here is a puzzle for you: try converting a 
>> System.Posix.Types.EpochTime into either a
>> System.Time.CalendarTime or a Data.Time.Clock.UTCTime without going 
>> through
>> read . show or a similar detour through strings.
> fromEnum and/or toEnum are helpful for this kind of thing, and I am 
> occasionally tempted to bind "cast = toEnum . fromEnum" because I need 
> it so much.

Let's ignore System.Time since it's obsoleted by Data.Time.

I just spent a little while trying to solve this puzzle, and it turns out 
there *is* a right way to do this: for t :: EpochTime,

   posixSecondsToUTCTime (fromRational (toRational t) :: POSIXTime)

You want to go via Data.Time.Clock.POSIXTime, because that's what an 
EpochTime is.   Now, EpochTime does not have an Integral instance, because 
it is the same as C's time_t type, which is not guaranteed to be an 
integral type.  You have fromEnum, but that would be a hack: there's no 
guarantee that EpochTime fits in an Int, and if EpochTime is a fractional 
value you lose information.  But you *can* convert to a Rational with 
toRational, and from there you can get to a POSIXTime with fromRational 
(also realToFrac would do).

It turns out there are good reasons for all this, it just ends up being 
quite obscure.  I'll put the above formula in the docs.


More information about the Haskell-Cafe mailing list