-
Notifications
You must be signed in to change notification settings - Fork 2
/
NSTimeZone+ISCLLocation.m
315 lines (260 loc) · 8.04 KB
/
NSTimeZone+ISCLLocation.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
//
// NSTimeZone+ISCLLocation.m
// ISKit
//
// Created by Felix Schwarz on 30.03.15.
// Copyright (c) 2015 IOSPIRIT GmbH. All rights reserved.
//
#import "NSTimeZone+ISCLLocation.h"
// Keys used in lookup table
static NSString *kISTZCLCountryCodeKey = @"CountryCode";
static NSString *kISTZCLCoordinatesKey = @"Coordinates";
static NSString *kISTZCLTimeZoneNameKey = @"TimeZone";
// Global lookup table
static NSDictionary *sISTimeZoneLocationDict = nil;
static NSMutableDictionary *sISTimeZoneLocationCacheDict = nil;
static NSMutableDictionary *sISTimeZoneCountryCodeCacheDict = nil;
// Class to help find the bundle from which this category originates to automatically locate the "zone.tab" file
@interface ISNSTimeZoneCLLocationCategoryFinder : NSObject
@end
@implementation ISNSTimeZoneCLLocationCategoryFinder
@end
// Implementation
@implementation NSTimeZone (ISCLLocation)
#pragma mark - Tools
+ (double)IS_degreesFromISO6709String:(NSString *)isoString
{
double resultDegrees = 0;
NSString *plusMinusString=nil, *degreesString=nil, *minutesString=nil, *secondsString=nil;
/*
# 2. Latitude and longitude of the area's principal location
# in ISO 6709 sign-degrees-minutes-seconds format,
# either +-DDMM+-DDDMM or +-DDMMSS+-DDDMMSS,
# first latitude (+ is north), then longitude (+ is east).
*/
if (isoString.length >= 5)
{
plusMinusString = [isoString substringToIndex:1];
switch (isoString.length)
{
case 5:
// +-DDMM
degreesString = [isoString substringWithRange:NSMakeRange(1, 2)];
minutesString = [isoString substringWithRange:NSMakeRange(3, 2)];
break;
case 6:
// +-DDDMM
degreesString = [isoString substringWithRange:NSMakeRange(1, 3)];
minutesString = [isoString substringWithRange:NSMakeRange(4, 2)];
break;
case 7:
// +-DDMMSS
degreesString = [isoString substringWithRange:NSMakeRange(1, 2)];
minutesString = [isoString substringWithRange:NSMakeRange(3, 2)];
secondsString = [isoString substringWithRange:NSMakeRange(5, 2)];
break;
case 8:
// +-DDDMMSS
degreesString = [isoString substringWithRange:NSMakeRange(1, 3)];
minutesString = [isoString substringWithRange:NSMakeRange(4, 2)];
secondsString = [isoString substringWithRange:NSMakeRange(6, 2)];
break;
}
if (degreesString!=nil)
{
resultDegrees += [degreesString doubleValue];
}
if (minutesString!=nil)
{
resultDegrees += ([minutesString doubleValue]/(double)60.0);
}
if (secondsString!=nil)
{
resultDegrees += ([secondsString doubleValue]/(double)3600.0);
}
if ([plusMinusString isEqual:@"-"])
{
resultDegrees *= (double)-1.0;
}
}
return (resultDegrees);
}
+ (NSDictionary *)IS_recordsByTimeZoneFromZoneTabFile:(NSURL *)zoneTabURL onlyZoneName:(NSString *)onlyZoneName
{
NSString *zoneTabContents = nil;
NSError *error = nil;
NSMutableDictionary *recordsByTimeZone = nil;
if (zoneTabURL == nil)
{
@synchronized(self)
{
if (sISTimeZoneLocationDict != nil)
{
return ([[sISTimeZoneLocationDict retain] autorelease]);
}
}
zoneTabURL = [[NSBundle bundleForClass:[ISNSTimeZoneCLLocationCategoryFinder class]] URLForResource:@"zone" withExtension:@"tab"];
}
if ((zoneTabContents = [[NSString alloc] initWithContentsOfURL:zoneTabURL encoding:NSUTF8StringEncoding error:&error]) != nil)
{
NSString *tab = [NSString stringWithFormat:@"\t"];
NSArray *zoneTabLines = nil;
recordsByTimeZone = [NSMutableDictionary dictionary];
// Split in lines
zoneTabLines = [zoneTabContents componentsSeparatedByString:[NSString stringWithFormat:@"\n"]];
// Parse lines
for (NSString *zoneTabLine in zoneTabLines)
{
// Ignore comments
if (![zoneTabLine hasPrefix:@"#"])
{
NSArray *zoneTabLineColumns;
// Split in columns
if ((zoneTabLineColumns = [zoneTabLine componentsSeparatedByString:tab]) != nil)
{
NSUInteger columnCount = [zoneTabLineColumns count];
// Minimum number of columns
if (columnCount >= 3)
{
BOOL addRecord = YES;
NSString *countryCode = zoneTabLineColumns[0]; // f.ex. DE
NSString *coordinates = zoneTabLineColumns[1]; // f.ex. +5230+01322
NSString *timeZoneName = zoneTabLineColumns[2]; // f.ex. Europe/Berlin
if (onlyZoneName != nil)
{
addRecord = [timeZoneName isEqual:onlyZoneName];
}
if (addRecord)
{
// Add record
[recordsByTimeZone setObject:@{
kISTZCLCountryCodeKey : countryCode,
kISTZCLCoordinatesKey : coordinates,
kISTZCLTimeZoneNameKey : timeZoneName
} forKey:timeZoneName];
if (onlyZoneName != nil)
{
// If only this time zone's record was requested, we can skip parsing the rest
break;
}
}
}
}
}
}
[zoneTabContents release];
}
return (recordsByTimeZone);
}
+ (CLLocation *)IS_locationFromCoordinatesString:(NSString *)coordinateString
{
NSUInteger coordinatesLength = [coordinateString length];
// Extract coordinates
if (coordinatesLength > 1)
{
NSRange signRange;
signRange = [coordinateString rangeOfString:@"+" options:0 range:NSMakeRange(1, coordinatesLength-1)];
if (signRange.location == NSNotFound)
{
signRange = [coordinateString rangeOfString:@"-" options:0 range:NSMakeRange(1, coordinatesLength-1)];
}
if (signRange.location != NSNotFound)
{
double longitude, latitude;
// Convert from ISO6709 to degrees
longitude = [self IS_degreesFromISO6709String:[coordinateString substringToIndex:signRange.location]];
latitude = [self IS_degreesFromISO6709String:[coordinateString substringFromIndex:signRange.location]];
// Create a CLLocation from this
return ([[[CLLocation alloc] initWithLatitude:(CLLocationDegrees)latitude longitude:(CLLocationDegrees)longitude] autorelease]);
}
}
return (nil);
}
+ (NSDictionary *)IS_timeZoneDictForTimeZoneName:(NSString *)timeZoneName
{
if (timeZoneName != nil)
{
NSDictionary *recordsByTimeZoneDict = [[self class] IS_recordsByTimeZoneFromZoneTabFile:nil onlyZoneName:timeZoneName];
if ((timeZoneName!=nil) && (recordsByTimeZoneDict!=nil))
{
return ([recordsByTimeZoneDict objectForKey:timeZoneName]);
}
}
return (nil);
}
#pragma mark - API
+ (void)preloadTimeZoneLocationTable
{
@synchronized(self)
{
if (sISTimeZoneLocationDict==nil)
{
sISTimeZoneLocationDict = [[self IS_recordsByTimeZoneFromZoneTabFile:nil onlyZoneName:nil] retain];
}
}
}
- (NSString *)ISO3166CountryCode
{
NSString *returnCountryCode = nil;
NSDictionary *timeZoneRecord = nil;
NSString *tzName = self.name;
if (tzName == nil) { return(nil); }
@synchronized(self)
{
if (sISTimeZoneCountryCodeCacheDict == nil)
{
sISTimeZoneCountryCodeCacheDict = [[NSMutableDictionary alloc] init];
}
else
{
if ((returnCountryCode = [sISTimeZoneCountryCodeCacheDict objectForKey:tzName]) != nil)
{
return (returnCountryCode);
}
}
}
if ((timeZoneRecord = [[self class] IS_timeZoneDictForTimeZoneName:self.name]) != nil)
{
if ((returnCountryCode = [timeZoneRecord objectForKey:kISTZCLCountryCodeKey]) != nil)
{
@synchronized(self)
{
[sISTimeZoneCountryCodeCacheDict setObject:returnCountryCode forKey:tzName];
}
}
}
return (returnCountryCode);
}
- (CLLocation *)approximateLocation
{
CLLocation *returnLocation = nil;
NSDictionary *timeZoneRecord = nil;
NSString *tzName = self.name;
if (tzName == nil) { return(nil); }
@synchronized(self)
{
if (sISTimeZoneLocationCacheDict == nil)
{
sISTimeZoneLocationCacheDict = [[NSMutableDictionary alloc] init];
}
else
{
if ((returnLocation = [sISTimeZoneLocationCacheDict objectForKey:tzName]) != nil)
{
return (returnLocation);
}
}
}
if ((timeZoneRecord = [[self class] IS_timeZoneDictForTimeZoneName:tzName]) != nil)
{
if ((returnLocation = [[self class] IS_locationFromCoordinatesString:[timeZoneRecord objectForKey:kISTZCLCoordinatesKey]]) != nil)
{
@synchronized(self)
{
[sISTimeZoneLocationCacheDict setObject:returnLocation forKey:tzName];
}
}
}
return (returnLocation);
}
@end