DateUtils.m
author Jens Alfke <jens@mooseyard.com>
Sun May 10 18:57:43 2009 -0700 (2009-05-10)
changeset 29 8874aff14cc9
parent 11 e5976864dfe9
permissions -rw-r--r--
* Added kv*Set utility functions for handling KV grunge when mutating an NSSet property.
* Change CFMakeCollectable to NSMakeCollectable.
     1 //
     2 //  DateUtils.m
     3 //  MYUtilities
     4 //
     5 //  Created by Jens Alfke on 3/25/08.
     6 //  Copyright 2008 Jens Alfke. 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     // Adapted from http://developer.apple.com/qa/qa2004/qa1398.html
    37     // Have to do some union tricks because AbsoluteToNanoseconds
    38     // works in terms of UnsignedWide, which is a structure rather
    39     // than a proper 64-bit integer.
    40     union {
    41         uint64_t asUInt64;
    42         UnsignedWide asUWide;
    43     } t;
    44     t.asUInt64 = mach_absolute_time();
    45     t.asUWide = AbsoluteToNanoseconds(t.asUWide);
    46     return t.asUInt64 / 1.0e9;
    47 }
    48 
    49 
    50 
    51 /*
    52  Copyright (c) 2008, Jens Alfke <jens@mooseyard.com>. All rights reserved.
    53  
    54  Redistribution and use in source and binary forms, with or without modification, are permitted
    55  provided that the following conditions are met:
    56  
    57  * Redistributions of source code must retain the above copyright notice, this list of conditions
    58  and the following disclaimer.
    59  * Redistributions in binary form must reproduce the above copyright notice, this list of conditions
    60  and the following disclaimer in the documentation and/or other materials provided with the
    61  distribution.
    62  
    63  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
    64  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 
    65  FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRI-
    66  BUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
    67  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
    68   PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
    69  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 
    70  THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    71  */