| 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 // Android WebView builds with the system ICU which is different |
| 110 // from Chromium's ICU; as such we can't easily get a UTF8 string |
| 111 // from formattedTime but we can go round-the-houses a bit. |
| 112 result = UTF16ToUTF8(string16(formattedTime.getBuffer(), |
| 113 static_cast<size_t>(formattedTime.length()))); |
| 111 if (success <= U_ZERO_ERROR) | 114 if (success <= U_ZERO_ERROR) |
| 112 return result; | 115 return result; |
| 113 } | 116 } |
| 114 LOG(WARNING) << "Calendar not created: error " << success; | 117 LOG(WARNING) << "Calendar not created: error " << success; |
| 115 return ""; | 118 return ""; |
| 116 } | 119 } |
| 117 | 120 |
| 118 void DateTimeFormatter::ExtractType( | 121 void DateTimeFormatter::ExtractType( |
| 119 const WebKit::WebDateTimeChooserParams& source) { | 122 const WebKit::WebDateTimeChooserParams& source) { |
| 120 switch (source.type) { | 123 switch (source.type) { |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 155 return false; | 158 return false; |
| 156 } | 159 } |
| 157 | 160 |
| 158 if (formatted_string_.empty()) { | 161 if (formatted_string_.empty()) { |
| 159 ClearAll(); | 162 ClearAll(); |
| 160 return true; | 163 return true; |
| 161 } | 164 } |
| 162 | 165 |
| 163 UErrorCode success = U_ZERO_ERROR; | 166 UErrorCode success = U_ZERO_ERROR; |
| 164 icu::UnicodeString icu_value = | 167 icu::UnicodeString icu_value = |
| 165 icu::UnicodeString::fromUTF8(formatted_string_); | 168 icu::UnicodeString::fromUTF8(formatted_string_.c_str()); |
| 166 if (type_ > 0 && type_ <= ui::TEXT_INPUT_TYPE_MAX) { | 169 if (type_ > 0 && type_ <= ui::TEXT_INPUT_TYPE_MAX) { |
| 167 const icu::UnicodeString pattern = patterns_[type_]; | 170 const icu::UnicodeString pattern = patterns_[type_]; |
| 168 icu::SimpleDateFormat formatter(pattern, success); | 171 icu::SimpleDateFormat formatter(pattern, success); |
| 169 formatter.parse(icu_value, success); | 172 formatter.parse(icu_value, success); |
| 170 if (success <= U_ZERO_ERROR) { | 173 if (success <= U_ZERO_ERROR) { |
| 171 const icu::Calendar* cal = formatter.getCalendar(); | 174 const icu::Calendar* cal = formatter.getCalendar(); |
| 172 year_ = ExtractValue(cal, UCAL_YEAR); | 175 year_ = ExtractValue(cal, UCAL_YEAR); |
| 173 month_ = ExtractValue(cal, UCAL_MONTH); | 176 month_ = ExtractValue(cal, UCAL_MONTH); |
| 174 day_ = ExtractValue(cal, UCAL_DATE); | 177 day_ = ExtractValue(cal, UCAL_DATE); |
| 175 hour_ = ExtractValue(cal, UCAL_HOUR_OF_DAY); // 24h format | 178 hour_ = ExtractValue(cal, UCAL_HOUR_OF_DAY); // 24h format |
| 176 minute_ = ExtractValue(cal, UCAL_MINUTE); | 179 minute_ = ExtractValue(cal, UCAL_MINUTE); |
| 177 second_ = ExtractValue(cal, UCAL_SECOND); | 180 second_ = ExtractValue(cal, UCAL_SECOND); |
| 178 } | 181 } |
| 179 } | 182 } |
| 180 | 183 |
| 181 return (success <= U_ZERO_ERROR); | 184 return (success <= U_ZERO_ERROR); |
| 182 } | 185 } |
| 183 | 186 |
| 184 void DateTimeFormatter::ClearAll() { | 187 void DateTimeFormatter::ClearAll() { |
| 185 year_ = 0; | 188 year_ = 0; |
| 186 month_ = 0; | 189 month_ = 0; |
| 187 day_ = 0; | 190 day_ = 0; |
| 188 hour_ = 0; | 191 hour_ = 0; |
| 189 minute_ = 0; | 192 minute_ = 0; |
| 190 second_ = 0; | 193 second_ = 0; |
| 191 } | 194 } |
| 192 | 195 |
| 193 } // namespace content | 196 } // namespace content |
| OLD | NEW |