Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(342)

Side by Side Diff: base/i18n/time_formatting.cc

Issue 7054032: Fix ChromeOS clock menu so that it can show 12-hour clock in all locales. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Removed unnecessary use of PlatformTest. Created 9 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « base/i18n/time_formatting.h ('k') | base/i18n/time_formatting_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/i18n/time_formatting.h" 5 #include "base/i18n/time_formatting.h"
6 6
7 #include "base/logging.h" 7 #include "base/logging.h"
8 #include "base/memory/scoped_ptr.h" 8 #include "base/memory/scoped_ptr.h"
9 #include "base/utf_string_conversions.h" 9 #include "base/utf_string_conversions.h"
10 #include "base/time.h" 10 #include "base/time.h"
11 #include "unicode/datefmt.h" 11 #include "unicode/datefmt.h"
12 #include "unicode/dtptngen.h" 12 #include "unicode/dtptngen.h"
13 #include "unicode/smpdtfmt.h" 13 #include "unicode/smpdtfmt.h"
14 14
15 using base::Time; 15 using base::Time;
16 16
17 namespace { 17 namespace {
18 18
19 string16 TimeFormat(const icu::DateFormat* formatter, 19 string16 TimeFormat(const icu::DateFormat* formatter,
20 const Time& time) { 20 const Time& time) {
21 DCHECK(formatter); 21 DCHECK(formatter);
22 icu::UnicodeString date_string; 22 icu::UnicodeString date_string;
23 23
24 formatter->format(static_cast<UDate>(time.ToDoubleT() * 1000), date_string); 24 formatter->format(static_cast<UDate>(time.ToDoubleT() * 1000), date_string);
25 return string16(date_string.getBuffer(), 25 return string16(date_string.getBuffer(),
26 static_cast<size_t>(date_string.length())); 26 static_cast<size_t>(date_string.length()));
27 } 27 }
28 28
29 string16 TimeFormatWithoutAmPm(const icu::DateFormat* formatter,
30 const Time& time) {
31 DCHECK(formatter);
32 icu::UnicodeString time_string;
33
34 icu::FieldPosition ampm_field(icu::DateFormat::kAmPmField);
35 formatter->format(
36 static_cast<UDate>(time.ToDoubleT() * 1000), time_string, ampm_field);
37 int ampm_length = ampm_field.getEndIndex() - ampm_field.getBeginIndex();
38 if (ampm_length) {
39 int begin = ampm_field.getBeginIndex();
40 // Doesn't include any spacing before the field.
41 if (begin)
42 begin--;
43 time_string.removeBetween(begin, ampm_field.getEndIndex());
44 }
45 return string16(time_string.getBuffer(),
46 static_cast<size_t>(time_string.length()));
47 }
48
29 } // namespace 49 } // namespace
30 50
31 namespace base { 51 namespace base {
32 52
33 string16 TimeFormatTimeOfDay(const Time& time) { 53 string16 TimeFormatTimeOfDay(const Time& time) {
34 // We can omit the locale parameter because the default should match 54 // We can omit the locale parameter because the default should match
35 // Chrome's application locale. 55 // Chrome's application locale.
36 scoped_ptr<icu::DateFormat> formatter( 56 scoped_ptr<icu::DateFormat> formatter(
37 icu::DateFormat::createTimeInstance(icu::DateFormat::kShort)); 57 icu::DateFormat::createTimeInstance(icu::DateFormat::kShort));
38 return TimeFormat(formatter.get(), time); 58 return TimeFormat(formatter.get(), time);
39 } 59 }
40 60
41 string16 TimeFormatTimeOfDayWithHourClockType(const Time& time, 61 string16 TimeFormatTimeOfDayWithHourClockType(const Time& time,
42 HourClockType type) { 62 HourClockType type,
63 AmPmClockType ampm) {
43 // Just redirect to the normal function if the default type matches the 64 // Just redirect to the normal function if the default type matches the
44 // given type. 65 // given type.
45 HourClockType default_type = GetHourClockType(); 66 HourClockType default_type = GetHourClockType();
46 if (default_type == type) { 67 if (default_type == type && (type == k24HourClock || ampm == kKeepAmPm)) {
47 return TimeFormatTimeOfDay(time); 68 return TimeFormatTimeOfDay(time);
48 } 69 }
49 70
50 // Generate a locale-dependent format pattern. The generator will take 71 // Generate a locale-dependent format pattern. The generator will take
51 // care of locale-dependent formatting issues like which separator to 72 // care of locale-dependent formatting issues like which separator to
52 // use (some locales use '.' instead of ':'), and where to put the am/pm 73 // use (some locales use '.' instead of ':'), and where to put the am/pm
53 // marker. 74 // marker.
54 UErrorCode status = U_ZERO_ERROR; 75 UErrorCode status = U_ZERO_ERROR;
55 icu::DateTimePatternGenerator *generator = 76 icu::DateTimePatternGenerator *generator =
56 icu::DateTimePatternGenerator::createInstance(status); 77 icu::DateTimePatternGenerator::createInstance(status);
57 CHECK(U_SUCCESS(status)); 78 CHECK(U_SUCCESS(status));
58 const char* base_pattern = (type == k12HourClock ? "ahm" : "Hm"); 79 const char* base_pattern = (type == k12HourClock ? "ahm" : "Hm");
59 icu::UnicodeString generated_pattern = 80 icu::UnicodeString generated_pattern =
60 generator->getBestPattern(icu::UnicodeString(base_pattern), status); 81 generator->getBestPattern(icu::UnicodeString(base_pattern), status);
61 CHECK(U_SUCCESS(status)); 82 CHECK(U_SUCCESS(status));
62 83
63 // Then, format the time using the generated pattern. 84 // Then, format the time using the generated pattern.
64 icu::SimpleDateFormat formatter(generated_pattern, status); 85 icu::SimpleDateFormat formatter(generated_pattern, status);
65 CHECK(U_SUCCESS(status)); 86 CHECK(U_SUCCESS(status));
66 return TimeFormat(&formatter, time); 87 if (ampm == kKeepAmPm) {
88 return TimeFormat(&formatter, time);
89 } else {
90 return TimeFormatWithoutAmPm(&formatter, time);
91 }
67 } 92 }
68 93
69 string16 TimeFormatShortDate(const Time& time) { 94 string16 TimeFormatShortDate(const Time& time) {
70 scoped_ptr<icu::DateFormat> formatter( 95 scoped_ptr<icu::DateFormat> formatter(
71 icu::DateFormat::createDateInstance(icu::DateFormat::kMedium)); 96 icu::DateFormat::createDateInstance(icu::DateFormat::kMedium));
72 return TimeFormat(formatter.get(), time); 97 return TimeFormat(formatter.get(), time);
73 } 98 }
74 99
75 string16 TimeFormatShortDateNumeric(const Time& time) { 100 string16 TimeFormatShortDateNumeric(const Time& time) {
76 scoped_ptr<icu::DateFormat> formatter( 101 scoped_ptr<icu::DateFormat> formatter(
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
130 // See http://userguide.icu-project.org/formatparse/datetime for details 155 // See http://userguide.icu-project.org/formatparse/datetime for details
131 // about the date/time format syntax. 156 // about the date/time format syntax.
132 if (pattern_unicode.indexOf('a') == -1) { 157 if (pattern_unicode.indexOf('a') == -1) {
133 return k24HourClock; 158 return k24HourClock;
134 } else { 159 } else {
135 return k12HourClock; 160 return k12HourClock;
136 } 161 }
137 } 162 }
138 163
139 } // namespace base 164 } // namespace base
OLDNEW
« no previous file with comments | « base/i18n/time_formatting.h ('k') | base/i18n/time_formatting_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698