2 * Copyright 2010, Haiku, Inc. All Rights Reserved.
3 * Distributed under the terms of the MIT License.
6 * Adrien Destugues <pulkomandy@gmail.com>
7 * Oliver Tappe <zooey@hirschkaefer.de>
11 #include <unicode/uversion.h>
12 #include <TimeUnitFormat.h>
16 #include <unicode/format.h>
17 #include <unicode/locid.h>
18 #include <unicode/tmutfmt.h>
19 #include <unicode/utypes.h>
20 #include <ICUWrapper.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()
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
;
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
;
66 if (!U_SUCCESS(icuStatus
))
67 fInitStatus
= B_ERROR
;
71 BTimeUnitFormat::BTimeUnitFormat(const BTimeUnitFormat
& 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()
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
))
96 if (fFormatter
== NULL
)
99 UErrorCode icuStatus
= U_ZERO_ERROR
;
100 TimeUnitAmount
* timeUnitAmount
101 = new TimeUnitAmount((double)value
, skUnitMap
[unit
], icuStatus
);
102 if (timeUnitAmount
== NULL
)
104 if (!U_SUCCESS(icuStatus
))
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
))
115 BStringByteSink
byteSink(&buffer
);
116 unicodeResult
.toUTF8(byteSink
);