jbenet / ios-ntp

SNTP implementation for iOS
http://code.google.com/p/ios-ntp/
MIT License
371 stars 112 forks source link

How to get the accurate network time after modifying the system time #64

Open fengzhonglingluandeduanfa opened 3 years ago

fengzhonglingluandeduanfa commented 3 years ago

There is a problem with the time obtained after modifying the system time. How can I use the correct network time

gavineadie commented 3 years ago

Please describe the problem in more detail. What you are doing, what do you expect, and what actually happens.

fengzhonglingluandeduanfa commented 3 years ago

For example, it's two o 'clock and I've changed the time on my phone to four o 'clock, and I've got four o 'clock via networkDate, but I want to get the correct time to be two o 'clock

gavineadie commented 3 years ago

OK, thanks. I'll take a look.

gavineadie commented 3 years ago

I ran a very quick test (on iOS 14.3+iPhone 12) after changing the system clock and the network time was still true. My log is below and it shows the client times (T1 and T4) to be wrong (set to 19:00 UTC), the server time right (about 05:30 UTC). The calculation of the offset between system time and network time is about 48653 seconds (about 13.5 hours) which is correct.

So the basic mechanism is working correctly. I'll take a deeper look tomorrow. Meanwhile would you be comfortable posting the snippet of code where you use iOS-ntp? If not, I'll post a sample. One question, silly I know, but you do have an Internet connection, right?

[s]ntp receive ..
        leap: No warning
     version: 4
        mode: server
     stratum: 1
        poll: 4
   precision: -19
       delay: 0.0
  dispersion: 0.00016784668
  timeSource: referenceDevice("Shared Memory Driver (GPS)")

     refTime: 1609392572.0576692 [date = 2020-12-31 05:29:32 +0000]

  clientT T1: 1609441225.309345 [date = 2020-12-31 19:00:25 +0000]
  serverR T2: 1609392572.2071106 [date = 2020-12-31 05:29:32 +0000]
  serverT T3: 1609392572.207123 [date = 2020-12-31 05:29:32 +0000]
  clientR T4: 1609441225.373373 [date = 2020-12-31 19:00:25 +0000]

    offset = -48653134.24217701 mSec
     delay = 0.06401562690734863 seconds
gavineadie commented 3 years ago

I did have a report, years ago, before WiFi on airliners, from an airline pilot who couldn't get network time at 50,000 feet !

fengzhonglingluandeduanfa commented 3 years ago

I don't know if I'm using it incorrectly, the offset I'm getting here is always 0, the time is always consistent with the system (wrong time)

gavineadie commented 3 years ago

I've not used this code myself for a long time (I since wrote a Swift implementation using the newer Apple Network framework) and now I realize that my test above uses that and not the GitHub code. I have a recollection that an exactly zero offset can be caused by network connectivity problems or incorrect ntp server hostnames .. but I'll need to take a look tomorrow to remind myself of such things.

fengzhonglingluandeduanfa commented 3 years ago

Thank you, I will study how to use your code, should I use the wrong

fengzhonglingluandeduanfa commented 3 years ago

Thank you very much, I change the server address from your address to time.apple.com is ok,Can't you use your own server address, Another question I found that if I went into the background (not kill application), again into the front desk will is wrong in the first few seconds

gavineadie commented 3 years ago

How does the sample app ("ntp-app") included in this project behave?

I've just run it and seen the network time stay true regardless of the setting of the system time. I know that my saying it works for me doesn't help you but I'm pretty convinced your problem is not coming from my code.

fengzhonglingluandeduanfa commented 3 years ago

Now I can directly run this project to ensure the real time, but there is an error that the app will enter the foreground for a short time after entering the background (without killing)

fengzhonglingluandeduanfa commented 3 years ago

I noticed that each time I entered the foreground the resulting transient error occurred in the following code for (NetAssociation timeAssociation in sortedArray) { if (timeAssociation.active) { if (timeAssociation.trusty) { usefulCount++; timeInterval = timeInterval + timeAssociation.offset; // NSLog(@"[%@]: %f (%d)", timeAssociation.server, timeAssociation.offset1000.0, usefulCount); } else { if (timeAssociations.count > 8) { NSLog(@"Clock•Drop: [%@]", timeAssociation.server); [timeAssociations removeObject:timeAssociation]; [timeAssociation finish]; } } timeAssociation.active Always no ,It doesn't display correctly until a refresh is yes,

destinyxxl commented 3 years ago

@fengzhonglingluandeduanfa hi,看您名字应该是同胞。他的demo跑起来服务器时间和手机时间都不对呢,跟北京时间差8个小时,请问您知道是哪里需要设置下吗?萌新请教 谢谢~