2 * Copyright 2010-2014, Haiku, Inc. All Rights Reserved.
3 * Distributed under the terms of the MIT License.
6 * Oliver Tappe <zooey@hirschkaefer.de>
9 #include <unicode/uversion.h>
10 #include <DateTimeFormat.h>
12 #include <AutoDeleter.h>
14 #include <FormattingConventionsPrivate.h>
15 #include <LanguagePrivate.h>
18 #include <ICUWrapper.h>
20 #include <unicode/datefmt.h>
21 #include <unicode/dtptngen.h>
22 #include <unicode/smpdtfmt.h>
25 BDateTimeFormat::BDateTimeFormat(const BLocale
* locale
)
31 BDateTimeFormat::BDateTimeFormat(const BLanguage
& language
,
32 const BFormattingConventions
& conventions
)
33 : BFormat(language
, conventions
)
38 BDateTimeFormat::BDateTimeFormat(const BDateTimeFormat
&other
)
44 BDateTimeFormat::~BDateTimeFormat()
50 BDateTimeFormat::SetDateTimeFormat(BDateFormatStyle dateStyle
,
51 BTimeFormatStyle timeStyle
, int32 elements
) {
52 UErrorCode error
= U_ZERO_ERROR
;
53 DateTimePatternGenerator
* generator
54 = DateTimePatternGenerator::createInstance(
55 *BLanguage::Private(&fLanguage
).ICULocale(), error
);
58 if (elements
& B_DATE_ELEMENT_YEAR
)
60 if (elements
& B_DATE_ELEMENT_MONTH
)
62 if (elements
& B_DATE_ELEMENT_WEEKDAY
)
64 if (elements
& B_DATE_ELEMENT_DAY
)
66 if (elements
& B_DATE_ELEMENT_AM_PM
)
68 if (elements
& B_DATE_ELEMENT_HOUR
) {
69 if (fConventions
.Use24HourClock())
74 if (elements
& B_DATE_ELEMENT_MINUTE
)
76 if (elements
& B_DATE_ELEMENT_SECOND
)
78 if (elements
& B_DATE_ELEMENT_TIMEZONE
)
81 UnicodeString pattern
= generator
->getBestPattern(
82 UnicodeString::fromUTF8(skeleton
.String()), error
);
85 BStringByteSink
stringConverter(&buffer
);
86 pattern
.toUTF8(stringConverter
);
88 fConventions
.SetExplicitDateTimeFormat(dateStyle
, timeStyle
, buffer
);
94 // #pragma mark - Formatting
98 BDateTimeFormat::Format(char* target
, size_t maxSize
, time_t time
,
99 BDateFormatStyle dateStyle
, BTimeFormatStyle timeStyle
) const
102 fConventions
.GetDateTimeFormat(dateStyle
, timeStyle
, format
);
103 ObjectDeleter
<DateFormat
> dateFormatter(_CreateDateTimeFormatter(format
));
104 if (dateFormatter
.Get() == NULL
)
107 UnicodeString icuString
;
108 dateFormatter
->format((UDate
)time
* 1000, icuString
);
110 CheckedArrayByteSink
stringConverter(target
, maxSize
);
111 icuString
.toUTF8(stringConverter
);
113 if (stringConverter
.Overflowed())
116 return stringConverter
.NumberOfBytesWritten();
121 BDateTimeFormat::Format(BString
& target
, const time_t time
,
122 BDateFormatStyle dateStyle
, BTimeFormatStyle timeStyle
,
123 const BTimeZone
* timeZone
) const
126 fConventions
.GetDateTimeFormat(dateStyle
, timeStyle
, format
);
127 ObjectDeleter
<DateFormat
> dateFormatter(_CreateDateTimeFormatter(format
));
128 if (dateFormatter
.Get() == NULL
)
131 if (timeZone
!= NULL
) {
132 ObjectDeleter
<TimeZone
> icuTimeZone(
133 TimeZone::createTimeZone(timeZone
->ID().String()));
134 if (icuTimeZone
.Get() == NULL
)
136 dateFormatter
->setTimeZone(*icuTimeZone
.Get());
139 UnicodeString icuString
;
140 dateFormatter
->format((UDate
)time
* 1000, icuString
);
143 BStringByteSink
stringConverter(&target
);
144 icuString
.toUTF8(stringConverter
);
151 BDateTimeFormat::_CreateDateTimeFormatter(const BString
& format
) const
154 = fConventions
.UseStringsFromPreferredLanguage()
155 ? BLanguage::Private(&fLanguage
).ICULocale()
156 : BFormattingConventions::Private(&fConventions
).ICULocale();
158 icu::DateFormat
* dateFormatter
= icu::DateFormat::createDateTimeInstance(
159 DateFormat::kDefault
, DateFormat::kDefault
, *icuLocale
);
160 if (dateFormatter
== NULL
)
163 SimpleDateFormat
* dateFormatterImpl
164 = static_cast<SimpleDateFormat
*>(dateFormatter
);
166 UnicodeString
pattern(format
.String());
167 dateFormatterImpl
->applyPattern(pattern
);
169 return dateFormatter
;