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

Unified Diff: content/renderer/date_time_formatter.cc

Issue 23623019: Support datalist for date/time input types on Android (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Used double to transfer value Created 7 years, 2 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 side-by-side diff with in-line comments
Download patch
Index: content/renderer/date_time_formatter.cc
diff --git a/content/renderer/date_time_formatter.cc b/content/renderer/date_time_formatter.cc
index a0ce77b91cf84928a093d127208c573580389ec5..18d08a5bfade401035c0a7940e44c6a06001edd2 100644
--- a/content/renderer/date_time_formatter.cc
+++ b/content/renderer/date_time_formatter.cc
@@ -28,69 +28,6 @@ void DateTimeFormatter::CreatePatternMap() {
patterns_[ui::TEXT_INPUT_TYPE_WEEK] = "Y-'W'ww";
}
-// Returns true if icu_value parses as a valid for the specified date/time
-// pattern. The date/time pattern given is for icu::SimpleDateFormat.
-static bool TryPattern(const char* pattern,
- const icu::UnicodeString& icu_value) {
- icu::UnicodeString time_pattern = pattern;
- UErrorCode success = U_ZERO_ERROR;
- icu::SimpleDateFormat formatter(time_pattern, success);
- formatter.parse(icu_value, success);
- return success == U_ZERO_ERROR;
-}
-
-// For a time value represented as a string find the longest time
-// pattern which matches it. A valid time can have hours and minutes
-// or hours, minutes and seconds or hour, minutes, seconds and upto 3
-// digits of fractional seconds. Specify step in milliseconds, it is 1000
-// times the value specified as "step" in the "<input type=time step=...>
-// HTML fragment. A value of 60000 or more indicates that seconds
-// are not expected and a value of 1000 or more indicates that fractional
-// seconds are not expected.
-static const char* FindLongestTimePatternWhichMatches(const std::string& value,
- double step) {
- const char* pattern = "HH:mm";
- if (step >= 60000)
- return pattern;
-
- icu::UnicodeString icu_value = icu::UnicodeString::fromUTF8(
- icu::StringPiece(value.data(), value.size()));
- const char* last_pattern = pattern;
- pattern = "HH:mm:ss";
- if (!TryPattern(pattern, icu_value))
- return last_pattern;
- if (step >= 1000)
- return pattern;
- last_pattern = pattern;
- pattern = "HH:mm:ss.S";
- if (!TryPattern(pattern, icu_value))
- return last_pattern;
- last_pattern = pattern;
- pattern = "HH:mm:ss.SS";
- if (!TryPattern(pattern, icu_value))
- return last_pattern;
- last_pattern = pattern;
- pattern = "HH:mm:ss.SSS";
- if (!TryPattern(pattern, icu_value))
- return last_pattern;
- return pattern;
-}
-
-DateTimeFormatter::DateTimeFormatter(
- const WebKit::WebDateTimeChooserParams& source)
- : formatted_string_(source.currentValue.utf8()) {
- CreatePatternMap();
- if (source.type == WebKit::WebDateTimeInputTypeTime)
- time_pattern_ =
- FindLongestTimePatternWhichMatches(formatted_string_, source.step);
- ExtractType(source);
- if (!ParseValues()) {
- type_ = ui::TEXT_INPUT_TYPE_NONE;
- ClearAll();
- LOG(WARNING) << "Problems parsing input <" << formatted_string_ << ">";
- }
-}
-
DateTimeFormatter::DateTimeFormatter(ui::TextInputType type,
int year,
int month,
@@ -133,72 +70,55 @@ DateTimeFormatter::DateTimeFormatter(ui::TextInputType type,
DateTimeFormatter::~DateTimeFormatter() {
}
-int DateTimeFormatter::GetYear() const {
- return year_;
-}
-
-int DateTimeFormatter::GetMonth() const {
- return month_;
-}
-
-int DateTimeFormatter::GetDay() const {
- return day_;
-}
-
-int DateTimeFormatter::GetHour() const {
- return hour_;
-}
-
-int DateTimeFormatter::GetMinute() const {
- return minute_;
-}
-
-int DateTimeFormatter::GetSecond() const {
- return second_;
-}
-
-int DateTimeFormatter::GetMilli() const { return milli_; }
-
-int DateTimeFormatter::GetWeekYear() const { return week_year_; }
-
-int DateTimeFormatter::GetWeek() const {
- return week_;
-}
-
-ui::TextInputType DateTimeFormatter::GetType() const {
- return type_;
-}
-
const std::string& DateTimeFormatter::GetFormattedValue() const {
return formatted_string_;
}
const std::string DateTimeFormatter::FormatString() const {
UErrorCode success = U_ZERO_ERROR;
- if (year_ == 0 && month_ == 0 && day_ == 0 && hour_ == 0 && minute_ == 0 &&
- second_ == 0 && milli_ == 0 && week_year_ == 0 && week_ == 0) {
+ if (type_ == ui::TEXT_INPUT_TYPE_NONE) {
return std::string();
}
std::string result;
icu::GregorianCalendar calendar(success);
if (success <= U_ZERO_ERROR) {
- if (type_ == ui::TEXT_INPUT_TYPE_WEEK) {
- // An ISO week starts with Monday.
- calendar.setFirstDayOfWeek(UCAL_MONDAY);
- // ISO 8601 defines that the week with the year's first Thursday is the
- // first week.
- calendar.setMinimalDaysInFirstWeek(4);
- calendar.set(UCAL_YEAR_WOY, week_year_);
- calendar.set(UCAL_WEEK_OF_YEAR, week_);
- } else {
- calendar.set(UCAL_YEAR, year_);
- calendar.set(UCAL_MONTH, month_);
- calendar.set(UCAL_DATE, day_);
- calendar.set(UCAL_HOUR_OF_DAY, hour_);
- calendar.set(UCAL_MINUTE, minute_);
- calendar.set(UCAL_SECOND, second_);
- calendar.set(UCAL_MILLISECOND, milli_);
+ switch (type_) {
+ case ui::TEXT_INPUT_TYPE_WEEK:
+ // An ISO week starts with Monday.
+ calendar.setFirstDayOfWeek(UCAL_MONDAY);
+ // ISO 8601 defines that the week with the year's first Thursday is the
+ // first week.
+ calendar.setMinimalDaysInFirstWeek(4);
+ calendar.set(UCAL_YEAR_WOY, week_year_);
+ calendar.set(UCAL_WEEK_OF_YEAR, week_);
+ break;
+ case ui::TEXT_INPUT_TYPE_MONTH:
+ calendar.set(UCAL_YEAR, year_);
+ calendar.set(UCAL_MONTH, month_);
+ break;
+ case ui::TEXT_INPUT_TYPE_DATE:
+ calendar.set(UCAL_YEAR, year_);
+ calendar.set(UCAL_MONTH, month_);
+ calendar.set(UCAL_DATE, day_);
+ break;
+ case ui::TEXT_INPUT_TYPE_TIME:
+ calendar.set(UCAL_HOUR_OF_DAY, hour_);
+ calendar.set(UCAL_MINUTE, minute_);
+ calendar.set(UCAL_SECOND, second_);
+ calendar.set(UCAL_MILLISECOND, milli_);
+ break;
+ case ui::TEXT_INPUT_TYPE_DATE_TIME_LOCAL:
+ calendar.set(UCAL_YEAR, year_);
+ calendar.set(UCAL_MONTH, month_);
+ calendar.set(UCAL_DATE, day_);
+ calendar.set(UCAL_HOUR_OF_DAY, hour_);
+ calendar.set(UCAL_MINUTE, minute_);
+ calendar.set(UCAL_SECOND, second_);
+ calendar.set(UCAL_MILLISECOND, milli_);
+ break;
+ default:
+ NOTREACHED();
}
icu::SimpleDateFormat formatter(*pattern_, success);
icu::UnicodeString formatted_time;
@@ -213,88 +133,4 @@ const std::string DateTimeFormatter::FormatString() const {
return std::string();
}
-void DateTimeFormatter::ExtractType(
- const WebKit::WebDateTimeChooserParams& source) {
- switch (source.type) {
- case WebKit::WebDateTimeInputTypeDate:
- type_ = ui::TEXT_INPUT_TYPE_DATE;
- break;
- case WebKit::WebDateTimeInputTypeDateTime:
- type_ = ui::TEXT_INPUT_TYPE_DATE_TIME;
- break;
- case WebKit::WebDateTimeInputTypeDateTimeLocal:
- type_ = ui::TEXT_INPUT_TYPE_DATE_TIME_LOCAL;
- break;
- case WebKit::WebDateTimeInputTypeMonth:
- type_ = ui::TEXT_INPUT_TYPE_MONTH;
- break;
- case WebKit::WebDateTimeInputTypeTime:
- type_ = ui::TEXT_INPUT_TYPE_TIME;
- break;
- case WebKit::WebDateTimeInputTypeWeek:
- type_ = ui::TEXT_INPUT_TYPE_WEEK;
- break;
- case WebKit::WebDateTimeInputTypeNone:
- default:
- type_ = ui::TEXT_INPUT_TYPE_NONE;
- }
-}
-
-// Not all fields are defined in all configurations and ICU might store
-// garbage if success <= U_ZERO_ERROR so the output is sanitized here.
-int DateTimeFormatter::ExtractValue(
- const icu::Calendar* calendar, UCalendarDateFields value) const {
- UErrorCode success = U_ZERO_ERROR;
- int result = calendar->get(value, success);
- return (success <= U_ZERO_ERROR) ? result : 0;
-}
-
-bool DateTimeFormatter::ParseValues() {
- if (type_ == ui::TEXT_INPUT_TYPE_NONE) {
- ClearAll();
- return false;
- }
-
- if (formatted_string_.empty()) {
- ClearAll();
- return true;
- }
-
- UErrorCode success = U_ZERO_ERROR;
- icu::UnicodeString icu_value = icu::UnicodeString::fromUTF8(
- icu::StringPiece(formatted_string_.data(), formatted_string_.size()));
- if (type_ > 0 && type_ <= ui::TEXT_INPUT_TYPE_MAX) {
- const icu::UnicodeString pattern =
- type_ == ui::TEXT_INPUT_TYPE_TIME ? time_pattern_ : patterns_[type_];
- icu::SimpleDateFormat formatter(pattern, success);
- formatter.parse(icu_value, success);
- if (success <= U_ZERO_ERROR) {
- const icu::Calendar* cal = formatter.getCalendar();
- year_ = ExtractValue(cal, UCAL_YEAR);
- month_ = ExtractValue(cal, UCAL_MONTH);
- day_ = ExtractValue(cal, UCAL_DATE);
- hour_ = ExtractValue(cal, UCAL_HOUR_OF_DAY); // 24h format
- minute_ = ExtractValue(cal, UCAL_MINUTE);
- second_ = ExtractValue(cal, UCAL_SECOND);
- milli_ = ExtractValue(cal, UCAL_MILLISECOND);
- week_year_ = ExtractValue(cal, UCAL_YEAR_WOY);
- week_ = ExtractValue(cal, UCAL_WEEK_OF_YEAR);
- }
- }
-
- return (success <= U_ZERO_ERROR);
-}
-
-void DateTimeFormatter::ClearAll() {
- year_ = 0;
- month_ = 0;
- day_ = 0;
- hour_ = 0;
- minute_ = 0;
- second_ = 0;
- milli_ = 0;
- week_year_ = 0;
- week_ = 0;
-}
-
} // namespace content

Powered by Google App Engine
This is Rietveld 408576698