Re: [Haskell-cafe] Converting CTime -> Int

2007-08-26 Thread Sven Panne
[ 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/95399/basedefs/sys/types.h.html#tag_13_67

   "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.
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Converting CTime -> Int

2007-05-16 Thread John Meacham
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⑈
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Converting CTime -> Int

2007-05-16 Thread Tomasz Zielonka
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
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Converting CTime -> Int

2007-05-15 Thread Brandon S. Allbery KF8NH


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 :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] [EMAIL PROTECTED]
system administrator [openafs,heimdal,too many hats] [EMAIL PROTECTED]
electrical and computer engineering, carnegie mellon universityKF8NH


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


Re: [Haskell-cafe] Converting CTime -> Int

2007-05-15 Thread Brandon S. Allbery 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] [EMAIL PROTECTED]
system administrator [openafs,heimdal,too many hats] [EMAIL PROTECTED]
electrical and computer engineering, carnegie mellon universityKF8NH


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


Re: [Haskell-cafe] Converting CTime -> Int

2007-05-15 Thread Stefan O'Rear
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
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


[Haskell-cafe] Converting CTime -> Int

2007-05-15 Thread Rob Hoelz
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
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe