Re: [Haskell-cafe] Serializing UTCTimes

2012-01-21 Thread 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

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Serializing UTCTimes

2012-01-21 Thread Bas van Dijk
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 g...@sefer.org wrote:
 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

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


[Haskell-cafe] Serializing UTCTimes

2012-01-20 Thread Bas van Dijk
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

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Serializing UTCTimes

2012-01-20 Thread Bas van Dijk
On 20 January 2012 15:03, Bas van Dijk v.dijk@gmail.com wrote:
 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

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Serializing UTCTimes

2012-01-20 Thread Ertugrul Söylemez
Bas van Dijk v.dijk@gmail.com wrote:

 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/


signature.asc
Description: PGP signature
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe