Sneha
Sneha

Reputation: 1444

How to calculate the time difference between two locations

I am having a problem with calculating time difference between two timeZones.

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)

Upvotes: 3

Views: 5164

Answers (1)

Jay
Jay

Reputation: 20136

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;

Upvotes: 4

Related Questions