[Haskell-cafe] Re: Sillyness in the standard libs.
Arthur van Leeuwen
arthurvl at cs.uu.nl
Thu Nov 29 12:12:11 EST 2007
On 29-nov-2007, at 14:44, Simon Marlow wrote:
> 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.
Yes. That's what I wanted to do at first. :)
> 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)
Which, unfortunately, does not work with time-1.1.1 which is in GHC
6.6.1.
Going through Rational is the right solution, though. My hackish
detour was to use fromIntegral . toInteger . fromEnum
> 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.
Yes, that would be good! Note that both the docs for DiffTime and
EpochTime state that
they contain seconds, and both are somewhat unclear as to whether
they contain any
higher precision than whole seconds.
With kind regards, Arthur.
--
/\ / | arthurvl at cs.uu.nl | Work like you don't need
the money
/__\ / | A friend is someone with whom | Love like you have never
been hurt
/ \/__ | you can dare to be yourself | Dance like there's nobody
watching
More information about the Haskell-Cafe
mailing list