forked from mozilla/gecko-dev
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDateTimeFormat.cpp
379 lines (326 loc) · 11.5 KB
/
DateTimeFormat.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "DateTimeFormat.h"
#include "nsCOMPtr.h"
#include "mozilla/intl/LocaleService.h"
#include "OSPreferences.h"
#include "mozIOSPreferences.h"
#include "unicode/dtfmtsym.h"
#include "unicode/udatpg.h"
namespace mozilla {
using namespace mozilla::intl;
nsCString* DateTimeFormat::mLocale = nullptr;
nsTHashMap<nsCStringHashKey, UDateFormat*>* DateTimeFormat::mFormatCache;
static const int32_t DATETIME_FORMAT_INITIAL_LEN = 127;
/*static*/
nsresult DateTimeFormat::Initialize() {
if (mLocale) {
return NS_OK;
}
mLocale = new nsCString();
AutoTArray<nsCString, 10> regionalPrefsLocales;
intl::LocaleService::GetInstance()->GetRegionalPrefsLocales(
regionalPrefsLocales);
mLocale->Assign(regionalPrefsLocales[0]);
return NS_OK;
}
// performs a locale sensitive date formatting operation on the PRTime parameter
/*static*/
nsresult DateTimeFormat::FormatPRTime(
const nsDateFormatSelector aDateFormatSelector,
const nsTimeFormatSelector aTimeFormatSelector, const PRTime aPrTime,
nsAString& aStringOut) {
return FormatUDateTime(aDateFormatSelector, aTimeFormatSelector,
(aPrTime / PR_USEC_PER_MSEC), nullptr, aStringOut);
}
// performs a locale sensitive date formatting operation on the PRExplodedTime
// parameter
/*static*/
nsresult DateTimeFormat::FormatPRExplodedTime(
const nsDateFormatSelector aDateFormatSelector,
const nsTimeFormatSelector aTimeFormatSelector,
const PRExplodedTime* aExplodedTime, nsAString& aStringOut) {
return FormatUDateTime(aDateFormatSelector, aTimeFormatSelector,
(PR_ImplodeTime(aExplodedTime) / PR_USEC_PER_MSEC),
&(aExplodedTime->tm_params), aStringOut);
}
// performs a locale sensitive date formatting operation on the PRExplodedTime
// parameter, using the specified options.
/*static*/
nsresult DateTimeFormat::FormatDateTime(
const PRExplodedTime* aExplodedTime,
const DateTimeFormat::Skeleton aSkeleton, nsAString& aStringOut) {
// set up locale data
nsresult rv = Initialize();
if (NS_FAILED(rv)) {
return rv;
}
aStringOut.Truncate();
UErrorCode status = U_ZERO_ERROR;
nsAutoCString skeleton;
switch (aSkeleton) {
case Skeleton::yyyyMM:
skeleton.AssignASCII("yyyyMM");
break;
case Skeleton::yyyyMMMM:
skeleton.AssignASCII("yyyyMMMM");
break;
case Skeleton::E:
skeleton.AssignASCII("E");
break;
case Skeleton::EEEE:
skeleton.AssignASCII("EEEE");
break;
default:
MOZ_ASSERT_UNREACHABLE("Unhandled skeleton enum");
}
nsAutoCString str;
if (!OSPreferences::GetPatternForSkeleton(skeleton, *mLocale, str)) {
return NS_ERROR_FAILURE;
}
nsAutoString pattern = NS_ConvertUTF8toUTF16(str);
nsAutoString timeZoneID;
BuildTimeZoneString(aExplodedTime->tm_params, timeZoneID);
UDateFormat* dateTimeFormat =
udat_open(UDAT_PATTERN, UDAT_PATTERN, mLocale->get(),
reinterpret_cast<const UChar*>(timeZoneID.BeginReading()),
timeZoneID.Length(),
reinterpret_cast<const UChar*>(pattern.BeginReading()),
pattern.Length(), &status);
if (U_SUCCESS(status) && dateTimeFormat) {
UDate udate =
static_cast<float>((PR_ImplodeTime(aExplodedTime) / PR_USEC_PER_MSEC));
aStringOut.SetLength(DATETIME_FORMAT_INITIAL_LEN);
int32_t dateTimeLen =
udat_format(dateTimeFormat, udate,
reinterpret_cast<UChar*>(aStringOut.BeginWriting()),
DATETIME_FORMAT_INITIAL_LEN, nullptr, &status);
aStringOut.SetLength(dateTimeLen);
if (status == U_BUFFER_OVERFLOW_ERROR) {
status = U_ZERO_ERROR;
udat_format(dateTimeFormat, udate,
reinterpret_cast<UChar*>(aStringOut.BeginWriting()),
dateTimeLen, nullptr, &status);
}
}
udat_close(dateTimeFormat);
if (U_FAILURE(status)) {
return NS_ERROR_FAILURE;
}
return rv;
}
/*static*/
nsresult DateTimeFormat::GetCalendarSymbol(const Field aField,
const Style aStyle,
const PRExplodedTime* aExplodedTime,
nsAString& aStringOut) {
nsresult rv = Initialize();
if (NS_FAILED(rv)) {
return rv;
}
icu::DateFormatSymbols::DtWidthType widthType;
switch (aStyle) {
case Style::Wide:
widthType = icu::DateFormatSymbols::DtWidthType::WIDE;
break;
case Style::Abbreviated:
widthType = icu::DateFormatSymbols::DtWidthType::ABBREVIATED;
break;
}
int32_t count;
UErrorCode status = U_ZERO_ERROR;
icu::Locale locale = icu::Locale::createCanonical(mLocale->get());
UDate udate =
static_cast<float>((PR_ImplodeTime(aExplodedTime) / PR_USEC_PER_MSEC));
nsAutoString timeZoneID;
BuildTimeZoneString(aExplodedTime->tm_params, timeZoneID);
std::unique_ptr<icu::TimeZone> timeZone(
icu::TimeZone::createTimeZone(timeZoneID.BeginReading()));
std::unique_ptr<icu::Calendar> cal(
icu::Calendar::createInstance(timeZone.release(), locale, status));
if (U_FAILURE(status)) {
return NS_ERROR_UNEXPECTED;
}
cal->setTime(udate, status);
if (U_FAILURE(status)) {
return NS_ERROR_UNEXPECTED;
}
std::unique_ptr<icu::DateFormatSymbols> dfs(
icu::DateFormatSymbols::createForLocale(locale, status));
if (U_FAILURE(status)) {
return NS_ERROR_UNEXPECTED;
}
if (aField == Field::Month) {
int32_t month = cal->get(UCAL_MONTH, status);
if (U_FAILURE(status)) {
return NS_ERROR_UNEXPECTED;
}
const auto* months = dfs->getMonths(
count, icu::DateFormatSymbols::DtContextType::STANDALONE, widthType);
if (month < 0 || month >= count) {
return NS_ERROR_INVALID_ARG;
}
aStringOut.Assign(months[month].getBuffer(), months[month].length());
} else if (aField == Field::Weekday) {
int32_t weekday = cal->get(UCAL_DAY_OF_WEEK, status);
if (U_FAILURE(status)) {
return NS_ERROR_UNEXPECTED;
}
const auto* weekdays = dfs->getWeekdays(
count, icu::DateFormatSymbols::DtContextType::STANDALONE, widthType);
if (weekday < 0 || weekday >= count) {
return NS_ERROR_INVALID_ARG;
}
aStringOut.Assign(weekdays[weekday].getBuffer(),
weekdays[weekday].length());
}
return NS_OK;
}
// performs a locale sensitive date formatting operation on the UDate parameter
/*static*/
nsresult DateTimeFormat::FormatUDateTime(
const nsDateFormatSelector aDateFormatSelector,
const nsTimeFormatSelector aTimeFormatSelector, const UDate aUDateTime,
const PRTimeParameters* aTimeParameters, nsAString& aStringOut) {
int32_t dateTimeLen = 0;
nsresult rv = NS_OK;
// return, nothing to format
if (aDateFormatSelector == kDateFormatNone &&
aTimeFormatSelector == kTimeFormatNone) {
aStringOut.Truncate();
return NS_OK;
}
// set up locale data
rv = Initialize();
if (NS_FAILED(rv)) {
return rv;
}
UErrorCode status = U_ZERO_ERROR;
nsAutoCString key;
key.AppendInt((int)aDateFormatSelector);
key.Append(':');
key.AppendInt((int)aTimeFormatSelector);
if (aTimeParameters) {
key.Append(':');
key.AppendInt(aTimeParameters->tp_gmt_offset);
key.Append(':');
key.AppendInt(aTimeParameters->tp_dst_offset);
}
if (mFormatCache && mFormatCache->Count() == kMaxCachedFormats) {
// Don't allow a pathological page to extend the cache unreasonably.
NS_WARNING("flushing UDateFormat cache");
DeleteCache();
}
if (!mFormatCache) {
mFormatCache =
new nsTHashMap<nsCStringHashKey, UDateFormat*>(kMaxCachedFormats);
}
UDateFormat*& dateTimeFormat = mFormatCache->LookupOrInsert(key);
if (!dateTimeFormat) {
// We didn't have a cached formatter for this key, so create one.
int32_t dateFormatStyle;
switch (aDateFormatSelector) {
case kDateFormatLong:
dateFormatStyle = mozIOSPreferences::dateTimeFormatStyleLong;
break;
case kDateFormatShort:
dateFormatStyle = mozIOSPreferences::dateTimeFormatStyleShort;
break;
case kDateFormatNone:
dateFormatStyle = mozIOSPreferences::dateTimeFormatStyleNone;
break;
default:
NS_ERROR("Unknown nsDateFormatSelector");
return NS_ERROR_ILLEGAL_VALUE;
}
int32_t timeFormatStyle;
switch (aTimeFormatSelector) {
case kTimeFormatLong:
timeFormatStyle = mozIOSPreferences::dateTimeFormatStyleLong;
break;
case kTimeFormatShort:
timeFormatStyle = mozIOSPreferences::dateTimeFormatStyleShort;
break;
case kTimeFormatNone:
timeFormatStyle = mozIOSPreferences::dateTimeFormatStyleNone;
break;
default:
NS_ERROR("Unknown nsDateFormatSelector");
return NS_ERROR_ILLEGAL_VALUE;
}
nsAutoCString str;
rv = OSPreferences::GetInstance()->GetDateTimePattern(
dateFormatStyle, timeFormatStyle, nsDependentCString(mLocale->get()),
str);
NS_ENSURE_SUCCESS(rv, rv);
nsAutoString pattern = NS_ConvertUTF8toUTF16(str);
if (aTimeParameters) {
nsAutoString timeZoneID;
BuildTimeZoneString(*aTimeParameters, timeZoneID);
dateTimeFormat =
udat_open(UDAT_PATTERN, UDAT_PATTERN, mLocale->get(),
reinterpret_cast<const UChar*>(timeZoneID.BeginReading()),
timeZoneID.Length(),
reinterpret_cast<const UChar*>(pattern.BeginReading()),
pattern.Length(), &status);
} else {
dateTimeFormat =
udat_open(UDAT_PATTERN, UDAT_PATTERN, mLocale->get(), nullptr, -1,
reinterpret_cast<const UChar*>(pattern.BeginReading()),
pattern.Length(), &status);
}
}
if (U_SUCCESS(status) && dateTimeFormat) {
aStringOut.SetLength(DATETIME_FORMAT_INITIAL_LEN);
dateTimeLen =
udat_format(dateTimeFormat, aUDateTime,
reinterpret_cast<UChar*>(aStringOut.BeginWriting()),
DATETIME_FORMAT_INITIAL_LEN, nullptr, &status);
aStringOut.SetLength(dateTimeLen);
if (status == U_BUFFER_OVERFLOW_ERROR) {
status = U_ZERO_ERROR;
udat_format(dateTimeFormat, aUDateTime,
reinterpret_cast<UChar*>(aStringOut.BeginWriting()),
dateTimeLen, nullptr, &status);
}
}
if (U_FAILURE(status)) {
rv = NS_ERROR_FAILURE;
}
return rv;
}
/*static*/
void DateTimeFormat::BuildTimeZoneString(
const PRTimeParameters& aTimeParameters, nsAString& aStringOut) {
aStringOut.Truncate();
aStringOut.Append(u"GMT");
int32_t totalOffsetMinutes =
(aTimeParameters.tp_gmt_offset + aTimeParameters.tp_dst_offset) / 60;
if (totalOffsetMinutes != 0) {
char sign = totalOffsetMinutes < 0 ? '-' : '+';
int32_t hours = abs(totalOffsetMinutes) / 60;
int32_t minutes = abs(totalOffsetMinutes) % 60;
aStringOut.AppendPrintf("%c%02d:%02d", sign, hours, minutes);
}
}
/*static*/
void DateTimeFormat::DeleteCache() {
if (mFormatCache) {
for (const auto& entry : mFormatCache->Values()) {
udat_close(entry);
}
delete mFormatCache;
mFormatCache = nullptr;
}
}
/*static*/
void DateTimeFormat::Shutdown() {
DeleteCache();
if (mLocale) {
delete mLocale;
}
}
} // namespace mozilla