OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2011,2012 Google Inc. All rights reserved. | 2 * Copyright (C) 2011,2012 Google Inc. All rights reserved. |
3 * | 3 * |
4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
5 * modification, are permitted provided that the following conditions are | 5 * modification, are permitted provided that the following conditions are |
6 * met: | 6 * met: |
7 * | 7 * |
8 * * Redistributions of source code must retain the above copyright | 8 * * Redistributions of source code must retain the above copyright |
9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
10 * * Redistributions in binary form must reproduce the above | 10 * * Redistributions in binary form must reproduce the above |
(...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
158 if (status != U_BUFFER_OVERFLOW_ERROR || !length) | 158 if (status != U_BUFFER_OVERFLOW_ERROR || !length) |
159 return emptyString(); | 159 return emptyString(); |
160 StringBuffer<UChar> buffer(length); | 160 StringBuffer<UChar> buffer(length); |
161 status = U_ZERO_ERROR; | 161 status = U_ZERO_ERROR; |
162 udat_toPattern(dateFormat, TRUE, buffer.characters(), length, &status); | 162 udat_toPattern(dateFormat, TRUE, buffer.characters(), length, &status); |
163 if (U_FAILURE(status)) | 163 if (U_FAILURE(status)) |
164 return emptyString(); | 164 return emptyString(); |
165 return String::adopt(buffer); | 165 return String::adopt(buffer); |
166 } | 166 } |
167 | 167 |
168 PassOwnPtr<Vector<String> > LocaleICU::createLabelVector(const UDateFormat* date
Format, UDateFormatSymbolType type, int32_t startIndex, int32_t size) | 168 PassOwnPtr<Vector<String>> LocaleICU::createLabelVector(const UDateFormat* dateF
ormat, UDateFormatSymbolType type, int32_t startIndex, int32_t size) |
169 { | 169 { |
170 if (!dateFormat) | 170 if (!dateFormat) |
171 return PassOwnPtr<Vector<String> >(); | 171 return PassOwnPtr<Vector<String>>(); |
172 if (udat_countSymbols(dateFormat, type) != startIndex + size) | 172 if (udat_countSymbols(dateFormat, type) != startIndex + size) |
173 return PassOwnPtr<Vector<String> >(); | 173 return PassOwnPtr<Vector<String>>(); |
174 | 174 |
175 OwnPtr<Vector<String> > labels = adoptPtr(new Vector<String>()); | 175 OwnPtr<Vector<String>> labels = adoptPtr(new Vector<String>()); |
176 labels->reserveCapacity(size); | 176 labels->reserveCapacity(size); |
177 for (int32_t i = 0; i < size; ++i) { | 177 for (int32_t i = 0; i < size; ++i) { |
178 UErrorCode status = U_ZERO_ERROR; | 178 UErrorCode status = U_ZERO_ERROR; |
179 int32_t length = udat_getSymbols(dateFormat, type, startIndex + i, 0, 0,
&status); | 179 int32_t length = udat_getSymbols(dateFormat, type, startIndex + i, 0, 0,
&status); |
180 if (status != U_BUFFER_OVERFLOW_ERROR) | 180 if (status != U_BUFFER_OVERFLOW_ERROR) |
181 return PassOwnPtr<Vector<String> >(); | 181 return PassOwnPtr<Vector<String>>(); |
182 StringBuffer<UChar> buffer(length); | 182 StringBuffer<UChar> buffer(length); |
183 status = U_ZERO_ERROR; | 183 status = U_ZERO_ERROR; |
184 udat_getSymbols(dateFormat, type, startIndex + i, buffer.characters(), l
ength, &status); | 184 udat_getSymbols(dateFormat, type, startIndex + i, buffer.characters(), l
ength, &status); |
185 if (U_FAILURE(status)) | 185 if (U_FAILURE(status)) |
186 return PassOwnPtr<Vector<String> >(); | 186 return PassOwnPtr<Vector<String>>(); |
187 labels->append(String::adopt(buffer)); | 187 labels->append(String::adopt(buffer)); |
188 } | 188 } |
189 return labels.release(); | 189 return labels.release(); |
190 } | 190 } |
191 | 191 |
192 static PassOwnPtr<Vector<String> > createFallbackWeekDayShortLabels() | 192 static PassOwnPtr<Vector<String>> createFallbackWeekDayShortLabels() |
193 { | 193 { |
194 OwnPtr<Vector<String> > labels = adoptPtr(new Vector<String>()); | 194 OwnPtr<Vector<String>> labels = adoptPtr(new Vector<String>()); |
195 labels->reserveCapacity(7); | 195 labels->reserveCapacity(7); |
196 labels->append("Sun"); | 196 labels->append("Sun"); |
197 labels->append("Mon"); | 197 labels->append("Mon"); |
198 labels->append("Tue"); | 198 labels->append("Tue"); |
199 labels->append("Wed"); | 199 labels->append("Wed"); |
200 labels->append("Thu"); | 200 labels->append("Thu"); |
201 labels->append("Fri"); | 201 labels->append("Fri"); |
202 labels->append("Sat"); | 202 labels->append("Sat"); |
203 return labels.release(); | 203 return labels.release(); |
204 } | 204 } |
205 | 205 |
206 void LocaleICU::initializeCalendar() | 206 void LocaleICU::initializeCalendar() |
207 { | 207 { |
208 if (m_weekDayShortLabels) | 208 if (m_weekDayShortLabels) |
209 return; | 209 return; |
210 | 210 |
211 if (!initializeShortDateFormat()) { | 211 if (!initializeShortDateFormat()) { |
212 m_firstDayOfWeek = 0; | 212 m_firstDayOfWeek = 0; |
213 m_weekDayShortLabels = createFallbackWeekDayShortLabels(); | 213 m_weekDayShortLabels = createFallbackWeekDayShortLabels(); |
214 return; | 214 return; |
215 } | 215 } |
216 m_firstDayOfWeek = ucal_getAttribute(udat_getCalendar(m_shortDateFormat), UC
AL_FIRST_DAY_OF_WEEK) - UCAL_SUNDAY; | 216 m_firstDayOfWeek = ucal_getAttribute(udat_getCalendar(m_shortDateFormat), UC
AL_FIRST_DAY_OF_WEEK) - UCAL_SUNDAY; |
217 | 217 |
218 m_weekDayShortLabels = createLabelVector(m_shortDateFormat, UDAT_SHORT_WEEKD
AYS, UCAL_SUNDAY, 7); | 218 m_weekDayShortLabels = createLabelVector(m_shortDateFormat, UDAT_SHORT_WEEKD
AYS, UCAL_SUNDAY, 7); |
219 if (!m_weekDayShortLabels) | 219 if (!m_weekDayShortLabels) |
220 m_weekDayShortLabels = createFallbackWeekDayShortLabels(); | 220 m_weekDayShortLabels = createFallbackWeekDayShortLabels(); |
221 } | 221 } |
222 | 222 |
223 static PassOwnPtr<Vector<String> > createFallbackMonthLabels() | 223 static PassOwnPtr<Vector<String>> createFallbackMonthLabels() |
224 { | 224 { |
225 OwnPtr<Vector<String> > labels = adoptPtr(new Vector<String>()); | 225 OwnPtr<Vector<String>> labels = adoptPtr(new Vector<String>()); |
226 labels->reserveCapacity(WTF_ARRAY_LENGTH(WTF::monthFullName)); | 226 labels->reserveCapacity(WTF_ARRAY_LENGTH(WTF::monthFullName)); |
227 for (unsigned i = 0; i < WTF_ARRAY_LENGTH(WTF::monthFullName); ++i) | 227 for (unsigned i = 0; i < WTF_ARRAY_LENGTH(WTF::monthFullName); ++i) |
228 labels->append(WTF::monthFullName[i]); | 228 labels->append(WTF::monthFullName[i]); |
229 return labels.release(); | 229 return labels.release(); |
230 } | 230 } |
231 | 231 |
232 const Vector<String>& LocaleICU::monthLabels() | 232 const Vector<String>& LocaleICU::monthLabels() |
233 { | 233 { |
234 if (m_monthLabels) | 234 if (m_monthLabels) |
235 return *m_monthLabels; | 235 return *m_monthLabels; |
(...skipping 17 matching lines...) Expand all Loading... |
253 initializeCalendar(); | 253 initializeCalendar(); |
254 return m_firstDayOfWeek; | 254 return m_firstDayOfWeek; |
255 } | 255 } |
256 | 256 |
257 bool LocaleICU::isRTL() | 257 bool LocaleICU::isRTL() |
258 { | 258 { |
259 UErrorCode status = U_ZERO_ERROR; | 259 UErrorCode status = U_ZERO_ERROR; |
260 return uloc_getCharacterOrientation(m_locale.data(), &status) == ULOC_LAYOUT
_RTL; | 260 return uloc_getCharacterOrientation(m_locale.data(), &status) == ULOC_LAYOUT
_RTL; |
261 } | 261 } |
262 | 262 |
263 static PassOwnPtr<Vector<String> > createFallbackAMPMLabels() | 263 static PassOwnPtr<Vector<String>> createFallbackAMPMLabels() |
264 { | 264 { |
265 OwnPtr<Vector<String> > labels = adoptPtr(new Vector<String>()); | 265 OwnPtr<Vector<String>> labels = adoptPtr(new Vector<String>()); |
266 labels->reserveCapacity(2); | 266 labels->reserveCapacity(2); |
267 labels->append("AM"); | 267 labels->append("AM"); |
268 labels->append("PM"); | 268 labels->append("PM"); |
269 return labels.release(); | 269 return labels.release(); |
270 } | 270 } |
271 | 271 |
272 void LocaleICU::initializeDateTimeFormat() | 272 void LocaleICU::initializeDateTimeFormat() |
273 { | 273 { |
274 if (m_didCreateTimeFormat) | 274 if (m_didCreateTimeFormat) |
275 return; | 275 return; |
276 | 276 |
277 // We assume ICU medium time pattern and short time pattern are compatible | 277 // We assume ICU medium time pattern and short time pattern are compatible |
278 // with LDML, because ICU specific pattern character "V" doesn't appear | 278 // with LDML, because ICU specific pattern character "V" doesn't appear |
279 // in both medium and short time pattern. | 279 // in both medium and short time pattern. |
280 m_mediumTimeFormat = openDateFormat(UDAT_MEDIUM, UDAT_NONE); | 280 m_mediumTimeFormat = openDateFormat(UDAT_MEDIUM, UDAT_NONE); |
281 m_timeFormatWithSeconds = getDateFormatPattern(m_mediumTimeFormat); | 281 m_timeFormatWithSeconds = getDateFormatPattern(m_mediumTimeFormat); |
282 | 282 |
283 m_shortTimeFormat = openDateFormat(UDAT_SHORT, UDAT_NONE); | 283 m_shortTimeFormat = openDateFormat(UDAT_SHORT, UDAT_NONE); |
284 m_timeFormatWithoutSeconds = getDateFormatPattern(m_shortTimeFormat); | 284 m_timeFormatWithoutSeconds = getDateFormatPattern(m_shortTimeFormat); |
285 | 285 |
286 UDateFormat* dateTimeFormatWithSeconds = openDateFormat(UDAT_MEDIUM, UDAT_SH
ORT); | 286 UDateFormat* dateTimeFormatWithSeconds = openDateFormat(UDAT_MEDIUM, UDAT_SH
ORT); |
287 m_dateTimeFormatWithSeconds = getDateFormatPattern(dateTimeFormatWithSeconds
); | 287 m_dateTimeFormatWithSeconds = getDateFormatPattern(dateTimeFormatWithSeconds
); |
288 udat_close(dateTimeFormatWithSeconds); | 288 udat_close(dateTimeFormatWithSeconds); |
289 | 289 |
290 UDateFormat* dateTimeFormatWithoutSeconds = openDateFormat(UDAT_SHORT, UDAT_
SHORT); | 290 UDateFormat* dateTimeFormatWithoutSeconds = openDateFormat(UDAT_SHORT, UDAT_
SHORT); |
291 m_dateTimeFormatWithoutSeconds = getDateFormatPattern(dateTimeFormatWithoutS
econds); | 291 m_dateTimeFormatWithoutSeconds = getDateFormatPattern(dateTimeFormatWithoutS
econds); |
292 udat_close(dateTimeFormatWithoutSeconds); | 292 udat_close(dateTimeFormatWithoutSeconds); |
293 | 293 |
294 OwnPtr<Vector<String> > timeAMPMLabels = createLabelVector(m_mediumTimeForma
t, UDAT_AM_PMS, UCAL_AM, 2); | 294 OwnPtr<Vector<String>> timeAMPMLabels = createLabelVector(m_mediumTimeFormat
, UDAT_AM_PMS, UCAL_AM, 2); |
295 if (!timeAMPMLabels) | 295 if (!timeAMPMLabels) |
296 timeAMPMLabels = createFallbackAMPMLabels(); | 296 timeAMPMLabels = createFallbackAMPMLabels(); |
297 m_timeAMPMLabels = *timeAMPMLabels; | 297 m_timeAMPMLabels = *timeAMPMLabels; |
298 | 298 |
299 m_didCreateTimeFormat = true; | 299 m_didCreateTimeFormat = true; |
300 } | 300 } |
301 | 301 |
302 String LocaleICU::dateFormat() | 302 String LocaleICU::dateFormat() |
303 { | 303 { |
304 if (!m_dateFormat.isNull()) | 304 if (!m_dateFormat.isNull()) |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
371 { | 371 { |
372 initializeDateTimeFormat(); | 372 initializeDateTimeFormat(); |
373 return m_dateTimeFormatWithoutSeconds; | 373 return m_dateTimeFormatWithoutSeconds; |
374 } | 374 } |
375 | 375 |
376 const Vector<String>& LocaleICU::shortMonthLabels() | 376 const Vector<String>& LocaleICU::shortMonthLabels() |
377 { | 377 { |
378 if (!m_shortMonthLabels.isEmpty()) | 378 if (!m_shortMonthLabels.isEmpty()) |
379 return m_shortMonthLabels; | 379 return m_shortMonthLabels; |
380 if (initializeShortDateFormat()) { | 380 if (initializeShortDateFormat()) { |
381 if (OwnPtr<Vector<String> > labels = createLabelVector(m_shortDateFormat
, UDAT_SHORT_MONTHS, UCAL_JANUARY, 12)) { | 381 if (OwnPtr<Vector<String>> labels = createLabelVector(m_shortDateFormat,
UDAT_SHORT_MONTHS, UCAL_JANUARY, 12)) { |
382 m_shortMonthLabels = *labels; | 382 m_shortMonthLabels = *labels; |
383 return m_shortMonthLabels; | 383 return m_shortMonthLabels; |
384 } | 384 } |
385 } | 385 } |
386 m_shortMonthLabels.reserveCapacity(WTF_ARRAY_LENGTH(WTF::monthName)); | 386 m_shortMonthLabels.reserveCapacity(WTF_ARRAY_LENGTH(WTF::monthName)); |
387 for (unsigned i = 0; i < WTF_ARRAY_LENGTH(WTF::monthName); ++i) | 387 for (unsigned i = 0; i < WTF_ARRAY_LENGTH(WTF::monthName); ++i) |
388 m_shortMonthLabels.append(WTF::monthName[i]); | 388 m_shortMonthLabels.append(WTF::monthName[i]); |
389 return m_shortMonthLabels; | 389 return m_shortMonthLabels; |
390 } | 390 } |
391 | 391 |
392 const Vector<String>& LocaleICU::standAloneMonthLabels() | 392 const Vector<String>& LocaleICU::standAloneMonthLabels() |
393 { | 393 { |
394 if (!m_standAloneMonthLabels.isEmpty()) | 394 if (!m_standAloneMonthLabels.isEmpty()) |
395 return m_standAloneMonthLabels; | 395 return m_standAloneMonthLabels; |
396 if (initializeShortDateFormat()) { | 396 if (initializeShortDateFormat()) { |
397 if (OwnPtr<Vector<String> > labels = createLabelVector(m_shortDateFormat
, UDAT_STANDALONE_MONTHS, UCAL_JANUARY, 12)) { | 397 if (OwnPtr<Vector<String>> labels = createLabelVector(m_shortDateFormat,
UDAT_STANDALONE_MONTHS, UCAL_JANUARY, 12)) { |
398 m_standAloneMonthLabels = *labels; | 398 m_standAloneMonthLabels = *labels; |
399 return m_standAloneMonthLabels; | 399 return m_standAloneMonthLabels; |
400 } | 400 } |
401 } | 401 } |
402 m_standAloneMonthLabels = monthLabels(); | 402 m_standAloneMonthLabels = monthLabels(); |
403 return m_standAloneMonthLabels; | 403 return m_standAloneMonthLabels; |
404 } | 404 } |
405 | 405 |
406 const Vector<String>& LocaleICU::shortStandAloneMonthLabels() | 406 const Vector<String>& LocaleICU::shortStandAloneMonthLabels() |
407 { | 407 { |
408 if (!m_shortStandAloneMonthLabels.isEmpty()) | 408 if (!m_shortStandAloneMonthLabels.isEmpty()) |
409 return m_shortStandAloneMonthLabels; | 409 return m_shortStandAloneMonthLabels; |
410 if (initializeShortDateFormat()) { | 410 if (initializeShortDateFormat()) { |
411 if (OwnPtr<Vector<String> > labels = createLabelVector(m_shortDateFormat
, UDAT_STANDALONE_SHORT_MONTHS, UCAL_JANUARY, 12)) { | 411 if (OwnPtr<Vector<String>> labels = createLabelVector(m_shortDateFormat,
UDAT_STANDALONE_SHORT_MONTHS, UCAL_JANUARY, 12)) { |
412 m_shortStandAloneMonthLabels = *labels; | 412 m_shortStandAloneMonthLabels = *labels; |
413 return m_shortStandAloneMonthLabels; | 413 return m_shortStandAloneMonthLabels; |
414 } | 414 } |
415 } | 415 } |
416 m_shortStandAloneMonthLabels = shortMonthLabels(); | 416 m_shortStandAloneMonthLabels = shortMonthLabels(); |
417 return m_shortStandAloneMonthLabels; | 417 return m_shortStandAloneMonthLabels; |
418 } | 418 } |
419 | 419 |
420 const Vector<String>& LocaleICU::timeAMPMLabels() | 420 const Vector<String>& LocaleICU::timeAMPMLabels() |
421 { | 421 { |
422 initializeDateTimeFormat(); | 422 initializeDateTimeFormat(); |
423 return m_timeAMPMLabels; | 423 return m_timeAMPMLabels; |
424 } | 424 } |
425 | 425 |
426 } // namespace blink | 426 } // namespace blink |
427 | 427 |
OLD | NEW |