jens@2: // jens@2: // DateUtils.m jens@2: // Cloudy jens@2: // jens@2: // Created by Jens Alfke on 3/25/08. jens@2: // Copyright 2008 __MyCompanyName__. All rights reserved. jens@2: // jens@2: jens@2: #import "DateUtils.h" jens@2: jens@2: #include jens@2: #include jens@2: #include jens@2: #include jens@2: #include jens@2: jens@2: jens@4: /** Absolute time (since 'reference date') to NSDate. 0.0 -> nil. */ jens@4: NSDate* $date( CFAbsoluteTime time ) jens@4: { jens@4: CAssert(time>=0.0 && time < 1.0e15, @"Bogus timestamp %g",time); jens@4: return time ?[NSDate dateWithTimeIntervalSinceReferenceDate: time] :nil; jens@4: } jens@4: jens@4: jens@4: /** NSDate to absolute time (since 'reference date'). nil -> 0.0 */ jens@4: CFAbsoluteTime $time( NSDate* date ) jens@4: { jens@4: return date ?[date timeIntervalSinceReferenceDate] :0.0; jens@4: } jens@4: jens@4: jens@4: jens@2: NSTimeInterval TimeIntervalSinceBoot(void) jens@2: { jens@2: // From http://developer.apple.com/qa/qa2004/qa1398.html jens@2: uint64_t abstime = mach_absolute_time(); jens@2: // Have to do some pointer fun because AbsoluteToNanoseconds jens@2: // works in terms of UnsignedWide, which is a structure rather jens@2: // than a proper 64-bit integer. jens@2: Nanoseconds elapsedNano = AbsoluteToNanoseconds( *(AbsoluteTime *) &abstime ); jens@2: return *(uint64_t*)&elapsedNano / 1.0e9; jens@2: } jens@2: