1dd21b466SAdrien Destugues/*
2dd21b466SAdrien Destugues * Copyright 2014 Haiku, Inc.
3dd21b466SAdrien Destugues * Distributed under the terms of the MIT License.
4dd21b466SAdrien Destugues */
5dd21b466SAdrien Destugues
6dd21b466SAdrien Destugues
7dd21b466SAdrien Destugues#include "DateFormatTest.h"
8dd21b466SAdrien Destugues
9dd21b466SAdrien Destugues#include <DateFormat.h>
10dd21b466SAdrien Destugues#include <FormattingConventions.h>
11dd21b466SAdrien Destugues#include <Language.h>
129577c31bSAdrien Destugues#include <TimeFormat.h>
13ffdbd128SAkshay Agarwal#include <TimeZone.h>
14dd21b466SAdrien Destugues
15dd21b466SAdrien Destugues#include <cppunit/TestCaller.h>
16dd21b466SAdrien Destugues#include <cppunit/TestSuite.h>
17dd21b466SAdrien Destugues
18dd21b466SAdrien Destugues
19dd21b466SAdrien DestuguesDateFormatTest::DateFormatTest()
20dd21b466SAdrien Destugues{
21dd21b466SAdrien Destugues}
22dd21b466SAdrien Destugues
23dd21b466SAdrien Destugues
24dd21b466SAdrien DestuguesDateFormatTest::~DateFormatTest()
25dd21b466SAdrien Destugues{
26dd21b466SAdrien Destugues}
27dd21b466SAdrien Destugues
28dd21b466SAdrien Destugues
29b16923f7SAdrien Destuguesvoid
30b16923f7SAdrien DestuguesDateFormatTest::TestCustomFormat()
31b16923f7SAdrien Destugues{
32980b1bd0SAdrien Destugues	struct Test {
33980b1bd0SAdrien Destugues		const char* language;
34980b1bd0SAdrien Destugues		const char* formatting;
35ffdbd128SAkshay Agarwal		const char* timeZone;
36980b1bd0SAdrien Destugues		int32 fields;
37824cb460SAdrien Destugues
38980b1bd0SAdrien Destugues		BString expected;
39980b1bd0SAdrien Destugues		BString force24;
40980b1bd0SAdrien Destugues		BString force12;
41980b1bd0SAdrien Destugues	};
42824cb460SAdrien Destugues
43980b1bd0SAdrien Destugues	BString buffer;
44824cb460SAdrien Destugues
45980b1bd0SAdrien Destugues	const Test tests[] = {
46ffdbd128SAkshay Agarwal		{ "en", "en_US", "GMT+1", B_DATE_ELEMENT_HOUR | B_DATE_ELEMENT_MINUTE,
47980b1bd0SAdrien Destugues			"10:21 PM", "22:21", "10:21 PM" },
48ffdbd128SAkshay Agarwal		{ "en", "en_US", "GMT+1",
49980b1bd0SAdrien Destugues			B_DATE_ELEMENT_HOUR | B_DATE_ELEMENT_MINUTE | B_DATE_ELEMENT_SECOND,
50980b1bd0SAdrien Destugues			"10:21:18 PM", "22:21:18", "10:21:18 PM" },
51ffdbd128SAkshay Agarwal		{ "en", "en_US", "GMT+1", B_DATE_ELEMENT_HOUR | B_DATE_ELEMENT_MINUTE
52980b1bd0SAdrien Destugues			| B_DATE_ELEMENT_TIMEZONE,
53980b1bd0SAdrien Destugues			"10:21 PM GMT+1", "22:21 GMT+1", "10:21 PM GMT+1" },
54ffdbd128SAkshay Agarwal		{ "en", "en_US", "GMT+1", B_DATE_ELEMENT_HOUR | B_DATE_ELEMENT_MINUTE
55980b1bd0SAdrien Destugues			| B_DATE_ELEMENT_SECOND | B_DATE_ELEMENT_TIMEZONE,
56980b1bd0SAdrien Destugues			"10:21:18 PM GMT+1", "22:21:18 GMT+1", "10:21:18 PM GMT+1" },
57ffdbd128SAkshay Agarwal		{ "fr", "fr_FR", "GMT+1", B_DATE_ELEMENT_HOUR | B_DATE_ELEMENT_MINUTE,
58980b1bd0SAdrien Destugues			"22:21", "22:21", "10:21 PM" },
59980b1bd0SAdrien Destugues		{ NULL }
60980b1bd0SAdrien Destugues	};
61b16923f7SAdrien Destugues
62980b1bd0SAdrien Destugues	for (int i = 0; tests[i].language != NULL; i++)
63980b1bd0SAdrien Destugues	{
64980b1bd0SAdrien Destugues		NextSubTest();
65980b1bd0SAdrien Destugues
66980b1bd0SAdrien Destugues		BLanguage language(tests[i].language);
67980b1bd0SAdrien Destugues		BFormattingConventions formatting(tests[i].formatting);
68ffdbd128SAkshay Agarwal		BTimeZone timeZone(tests[i].timeZone);
695d5ec05bSAdrien Destugues		status_t result;
70980b1bd0SAdrien Destugues
71980b1bd0SAdrien Destugues		// Test default for language/formatting
725d5ec05bSAdrien Destugues		{
735d5ec05bSAdrien Destugues			BDateTimeFormat format(language, formatting);
745d5ec05bSAdrien Destugues			format.SetDateTimeFormat(B_SHORT_DATE_FORMAT, B_SHORT_TIME_FORMAT,
755d5ec05bSAdrien Destugues				tests[i].fields);
76980b1bd0SAdrien Destugues
775d5ec05bSAdrien Destugues			result = format.Format(buffer, 12345678, B_SHORT_DATE_FORMAT,
78ffdbd128SAkshay Agarwal				B_SHORT_TIME_FORMAT, &timeZone);
79980b1bd0SAdrien Destugues
805d5ec05bSAdrien Destugues			CPPUNIT_ASSERT_EQUAL(B_OK, result);
815d5ec05bSAdrien Destugues			CPPUNIT_ASSERT_EQUAL(tests[i].expected, buffer);
825d5ec05bSAdrien Destugues		}
83980b1bd0SAdrien Destugues
845d5ec05bSAdrien Destugues		// Test forced 24 hours
855d5ec05bSAdrien Destugues		{
865d5ec05bSAdrien Destugues			formatting.SetExplicitUse24HourClock(true);
875d5ec05bSAdrien Destugues			BDateTimeFormat format(language, formatting);
885d5ec05bSAdrien Destugues			format.SetDateTimeFormat(B_SHORT_DATE_FORMAT, B_SHORT_TIME_FORMAT,
895d5ec05bSAdrien Destugues				tests[i].fields);
905d5ec05bSAdrien Destugues			result = format.Format(buffer, 12345678, B_SHORT_DATE_FORMAT,
91ffdbd128SAkshay Agarwal				B_SHORT_TIME_FORMAT, &timeZone);
925d5ec05bSAdrien Destugues
935d5ec05bSAdrien Destugues			CPPUNIT_ASSERT_EQUAL(B_OK, result);
945d5ec05bSAdrien Destugues			CPPUNIT_ASSERT_EQUAL(tests[i].force24, buffer);
955d5ec05bSAdrien Destugues		}
96980b1bd0SAdrien Destugues
97980b1bd0SAdrien Destugues		// Test forced 12 hours
985d5ec05bSAdrien Destugues		{
995d5ec05bSAdrien Destugues			formatting.SetExplicitUse24HourClock(false);
1005d5ec05bSAdrien Destugues			BDateTimeFormat format(language, formatting);
1015d5ec05bSAdrien Destugues			format.SetDateTimeFormat(B_SHORT_DATE_FORMAT, B_SHORT_TIME_FORMAT,
1025d5ec05bSAdrien Destugues				tests[i].fields);
1035d5ec05bSAdrien Destugues			result = format.Format(buffer, 12345678, B_SHORT_DATE_FORMAT,
104ffdbd128SAkshay Agarwal				B_SHORT_TIME_FORMAT, &timeZone);
1055d5ec05bSAdrien Destugues
1065d5ec05bSAdrien Destugues			CPPUNIT_ASSERT_EQUAL(B_OK, result);
1075d5ec05bSAdrien Destugues			CPPUNIT_ASSERT_EQUAL(tests[i].force12, buffer);
1085d5ec05bSAdrien Destugues		}
109980b1bd0SAdrien Destugues	}
110b16923f7SAdrien Destugues}
111b16923f7SAdrien Destugues
112b16923f7SAdrien Destugues
113dd21b466SAdrien Destuguesvoid
114dd21b466SAdrien DestuguesDateFormatTest::TestFormat()
115dd21b466SAdrien Destugues{
116dd21b466SAdrien Destugues	struct Value {
1174a3a4742SAdrien Destugues		const char* language;
1184a3a4742SAdrien Destugues		const char* convention;
119ffdbd128SAkshay Agarwal		const char* timeZone;
120dd21b466SAdrien Destugues		time_t time;
1214a3a4742SAdrien Destugues		const char* shortDate;
1224a3a4742SAdrien Destugues		const char* longDate;
1234a3a4742SAdrien Destugues		const char* shortTime;
1244a3a4742SAdrien Destugues		const char* longTime;
1254a3a4742SAdrien Destugues		const char* shortDateTime;
126dd21b466SAdrien Destugues	};
127dd21b466SAdrien Destugues
128dd21b466SAdrien Destugues	static const Value values[] = {
129ffdbd128SAkshay Agarwal		{"en", "en_US", "GMT+1", 12345, "1/1/70", "January 1, 1970",
1304a3a4742SAdrien Destugues			"4:25 AM", "4:25:45 AM", "1/1/70, 4:25 AM"},
131ffdbd128SAkshay Agarwal		{"fr", "fr_FR", "GMT+1", 12345, "01/01/1970", "1 janvier 1970",
1324a3a4742SAdrien Destugues			"04:25", "04:25:45", "01/01/1970 04:25"},
133ffdbd128SAkshay Agarwal		{"fr", "fr_FR", "GMT+1", 12345678, "23/05/1970", "23 mai 1970",
1344a3a4742SAdrien Destugues			"22:21", "22:21:18", "23/05/1970 22:21"},
135dd21b466SAdrien Destugues		{NULL}
136dd21b466SAdrien Destugues	};
137dd21b466SAdrien Destugues
138dd21b466SAdrien Destugues	BString output;
139dd21b466SAdrien Destugues	status_t result;
140dd21b466SAdrien Destugues
141dd21b466SAdrien Destugues	for (int i = 0; values[i].language != NULL; i++) {
142dd21b466SAdrien Destugues		NextSubTest();
143dd21b466SAdrien Destugues
144dd21b466SAdrien Destugues		BLanguage language(values[i].language);
145dd21b466SAdrien Destugues		BFormattingConventions formatting(values[i].convention);
146ffdbd128SAkshay Agarwal		BTimeZone timeZone(values[i].timeZone);
1475d5ec05bSAdrien Destugues		BDateFormat dateFormat(language, formatting);
1485d5ec05bSAdrien Destugues		BTimeFormat timeFormat(language, formatting);
1495d5ec05bSAdrien Destugues		BDateTimeFormat dateTimeFormat(language, formatting);
150dd21b466SAdrien Destugues
151ffdbd128SAkshay Agarwal		result = dateFormat.Format(output, values[i].time, B_SHORT_DATE_FORMAT,
152ffdbd128SAkshay Agarwal			&timeZone);
153dd21b466SAdrien Destugues		CPPUNIT_ASSERT_EQUAL(B_OK, result);
154dd21b466SAdrien Destugues		CPPUNIT_ASSERT_EQUAL(BString(values[i].shortDate), output);
155dd21b466SAdrien Destugues
156ffdbd128SAkshay Agarwal		result = dateFormat.Format(output, values[i].time, B_LONG_DATE_FORMAT,
157ffdbd128SAkshay Agarwal			&timeZone);
158dd21b466SAdrien Destugues		CPPUNIT_ASSERT_EQUAL(B_OK, result);
159dd21b466SAdrien Destugues		CPPUNIT_ASSERT_EQUAL(BString(values[i].longDate), output);
1609577c31bSAdrien Destugues
161ffdbd128SAkshay Agarwal		result = timeFormat.Format(output, values[i].time, B_SHORT_TIME_FORMAT,
162ffdbd128SAkshay Agarwal			&timeZone);
1639577c31bSAdrien Destugues		CPPUNIT_ASSERT_EQUAL(B_OK, result);
1649577c31bSAdrien Destugues		CPPUNIT_ASSERT_EQUAL(BString(values[i].shortTime), output);
1659577c31bSAdrien Destugues
166ffdbd128SAkshay Agarwal		result = timeFormat.Format(output, values[i].time, B_MEDIUM_TIME_FORMAT,
167ffdbd128SAkshay Agarwal			&timeZone);
1689577c31bSAdrien Destugues		CPPUNIT_ASSERT_EQUAL(B_OK, result);
1699577c31bSAdrien Destugues		CPPUNIT_ASSERT_EQUAL(BString(values[i].longTime), output);
1704a3a4742SAdrien Destugues
1714a3a4742SAdrien Destugues		result = dateTimeFormat.Format(output, values[i].time,
172ffdbd128SAkshay Agarwal			B_SHORT_DATE_FORMAT, B_SHORT_TIME_FORMAT, &timeZone);
1734a3a4742SAdrien Destugues		CPPUNIT_ASSERT_EQUAL(B_OK, result);
1744a3a4742SAdrien Destugues		CPPUNIT_ASSERT_EQUAL(BString(values[i].shortDateTime), output);
175dd21b466SAdrien Destugues	}
176dd21b466SAdrien Destugues}
177dd21b466SAdrien Destugues
178dd21b466SAdrien Destugues
179dd21b466SAdrien Destuguesvoid
180dd21b466SAdrien DestuguesDateFormatTest::TestFormatDate()
181dd21b466SAdrien Destugues{
182dd21b466SAdrien Destugues		BLanguage language("en");
183dd21b466SAdrien Destugues		BFormattingConventions formatting("en_US");
1845d5ec05bSAdrien Destugues		BDateFormat format(language, formatting);
185dd21b466SAdrien Destugues
186dd21b466SAdrien Destugues		BString output;
187dd21b466SAdrien Destugues		status_t result;
188dd21b466SAdrien Destugues
189dd21b466SAdrien Destugues		BDate date(2014, 9, 29);
190dd21b466SAdrien Destugues
191dd21b466SAdrien Destugues		result = format.Format(output, date, B_LONG_DATE_FORMAT);
192dd21b466SAdrien Destugues		CPPUNIT_ASSERT_EQUAL(B_OK, result);
193dd21b466SAdrien Destugues		CPPUNIT_ASSERT_EQUAL(BString("September 29, 2014"), output);
194dd21b466SAdrien Destugues
195dd21b466SAdrien Destugues		// Test an invalid date - must return B_BAD_DATA
196dd21b466SAdrien Destugues		date.SetDate(2014, 29, 29);
197dd21b466SAdrien Destugues		result = format.Format(output, date, B_LONG_DATE_FORMAT);
198dd21b466SAdrien Destugues		CPPUNIT_ASSERT_EQUAL(B_BAD_DATA, result);
199dd21b466SAdrien Destugues}
200dd21b466SAdrien Destugues
201dd21b466SAdrien Destugues
202090a0d76SAdrien Destuguesvoid
203090a0d76SAdrien DestuguesDateFormatTest::TestMonthNames()
204090a0d76SAdrien Destugues{
205090a0d76SAdrien Destugues	BLanguage language("en");
206090a0d76SAdrien Destugues	BFormattingConventions formatting("en_US");
2075d5ec05bSAdrien Destugues	BDateFormat format(language, formatting);
208090a0d76SAdrien Destugues
209090a0d76SAdrien Destugues	BString buffer;
210090a0d76SAdrien Destugues	status_t result = format.GetMonthName(1, buffer);
211090a0d76SAdrien Destugues
212090a0d76SAdrien Destugues	CPPUNIT_ASSERT_EQUAL(BString("January"), buffer);
213090a0d76SAdrien Destugues	CPPUNIT_ASSERT_EQUAL(B_OK, result);
214090a0d76SAdrien Destugues
215090a0d76SAdrien Destugues	buffer.Truncate(0);
216090a0d76SAdrien Destugues	result = format.GetMonthName(12, buffer);
217090a0d76SAdrien Destugues
218090a0d76SAdrien Destugues	CPPUNIT_ASSERT_EQUAL(BString("December"), buffer);
219090a0d76SAdrien Destugues	CPPUNIT_ASSERT_EQUAL(B_OK, result);
220ed3f47feSAkshay Agarwal
221ed3f47feSAkshay Agarwal	buffer.Truncate(0);
222ed3f47feSAkshay Agarwal	result = format.GetMonthName(1, buffer, B_FULL_DATE_FORMAT);
223ed3f47feSAkshay Agarwal
224ed3f47feSAkshay Agarwal	CPPUNIT_ASSERT_EQUAL(BString("January"), buffer);
225ed3f47feSAkshay Agarwal	CPPUNIT_ASSERT_EQUAL(B_OK, result);
226ed3f47feSAkshay Agarwal
227ed3f47feSAkshay Agarwal	buffer.Truncate(0);
228ed3f47feSAkshay Agarwal	result = format.GetMonthName(12, buffer, B_FULL_DATE_FORMAT);
229ed3f47feSAkshay Agarwal
230ed3f47feSAkshay Agarwal	CPPUNIT_ASSERT_EQUAL(BString("December"), buffer);
231ed3f47feSAkshay Agarwal	CPPUNIT_ASSERT_EQUAL(B_OK, result);
232ed3f47feSAkshay Agarwal
233ed3f47feSAkshay Agarwal	buffer.Truncate(0);
234ed3f47feSAkshay Agarwal	result = format.GetMonthName(1, buffer, B_LONG_DATE_FORMAT);
235ed3f47feSAkshay Agarwal
236ed3f47feSAkshay Agarwal	CPPUNIT_ASSERT_EQUAL(BString("Jan"), buffer);
237ed3f47feSAkshay Agarwal	CPPUNIT_ASSERT_EQUAL(B_OK, result);
238ed3f47feSAkshay Agarwal
239ed3f47feSAkshay Agarwal	buffer.Truncate(0);
240ed3f47feSAkshay Agarwal	result = format.GetMonthName(12, buffer, B_LONG_DATE_FORMAT);
241ed3f47feSAkshay Agarwal
242ed3f47feSAkshay Agarwal	CPPUNIT_ASSERT_EQUAL(BString("Dec"), buffer);
243ed3f47feSAkshay Agarwal	CPPUNIT_ASSERT_EQUAL(B_OK, result);
244ed3f47feSAkshay Agarwal
245ed3f47feSAkshay Agarwal	buffer.Truncate(0);
246ed3f47feSAkshay Agarwal	result = format.GetMonthName(1, buffer, B_MEDIUM_DATE_FORMAT);
247ed3f47feSAkshay Agarwal
248ed3f47feSAkshay Agarwal	CPPUNIT_ASSERT_EQUAL(BString("Jan"), buffer);
249ed3f47feSAkshay Agarwal	CPPUNIT_ASSERT_EQUAL(B_OK, result);
250ed3f47feSAkshay Agarwal
251ed3f47feSAkshay Agarwal	buffer.Truncate(0);
252ed3f47feSAkshay Agarwal	result = format.GetMonthName(12, buffer, B_MEDIUM_DATE_FORMAT);
253ed3f47feSAkshay Agarwal
254ed3f47feSAkshay Agarwal	CPPUNIT_ASSERT_EQUAL(BString("Dec"), buffer);
255ed3f47feSAkshay Agarwal	CPPUNIT_ASSERT_EQUAL(B_OK, result);
256ed3f47feSAkshay Agarwal
257ed3f47feSAkshay Agarwal	buffer.Truncate(0);
258ed3f47feSAkshay Agarwal	result = format.GetMonthName(1, buffer, B_SHORT_DATE_FORMAT);
259ed3f47feSAkshay Agarwal
260ed3f47feSAkshay Agarwal	CPPUNIT_ASSERT_EQUAL(BString("J"), buffer);
261ed3f47feSAkshay Agarwal	CPPUNIT_ASSERT_EQUAL(B_OK, result);
262ed3f47feSAkshay Agarwal
263ed3f47feSAkshay Agarwal	buffer.Truncate(0);
264ed3f47feSAkshay Agarwal	result = format.GetMonthName(12, buffer, B_SHORT_DATE_FORMAT);
265ed3f47feSAkshay Agarwal
266ed3f47feSAkshay Agarwal	CPPUNIT_ASSERT_EQUAL(BString("D"), buffer);
267ed3f47feSAkshay Agarwal	CPPUNIT_ASSERT_EQUAL(B_OK, result);
268ed3f47feSAkshay Agarwal
269ed3f47feSAkshay Agarwal}
270ed3f47feSAkshay Agarwal
271ed3f47feSAkshay Agarwal
272ed3f47feSAkshay Agarwalvoid
273ed3f47feSAkshay AgarwalDateFormatTest::TestDayNames()
274ed3f47feSAkshay Agarwal{
275ed3f47feSAkshay Agarwal	BLanguage language("en");
276ed3f47feSAkshay Agarwal	BFormattingConventions formatting("en_US");
277ed3f47feSAkshay Agarwal	BDateFormat format(language, formatting);
278ed3f47feSAkshay Agarwal
279ed3f47feSAkshay Agarwal	BString buffer;
280ed3f47feSAkshay Agarwal	status_t result = format.GetDayName(1, buffer);
281ed3f47feSAkshay Agarwal
282ed3f47feSAkshay Agarwal	CPPUNIT_ASSERT_EQUAL(BString("Sunday"), buffer);
283ed3f47feSAkshay Agarwal	CPPUNIT_ASSERT_EQUAL(B_OK, result);
284ed3f47feSAkshay Agarwal
285ed3f47feSAkshay Agarwal	buffer.Truncate(0);
286ed3f47feSAkshay Agarwal	result = format.GetDayName(2, buffer);
287ed3f47feSAkshay Agarwal
288ed3f47feSAkshay Agarwal	CPPUNIT_ASSERT_EQUAL(BString("Monday"), buffer);
289ed3f47feSAkshay Agarwal	CPPUNIT_ASSERT_EQUAL(B_OK, result);
290ed3f47feSAkshay Agarwal
291ed3f47feSAkshay Agarwal	buffer.Truncate(0);
292ed3f47feSAkshay Agarwal	result = format.GetDayName(1, buffer, B_FULL_DATE_FORMAT);
293ed3f47feSAkshay Agarwal
294ed3f47feSAkshay Agarwal	CPPUNIT_ASSERT_EQUAL(BString("Sunday"), buffer);
295ed3f47feSAkshay Agarwal	CPPUNIT_ASSERT_EQUAL(B_OK, result);
296ed3f47feSAkshay Agarwal
297ed3f47feSAkshay Agarwal	buffer.Truncate(0);
298ed3f47feSAkshay Agarwal	result = format.GetDayName(2, buffer, B_FULL_DATE_FORMAT);
299ed3f47feSAkshay Agarwal
300ed3f47feSAkshay Agarwal	CPPUNIT_ASSERT_EQUAL(BString("Monday"), buffer);
301ed3f47feSAkshay Agarwal	CPPUNIT_ASSERT_EQUAL(B_OK, result);
302ed3f47feSAkshay Agarwal
303ed3f47feSAkshay Agarwal	buffer.Truncate(0);
304ed3f47feSAkshay Agarwal	result = format.GetDayName(1, buffer, B_LONG_DATE_FORMAT);
305ed3f47feSAkshay Agarwal
306ed3f47feSAkshay Agarwal	CPPUNIT_ASSERT_EQUAL(BString("Sun"), buffer);
307ed3f47feSAkshay Agarwal	CPPUNIT_ASSERT_EQUAL(B_OK, result);
308ed3f47feSAkshay Agarwal
309ed3f47feSAkshay Agarwal	buffer.Truncate(0);
310ed3f47feSAkshay Agarwal	result = format.GetDayName(2, buffer, B_LONG_DATE_FORMAT);
311ed3f47feSAkshay Agarwal
312ed3f47feSAkshay Agarwal	CPPUNIT_ASSERT_EQUAL(BString("Mon"), buffer);
313ed3f47feSAkshay Agarwal	CPPUNIT_ASSERT_EQUAL(B_OK, result);
314ed3f47feSAkshay Agarwal
315ed3f47feSAkshay Agarwal	buffer.Truncate(0);
316ed3f47feSAkshay Agarwal	result = format.GetDayName(1, buffer, B_MEDIUM_DATE_FORMAT);
317ed3f47feSAkshay Agarwal
318ed3f47feSAkshay Agarwal	CPPUNIT_ASSERT_EQUAL(BString("Su"), buffer);
319ed3f47feSAkshay Agarwal	CPPUNIT_ASSERT_EQUAL(B_OK, result);
320ed3f47feSAkshay Agarwal
321ed3f47feSAkshay Agarwal	buffer.Truncate(0);
322ed3f47feSAkshay Agarwal	result = format.GetDayName(2, buffer, B_MEDIUM_DATE_FORMAT);
323ed3f47feSAkshay Agarwal
324ed3f47feSAkshay Agarwal	CPPUNIT_ASSERT_EQUAL(BString("Mo"), buffer);
325ed3f47feSAkshay Agarwal	CPPUNIT_ASSERT_EQUAL(B_OK, result);
326ed3f47feSAkshay Agarwal
327ed3f47feSAkshay Agarwal	buffer.Truncate(0);
328ed3f47feSAkshay Agarwal	result = format.GetDayName(1, buffer, B_SHORT_DATE_FORMAT);
329ed3f47feSAkshay Agarwal
330ed3f47feSAkshay Agarwal	CPPUNIT_ASSERT_EQUAL(BString("S"), buffer);
331ed3f47feSAkshay Agarwal	CPPUNIT_ASSERT_EQUAL(B_OK, result);
332ed3f47feSAkshay Agarwal
333ed3f47feSAkshay Agarwal	buffer.Truncate(0);
334ed3f47feSAkshay Agarwal	result = format.GetDayName(2, buffer, B_SHORT_DATE_FORMAT);
335ed3f47feSAkshay Agarwal
336ed3f47feSAkshay Agarwal	CPPUNIT_ASSERT_EQUAL(BString("M"), buffer);
337ed3f47feSAkshay Agarwal	CPPUNIT_ASSERT_EQUAL(B_OK, result);
338090a0d76SAdrien Destugues}
339090a0d76SAdrien Destugues
34026b0a53dSAdrien Destugues
34197d07c8aSAdrien Destuguesstd::ostream& operator<<(std::ostream& stream, const BDate& date)
34297d07c8aSAdrien Destugues{
34326b0a53dSAdrien Destugues	stream << date.Year();
34426b0a53dSAdrien Destugues	stream << '-';
34526b0a53dSAdrien Destugues	stream << date.Month();
34626b0a53dSAdrien Destugues	stream << '-';
34726b0a53dSAdrien Destugues	stream << date.Day();
34897d07c8aSAdrien Destugues
34926b0a53dSAdrien Destugues	return stream;
35026b0a53dSAdrien Destugues}
35126b0a53dSAdrien Destugues
35226b0a53dSAdrien Destugues
35326b0a53dSAdrien Destuguesstd::ostream& operator<<(std::ostream& stream, const BTime& date)
35426b0a53dSAdrien Destugues{
35526b0a53dSAdrien Destugues	stream << date.Hour();
35626b0a53dSAdrien Destugues	stream << ':';
35726b0a53dSAdrien Destugues	stream << date.Minute();
35826b0a53dSAdrien Destugues	stream << ':';
35926b0a53dSAdrien Destugues	stream << date.Second();
36026b0a53dSAdrien Destugues
36126b0a53dSAdrien Destugues	return stream;
36297d07c8aSAdrien Destugues}
36397d07c8aSAdrien Destugues
36497d07c8aSAdrien Destugues
36597d07c8aSAdrien Destuguesvoid
36697d07c8aSAdrien DestuguesDateFormatTest::TestParseDate()
36797d07c8aSAdrien Destugues{
36897d07c8aSAdrien Destugues	BLanguage language("en");
36997d07c8aSAdrien Destugues	BFormattingConventions formatting("en_US");
3705d5ec05bSAdrien Destugues	BDateFormat format(language, formatting);
37197d07c8aSAdrien Destugues	BDate date;
37297d07c8aSAdrien Destugues	status_t result;
37397d07c8aSAdrien Destugues
37497d07c8aSAdrien Destugues	struct Test {
37597d07c8aSAdrien Destugues		const char* input;
37697d07c8aSAdrien Destugues		BDate output;
37797d07c8aSAdrien Destugues	};
37897d07c8aSAdrien Destugues
37997d07c8aSAdrien Destugues	static const Test tests[] = {
38097d07c8aSAdrien Destugues		{"01/01/1970", BDate(1970, 1, 1)},
38197d07c8aSAdrien Destugues		{"05/07/1988", BDate(1988, 5, 7)},
38297d07c8aSAdrien Destugues		{"07/31/2345", BDate(2345, 7, 31)},
38397d07c8aSAdrien Destugues		{NULL}
38497d07c8aSAdrien Destugues	};
38597d07c8aSAdrien Destugues
38697d07c8aSAdrien Destugues	for (int i = 0; tests[i].input != NULL; i++) {
38797d07c8aSAdrien Destugues		NextSubTest();
38897d07c8aSAdrien Destugues		result = format.Parse(tests[i].input, B_SHORT_DATE_FORMAT, date);
38997d07c8aSAdrien Destugues
39097d07c8aSAdrien Destugues		CPPUNIT_ASSERT_EQUAL(tests[i].output, date);
39197d07c8aSAdrien Destugues		CPPUNIT_ASSERT_EQUAL(B_OK, result);
39297d07c8aSAdrien Destugues	}
39397d07c8aSAdrien Destugues}
39497d07c8aSAdrien Destugues
395090a0d76SAdrien Destugues
39626b0a53dSAdrien Destuguesvoid
39726b0a53dSAdrien DestuguesDateFormatTest::TestParseTime()
39826b0a53dSAdrien Destugues{
39926b0a53dSAdrien Destugues	BLanguage language("fr");
40026b0a53dSAdrien Destugues	BFormattingConventions formatting("fr_FR");
4015d5ec05bSAdrien Destugues	BTimeFormat format(language, formatting);
40226b0a53dSAdrien Destugues	BTime date;
40326b0a53dSAdrien Destugues	status_t result;
40426b0a53dSAdrien Destugues
40526b0a53dSAdrien Destugues	struct Test {
40626b0a53dSAdrien Destugues		const char* input;
40726b0a53dSAdrien Destugues		BTime output;
40826b0a53dSAdrien Destugues	};
40926b0a53dSAdrien Destugues
41026b0a53dSAdrien Destugues	static const Test tests[] = {
41126b0a53dSAdrien Destugues		{"03:25", BTime(3, 25, 0)},
41226b0a53dSAdrien Destugues		{"16:18", BTime(16, 18, 0)},
41326b0a53dSAdrien Destugues		{"23:59", BTime(23, 59, 0)},
41426b0a53dSAdrien Destugues		{NULL}
41526b0a53dSAdrien Destugues	};
41626b0a53dSAdrien Destugues
41726b0a53dSAdrien Destugues	for (int i = 0; tests[i].input != NULL; i++) {
41826b0a53dSAdrien Destugues		NextSubTest();
41926b0a53dSAdrien Destugues		result = format.Parse(tests[i].input, B_SHORT_TIME_FORMAT, date);
42026b0a53dSAdrien Destugues
42126b0a53dSAdrien Destugues		CPPUNIT_ASSERT_EQUAL(tests[i].output, date);
42226b0a53dSAdrien Destugues		CPPUNIT_ASSERT_EQUAL(B_OK, result);
42326b0a53dSAdrien Destugues	}
42426b0a53dSAdrien Destugues}
42526b0a53dSAdrien Destugues
42626b0a53dSAdrien Destugues
427dd21b466SAdrien Destugues/*static*/ void
428dd21b466SAdrien DestuguesDateFormatTest::AddTests(BTestSuite& parent)
429dd21b466SAdrien Destugues{
430dd21b466SAdrien Destugues	CppUnit::TestSuite& suite = *new CppUnit::TestSuite("DateFormatTest");
431dd21b466SAdrien Destugues
432b16923f7SAdrien Destugues	suite.addTest(new CppUnit::TestCaller<DateFormatTest>(
433b16923f7SAdrien Destugues		"DateFormatTest::TestCustomFormat", &DateFormatTest::TestCustomFormat));
434dd21b466SAdrien Destugues	suite.addTest(new CppUnit::TestCaller<DateFormatTest>(
435dd21b466SAdrien Destugues		"DateFormatTest::TestFormat", &DateFormatTest::TestFormat));
436dd21b466SAdrien Destugues	suite.addTest(new CppUnit::TestCaller<DateFormatTest>(
437dd21b466SAdrien Destugues		"DateFormatTest::TestFormatDate", &DateFormatTest::TestFormatDate));
438090a0d76SAdrien Destugues	suite.addTest(new CppUnit::TestCaller<DateFormatTest>(
439090a0d76SAdrien Destugues		"DateFormatTest::TestMonthNames", &DateFormatTest::TestMonthNames));
440ed3f47feSAkshay Agarwal	suite.addTest(new CppUnit::TestCaller<DateFormatTest>(
441ed3f47feSAkshay Agarwal		"DateFormatTest::TestDayNames", &DateFormatTest::TestDayNames));
44297d07c8aSAdrien Destugues	suite.addTest(new CppUnit::TestCaller<DateFormatTest>(
44397d07c8aSAdrien Destugues		"DateFormatTest::TestParseDate", &DateFormatTest::TestParseDate));
44426b0a53dSAdrien Destugues	suite.addTest(new CppUnit::TestCaller<DateFormatTest>(
44526b0a53dSAdrien Destugues		"DateFormatTest::TestParseTime", &DateFormatTest::TestParseTime));
446dd21b466SAdrien Destugues
447dd21b466SAdrien Destugues	parent.addTest("DateFormatTest", &suite);
448dd21b466SAdrien Destugues}
449