How to get current time on another city correctly?

筅森魡賤 提交于 2020-01-25 11:18:51

问题


I have managed to read the web service to get current time of any given city. I could get 2 important values from web service, current time (String) and the offset.

Question is How to set time of any given city correctly?

Option 1:

  • Read machine/local time
  • Calculate UTC/GMT time out of machine time
  • City time = UTC time +/- offset value
  • But then what happens when machine time is wrong? You will also got wrong time right?

Option 2:

  • Read current city time in String (2012-11-24 19:30)
  • Parse this time value and set it into Calendar
  • We got correct City time
  • But how about the next minute? Of course requesting the web service every minute to get current time is not a good solution right? Is it possible to maintain this Calendar instance keep running automatically every minute once we set it?

NB : I'm developing Android clock widget here.

Thanks


回答1:


Option 1 is far better, in my eyes. Most cell phones have amazingly accurate time as time synchronization is an integral part of GSM and CDMA. Beyond that, I would far prefer a clock to work offline than to require internet connectivity.

If you are worried about ensuring accuracy in the face of incorrect system time, consider placing a call to a web service to get the current time for verification.

This verification could be done in the background, but keep in mind that web services are not the best time sync providers. I would let anything with under 5 minute difference go as it could be due to your server being out of sync or the call taking too long.



来源:https://stackoverflow.com/questions/13547720/how-to-get-current-time-on-another-city-correctly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!