1

I need to display date and time based on the given lat/long in the app. I worked with Google API to get the timezone ID, but it should work offline too. Is there any way to get timezone without being connected to the internet?

Michal
  • 15,429
  • 10
  • 73
  • 104
  • http://stackoverflow.com/questions/4632100/figure-out-time-by-latitude-longitude/4633247#4633247 this is relevant. – Manpreet Singh May 29 '15 at 11:39
  • http://stackoverflow.com/questions/16086962/how-to-get-a-time-zone-from-a-location-using-latitude-and-longitude-coordinates this is also relevant.. – Nilesh Patel May 29 '15 at 11:49
  • 1
    Ridiculous, this is not a duplicate of that question. It's specifically about iOS and this question comes up first on Google and have no good answers for 2019 (Swift, modern iOS versions). The linked question doesn't have solutions for iOS. – mojuba May 27 '19 at 18:05

1 Answers1

3

Try this one :

CLLocation *location = [[CLLocation alloc] initWithLatitude:50.449846
                                                  longitude:30.523629];

NSTimeZone *timeZone = [[APTimeZones sharedInstance] timeZoneWithLocation:location];
NSLog(@"%@", timeZone);

Find Library Here : https://github.com/Alterplay/APTimeZones


You can also do it by code (answered by https://stackoverflow.com/a/27054583/3202193):

CLLocation *currentLocaiton = [[CLLocation alloc] initWithLatitude:latitude longitude:longitude];

[geoCoder reverseGeocodeLocation:currentLocaiton completionHandler:^(NSArray *placemarks, NSError *error) {

    if (error == nil && [placemarks count] > 0) {

        placeMark = [placemarks lastObject];
         NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"identifier = \"[a-z]*\\/[a-z]*_*[a-z]*\"" options:NSRegularExpressionCaseInsensitive error:NULL];
        NSTextCheckingResult *newSearchString = [regex firstMatchInString:[placeMark description] options:0 range:NSMakeRange(0, [placeMark.description length])];
        NSString *substr = [placeMark.description substringWithRange:newSearchString.range];
        NSLog(@"timezone id %@",substr); 

    }
Community
  • 1
  • 1
Ashish Kakkad
  • 23,586
  • 12
  • 103
  • 136
  • +1 for the APTimeZones lib, it has offline database of timezones and work with it. But may be inaccurate in some cases, look at Issues tab of the project. Problems may arise with daylight saving timezone change, and another cases – Alexander Tkachenko May 29 '15 at 11:47
  • APTimeZones does a nearest-point lookup, which is often going to be wrong. See their [issue #2](https://github.com/Alterplay/APTimeZones/issues/2) and the part of [this answer](http://stackoverflow.com/a/16086964/634824) with the diagram of the two squares. – Matt Johnson-Pint May 29 '15 at 16:53