如何计算两个位置之间的时间差 [英] How to calculate the time difference between two locations
问题描述
我在计算两个时区之间的时差时遇到问题。
I am having a problem with calculating time difference between two timeZones.
如果我在A位置,我知道纬度和经度以及当前时间。
我去位置BI知道纬度和经度和当前时间。
If I am at location A I know the latitude and longitude and the current time. I go to location B I know the latitude and longitude and the current time.
如何计算两个当前点之间的时间差.. )
How to calculate the time difference between the two current points .. (in UTC)
推荐答案
首先得到一个数据库或库,转换lat / long得到国家和州/省。然后使用NSTimeZone类检索特定国家/地区的时区。您可以从许多网站购买此类数据库,即:
http://www.ip2location.com/
Firstly get a database or library that converts lat/long to get the country and state/province. Then use the NSTimeZone class to retrieve the timezone for a particular country. You can purchase such databases from many sites, ie: http://www.ip2location.com/
要进行实际的转换,您可以这样做:
To do the actual conversion you would do something like this:
NSTimeZone *sourceTimeZone =
[NSTimeZone timeZoneWithAbbreviation: @"GMT"];
NSTimeZone *destinationTimeZone =
[NSTimeZone timeZoneWithAbbreviation: @"EST"];
NSInteger sourceSeconds =
[sourceTimeZone secondsFromGMTForDate:date];
NSInteger destinationSeconds =
[destinationTimeZone secondsFromGMTForDate:date];
NSTimeInterval interval = destinationSeconds - sourceSeconds;
这篇关于如何计算两个位置之间的时间差的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!