Revision: 5215 http://trac.macosforge.org/projects/ruby/changeset/5215 Author: vincent.isambart@gmail.com Date: 2011-01-31 15:20:40 -0800 (Mon, 31 Jan 2011) Log Message: ----------- in 32-bit immediate float are very imprecise so to convert NSDate to Time, use Time.at(sec, usec) instead of Time.at(time in float) fixes #1143 Modified Paths: -------------- MacRuby/trunk/objc.m Modified: MacRuby/trunk/objc.m =================================================================== --- MacRuby/trunk/objc.m 2011-01-31 13:50:56 UTC (rev 5214) +++ MacRuby/trunk/objc.m 2011-01-31 23:20:40 UTC (rev 5215) @@ -554,9 +554,22 @@ } else if (k == (Class)rb_cNSDate) { @try { - CFAbsoluteTime time = CFDateGetAbsoluteTime((CFDateRef)obj); - VALUE arg = DBL2NUM(time + CF_REFERENCE_DATE); + CFAbsoluteTime time = CFDateGetAbsoluteTime((CFDateRef)obj) + CF_REFERENCE_DATE; +#if __LP64__ + VALUE arg = DBL2NUM(time); return rb_vm_call(rb_cTime, sel_at, 1, &arg); +#else + // immediate floats have not enough precision in 32-bit + // so instead of using Time.at(time in float), + // we use Time.at(sec, usec) + double integral, fractional; + fractional = modf(time, &integral); + VALUE args[] = { + LONG2NUM((long)integral), + LONG2NUM((long)(fractional * 1e6 + 0.5)) + }; + return rb_vm_call(rb_cTime, sel_at, 2, args); +#endif } @catch (NSException *e) { // Some NSDates might return an exception (example: uninitialized objects).