Search code examples
objective-ccocoanstimezone

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)


Solution

  • 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;