BPicture: Fix archive constructor.
[haiku.git] / src / kits / locale / DateTimeFormat.cpp
blob7172257dd79d22c123aacb8c9d17d27a3cbea9c7
1 /*
2 * Copyright 2010-2014, Haiku, Inc. All Rights Reserved.
3 * Distributed under the terms of the MIT License.
5 * Authors:
6 * Oliver Tappe <zooey@hirschkaefer.de>
7 */
9 #include <unicode/uversion.h>
10 #include <DateTimeFormat.h>
12 #include <AutoDeleter.h>
13 #include <Autolock.h>
14 #include <FormattingConventionsPrivate.h>
15 #include <LanguagePrivate.h>
16 #include <TimeZone.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)
26 : BFormat(locale)
31 BDateTimeFormat::BDateTimeFormat(const BLanguage& language,
32 const BFormattingConventions& conventions)
33 : BFormat(language, conventions)
38 BDateTimeFormat::BDateTimeFormat(const BDateTimeFormat &other)
39 : BFormat(other)
44 BDateTimeFormat::~BDateTimeFormat()
49 void
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);
57 BString skeleton;
58 if (elements & B_DATE_ELEMENT_YEAR)
59 skeleton << "yyyy";
60 if (elements & B_DATE_ELEMENT_MONTH)
61 skeleton << "MM";
62 if (elements & B_DATE_ELEMENT_WEEKDAY)
63 skeleton << "eee";
64 if (elements & B_DATE_ELEMENT_DAY)
65 skeleton << "dd";
66 if (elements & B_DATE_ELEMENT_AM_PM)
67 skeleton << "a";
68 if (elements & B_DATE_ELEMENT_HOUR) {
69 if (fConventions.Use24HourClock())
70 skeleton << "k";
71 else
72 skeleton << "K";
74 if (elements & B_DATE_ELEMENT_MINUTE)
75 skeleton << "mm";
76 if (elements & B_DATE_ELEMENT_SECOND)
77 skeleton << "ss";
78 if (elements & B_DATE_ELEMENT_TIMEZONE)
79 skeleton << "z";
81 UnicodeString pattern = generator->getBestPattern(
82 UnicodeString::fromUTF8(skeleton.String()), error);
84 BString buffer;
85 BStringByteSink stringConverter(&buffer);
86 pattern.toUTF8(stringConverter);
88 fConventions.SetExplicitDateTimeFormat(dateStyle, timeStyle, buffer);
90 delete generator;
94 // #pragma mark - Formatting
97 ssize_t
98 BDateTimeFormat::Format(char* target, size_t maxSize, time_t time,
99 BDateFormatStyle dateStyle, BTimeFormatStyle timeStyle) const
101 BString format;
102 fConventions.GetDateTimeFormat(dateStyle, timeStyle, format);
103 ObjectDeleter<DateFormat> dateFormatter(_CreateDateTimeFormatter(format));
104 if (dateFormatter.Get() == NULL)
105 return B_NO_MEMORY;
107 UnicodeString icuString;
108 dateFormatter->format((UDate)time * 1000, icuString);
110 CheckedArrayByteSink stringConverter(target, maxSize);
111 icuString.toUTF8(stringConverter);
113 if (stringConverter.Overflowed())
114 return B_BAD_VALUE;
116 return stringConverter.NumberOfBytesWritten();
120 status_t
121 BDateTimeFormat::Format(BString& target, const time_t time,
122 BDateFormatStyle dateStyle, BTimeFormatStyle timeStyle,
123 const BTimeZone* timeZone) const
125 BString format;
126 fConventions.GetDateTimeFormat(dateStyle, timeStyle, format);
127 ObjectDeleter<DateFormat> dateFormatter(_CreateDateTimeFormatter(format));
128 if (dateFormatter.Get() == NULL)
129 return B_NO_MEMORY;
131 if (timeZone != NULL) {
132 ObjectDeleter<TimeZone> icuTimeZone(
133 TimeZone::createTimeZone(timeZone->ID().String()));
134 if (icuTimeZone.Get() == NULL)
135 return B_NO_MEMORY;
136 dateFormatter->setTimeZone(*icuTimeZone.Get());
139 UnicodeString icuString;
140 dateFormatter->format((UDate)time * 1000, icuString);
142 target.Truncate(0);
143 BStringByteSink stringConverter(&target);
144 icuString.toUTF8(stringConverter);
146 return B_OK;
150 DateFormat*
151 BDateTimeFormat::_CreateDateTimeFormatter(const BString& format) const
153 Locale* icuLocale
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)
161 return NULL;
163 SimpleDateFormat* dateFormatterImpl
164 = static_cast<SimpleDateFormat*>(dateFormatter);
166 UnicodeString pattern(format.String());
167 dateFormatterImpl->applyPattern(pattern);
169 return dateFormatter;