jens@2: // jens@2: // DateUtils.m jens@11: // MYUtilities jens@2: // jens@2: // Created by Jens Alfke on 3/25/08. jens@11: // Copyright 2008 Jens Alfke. All rights reserved. jens@2: // jens@2: jens@2: #import "DateUtils.h" jens@2: jens@2: #include jens@2: #include jens@2: #include jens@2: #include jens@2: #include jens@2: jens@2: jens@4: /** Absolute time (since 'reference date') to NSDate. 0.0 -> nil. */ jens@4: NSDate* $date( CFAbsoluteTime time ) jens@4: { jens@4: CAssert(time>=0.0 && time < 1.0e15, @"Bogus timestamp %g",time); jens@4: return time ?[NSDate dateWithTimeIntervalSinceReferenceDate: time] :nil; jens@4: } jens@4: jens@4: jens@4: /** NSDate to absolute time (since 'reference date'). nil -> 0.0 */ jens@4: CFAbsoluteTime $time( NSDate* date ) jens@4: { jens@4: return date ?[date timeIntervalSinceReferenceDate] :0.0; jens@4: } jens@4: jens@4: jens@4: jens@2: NSTimeInterval TimeIntervalSinceBoot(void) jens@2: { snej@19: // Adapted from http://developer.apple.com/qa/qa2004/qa1398.html snej@19: // Have to do some union tricks because AbsoluteToNanoseconds jens@2: // works in terms of UnsignedWide, which is a structure rather jens@2: // than a proper 64-bit integer. snej@19: union { snej@19: uint64_t asUInt64; snej@19: UnsignedWide asUWide; snej@19: } t; snej@19: t.asUInt64 = mach_absolute_time(); snej@19: t.asUWide = AbsoluteToNanoseconds(t.asUWide); snej@19: return t.asUInt64 / 1.0e9; jens@2: } jens@2: jens@11: jens@11: jens@11: /* jens@11: Copyright (c) 2008, Jens Alfke . All rights reserved. jens@11: jens@11: Redistribution and use in source and binary forms, with or without modification, are permitted jens@11: provided that the following conditions are met: jens@11: jens@11: * Redistributions of source code must retain the above copyright notice, this list of conditions jens@11: and the following disclaimer. jens@11: * Redistributions in binary form must reproduce the above copyright notice, this list of conditions jens@11: and the following disclaimer in the documentation and/or other materials provided with the jens@11: distribution. jens@11: jens@11: THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR jens@11: IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND jens@11: FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRI- jens@11: BUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES jens@11: (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR jens@11: PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN jens@11: CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF jens@11: THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. jens@11: */