Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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,

like image 918
barfoon Avatar asked Sep 15 '11 20:09

barfoon


2 Answers

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.

like image 144
justin Avatar answered Sep 25 '22 00:09

justin


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.

like image 20
Hot Licks Avatar answered Sep 23 '22 00:09

Hot Licks