Actual timestamp

We are attempting to create a real-time app with simulation and require grabbing the timestamp for referencing last close time for use in simulating to the current timestamp.


Because the user can change the device time, I'm trying to stay away from NSDate.


Any advice on an approach would be much appreciated!

Accepted Reply

ObjectHub wrote:

I suggest using CFAbsoluteTimeGetCurrent() …

CFAbsoluteTimeGetCurrent
is based on
gettimeofday
and thus will change with the system clock.

benrimmington wrote:

For real time threads, see Technical Note TN2169 …

Mach absolute time may well be the right option here, and all of the APIs you mentioned are based on Mach absolute time.

The big gotcha with these APIs is that Mach absolute time stops when the CPU sleeps. @OmniOnline, if you only need to track time while your app is in the foreground, Mach absolute time (or one of its derivatives) is your best option. Otherwise things get more complex and you should post a follow-up with more information about your goals.

Share and Enjoy

Quinn "The Eskimo!"
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Replies

For real time threads, see Technical Note TN2169: High Precision Timers in iOS / OS X


import Darwin.Mach.mach_time
let t1 = mach_absolute_time()

import Dispatch.time
let t2 = dispatch_time(DISPATCH_TIME_NOW, 0)

import CoreAudio.HostTime
let t3 = AudioGetCurrentHostTime()

import CoreVideo.CVHostTime
let t4 = CVGetCurrentHostTime()

import CoreMedia.CMSync
let t5 = CMClockConvertHostTimeToSystemUnits(CMClockGetTime(CMClockGetHostTimeClock()))


C++11 has std::chrono::high_resolution_clock::now() in <chrono>

I suggest using

CFAbsoluteTimeGetCurrent()
from CoreFoundation. It returns the current absolute time via a
CFAbsoluteTime
value (which is a double). Absolute time is expressed in terms of (fractional) seconds relative to January 1, 2001 GMT.


func CFAbsoluteTimeGetCurrent() -> CFAbsoluteTime

ObjectHub wrote:

I suggest using CFAbsoluteTimeGetCurrent() …

CFAbsoluteTimeGetCurrent
is based on
gettimeofday
and thus will change with the system clock.

benrimmington wrote:

For real time threads, see Technical Note TN2169 …

Mach absolute time may well be the right option here, and all of the APIs you mentioned are based on Mach absolute time.

The big gotcha with these APIs is that Mach absolute time stops when the CPU sleeps. @OmniOnline, if you only need to track time while your app is in the foreground, Mach absolute time (or one of its derivatives) is your best option. Otherwise things get more complex and you should post a follow-up with more information about your goals.

Share and Enjoy

Quinn "The Eskimo!"
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Hi there,


I am looking for real time since we got bug on system time user can go into device and can change time.I found similar to what nimrodgutman found


mach_timebase_info(&timebaseInfo).:-- question is what is timebaseInfo

startTime = machAbsoluteToSeconds()


func machAbsoluteToSeconds(machAbsolute: UInt64 = mach_absolute_time()) -> Double {

let nanos = Double(machAbsolute * UInt64(timebaseInfo.numer)) / Double(timebaseInfo.denom)

return nanos / 1.0e9;

// This method converts nanoseconds into seconds


let elapsed = self.machAbsoluteToSeconds() - self.startTime:-- question is elapsed time giving me difference of calcuation of seconds to nanoseconds ?? then time should be same correct??


Important Question machAbsoluteToSeconds does it give time when application is on background or when on sleep ???

For those following along at home, I responded to Peter2131’s query on the other thread they posted to.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"