1 /* 2 * Copyright 2003-2009, Axel Dörfler, axeld@pinc-software.de. 3 * Distributed under the terms of the MIT License. 4 */ 5 6 7 #include <SupportDefs.h> 8 #include <parsedate.h> 9 10 #include <stdio.h> 11 12 13 #define ABSOLUTE 0 14 #define UNKNOWN 0 15 #define DAY_RELATIVE PARSEDATE_RELATIVE_TIME 16 #define MINUTE_RELATIVE (PARSEDATE_RELATIVE_TIME \ 17 | PARSEDATE_MINUTE_RELATIVE_TIME) 18 #define INVALID PARSEDATE_INVALID_DATE 19 20 21 char* 22 parsedate_flags_to_string(time_t result, int flags) 23 { 24 if (result == -1) { 25 if ((flags & PARSEDATE_INVALID_DATE) != 0) 26 return "invalid"; 27 return "unknown"; 28 } 29 30 if ((flags & PARSEDATE_RELATIVE_TIME) != 0) { 31 if ((flags & PARSEDATE_MINUTE_RELATIVE_TIME) != 0) 32 return "minute relative"; 33 34 return "day relative"; 35 } 36 37 return "absolute"; 38 } 39 40 41 int 42 main(int argc, char** argv) 43 { 44 time_t now = 1249471430; 45 // August 5th, 2009, 11:23:50 46 47 struct test { 48 time_t result; 49 const char* date; 50 int flags; 51 }; 52 static const test kDates[] = { 53 {1248739200, "last tuesday", DAY_RELATIVE}, 54 {1249430400, "today", DAY_RELATIVE}, 55 {1249948800, "next tuesday", DAY_RELATIVE}, 56 {1249344000, "tuesday", DAY_RELATIVE}, 57 {1249467830, "last hour", MINUTE_RELATIVE}, 58 {1249475030, "1 hour", MINUTE_RELATIVE}, 59 {now, "now", MINUTE_RELATIVE}, 60 {219456000, "1976-12-15", ABSOLUTE}, 61 {219456000, "12/15/1976", ABSOLUTE}, 62 {219456000, "1976/12/15", ABSOLUTE}, 63 {219456000, "15.12.1976", ABSOLUTE}, 64 {208051200, "5.8.1976", ABSOLUTE}, 65 {1061596800, "Sat, 08/23/2003", ABSOLUTE}, 66 {1061596800, "Sun, 08/23/2003", ABSOLUTE}, 67 {-1, "", INVALID}, 68 {1249873200, "next monday 3:00", DAY_RELATIVE}, 69 {1249533720, "thursday 4:42", DAY_RELATIVE}, 70 {1249533740, "thursday +4:42:20", DAY_RELATIVE}, 71 {1249533720, "this thursday 4:42", DAY_RELATIVE}, 72 {1249473950, "42 minutes", MINUTE_RELATIVE}, 73 {1250640000, "2 weeks", DAY_RELATIVE}, 74 {1249471730, "next 5 minutes", MINUTE_RELATIVE}, 75 {1249470530, "last 15 minutes", MINUTE_RELATIVE}, 76 {1249470530, "-15 minutes", MINUTE_RELATIVE}, 77 {1249486380, "3:33pm GMT", DAY_RELATIVE}, 78 {739706403, "Mon, June 10th, 1993 10:00:03 am GMT", ABSOLUTE}, 79 {-1, "Sat, 16 Aug Ìîñêîâñêîå âðåìÿ (çèìà)", INVALID}, 80 {739706403, "Mon, June 10th, 1993 10:00:03 am UTC", ABSOLUTE}, 81 {739699203, "Mon, June 10th, 1993 10:00:03 am CEST", ABSOLUTE}, 82 {739706403, "Mon, June 10th, 1993 10:00:03 am +0000", ABSOLUTE}, 83 {-1, "Mon, June 10th, 1993 10:00:03 am 0000", UNKNOWN}, 84 {739702803, "Mon, June 10th, 1993 10:00:03 am +0100", ABSOLUTE}, 85 {739731603, "Mon, June 10th, 1993 10:00:03 am -0700", ABSOLUTE}, 86 {739654203, "Mon, June 10th, 1993 06:00:03 am ACDT", ABSOLUTE}, 87 {-1, NULL, 0} 88 }; 89 90 bool verbose = argc > 1; 91 92 if (verbose) 93 printf("All times relative to: %s (%ld)\n", ctime(&now), now); 94 95 for (int32 i = 0; kDates[i].date; i++) { 96 int flags = 0; 97 time_t result = parsedate_etc(kDates[i].date, now, &flags); 98 99 bool failure = false; 100 if (flags != kDates[i].flags || result != kDates[i].result) 101 failure = true; 102 if (failure) { 103 printf("FAILURE: parsing time at index %ld (should be %ld, %s)\n", 104 i, kDates[i].result, 105 parsedate_flags_to_string(kDates[i].result, flags)); 106 } 107 108 if (verbose || failure) { 109 printf("\"%s\" = %ld (%s) -> %s", kDates[i].date, result, 110 parsedate_flags_to_string(result, flags), result == -1 111 ? "-\n" : ctime(&result)); 112 } 113 } 114 115 return 0; 116 } 117