barfoon
barfoon

Reputation: 28167

How is [NSDate date] calculated?

I'm using part of the current date in a scheme on a web server and I want to generate the same value on the device as on the server. In theory I thought about a case where these two values wouldn't be the same and the values on either end wouldn't match.

Which then led me to think - how, specifically, is [NSDate date] calculated when an app is running?

Edit: I guess what I'm really asking, is how does an iPhone get its time from an authoritative source? It appears it comes from a cell tower which I would assume are all synced somehow. Is this true?

Edit: Looks like it uses gettimeofday() which uses the system clock. And from Apple docs, looks like when you have the iPhone set to 'Set Automatically', it uses the correct time over the cell network.

Thanks,

Upvotes: 7

Views: 350

Answers (3)

Ivan Vučica
Ivan Vučica

Reputation: 9679

UNIX system clock, using (as other people said) gettimeofday(). Phone probably synchronizes with an NTP server such as time.apple.com - this is what desktop OS X does.

Upvotes: 1

Hot Licks
Hot Licks

Reputation: 47729

Keep in mind that your app may run on an iPod Touch, or on a phone that simply has no 3G connection. You get whatever time the phone is displaying on its top bar.

Upvotes: 1

justin
justin

Reputation: 104698

you can find out by stepping through in the debugger or simply sample it for the callstacks.

it uses gettimeofday.

as such, you can't expect this value to be equal or perfectly synchronized on two devices.

Upvotes: 3

Related Questions