BPicture: Fix archive constructor.
[haiku.git] / src / kits / locale / TimeUnitFormat.cpp
blob61a8ba5a0a44f20941d6aae45d21694ef6f3da33
1 /*
2 * Copyright 2010, Haiku, Inc. All Rights Reserved.
3 * Distributed under the terms of the MIT License.
5 * Authors:
6 * Adrien Destugues <pulkomandy@gmail.com>
7 * Oliver Tappe <zooey@hirschkaefer.de>
8 */
11 #include <unicode/uversion.h>
12 #include <TimeUnitFormat.h>
14 #include <new>
16 #include <unicode/format.h>
17 #include <unicode/locid.h>
18 #include <unicode/tmutfmt.h>
19 #include <unicode/utypes.h>
20 #include <ICUWrapper.h>
22 #include <Language.h>
23 #include <Locale.h>
24 #include <LocaleRoster.h>
26 // maps our unit element to the corresponding ICU unit
27 static const TimeUnit::UTimeUnitFields skUnitMap[] = {
28 TimeUnit::UTIMEUNIT_YEAR,
29 TimeUnit::UTIMEUNIT_MONTH,
30 TimeUnit::UTIMEUNIT_WEEK,
31 TimeUnit::UTIMEUNIT_DAY,
32 TimeUnit::UTIMEUNIT_HOUR,
33 TimeUnit::UTIMEUNIT_MINUTE,
34 TimeUnit::UTIMEUNIT_SECOND,
38 BTimeUnitFormat::BTimeUnitFormat()
39 : Inherited()
41 Locale icuLocale(fLanguage.Code());
42 UErrorCode icuStatus = U_ZERO_ERROR;
43 fFormatter = new TimeUnitFormat(icuLocale, icuStatus);
44 if (fFormatter == NULL) {
45 fInitStatus = B_NO_MEMORY;
46 return;
49 if (!U_SUCCESS(icuStatus))
50 fInitStatus = B_ERROR;
54 BTimeUnitFormat::BTimeUnitFormat(const BLanguage& language,
55 const BFormattingConventions& conventions)
56 : Inherited(language, conventions)
58 Locale icuLocale(fLanguage.Code());
59 UErrorCode icuStatus = U_ZERO_ERROR;
60 fFormatter = new TimeUnitFormat(icuLocale, icuStatus);
61 if (fFormatter == NULL) {
62 fInitStatus = B_NO_MEMORY;
63 return;
66 if (!U_SUCCESS(icuStatus))
67 fInitStatus = B_ERROR;
71 BTimeUnitFormat::BTimeUnitFormat(const BTimeUnitFormat& other)
73 Inherited(other),
74 fFormatter(other.fFormatter != NULL
75 ? new TimeUnitFormat(*other.fFormatter) : NULL)
77 if (fFormatter == NULL && other.fFormatter != NULL)
78 fInitStatus = B_NO_MEMORY;
82 BTimeUnitFormat::~BTimeUnitFormat()
84 delete fFormatter;
88 status_t
89 BTimeUnitFormat::Format(BString& buffer, const int32 value,
90 const time_unit_element unit, time_unit_style style) const
92 if (unit < 0 || unit > B_TIME_UNIT_LAST
93 || (style != B_TIME_UNIT_ABBREVIATED && style != B_TIME_UNIT_FULL))
94 return B_BAD_VALUE;
96 if (fFormatter == NULL)
97 return B_NO_INIT;
99 UErrorCode icuStatus = U_ZERO_ERROR;
100 TimeUnitAmount* timeUnitAmount
101 = new TimeUnitAmount((double)value, skUnitMap[unit], icuStatus);
102 if (timeUnitAmount == NULL)
103 return B_NO_MEMORY;
104 if (!U_SUCCESS(icuStatus))
105 return B_ERROR;
107 Formattable formattable;
108 formattable.adoptObject(timeUnitAmount);
109 FieldPosition pos(FieldPosition::DONT_CARE);
110 UnicodeString unicodeResult;
111 fFormatter->format(formattable, unicodeResult, pos, icuStatus);
112 if (!U_SUCCESS(icuStatus))
113 return B_ERROR;
115 BStringByteSink byteSink(&buffer);
116 unicodeResult.toUTF8(byteSink);
118 return B_OK;