Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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 "content/renderer/date_time_formatter.h" | 5 #include "content/renderer/date_time_formatter.h" |
| 6 | 6 |
| 7 #include "base/string_util.h" | 7 #include "base/string_util.h" |
| 8 #include "base/utf_string_conversions.h" | |
| 8 #include "third_party/WebKit/Source/Platform/chromium/public/WebCString.h" | 9 #include "third_party/WebKit/Source/Platform/chromium/public/WebCString.h" |
| 9 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDateTimeChooserPar ams.h" | 10 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDateTimeChooserPar ams.h" |
| 10 #include "third_party/icu/public/i18n/unicode/smpdtfmt.h" | 11 #include "third_party/icu/public/i18n/unicode/smpdtfmt.h" |
| 11 | 12 |
| 12 | 13 |
| 13 namespace content { | 14 namespace content { |
| 14 | 15 |
| 15 void DateTimeFormatter::CreatePatternMap() { | 16 void DateTimeFormatter::CreatePatternMap() { |
| 16 // Initialize all the UI elements with empty patterns, | 17 // Initialize all the UI elements with empty patterns, |
| 17 // then fill in the ones that are actually date/time inputs and | 18 // then fill in the ones that are actually date/time inputs and |
| (...skipping 27 matching lines...) Expand all Loading... | |
| 45 year_(year), | 46 year_(year), |
| 46 month_(month), | 47 month_(month), |
| 47 day_(day), | 48 day_(day), |
| 48 hour_(hour), | 49 hour_(hour), |
| 49 minute_(minute), | 50 minute_(minute), |
| 50 second_(second) { | 51 second_(second) { |
| 51 CreatePatternMap(); | 52 CreatePatternMap(); |
| 52 pattern_ = type_ > 0 && type_ <= ui::TEXT_INPUT_TYPE_MAX ? | 53 pattern_ = type_ > 0 && type_ <= ui::TEXT_INPUT_TYPE_MAX ? |
| 53 &patterns_[type_] : &patterns_[ui::TEXT_INPUT_TYPE_NONE]; | 54 &patterns_[type_] : &patterns_[ui::TEXT_INPUT_TYPE_NONE]; |
| 54 | 55 |
| 55 std::string patt; | |
| 56 pattern_->toUTF8String(patt); | |
| 57 formatted_string_ = FormatString(); | 56 formatted_string_ = FormatString(); |
| 58 } | 57 } |
| 59 | 58 |
| 60 DateTimeFormatter::~DateTimeFormatter() { | 59 DateTimeFormatter::~DateTimeFormatter() { |
| 61 } | 60 } |
| 62 | 61 |
| 63 int DateTimeFormatter::GetYear() const { | 62 int DateTimeFormatter::GetYear() const { |
| 64 return year_; | 63 return year_; |
| 65 } | 64 } |
| 66 | 65 |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 100 } | 99 } |
| 101 | 100 |
| 102 std::string result; | 101 std::string result; |
| 103 const icu::GregorianCalendar calendar( | 102 const icu::GregorianCalendar calendar( |
| 104 year_, month_, day_, hour_, minute_, second_, success); | 103 year_, month_, day_, hour_, minute_, second_, success); |
| 105 if (success <= U_ZERO_ERROR) { | 104 if (success <= U_ZERO_ERROR) { |
| 106 UDate time = calendar.getTime(success); | 105 UDate time = calendar.getTime(success); |
| 107 icu::SimpleDateFormat formatter(*pattern_, success); | 106 icu::SimpleDateFormat formatter(*pattern_, success); |
| 108 icu::UnicodeString formattedTime; | 107 icu::UnicodeString formattedTime; |
| 109 formatter.format(time, formattedTime, success); | 108 formatter.format(time, formattedTime, success); |
| 110 formattedTime.toUTF8String(result); | 109 result = UTF16ToUTF8(string16(formattedTime.getBuffer(), |
|
Miguel Garcia
2013/02/08 10:09:18
Can you add a comment on why we need to convert to
benm (inactive)
2013/02/08 10:22:08
I guess we can mitigate this perf impact with an i
| |
| 110 static_cast<size_t>(formattedTime.length()))); | |
| 111 if (success <= U_ZERO_ERROR) | 111 if (success <= U_ZERO_ERROR) |
| 112 return result; | 112 return result; |
| 113 } | 113 } |
| 114 LOG(WARNING) << "Calendar not created: error " << success; | 114 LOG(WARNING) << "Calendar not created: error " << success; |
| 115 return ""; | 115 return ""; |
| 116 } | 116 } |
| 117 | 117 |
| 118 void DateTimeFormatter::ExtractType( | 118 void DateTimeFormatter::ExtractType( |
| 119 const WebKit::WebDateTimeChooserParams& source) { | 119 const WebKit::WebDateTimeChooserParams& source) { |
| 120 switch (source.type) { | 120 switch (source.type) { |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 155 return false; | 155 return false; |
| 156 } | 156 } |
| 157 | 157 |
| 158 if (formatted_string_.empty()) { | 158 if (formatted_string_.empty()) { |
| 159 ClearAll(); | 159 ClearAll(); |
| 160 return true; | 160 return true; |
| 161 } | 161 } |
| 162 | 162 |
| 163 UErrorCode success = U_ZERO_ERROR; | 163 UErrorCode success = U_ZERO_ERROR; |
| 164 icu::UnicodeString icu_value = | 164 icu::UnicodeString icu_value = |
| 165 icu::UnicodeString::fromUTF8(formatted_string_); | 165 icu::UnicodeString::fromUTF8(formatted_string_.c_str()); |
| 166 if (type_ > 0 && type_ <= ui::TEXT_INPUT_TYPE_MAX) { | 166 if (type_ > 0 && type_ <= ui::TEXT_INPUT_TYPE_MAX) { |
| 167 const icu::UnicodeString pattern = patterns_[type_]; | 167 const icu::UnicodeString pattern = patterns_[type_]; |
| 168 icu::SimpleDateFormat formatter(pattern, success); | 168 icu::SimpleDateFormat formatter(pattern, success); |
| 169 formatter.parse(icu_value, success); | 169 formatter.parse(icu_value, success); |
| 170 if (success <= U_ZERO_ERROR) { | 170 if (success <= U_ZERO_ERROR) { |
| 171 const icu::Calendar* cal = formatter.getCalendar(); | 171 const icu::Calendar* cal = formatter.getCalendar(); |
| 172 year_ = ExtractValue(cal, UCAL_YEAR); | 172 year_ = ExtractValue(cal, UCAL_YEAR); |
| 173 month_ = ExtractValue(cal, UCAL_MONTH); | 173 month_ = ExtractValue(cal, UCAL_MONTH); |
| 174 day_ = ExtractValue(cal, UCAL_DATE); | 174 day_ = ExtractValue(cal, UCAL_DATE); |
| 175 hour_ = ExtractValue(cal, UCAL_HOUR_OF_DAY); // 24h format | 175 hour_ = ExtractValue(cal, UCAL_HOUR_OF_DAY); // 24h format |
| 176 minute_ = ExtractValue(cal, UCAL_MINUTE); | 176 minute_ = ExtractValue(cal, UCAL_MINUTE); |
| 177 second_ = ExtractValue(cal, UCAL_SECOND); | 177 second_ = ExtractValue(cal, UCAL_SECOND); |
| 178 } | 178 } |
| 179 } | 179 } |
| 180 | 180 |
| 181 return (success <= U_ZERO_ERROR); | 181 return (success <= U_ZERO_ERROR); |
| 182 } | 182 } |
| 183 | 183 |
| 184 void DateTimeFormatter::ClearAll() { | 184 void DateTimeFormatter::ClearAll() { |
| 185 year_ = 0; | 185 year_ = 0; |
| 186 month_ = 0; | 186 month_ = 0; |
| 187 day_ = 0; | 187 day_ = 0; |
| 188 hour_ = 0; | 188 hour_ = 0; |
| 189 minute_ = 0; | 189 minute_ = 0; |
| 190 second_ = 0; | 190 second_ = 0; |
| 191 } | 191 } |
| 192 | 192 |
| 193 } // namespace content | 193 } // namespace content |
| OLD | NEW |