
Hello, What's the recommended way for serializing (with the cereal package) an UTCTime? It's easy to give Serialize instances for UTCTime and Day: instance Serialize UTCTime where get = liftM2 UTCTime get get put (UTCTime day time) = put day >> put time instance Serialize Day where get = liftM Day get put = put . toModifiedJulianDay However I have no idea how to serialize the DiffTime stored in an UTCTime: instance Serialize DiffTime where get = ? put = ? Regards, Bas

On 20 January 2012 15:03, Bas van Dijk
What's the recommended way for serializing (with the cereal package) an UTCTime?
I'm now using the datetime package so I can do: import Data.DateTime (fromSeconds, toSeconds) instance Serialize UTCTime where get = fromSeconds <$> get put = put . toSeconds But I will have to look at the code of datetime to see if I'm not loosing precision. Bas

Bas van Dijk wrote:
What's the recommended way for serializing (with the cereal package) an UTCTime?
Serialize the Day part as an Integer using toModifiedJulianDay/ModifiedJulianDay, (Note that Day is not a constructor, it's just the name of the type.) Serialize the DiffTime as a Rational, as Ertugrul said.
I'm now using the datetime package
Why? It just obscures the time library.
But I will have to look at the code of datetime to see if I'm not losing precision.
You are losing precision. If you only care about time to the nearest second, you can truncate the Rational of the DiffTime (don't round, because this may be the last second of a day) and then use fromIntegral to deserialize. Regards, Yitz

Thanks Ertugrul and Yitzchak. I failed to notice the Real and
Fractional instances for DiffTime. Thanks very much for pointing me to
it. I dropped the dependency on datetime and implemented your
suggestions.
Bas
On 21 January 2012 22:29, Yitzchak Gale
Bas van Dijk wrote:
What's the recommended way for serializing (with the cereal package) an UTCTime?
Serialize the Day part as an Integer using toModifiedJulianDay/ModifiedJulianDay, (Note that Day is not a constructor, it's just the name of the type.)
Serialize the DiffTime as a Rational, as Ertugrul said.
I'm now using the datetime package
Why? It just obscures the time library.
But I will have to look at the code of datetime to see if I'm not losing precision.
You are losing precision. If you only care about time to the nearest second, you can truncate the Rational of the DiffTime (don't round, because this may be the last second of a day) and then use fromIntegral to deserialize.
Regards, Yitz

Bas van Dijk
However I have no idea how to serialize the DiffTime stored in an UTCTime:
instance Serialize DiffTime where get = ? put = ?
Note that DiffTime has this weird property that there is a Real instance, so you have a toRational function. ;) To go the other way you have a Fractional instance, so you also have 'fromRational'. Greets, Ertugrul -- nightmare = unsafePerformIO (getWrongWife >>= sex) http://ertes.de/
participants (3)
-
Bas van Dijk
-
Ertugrul Söylemez
-
Yitzchak Gale