DateUtils.m
author Jens Alfke <jens@mooseyard.com>
Mon Apr 14 13:58:48 2008 -0700 (2008-04-14)
changeset 4 64823cdde6a5
parent 2 3d3dcc3116d5
child 11 e5976864dfe9
permissions -rw-r--r--
Minor improvements.
     1 //
     2 //  DateUtils.m
     3 //  Cloudy
     4 //
     5 //  Created by Jens Alfke on 3/25/08.
     6 //  Copyright 2008 __MyCompanyName__. All rights reserved.
     7 //
     8 
     9 #import "DateUtils.h"
    10 
    11 #include <assert.h>
    12 #include <CoreServices/CoreServices.h>
    13 #include <mach/mach.h>
    14 #include <mach/mach_time.h>
    15 #include <unistd.h>
    16 
    17 
    18 /** Absolute time (since 'reference date') to NSDate. 0.0 -> nil. */
    19 NSDate* $date( CFAbsoluteTime time )
    20 {
    21     CAssert(time>=0.0 && time < 1.0e15, @"Bogus timestamp %g",time);
    22     return time ?[NSDate dateWithTimeIntervalSinceReferenceDate: time] :nil;
    23 }
    24 
    25 
    26 /** NSDate to absolute time (since 'reference date'). nil -> 0.0 */
    27 CFAbsoluteTime $time( NSDate* date )
    28 {
    29     return date ?[date timeIntervalSinceReferenceDate] :0.0;
    30 }
    31 
    32 
    33 
    34 NSTimeInterval TimeIntervalSinceBoot(void)
    35 {
    36     // From http://developer.apple.com/qa/qa2004/qa1398.html
    37     uint64_t abstime = mach_absolute_time();
    38     // Have to do some pointer fun because AbsoluteToNanoseconds
    39     // works in terms of UnsignedWide, which is a structure rather
    40     // than a proper 64-bit integer.
    41     Nanoseconds elapsedNano = AbsoluteToNanoseconds( *(AbsoluteTime *) &abstime );
    42     return *(uint64_t*)&elapsedNano / 1.0e9;
    43 }
    44