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