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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
|
//=============================================================================
// File: dw_date.cpp
// Contents: Date parsing function
// Maintainer: Doug Sauder <dwsauder@fwb.gulf.net>
// WWW: http://www.fwb.gulf.net/~dwsauder/mimepp.html
//
// Copyright (c) 1996, 1997 Douglas W. Sauder
// All rights reserved.
//
// IN NO EVENT SHALL DOUGLAS W. SAUDER BE LIABLE TO ANY PARTY FOR DIRECT,
// INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF
// THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF DOUGLAS W. SAUDER
// HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// DOUGLAS W. SAUDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT
// NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
// PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN "AS IS"
// BASIS, AND DOUGLAS W. SAUDER HAS NO OBLIGATION TO PROVIDE MAINTENANCE,
// SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
//
//=============================================================================
/*
* For maximum code reuse, the functions in this file are written in C.
*/
#include <mimelib/config.h>
#include <mimelib/debug.h>
#include <ctype.h>
#include <time.h>
static int CommentLength(const char *str)
{
int ch, pos, level, quoteNext, done, len;
level = 0;
quoteNext = 0;
pos = 0;
len = 0;
ch = str[pos];
done = 0;
while (1) {
switch (ch) {
case 0:
len = pos;
done = 1;
break;
case '\\':
quoteNext = 1;
break;
case '(':
if (!quoteNext) {
++level;
}
quoteNext = 0;
break;
case ')':
if (!quoteNext) {
--level;
if (level == 0) {
len = pos + 1;
done = 1;
}
}
quoteNext = 0;
break;
default:
quoteNext = 0;
}
if (done) {
break;
}
++pos;
ch = str[pos];
}
return len;
}
/*
* ParseRfc822Date() -- Parse a date in RFC-822 (RFC-1123) format
*
* If the parsing succeeds:
* - tms is set to contain the year, month, day, hour, minute, and second
* - z is set to contain the time zone in minutes offset from UTC
* - 0 is returned
* If the parsing fails:
* - (-1) is returned
* - the information in tms and z is undefined
*/
#ifdef __cplusplus
extern "C"
#endif
int ParseRfc822Date(const char *str, struct tm *tms, int *z)
{
int pos, ch, n, sgn, numZoneDigits;
int day=1, month=0, year=1970, hour=0, minute=0, second=0, zone=0;
int isValid = 1;
if (!str) {
return -1;
}
/*
* Ignore optional day of the week.
*/
/*
* Day -- one or two digits
*/
/* -- skip over non-digits */
pos = 0;
ch = str[pos];
while (ch && !('0' <= ch && ch <= '9')) {
if (ch == '(') {
pos += CommentLength(&str[pos]);
}
else {
++pos;
}
ch = str[pos];
}
/* -- convert next one or two digits */
n = -1;
if ('0' <= ch && ch <= '9') {
n = ch - '0';
++pos;
ch = str[pos];
}
if ('0' <= ch && ch <= '9') {
n *= 10;
n += ch - '0';
++pos;
ch = str[pos];
}
if (1 <= n && n <= 31) {
day = n;
}
else {
isValid = 0;
}
/*
* Month. Use case-insensitive string compare for added robustness
*/
/* -- skip over chars to first possible month char */
while (ch && !('A' <= ch && ch <= 'S') && !('a' <= ch && ch <= 's')) {
if (ch == '(') {
pos += CommentLength(&str[pos]);
}
else {
++pos;
}
ch = str[pos];
}
/* -- convert the month name */
n = -1;
switch (ch) {
case 'A':
case 'a':
/* Apr */
if ((str[pos+1] == 'p' || str[pos+1] == 'P')
&& (str[pos+2] == 'r' || str[pos+2] == 'R')) {
n = 3;
pos += 3;
ch = str[pos];
}
/* Aug */
else if ((str[pos+1] == 'u' || str[pos+1] == 'U')
&& (str[pos+2] == 'g' || str[pos+2] == 'G')) {
n = 7;
pos += 3;
ch = str[pos];
}
break;
case 'D':
case 'd':
/* Dec */
if ((str[pos+1] == 'e' || str[pos+1] == 'E')
&& (str[pos+2] == 'c' || str[pos+2] == 'C')) {
n = 11;
pos += 3;
ch = str[pos];
}
break;
case 'F':
case 'f':
/* Feb */
if ((str[pos+1] == 'e' || str[pos+1] == 'E')
&& (str[pos+2] == 'b' || str[pos+2] == 'B')) {
n = 1;
pos += 3;
ch = str[pos];
}
break;
case 'J':
case 'j':
/* Jan */
if ((str[pos+1] == 'a' || str[pos+1] == 'A')
&& (str[pos+2] == 'n' || str[pos+2] == 'N')) {
n = 0;
pos += 3;
ch = str[pos];
}
/* Jul */
else if ((str[pos+1] == 'u' || str[pos+1] == 'U')
&& (str[pos+2] == 'l' || str[pos+2] == 'L')) {
n = 6;
pos += 3;
ch = str[pos];
}
/* Jun */
else if ((str[pos+1] == 'u' || str[pos+1] == 'U')
&& (str[pos+2] == 'n' || str[pos+2] == 'N')) {
n = 5;
pos += 3;
ch = str[pos];
}
break;
case 'M':
case 'm':
/* Mar */
if ((str[pos+1] == 'a' || str[pos+1] == 'A')
&& (str[pos+2] == 'r' || str[pos+2] == 'R')) {
n = 2;
pos += 3;
ch = str[pos];
}
/* May */
else if ((str[pos+1] == 'a' || str[pos+1] == 'A')
&& (str[pos+2] == 'y' || str[pos+2] == 'Y')) {
n = 4;
pos += 3;
ch = str[pos];
}
break;
case 'N':
case 'n':
/* Nov */
if ((str[pos+1] == 'o' || str[pos+1] == 'O')
&& (str[pos+2] == 'v' || str[pos+2] == 'V')) {
n = 10;
pos += 3;
ch = str[pos];
}
break;
case 'O':
case 'o':
/* Oct */
if ((str[pos+1] == 'c' || str[pos+1] == 'c')
&& (str[pos+2] == 't' || str[pos+2] == 'T')) {
n = 9;
pos += 3;
ch = str[pos];
}
break;
case 'S':
case 's':
/* Sep */
if ((str[pos+1] == 'e' || str[pos+1] == 'E')
&& (str[pos+2] == 'p' || str[pos+2] == 'P')) {
n = 8;
pos += 3;
ch = str[pos];
}
break;
}
if (0 <= n && n <= 11) {
month = n;
}
else {
isValid = 0;
}
/*
* Year -- two or four digits (four preferred)
*/
/* -- skip over non-digits */
while (ch && !('0' <= ch && ch <= '9')) {
if (ch == '(') {
pos += CommentLength(&str[pos]);
}
else {
++pos;
}
ch = str[pos];
}
/* -- convert up to four digits */
n = -1;
if ('0' <= ch && ch <= '9') {
n = ch - '0';
++pos;
ch = str[pos];
}
if ('0' <= ch && ch <= '9') {
n *= 10;
n += ch - '0';
++pos;
ch = str[pos];
}
if ('0' <= ch && ch <= '9') {
n *= 10;
n += ch - '0';
++pos;
ch = str[pos];
}
if ('0' <= ch && ch <= '9') {
n *= 10;
n += ch - '0';
++pos;
ch = str[pos];
}
if (n != -1) {
/* Fixed year 2000 problem (fix by tony@lasernet.globalnet.co.uk) */
if (n < 70)
n += 2000; /* When less than 70 assume after year 2000 */
else if (n <= 99)
n += 1900; /* When >69 and <100 assume 1970 to 1999 */
/* Additional check to limit valid range to 1970 to 2037 */
if ((n >= 1970) && (n < 2038))
year = n;
else
isValid = 0;
}
else {
isValid = 0;
}
/*
* Hour -- two digits
*/
/* -- skip over non-digits */
while (ch && !('0' <= ch && ch <= '9')) {
if (ch == '(') {
pos += CommentLength(&str[pos]);
}
else {
++pos;
}
ch = str[pos];
}
/* -- convert next one or two digits */
n = -1;
if ('0' <= ch && ch <= '9') {
n = ch - '0';
++pos;
ch = str[pos];
}
if ('0' <= ch && ch <= '9') {
n *= 10;
n += ch - '0';
++pos;
ch = str[pos];
}
if (0 <= n && n <= 23) {
hour = n;
}
else {
isValid = 0;
}
/*
* Minute -- two digits
*/
/* -- scan for ':' */
while (ch && ch != ':') {
if (ch == '(') {
pos += CommentLength(&str[pos]);
}
else {
++pos;
}
ch = str[pos];
}
/* -- skip over non-digits */
while (ch && !('0' <= ch && ch <= '9')) {
if (ch == '(') {
pos += CommentLength(&str[pos]);
}
else {
++pos;
}
ch = str[pos];
}
/* -- convert next one or two digits */
n = -1;
if ('0' <= ch && ch <= '9') {
n = ch - '0';
++pos;
ch = str[pos];
}
if ('0' <= ch && ch <= '9') {
n *= 10;
n += ch - '0';
++pos;
ch = str[pos];
}
if (0 <= n && n <= 59) {
minute = n;
}
else {
isValid = 0;
}
/*
* Second (optional) -- two digits
*/
/* -- scan for ':' or start of time zone */
while (ch && !(ch == ':' || ch == '+' || ch == '-' || isalpha(ch))) {
if (ch == '(') {
pos += CommentLength(&str[pos]);
}
else {
++pos;
}
ch = str[pos];
}
/* -- get the seconds, if it's there */
if (ch == ':') {
++pos;
/* -- skip non-digits */
ch = str[pos];
while (ch && !('0' <= ch && ch <= '9')) {
if (ch == '(') {
pos += CommentLength(&str[pos]);
}
else {
++pos;
}
ch = str[pos];
}
/* -- convert next one or two digits */
n = -1;
if ('0' <= ch && ch <= '9') {
n = ch - '0';
++pos;
ch = str[pos];
}
if ('0' <= ch && ch <= '9') {
n *= 10;
n += ch - '0';
++pos;
ch = str[pos];
}
if (0 <= n && n <= 59) {
second = n;
}
else {
isValid = 0;
}
/* -- scan for start of time zone */
while (ch && !(ch == '+' || ch == '-' || isalpha(ch))) {
if (ch == '(') {
pos += CommentLength(&str[pos]);
}
else {
++pos;
}
ch = str[pos];
}
}
else /* if (ch != ':') */ {
second = 0;
}
/*
* Time zone
*
* Note: According to RFC-1123, the military time zones are specified
* incorrectly in RFC-822. RFC-1123 then states that "military time
* zones in RFC-822 headers carry no information."
* Here, we follow the specification in RFC-822. What else could we
* do? Military time zones should *never* be used!
*/
sgn = 1;
numZoneDigits = 0;
switch (ch) {
case '-':
sgn = -1;
/* fall through */
case '+':
++pos;
/* -- skip non-digits */
ch = str[pos];
while (ch && !('0' <= ch && ch <= '9')) {
++pos;
ch = str[pos];
}
while( str[pos + numZoneDigits] && isdigit(str[pos + numZoneDigits] ) )
++numZoneDigits;
/* -- convert next four digits */
n = 0;
while ( numZoneDigits ) {
switch(numZoneDigits) {
case 4:
if ('0' <= ch && ch <= '9') {
n = (ch - '0')*600;
++pos;
ch = str[pos];
}
break;
case 3:
if ('0' <= ch && ch <= '9') {
n += (ch - '0')*60;
++pos;
ch = str[pos];
}
break;
case 2:
if ('0' <= ch && ch <= '9') {
n += (ch - '0')*10;
++pos;
ch = str[pos];
}
break;
case 1:
if ('0' <= ch && ch <= '9') {
n += ch - '0';
}
break;
default:
break;
}
--numZoneDigits;
}
zone = sgn*n;
break;
case 'U':
case 'u':
if (str[pos+1] == 'T' || str[pos+1] == 't') {
zone = 0;
}
break;
case 'G':
case 'g':
if ((str[pos+1] == 'M' || str[pos+1] == 'm')
&& (str[pos+2] == 'T' || str[pos+2] == 't')) {
zone = 0;
}
break;
case 'E':
case 'e':
if ((str[pos+1] == 'S' || str[pos+1] == 's')
&& (str[pos+2] == 'T' || str[pos+2] == 't')) {
zone = -300;
}
else if ((str[pos+1] == 'D' || str[pos+1] == 'd')
&& (str[pos+2] == 'T' || str[pos+2] == 't')) {
zone = -240;
}
break;
case 'C':
case 'c':
if ((str[pos+1] == 'S' || str[pos+1] == 's')
&& (str[pos+2] == 'T' || str[pos+2] == 't')) {
zone = -360;
}
else if ((str[pos+1] == 'D' || str[pos+1] == 'd')
&& (str[pos+2] == 'T' || str[pos+2] == 't')) {
zone = -300;
}
else if ((str[pos+1] == 'E' || str[pos+1] == 'e') // allow non-RFC822 "CET"
&& (str[pos+2] == 'T' || str[pos+2] == 't')) {
zone = 60;
}
else if ((str[pos+1] == 'E' || str[pos+1] == 'e') // allow non-RFC822 "CEST"
&& (str[pos+2] == 'S' || str[pos+2] == 's')
&& (str[pos+3] == 'T' || str[pos+3] == 't')) {
zone = 120;
}
break;
case 'M':
case 'm':
if ((str[pos+1] == 'S' || str[pos+1] == 's')
&& (str[pos+2] == 'T' || str[pos+2] == 't')) {
zone = -420;
}
else if ((str[pos+1] == 'D' || str[pos+1] == 'd')
&& (str[pos+2] == 'T' || str[pos+2] == 't')) {
zone = -360;
}
break;
case 'P':
case 'p':
if ((str[pos+1] == 'S' || str[pos+1] == 's')
&& (str[pos+2] == 'T' || str[pos+2] == 't')) {
zone = -480;
}
else if ((str[pos+1] == 'D' || str[pos+1] == 'd')
&& (str[pos+2] == 'T' || str[pos+2] == 't')) {
zone = -420;
}
break;
case 'Z':
/* Military time zone */
zone = 0;
break;
default:
/* Military time zone */
if ('A' <= ch && ch <= 'I') {
zone = 'A' - 1 - ch;
}
else if ('K' <= ch && ch <= 'M') {
zone = 'A' - ch;
}
else if ('N' <= ch && ch <= 'Y') {
zone = ch - 'N' + 1;
}
/* Some software doesn't set the timezone, so we default
to +/-0 so KMail isn't too strict. --dnaber@mini.gt.owl.de, 2000-06-11
else {
isValid = 0;
} */
break;
}
if (isValid) {
if (tms) {
tms->tm_year = year - 1900;
tms->tm_mon = month;
tms->tm_mday = day;
tms->tm_hour = hour;
tms->tm_min = minute;
tms->tm_sec = second;
}
if (z) {
*z = zone;
}
}
else {
if (tms) {
tms->tm_year = 70;
tms->tm_mon = 0;
tms->tm_mday = 1;
tms->tm_hour = 0;
tms->tm_min = 0;
tms->tm_sec = 0;
}
if (z) {
*z = 0;
}
}
return isValid ? 0 : -1;
}
const char* wdays[] = {
"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
};
const char* months[] = {
"Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
};
#ifdef DW_TESTING_DATEPARSER
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
const char* testStr[] = {
""
};
int main()
{
struct tm *ptms, tms1, tms2;
time_t tt;
int i, zone1, zone2;
char buf[100], sgn;
/* try a bunch of random dates */
srand(100);
for (i=0; i < 1000; ++i) {
tt = rand()*((double)0x7fffffff/RAND_MAX);
zone1 = (rand()%49 - 24)*30;
gmtime(&tt, &ptms);
tms1 = *ptms;
sgn = (zone1 >= 0) ? '+' : '-';
snprintf(buf, sizeof(buf), "%s, %2d %s %d %d%d:%d%d:%d%d %c%d%d%d%d",
wdays[tms1.tm_wday], tms1.tm_mday, months[tms1.tm_mon],
tms1.tm_year+1900,
tms1.tm_hour/10, tms1.tm_hour%10,
tms1.tm_min/10, tms1.tm_min%10,
tms1.tm_sec/10, tms1.tm_sec%10,
sgn, abs(zone1)/60/10, abs(zone1)/60%10,
abs(zone1)%60/10, abs(zone1)%60%10);
ParseRfc822Date(buf, &tms2, &zone2);
if (tms1.tm_year != tms2.tm_year) {
fprintf(stderr, "Bad year\n");
}
if (tms1.tm_mon != tms2.tm_mon) {
fprintf(stderr, "Bad month\n");
}
if (tms1.tm_mday != tms2.tm_mday) {
fprintf(stderr, "Bad day\n");
}
if (tms1.tm_hour != tms2.tm_hour) {
fprintf(stderr, "Bad hour\n");
}
if (tms1.tm_min != tms2.tm_min) {
fprintf(stderr, "Bad minute\n");
}
if (tms1.tm_sec != tms2.tm_sec) {
fprintf(stderr, "Bad second\n");
}
if (zone1 != zone2) {
fprintf(stderr, "Bad zone\n");
}
}
return 0;
}
#endif
// try to parse a date/time string given in a format not
// correctly specified in RFC822 format
// Here we detect the following format:
// "WWW MMM dd HH:MM:SS [Z] YYYY" zone is optional
// e.g.: Fri Oct 14 09:21:49 CEST 2005
// or: Tue Mar 23 18:00:02 2004
#include <string.h>
#include <stdio.h>
#ifdef __cplusplus
extern "C"
#endif
int ParseDate(const char *str, struct tm *tms, int *z)
{
if ( !str )
return -1;
size_t len = strlen(str);
if ( len < 24 ) // at least "WWW MMM dd HH:MM:SS YYYY"
return -1;
int day=1, month=0, year=1970, hour=0, minute=0, second=0, zone=0;
int i;
for (i = 0; i < 7; i++)
if ( strncmp(str, wdays[i], 3) == 0 )
break;
if ( i == 7 )
return -1;
for (i = 0; i < 12; i++)
if ( strncmp(str+4, months[i], 3) == 0 )
break;
if ( i == 12 )
return -1;
month = i;
if ( sscanf(str+8, "%d %d:%d:%d", &day, &hour, &minute, &second) != 4 )
return -1;
if ( isdigit(str[20]) ) { // year without zone info, as in ctime()
if ( sscanf(str+20, "%d", &year) != 1 )
return -1;
}
else {
if ( sscanf(str+20, "%*s %d", &year) != 1 )
return -1;
if ( strncmp(str+20, "EST" , 3) == 0 ) zone = -5 * 60;
else if ( strncmp(str+20, "EDT" , 3) == 0 ) zone = -4 * 60;
else if ( strncmp(str+20, "CST" , 3) == 0 ) zone = -6 * 60;
else if ( strncmp(str+20, "CDT" , 3) == 0 ) zone = -5 * 60;
else if ( strncmp(str+20, "MST" , 3) == 0 ) zone = -7 * 60;
else if ( strncmp(str+20, "MDT" , 3) == 0 ) zone = -6 * 60;
else if ( strncmp(str+20, "PST" , 3) == 0 ) zone = -8 * 60;
else if ( strncmp(str+20, "PDT" , 3) == 0 ) zone = -7 * 60;
else if ( strncmp(str+20, "CET" , 3) == 0 ) zone = 60;
else if ( strncmp(str+20, "CEST", 4) == 0 ) zone = 120;
}
if ( (day < 1) || (day > 31) ||
(hour < 0) || (hour > 23) ||
(minute < 0) || (minute > 59) ||
(second < 0) || (second > 59) ||
(year < 1900) )
return -1;
if ( tms ) {
tms->tm_year = year - 1900;
tms->tm_mon = month;
tms->tm_mday = day;
tms->tm_hour = hour;
tms->tm_min = minute;
tms->tm_sec = second;
}
if ( z ) *z = zone;
return 0;
}
|