
Hello everyone, I'm writing an interface to C code, and one of the functions I'm wrapping returns time_t. I see that this maps to CTime in Foreign.C.Types, but I can't figure out how to convert it to an Int (or any other useful Haskell type, for that matter) for the life of me. I've poured over the standard library docs, but to no avail. Could someone give me a hint? Thanks, Rob Hoelz

On Tue, May 15, 2007 at 11:35:52PM -0500, Rob Hoelz wrote:
Hello everyone,
I'm writing an interface to C code, and one of the functions I'm wrapping returns time_t. I see that this maps to CTime in Foreign.C.Types, but I can't figure out how to convert it to an Int (or any other useful Haskell type, for that matter) for the life of me. I've poured over the standard library docs, but to no avail. Could someone give me a hint?
fromIntegral Stefan

On May 16, 2007, at 0:37 , Stefan O'Rear wrote:
On Tue, May 15, 2007 at 11:35:52PM -0500, Rob Hoelz wrote:
I've poured over the standard library docs, but to no avail. Could someone give me a hint?
fromIntegral
No instance for (Integral CTime) arising from a use of `fromIntegral' at <interactive>:1:0-24 Possible fix: add an instance declaration for (Integral CTime) In the expression: fromIntegral (0 :: CTime) In the definition of `it': it = fromIntegral (0 :: CTime) -- brandon s. allbery [solaris,freebsd,perl,pugs,haskell] allbery@kf8nh.com system administrator [openafs,heimdal,too many hats] allbery@ece.cmu.edu electrical and computer engineering, carnegie mellon university KF8NH

On May 16, 2007, at 0:35 , Rob Hoelz wrote:
wrapping returns time_t. I see that this maps to CTime in Foreign.C.Types, but I can't figure out how to convert it to an Int (or any other useful Haskell type, for that matter) for the life of me. I've poured over the standard library docs, but to no avail. Could someone give me a hint?
It's an instance of Enum, so use fromEnum to create an Int. (Don't feel too bad, it took me an embarrasingly long amount of time to figure that out as well; before that I used read . show :) -- brandon s. allbery [solaris,freebsd,perl,pugs,haskell] allbery@kf8nh.com system administrator [openafs,heimdal,too many hats] allbery@ece.cmu.edu electrical and computer engineering, carnegie mellon university KF8NH

On Wed, May 16, 2007 at 12:38:55AM -0400, Brandon S. Allbery KF8NH wrote:
On May 16, 2007, at 0:35 , Rob Hoelz wrote:
wrapping returns time_t. I see that this maps to CTime in Foreign.C.Types, but I can't figure out how to convert it to an Int (or any other useful Haskell type, for that matter) for the life of me. I've poured over the standard library docs, but to no avail. Could someone give me a hint?
It's an instance of Enum, so use fromEnum to create an Int. (Don't feel too bad, it took me an embarrasingly long amount of time to figure that out as well; before that I used read . show :)
I'm not sure it's a good idea - in theory, Int can have fewer bits than CTime. I wonder why CTime is not Integral - maybe there is no such guarantee for time_t? If so, then you shouldn't rely on Enum. The safest bet seems to be toRational - CTime is Real. Best regards Tomek

[ Sorry for the *extremely* slow response, but I'm currently working through my backlog of >6000 mails... :-P ] On Wednesday 16 May 2007 09:35, Tomasz Zielonka wrote:
I wonder why CTime is not Integral - maybe there is no such guarantee for time_t? If so, then you shouldn't rely on Enum. The safest bet seems to be toRational - CTime is Real.
The Single Unix Specification has the answer: http://www.opengroup.org/onlinepubs/000095399/basedefs/sys/types.h.html#tag_... "time_t and clock_t shall be integer or real-floating types." CTime's Enum instance is as debatable as the ones for Float and Double, but for consistency reasons we included it in the FFI spec. Cheers, S.

On Wed, May 16, 2007 at 12:38:55AM -0400, Brandon S. Allbery KF8NH wrote:
On May 16, 2007, at 0:35 , Rob Hoelz wrote:
wrapping returns time_t. I see that this maps to CTime in Foreign.C.Types, but I can't figure out how to convert it to an Int (or any other useful Haskell type, for that matter) for the life of me. I've poured over the standard library docs, but to no avail. Could someone give me a hint?
It's an instance of Enum, so use fromEnum to create an Int. (Don't feel too bad, it took me an embarrasingly long amount of time to figure that out as well; before that I used read . show :)
'fromInteger . round' is probably a better idea if you only need second accuracy, there is no guarentee that a time_t will fit in an Int, if you need subsecond accurancy, then realToFrac will convert it to a Float or Double, but I doubt any system that ghc supports provides such accuracy via CTime. John -- John Meacham - ⑆repetae.net⑆john⑈
participants (6)
-
Brandon S. Allbery KF8NH
-
John Meacham
-
Rob Hoelz
-
Stefan O'Rear
-
Sven Panne
-
Tomasz Zielonka