xref: /haiku/src/kits/locale/TimeFormat.cpp (revision b2c385c0727b400b447efa535a3a67042bd52af8)
1*b2c385c0SAdrien Destugues /*
2*b2c385c0SAdrien Destugues  * Copyright 2010, Adrien Destugues, pulkomandy@gmail.com
3*b2c385c0SAdrien Destugues  * Distributed under the terms of the MIT License.
4*b2c385c0SAdrien Destugues  */
5*b2c385c0SAdrien Destugues 
6*b2c385c0SAdrien Destugues #include <TimeFormat.h>
7*b2c385c0SAdrien Destugues 
8*b2c385c0SAdrien Destugues #include <unicode/format.h>
9*b2c385c0SAdrien Destugues #include <unicode/tmutfmt.h>
10*b2c385c0SAdrien Destugues #include <unicode/utypes.h>
11*b2c385c0SAdrien Destugues #include <ICUWrapper.h>
12*b2c385c0SAdrien Destugues 
13*b2c385c0SAdrien Destugues status_t BTimeFormat::Format(int64 number, BString* buffer) const
14*b2c385c0SAdrien Destugues {
15*b2c385c0SAdrien Destugues 	// create time unit amount instance - a combination of Number and time unit
16*b2c385c0SAdrien Destugues 	UErrorCode status = U_ZERO_ERROR;
17*b2c385c0SAdrien Destugues 	TimeUnitAmount* source = new TimeUnitAmount(number/1000000, TimeUnit::UTIMEUNIT_SECOND, status);
18*b2c385c0SAdrien Destugues 	// create time unit format instance
19*b2c385c0SAdrien Destugues 	TimeUnitFormat* format = new TimeUnitFormat(status);
20*b2c385c0SAdrien Destugues 	// format a time unit amount
21*b2c385c0SAdrien Destugues 	UnicodeString formatted;
22*b2c385c0SAdrien Destugues 	Formattable formattable(source);
23*b2c385c0SAdrien Destugues 	if (!U_SUCCESS(status)) {
24*b2c385c0SAdrien Destugues 		delete source;
25*b2c385c0SAdrien Destugues 		delete format;
26*b2c385c0SAdrien Destugues 		return B_ERROR;
27*b2c385c0SAdrien Destugues 	}
28*b2c385c0SAdrien Destugues 
29*b2c385c0SAdrien Destugues 	formatted = ((icu_4_2::Format*)format)->format(formattable, formatted, status);
30*b2c385c0SAdrien Destugues 
31*b2c385c0SAdrien Destugues 	BStringByteSink bbs(buffer);
32*b2c385c0SAdrien Destugues 	formatted.toUTF8(bbs);
33*b2c385c0SAdrien Destugues 
34*b2c385c0SAdrien Destugues 	delete source;
35*b2c385c0SAdrien Destugues 	delete format;
36*b2c385c0SAdrien Destugues 	return B_OK;
37*b2c385c0SAdrien Destugues }
38