将 iPhone 时间与服务器同步的最佳方法
我有一个客户端服务器模型,其中服务器将发送一些带有日期参数的数据,iPhone 需要渲染它。服务器和 iPhone 将驻留在不同的时区。所以我需要同步两者的时间。假设用户在“07/18/2011/04 /45/EDT”(服务器时间)完成了一些活动。从 iPhone 端,我需要将此日期字符串转换为 NSDate 对象。
实现这一目标的最佳方法是什么。
谢谢
I have a client server model where server will send some data which has date parameters and iPhone will need to render it. Server and iPhone will resides in different timezones. So I need to synchronize times in both. Let say user has done some activity at "07/18/2011/04 /45/EDT" (server time). From the iPhone end I need to convert this date string into a NSDate object.
What is the best way to achieve this.
Thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
抱歉回复晚了,只是在谷歌上查找“NSDate Sync to Server”并找到了这里。我遇到了同样的问题,并写了这个有用的小类别。
它可以仅通过单个 HTTP 请求(每个会话)返回同步到任何 Web 服务器的 NSDate 对象。
https://github.com/freak4pc/NSDate-ServerDate
如果您有任何疑问,请随时让我知道。
Sorry for the late response, just looked for "NSDate Sync to Server" on google and got here. I got stuck with the same issue and written this nice little category that helps.
It can return an NSDate object synced to any web server with only a single HTTP Request (per-session).
https://github.com/freak4pc/NSDate-ServerDate
If you have any questions feel free to let me know.
如果您希望服务器和客户端的时间相同,请使用 NTP(网络时间协议)同步两者。有一个 obj-c ntp 客户端,位于 http://code.google.com/p/ios-ntp/source/browse/trunk/ntpA.xcodeproj/project.pbxproj?spec=svn25&r=25
如上所述,aways 将时间存储/发送为unix 时间戳(自纪元UTC 以来的秒数)并根据需要转换为时区。
If you want time to be the same on server and client, synchronize both with NTP (network time protocol). There is an obj-c ntp client available at http://code.google.com/p/ios-ntp/source/browse/trunk/ntpA.xcodeproj/project.pbxproj?spec=svn25&r=25
As suggested above, aways store / send times as unix timestamps (seconds since the epoch UTC) and convert to timezones as necessary.
您绝对可以通过以下方式实现此目的:
1)当您第一次从服务器获取数据时,然后将日期值作为“”(空白)传递。这样,服务器就会识别出用户需要所有数据。
2) 在上述服务中,请在服务器网络服务中添加“日期”标签。所以,当你获取其他数据时,你也可以获取服务器日期。因此,将此服务器日期存储在本地。
3)下次,当您获取其他数据时,请传递此存储的日期(将采用服务器格式和时区),以便服务器仅向您提供在该数据之后新添加或修改的那些数据。
4)这样就可以处理日期问题了。而且本地时间或时区也不会有问题。
请查看上述要点,如有任何困难疑问请告诉我。
我已经在我的大部分应用程序中实现了这一点,并且运行良好。
对于UTC日期,您可以使用以下功能:
希望对您有所帮助。
谢谢。
You can definitely achieve this in following way:
1) When you fetch data from server first time, then pass the date value as "" (blank). So that, server will identify that, user require all data.
2) In above service, please add "Date" tag in server web-service. So, when you get other data, you can also get the server date. So, store this server date in local.
3) Next time, when you get other data then pass this stored date (which will be in server format and timezone), so that server will give you only those data which are newly added or modified after this data.
4) This way you can handle the date problem. And also there will be no issue with local time or time zone.
Please review above point and let me know in case of any query of difficulty.
I have implement this in most of my application and it's working fine.
For UTC Date, you can use below function:
I hope, it will be helpful to you.
Thanks.