views:

465

answers:

1

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)

+1  A: 

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;
Jacob
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
Do you know how to calculate the day time and the night time at that location
sneha
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
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.
Jacob
There you go i found it (: http://williams.best.vwh.net/sunrise_sunset_algorithm.htm
Jacob