3

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)

Jay
  • 19,649
  • 38
  • 121
  • 184
Sneha
  • 1,444
  • 15
  • 24

1 Answers1

4

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;
Jay
  • 19,649
  • 38
  • 121
  • 184
  • hey thanks for ur answer .. I mean i did this way with the timeZone.. NSTimeZone *sourceTimeZone =[NSTimeZone timeZoneWithName:[NSString stringWithFormat:@"%@",@"America/Menominee"]]; NSTimeZone *destinationTimeZone =[NSTimeZone timeZoneWithName:[NSString stringWithFormat:@"%@",@"America/Juneau"]]; NSDate *date1=[NSDate date]; NSInteger sourceSeconds = [sourceTimeZone secondsFromGMTForDate:date1]; NSInteger destinationSeconds =[destinationTimeZone secondsFromGMTForDate:date1]; NSTimeInterval interval = destinationSeconds - sourceSeconds; Thanks again – Sneha Sep 29 '09 at 09:03
  • Do you know how to calculate the day time and the night time at that location – Sneha Sep 29 '09 at 09:05
  • because the at each place the sunset and the sunrise time will be different http://www.mindspring.com/~cavu/sunset.html i have got this form where we cn actually find out the difference and all but dont know how to implement it :( – Sneha Sep 29 '09 at 09:09
  • A long time ago i did see some code online that calculates the sunrise/sunset based on longitude and latitude. Im sure you can find it if you look on google. – Jay Sep 29 '09 at 10:55
  • There you go i found it (: http://williams.best.vwh.net/sunrise_sunset_algorithm.htm – Jay Sep 29 '09 at 10:56