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