DateUtils.m
author Jens Alfke <jens@mooseyard.com>
Thu Jul 17 13:29:34 2008 -0700 (2008-07-17)
changeset 18 d6ab9f52b4d7
parent 4 64823cdde6a5
child 19 5ade3e09a827
permissions -rw-r--r--
Initial checkin of MYAddressField, a combo-box for entering email or IM addresses.
jens@2
     1
//
jens@2
     2
//  DateUtils.m
jens@11
     3
//  MYUtilities
jens@2
     4
//
jens@2
     5
//  Created by Jens Alfke on 3/25/08.
jens@11
     6
//  Copyright 2008 Jens Alfke. 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
jens@11
    45
jens@11
    46
jens@11
    47
/*
jens@11
    48
 Copyright (c) 2008, Jens Alfke <jens@mooseyard.com>. All rights reserved.
jens@11
    49
 
jens@11
    50
 Redistribution and use in source and binary forms, with or without modification, are permitted
jens@11
    51
 provided that the following conditions are met:
jens@11
    52
 
jens@11
    53
 * Redistributions of source code must retain the above copyright notice, this list of conditions
jens@11
    54
 and the following disclaimer.
jens@11
    55
 * Redistributions in binary form must reproduce the above copyright notice, this list of conditions
jens@11
    56
 and the following disclaimer in the documentation and/or other materials provided with the
jens@11
    57
 distribution.
jens@11
    58
 
jens@11
    59
 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
jens@11
    60
 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 
jens@11
    61
 FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRI-
jens@11
    62
 BUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
jens@11
    63
 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
jens@11
    64
  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
jens@11
    65
 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 
jens@11
    66
 THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
jens@11
    67
 */