xref: /haiku/src/tests/system/libroot/os/ParseDateTest.cpp (revision d3d8b26997fac34a84981e6d2b649521de2cc45a)
1 /*
2 ** Copyright 2003, Axel Dörfler, axeld@pinc-software.de. All rights reserved.
3 ** Distributed under the terms of the OpenBeOS License.
4 */
5 
6 
7 #include <SupportDefs.h>
8 #include <parsedate.h>
9 
10 #include <stdio.h>
11 
12 // this file can also be compiled against the R5 parsedate() implementation
13 #ifndef PARSEDATE_INVALID_DATE
14 #	define PARSEDATE_INVALID_DATE 4
15 #	define PARSEDATE_MINUTE_RELATIVE_TIME 8
16 #endif
17 
18 
19 char *
20 parsedate_flags_to_string(time_t result, int flags)
21 {
22 	if (result == -1) {
23 		if (flags & PARSEDATE_INVALID_DATE)
24 			return "invalid";
25 		return "unknown";
26 	}
27 
28 	if (flags & PARSEDATE_RELATIVE_TIME) {
29 		if (flags & PARSEDATE_MINUTE_RELATIVE_TIME)
30 			return "minute relative";
31 
32 		return "day relative";
33 	}
34 
35 	return "absolute";
36 }
37 
38 
39 int
40 main(int argc, char **argv)
41 {
42 	const char *dates[] = {
43 		"last tuesday",
44 		"today",
45 		"next tuesday",
46 		"tuesday",
47 		"1976-12-15",
48 		"5.8.1976",
49 		"last hour",
50 		"1 hour",
51 		"now",
52 		"12/15/1976",
53 		"Sat, 08/23/2003",
54 		"Sun, 08/23/2003",
55 		"",
56 		"next monday 3:00",
57 		"thursday 4:42",
58 		"thursday +4:42",
59 		"this thursday 4:42",
60 		"42 minutes",
61 		"2 weeks",
62 		"next 5 minutes",
63 		"last 15 minutes",
64 		"-15 minutes",
65 		"3:33pm GMT",
66 		"Mon, June 10th, 1993 10:00:03 am GMT",
67 		NULL
68 	};
69 
70 #if 0
71 	time_t now = time(NULL);
72 	for (int i = 0; i < 500000; i++) {
73 		int flags = 0;
74 		parsedate_etc(dates[0], now, &flags);
75 	}
76 #else
77 	// this crashes the R5 version but not ours:
78 	// parsedate(NULL, -1);
79 
80 	for (int32 i = 0; dates[i]; i++) {
81 		int flags = 0;
82 		time_t result = parsedate_etc(dates[i], -1, &flags);
83 		printf("\"%s\" = %ld (%s) -> %s", dates[i], result,
84 			parsedate_flags_to_string(result, flags), result == -1 ? "-\n" : ctime(&result));
85 	}
86 #endif
87 	return 0;
88 }
89