aboutsummaryrefslogtreecommitdiff
path: root/runtime/CFGregorianDateCreate.hpp
blob: 16345103cfe2fee31c53e74be1761d0ef2fc26b7 (plain)
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
/* Arrays of asctime-date day and month strs, rfc1123-date day and month strs, and rfc850-date day and month strs. */
static const char* kDayStrs[] = {
    "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday",
	"Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"};

static const char* kMonthStrs[] = {
	"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December",
	"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};

/* NOTE that these are ordered this way on purpose. */
static const char* kUSTimeZones[] = {"PST", "PDT", "MST", "MDT", "CST", "CDT", "EST", "EDT"};


/* extern */ const UInt8*
_CFGregorianDateCreateWithBytes(CFAllocatorRef alloc, const UInt8* bytes, CFIndex length, CFGregorianDate* date, CFTimeZoneRef* tz) {

	UInt8 buffer[256];					/* Any dates longer than this are not understood. */

	length = (length == 256) ? 255 : length;
	memmove(buffer, bytes, length);
	buffer[length] = '\0';				/* Guarantees every compare will fail if trying to index off the end. */
	
	memset(date, 0, sizeof(date[0]));
	if (tz) *tz = NULL;
	
	do {
		size_t i;
		CFIndex scan = 0;
		UInt8 c = buffer[scan];
			
		/* Skip leading whitespace */
		while (isspace(c))
			c = buffer[++scan];
		
		/* Check to see if there is a weekday up front. */
		if (!isdigit(c)) {
			
			for (i = 0; i < (sizeof(kDayStrs) / sizeof(kDayStrs[0])); i++) {
				if (!memcmp(kDayStrs[i], &buffer[scan], strlen(kDayStrs[i])))
					break;
			}

			if (i >=(sizeof(kDayStrs) / sizeof(kDayStrs[0])))
				break;
			
			scan += strlen(kDayStrs[i]);
			c = buffer[scan];
			
			while (isspace(c) || c == ',')
				c = buffer[++scan];
		}
		
		/* check for asctime where month comes first */
		if (!isdigit(c)) {
			
			for (i = 0; i < (sizeof(kMonthStrs) / sizeof(kMonthStrs[0])); i++) {
				if (!memcmp(kMonthStrs[i], &buffer[scan], strlen(kMonthStrs[i])))
					break;
			}
			
			if (i >= (sizeof(kMonthStrs) / sizeof(kMonthStrs[0])))
				break;
			
			date->month = (i % 12) + 1;
			
			scan += strlen(kMonthStrs[i]);
			c = buffer[scan];
			
			while (isspace(c))
				c = buffer[++scan];
			
			if (!isdigit(c))
				break;
		}
		
		/* Read the day of month */
		for (i = 0; isdigit(c) && (i < 2); i++) {
			date->day *= 10;
			date->day += c - '0';
			c = buffer[++scan];
		}		
		
		while (isspace(c) || c == '-')
			c = buffer[++scan];
		
		/* Not asctime so now comes the month. */
		if (date->month == 0) {
			
			if (isdigit(c)) {
				for (i = 0; isdigit(c) && (i < 2); i++) {
					date->month *= 10;
					date->month += c - '0';
					c = buffer[++scan];
				}		
			}
			else {
				for (i = 0; i < (sizeof(kMonthStrs) / sizeof(kMonthStrs[0])); i++) {
					if (!memcmp(kMonthStrs[i], &buffer[scan], strlen(kMonthStrs[i])))
						break;
				}
				
				if (i >= (sizeof(kMonthStrs) / sizeof(kMonthStrs[0])))
					break;
				
				date->month = (i % 12) + 1;
				
				scan += strlen(kMonthStrs[i]);
				c = buffer[scan];
			}
			
			while (isspace(c) || c == '-')
				c = buffer[++scan];
			
			/* Read the year */
			for (i = 0; isdigit(c) && (i < 4); i++) {
				date->year *= 10;
				date->year += c - '0';
				c = buffer[++scan];
			}
			
			while (isspace(c))
				c = buffer[++scan];
		}
		
		/* Read the hours */
		for (i = 0; isdigit(c) && (i < 2); i++) {
			date->hour *= 10;
			date->hour += c - '0';
			c = buffer[++scan];
		}		
		
		if (c != ':')
			break;
		c = buffer[++scan];
		
		/* Read the minutes */
		for (i = 0; isdigit(c) && (i < 2); i++) {
			date->minute *= 10;
			date->minute += c - '0';
			c = buffer[++scan];
		}		
		
		if (c == ':') {
			
			c = buffer[++scan];
			
			/* Read the seconds */
			for (i = 0; isdigit(c) && (i < 2); i++) {
				date->second *= 10;
				date->second += c - '0';
				c = buffer[++scan];
			}		
			c = buffer[++scan];
		}
		
		/* If haven't read the year yet, now is the time. */
		if (date->year == 0) {
			
			while (isspace(c))
				c = buffer[++scan];
			
			/* Read the year */
			for (i = 0; isdigit(c) && (i < 4); i++) {
				date->year *= 10;
				date->year += c - '0';
				c = buffer[++scan];
			}
		}
		
		if (date->year && date->year < 100) {
			
			if (date->year < 70)
				date->year += 2000;		/* My CC is still using 2-digit years! */
			else
				date->year += 1900;		/* Bad 2 byte clients */
		}
		
		while (isspace(c))
			c = buffer[++scan];

		if (c && tz) {
			
			/* If it has absolute offset, read the hours and minutes. */
			if ((c == '+') || (c == '-')) {
				
				char sign = c;
				CFTimeInterval minutes = 0, offset = 0;
				
				c = buffer[++scan];
				
				/* Read the hours */
				for (i = 0; isdigit(c) && (i < 2); i++) {
					offset *= 10;
					offset += c - '0';
					c = buffer[++scan];
				}
				
				/* Read the minutes */
				for (i = 0; isdigit(c) && (i < 2); i++) {
					minutes *= 10;
					minutes += c - '0';
					c = buffer[++scan];
				}
				
				offset *= 60;
				offset += minutes;

				if (sign == '-') offset *= -60;
				else offset *= 60;
				
				*tz = CFTimeZoneCreateWithTimeIntervalFromGMT(alloc, offset);
			}
			
			/* If it's not GMT/UT time, need to parse the alpha offset. */
			else if (!strncmp((const char*)(&buffer[scan]), "UT", 2)) {
				*tz = CFTimeZoneCreateWithTimeIntervalFromGMT(alloc, 0);
				scan += 2;
			}
				
			else if (!strncmp((const char*)(&buffer[scan]), "GMT", 3)) {
				*tz = CFTimeZoneCreateWithTimeIntervalFromGMT(alloc, 0);
				scan += 3;
			}
			
			else if (isalpha(c)) {
				
				UInt8 next = buffer[scan + 1];
				
				/* Check for military time. */
				if ((c != 'J') && (!next || isspace(next) || (next == '*'))) {
					
					if (c == 'Z')
						*tz = CFTimeZoneCreateWithTimeIntervalFromGMT(alloc, 0);
					
					else {

						CFTimeInterval offset = (c < 'N') ? (c - 'A' + 1) : ('M' - c);
					
						offset *= 60;
						
						if (next == '*') {
							scan++;
							offset = (offset < 0) ? offset - 30 : offset + 30;
						}
						
						offset *= 60;
						
						*tz = CFTimeZoneCreateWithTimeIntervalFromGMT(alloc, 0);
					}
				}
					
				else {
					
					for (i = 0; i < (sizeof(kUSTimeZones) / sizeof(kUSTimeZones[0])); i++) {
						
						if (!memcmp(kUSTimeZones[i], &buffer[scan], strlen(kUSTimeZones[i]))) {
							
							*tz = CFTimeZoneCreateWithTimeIntervalFromGMT(alloc, (-8 + (i >> 2) + (i & 0x1)) * 3600);
							
							scan += strlen(kUSTimeZones[i]);
							
							break;
						}
					}
				}
			}				
		}
		
		if (!CFGregorianDateIsValid(*date, kCFGregorianAllUnits))
			break;
		
		return bytes + scan;
			
	} while (1);
	
	memset(date, 0, sizeof(date[0]));
	if (tz) {
		if (*tz) CFRelease(*tz);
		*tz = NULL;
	}
	
	return bytes;
}

/* extern */ CFIndex
_CFGregorianDateCreateWithString(CFAllocatorRef alloc, CFStringRef str, CFGregorianDate* date, CFTimeZoneRef* tz) {
	
	UInt8 buffer[256];					/* Any dates longer than this are not understood. */
	CFIndex length = CFStringGetLength(str);
	CFIndex result = 0;
	
	CFStringGetBytes(str, CFRangeMake(0, length), kCFStringEncodingASCII, 0, FALSE, buffer, sizeof(buffer), &length);
	
	if (length)
		result = _CFGregorianDateCreateWithBytes(alloc, buffer, length, date, tz) - buffer;
	
	else {
		memset(date, 0, sizeof(date[0]));
		if (tz) *tz = NULL;
	}
	
	return result;
}