xref: /haiku/src/kits/locale/TimeFormat.cpp (revision 33eead339b2cf4b27f271e3f7fdae433b02bef6a)
1b2c385c0SAdrien Destugues /*
2b2c385c0SAdrien Destugues  * Copyright 2010, Adrien Destugues, pulkomandy@gmail.com
3b2c385c0SAdrien Destugues  * Distributed under the terms of the MIT License.
4b2c385c0SAdrien Destugues  */
5b2c385c0SAdrien Destugues 
6b2c385c0SAdrien Destugues #include <TimeFormat.h>
7b2c385c0SAdrien Destugues 
8b2c385c0SAdrien Destugues #include <unicode/format.h>
9b2c385c0SAdrien Destugues #include <unicode/tmutfmt.h>
10b2c385c0SAdrien Destugues #include <unicode/utypes.h>
11b2c385c0SAdrien Destugues #include <ICUWrapper.h>
12b2c385c0SAdrien Destugues 
13b2c385c0SAdrien Destugues status_t BTimeFormat::Format(int64 number, BString* buffer) const
14b2c385c0SAdrien Destugues {
15b2c385c0SAdrien Destugues 	// create time unit amount instance - a combination of Number and time unit
16b2c385c0SAdrien Destugues 	UErrorCode status = U_ZERO_ERROR;
17*33eead33SAdrien Destugues 
18*33eead33SAdrien Destugues 	int64 days, hours, minutes, seconds, remainder;
19*33eead33SAdrien Destugues 
20*33eead33SAdrien Destugues 	days = number / (24 * 3600);
21*33eead33SAdrien Destugues 	remainder = number % (24 * 3600);
22*33eead33SAdrien Destugues 
23*33eead33SAdrien Destugues 	hours = remainder / 3600;
24*33eead33SAdrien Destugues 	remainder %= 3600;
25*33eead33SAdrien Destugues 
26*33eead33SAdrien Destugues 	minutes = remainder / 60;
27*33eead33SAdrien Destugues 	remainder %= 60;
28*33eead33SAdrien Destugues 
29*33eead33SAdrien Destugues 	seconds = remainder;
30*33eead33SAdrien Destugues 
31b2c385c0SAdrien Destugues 	TimeUnitFormat* format = new TimeUnitFormat(status);
32b2c385c0SAdrien Destugues 	UnicodeString formatted;
33*33eead33SAdrien Destugues 	Formattable formattable;
34*33eead33SAdrien Destugues 	BStringByteSink bbs(buffer);
35*33eead33SAdrien Destugues 
36b2c385c0SAdrien Destugues 	if (!U_SUCCESS(status)) {
37b2c385c0SAdrien Destugues 		delete format;
38b2c385c0SAdrien Destugues 		return B_ERROR;
39b2c385c0SAdrien Destugues 	}
40b2c385c0SAdrien Destugues 
41*33eead33SAdrien Destugues 	if (days) {
42*33eead33SAdrien Destugues 		TimeUnitAmount* daysAmount = new TimeUnitAmount(days,
43*33eead33SAdrien Destugues 			TimeUnit::UTIMEUNIT_DAY, status);
44b2c385c0SAdrien Destugues 
45*33eead33SAdrien Destugues 		formattable.adoptObject(daysAmount);
46*33eead33SAdrien Destugues 		formatted = ((icu_4_2::Format*)format)->format(formattable, formatted,
47*33eead33SAdrien Destugues 			status);
48*33eead33SAdrien Destugues 	}
49*33eead33SAdrien Destugues 
50*33eead33SAdrien Destugues 	if (hours) {
51*33eead33SAdrien Destugues 		TimeUnitAmount* hoursAmount = new TimeUnitAmount(hours,
52*33eead33SAdrien Destugues 			TimeUnit::UTIMEUNIT_HOUR, status);
53*33eead33SAdrien Destugues 
54*33eead33SAdrien Destugues 		formattable.adoptObject(hoursAmount);
55*33eead33SAdrien Destugues 		if (days)
56*33eead33SAdrien Destugues 			formatted.append(", ");
57*33eead33SAdrien Destugues 		formatted = ((icu_4_2::Format*)format)->format(formattable, formatted,
58*33eead33SAdrien Destugues 			status);
59*33eead33SAdrien Destugues 	}
60*33eead33SAdrien Destugues 
61*33eead33SAdrien Destugues 	if (minutes) {
62*33eead33SAdrien Destugues 		TimeUnitAmount* minutesAmount = new TimeUnitAmount(minutes,
63*33eead33SAdrien Destugues 			TimeUnit::UTIMEUNIT_MINUTE, status);
64*33eead33SAdrien Destugues 
65*33eead33SAdrien Destugues 		formattable.adoptObject(minutesAmount);
66*33eead33SAdrien Destugues 		if (days || hours)
67*33eead33SAdrien Destugues 			formatted.append(", ");
68*33eead33SAdrien Destugues 		formatted = ((icu_4_2::Format*)format)->format(formattable, formatted,
69*33eead33SAdrien Destugues 			status);
70*33eead33SAdrien Destugues 	}
71*33eead33SAdrien Destugues 
72*33eead33SAdrien Destugues 
73*33eead33SAdrien Destugues 	if (seconds || (minutes == 0 && hours == 0 && days == 0)) {
74*33eead33SAdrien Destugues 		TimeUnitAmount* secondsAmount = new TimeUnitAmount(seconds,
75*33eead33SAdrien Destugues 			TimeUnit::UTIMEUNIT_SECOND, status);
76*33eead33SAdrien Destugues 
77*33eead33SAdrien Destugues 		formattable.adoptObject(secondsAmount);
78*33eead33SAdrien Destugues 		if (days || hours || minutes)
79*33eead33SAdrien Destugues 			formatted.append(", ");
80*33eead33SAdrien Destugues 		formatted = ((icu_4_2::Format*)format)->format(formattable, formatted,
81*33eead33SAdrien Destugues 			status);
82*33eead33SAdrien Destugues 	}
83b2c385c0SAdrien Destugues 	formatted.toUTF8(bbs);
84b2c385c0SAdrien Destugues 
85*33eead33SAdrien Destugues 
86b2c385c0SAdrien Destugues 	delete format;
87b2c385c0SAdrien Destugues 	return B_OK;
88b2c385c0SAdrien Destugues }
89