DateUtils.m
author Jens Alfke <jens@mooseyard.com>
Sat May 24 13:24:33 2008 -0700 (2008-05-24)
changeset 11 e5976864dfe9
parent 4 64823cdde6a5
child 19 5ade3e09a827
permissions -rw-r--r--
* Added BSD license to more .m files.
* Updated some copyright notices.
* Added some #imports to make the MYNetwork classes build without using the MYUtilities.pch prefix header.
     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     // 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 
    45 
    46 
    47 /*
    48  Copyright (c) 2008, Jens Alfke <jens@mooseyard.com>. All rights reserved.
    49  
    50  Redistribution and use in source and binary forms, with or without modification, are permitted
    51  provided that the following conditions are met:
    52  
    53  * Redistributions of source code must retain the above copyright notice, this list of conditions
    54  and the following disclaimer.
    55  * Redistributions in binary form must reproduce the above copyright notice, this list of conditions
    56  and the following disclaimer in the documentation and/or other materials provided with the
    57  distribution.
    58  
    59  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
    60  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 
    61  FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRI-
    62  BUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
    63  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
    64   PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
    65  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 
    66  THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    67  */