XMPPDateTimeProfiles.m
8.64 KB
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
#import "XMPPDateTimeProfiles.h"
#import "NSNumber+XMPP.h"
#if ! __has_feature(objc_arc)
#warning This file must be compiled with ARC. Use -fobjc-arc flag (or convert project to ARC).
#endif
static NSString * const kXEP0082SharedDateFormatterKey = @"xep0082_shared_date_formatter_key";
@interface XMPPDateTimeProfiles (PrivateAPI)
+ (NSDate *)parseDateTime:(NSString *)dateTimeStr withMandatoryTimeZone:(BOOL)mandatoryTZ;
@end
@implementation XMPPDateTimeProfiles
/**
* The following acronyms and characters are used from XEP-0082 to represent time-related concepts:
*
* CCYY four-digit year portion of Date
* MM two-digit month portion of Date
* DD two-digit day portion of Date
* - ISO 8601 separator among Date portions
* T ISO 8601 separator between Date and Time
* hh two-digit hour portion of Time (00 through 23)
* mm two-digit minutes portion of Time (00 through 59)
* ss two-digit seconds portion of Time (00 through 59)
* : ISO 8601 separator among Time portions
* . ISO 8601 separator between seconds and milliseconds
* sss fractional second addendum to Time (MAY contain any number of digits)
* TZD Time Zone Definition (either "Z" for UTC or "(+|-)hh:mm" for a specific time zone)
*
**/
+ (NSDate *)parseDate:(NSString *)dateStr
{
if ([dateStr length] < 10) return nil;
// The Date profile defines a date without including the time of day.
// The lexical representation is as follows:
//
// CCYY-MM-DD
//
// Example:
//
// 1776-07-04
NSDateFormatter *df = [self threadDateFormatter];
[df setFormatterBehavior:NSDateFormatterBehavior10_4]; // Use unicode patterns (as opposed to 10_3)
[df setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]];
[df setDateFormat:@"yyyy-MM-dd"];
NSDate *result = [df dateFromString:dateStr];
return result;
}
+ (NSDate *)parseTime:(NSString *)timeStr
{
// The Time profile is used to specify an instant of time that recurs (e.g., every day).
// The lexical representation is as follows:
//
// hh:mm:ss[.sss][TZD]
//
// The Time Zone Definition is optional; if included, it MUST be either UTC (denoted by addition
// of the character 'Z' to the end of the string) or some offset from UTC (denoted by addition
// of '[+|-]' and 'hh:mm' to the end of the string).
//
// Examples:
//
// 16:00:00
// 16:00:00Z
// 16:00:00+07:00
// 16:00:00.123
// 16:00:00.123Z
// 16:00:00.123+07:00
// Extract the current day so the result can be on the current day.
// Why do we bother doing this?
//
// First, it is rather intuitive.
// Second, if we don't we risk being on a date with a conflicting DST (daylight saving time).
//
// For example, -0800 instead of the current -0700.
// This can be rather confusing when printing the result.
NSDateFormatter *df = [self threadDateFormatter];
[df setFormatterBehavior:NSDateFormatterBehavior10_4]; // Use unicode patterns (as opposed to 10_3)
[df setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]];
[df setDateFormat:@"yyyy-MM-dd"];
NSString *today = [df stringFromDate:[NSDate date]];
NSString *dateTimeStr = [NSString stringWithFormat:@"%@T%@", today, timeStr];
return [self parseDateTime:dateTimeStr withMandatoryTimeZone:NO];
}
+ (NSDate *)parseDateTime:(NSString *)dateTimeStr
{
// The DateTime profile is used to specify a non-recurring moment in time to an accuracy of seconds (or,
// optionally, fractions of a second). The format is as follows:
//
// CCYY-MM-DDThh:mm:ss[.sss]TZD
//
// The Time Zone Definition is mandatory and MUST be either UTC (denoted by addition of the character 'Z'
// to the end of the string) or some offset from UTC (denoted by addition of '[+|-]' and 'hh:mm' to the
// end of the string).
//
// Examples:
//
// 1969-07-21T02:56:15Z
// 1969-07-20T21:56:15-05:00
// 1969-07-21T02:56:15.123Z
// 1969-07-20T21:56:15.123-05:00
return [self parseDateTime:dateTimeStr withMandatoryTimeZone:YES];
}
+ (NSDate *)parseDateTime:(NSString *)dateTimeStr withMandatoryTimeZone:(BOOL)mandatoryTZ
{
if ([dateTimeStr length] < 19) return nil;
// The DateTime profile is used to specify a non-recurring moment in time to an accuracy of seconds (or,
// optionally, fractions of a second). The format is as follows:
//
// CCYY-MM-DDThh:mm:ss[.sss]{TZD}
//
// Examples:
//
// 1969-07-21T02:56:15
// 1969-07-21T02:56:15Z
// 1969-07-20T21:56:15-05:00
// 1969-07-21T02:56:15.123
// 1969-07-21T02:56:15.123Z
// 1969-07-20T21:56:15.123-05:00
BOOL hasMilliseconds = NO;
BOOL hasTimeZoneInfo = NO;
BOOL hasTimeZoneOffset = NO;
NSInteger fractionalDigits = 0;
if ([dateTimeStr length] > 19)
{
unichar c = [dateTimeStr characterAtIndex:19];
// Check for optional milliseconds
if (c == '.')
{
hasMilliseconds = YES;
// At least one fractional digit?
if ([dateTimeStr length] < 21) return nil;
}
// Check for optional time zone info, which is at the last char (Z), or the
// char 6 chars from the end
if ([dateTimeStr characterAtIndex:[dateTimeStr length] - 1] == 'Z')
{
hasTimeZoneInfo = YES;
hasTimeZoneOffset = NO;
if (hasMilliseconds)
{
// 1969-07-21T02:56:15.1234Z -> 25 - 1 - 20 = 4
fractionalDigits = [dateTimeStr length] - 1 - 20;
}
}
else
{
c = [dateTimeStr characterAtIndex:[dateTimeStr length] - 6];
if (c == '+' || c == '-')
{
hasTimeZoneInfo = YES;
hasTimeZoneOffset = YES;
if (hasMilliseconds)
{
// 1969-07-21T02:56:15.1234+00:00 -> 30 - 6 - 20 = 4
fractionalDigits = [dateTimeStr length] - 6 - 20;
}
}
else if (hasMilliseconds)
{
// 1969-07-21T02:56:15.1234 -> 24 - 20 = 4
fractionalDigits = [dateTimeStr length] - 20;
}
}
}
if (mandatoryTZ && !hasTimeZoneInfo) return nil;
NSDateFormatter *df = [self threadDateFormatter];
[df setFormatterBehavior:NSDateFormatterBehavior10_4]; // Use unicode patterns (as opposed to 10_3)
[df setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]];
[df setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss"];
NSDate *result = nil;
NSString *dateAndTime = [dateTimeStr substringToIndex:19];
NSString *fraction = fractionalDigits != 0 ? [NSString stringWithFormat:@"0%@", [dateTimeStr substringWithRange:NSMakeRange(19, fractionalDigits + 1)]] : nil;
if (hasTimeZoneInfo && !hasTimeZoneOffset)
{
[df setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
result = [df dateFromString:dateAndTime];
}
else if (hasTimeZoneInfo && hasTimeZoneOffset)
{
NSString *timeZone = [dateTimeStr substringFromIndex:[dateTimeStr length] - 6];
NSTimeZone *tz = [self parseTimeZoneOffset:timeZone];
if (tz == nil)
{
result = nil;
}
else
{
[df setTimeZone:tz];
result = [df dateFromString:dateAndTime];
}
}
else
{
result = [df dateFromString:dateAndTime];
}
if (result && fraction)
{
static NSNumberFormatter *numberFormatter = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
numberFormatter = [[NSNumberFormatter alloc] init];
[numberFormatter setFormatterBehavior:NSNumberFormatterBehavior10_4];
[numberFormatter setDecimalSeparator:@"."];
});
NSTimeInterval fractionInterval = [[numberFormatter numberFromString:fraction] doubleValue];
NSTimeInterval current = [result timeIntervalSinceReferenceDate];
result = [NSDate dateWithTimeIntervalSinceReferenceDate:floor(current) + fractionInterval];
}
return result;
}
+ (NSTimeZone *)parseTimeZoneOffset:(NSString *)tzo
{
// The tzo value is supposed to start with '+' or '-'.
// Spec says: (+-)hh:mm
//
// hh : two-digit hour portion (00 through 23)
// mm : two-digit minutes portion (00 through 59)
if ([tzo length] != 6)
{
return nil;
}
NSString *hoursStr = [tzo substringWithRange:NSMakeRange(1, 2)];
NSString *minutesStr = [tzo substringWithRange:NSMakeRange(4, 2)];
NSUInteger hours;
if (![NSNumber xmpp_parseString:hoursStr intoNSUInteger:&hours])
return nil;
NSUInteger minutes;
if (![NSNumber xmpp_parseString:minutesStr intoNSUInteger:&minutes])
return nil;
if (hours > 23) return nil;
if (minutes > 59) return nil;
NSInteger secondsOffset = (NSInteger)((hours * 60 * 60) + (minutes * 60));
if ([tzo hasPrefix:@"-"])
{
secondsOffset = -1 * secondsOffset;
}
else if (![tzo hasPrefix:@"+"])
{
return nil;
}
return [NSTimeZone timeZoneForSecondsFromGMT:secondsOffset];
}
+ (NSDateFormatter *)threadDateFormatter {
NSMutableDictionary *currentThreadStorage = [[NSThread currentThread] threadDictionary];
NSDateFormatter *sharedDateFormatter = currentThreadStorage[kXEP0082SharedDateFormatterKey];
if (!sharedDateFormatter) {
sharedDateFormatter = [NSDateFormatter new];
currentThreadStorage[kXEP0082SharedDateFormatterKey] = sharedDateFormatter;
}
return sharedDateFormatter;
}
@end