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

Side by Side Diff: content/renderer/date_time_formatter.cc

Issue 15057004: Week picker for android (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 7 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
OLDNEW
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 "base/utf_string_conversions.h"
9 #include "third_party/WebKit/Source/Platform/chromium/public/WebCString.h" 9 #include "third_party/WebKit/Source/Platform/chromium/public/WebCString.h"
10 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDateTimeChooserPar ams.h" 10 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDateTimeChooserPar ams.h"
11 #include "third_party/icu/public/i18n/unicode/smpdtfmt.h" 11 #include "third_party/icu/public/i18n/unicode/smpdtfmt.h"
12 12
13 13
14 namespace content { 14 namespace content {
15 15
16 void DateTimeFormatter::CreatePatternMap() { 16 void DateTimeFormatter::CreatePatternMap() {
17 // Initialize all the UI elements with empty patterns, 17 // Initialize all the UI elements with empty patterns,
18 // 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
19 // are implemented. 19 // are implemented.
20 for (int i = 0 ; i <= ui::TEXT_INPUT_TYPE_MAX; ++i) { 20 for (int i = 0 ; i <= ui::TEXT_INPUT_TYPE_MAX; ++i) {
21 patterns_[i] = ""; 21 patterns_[i] = "";
22 } 22 }
23 patterns_[ui::TEXT_INPUT_TYPE_DATE] = "yyyy-MM-dd"; 23 patterns_[ui::TEXT_INPUT_TYPE_DATE] = "yyyy-MM-dd";
24 patterns_[ui::TEXT_INPUT_TYPE_DATE_TIME] = "yyyy-MM-dd'T'HH:mm'Z'"; 24 patterns_[ui::TEXT_INPUT_TYPE_DATE_TIME] = "yyyy-MM-dd'T'HH:mm'Z'";
25 patterns_[ui::TEXT_INPUT_TYPE_DATE_TIME_LOCAL] = "yyyy-MM-dd'T'HH:mm"; 25 patterns_[ui::TEXT_INPUT_TYPE_DATE_TIME_LOCAL] = "yyyy-MM-dd'T'HH:mm";
26 patterns_[ui::TEXT_INPUT_TYPE_MONTH] = "yyyy-MM"; 26 patterns_[ui::TEXT_INPUT_TYPE_MONTH] = "yyyy-MM";
27 patterns_[ui::TEXT_INPUT_TYPE_TIME] = "HH:mm"; 27 patterns_[ui::TEXT_INPUT_TYPE_TIME] = "HH:mm";
28 patterns_[ui::TEXT_INPUT_TYPE_WEEK] = "Y-'W'ww";
28 } 29 }
29 30
30 DateTimeFormatter::DateTimeFormatter( 31 DateTimeFormatter::DateTimeFormatter(
31 const WebKit::WebDateTimeChooserParams& source) 32 const WebKit::WebDateTimeChooserParams& source)
32 : formatted_string_(source.currentValue.utf8()) { 33 : formatted_string_(source.currentValue.utf8()) {
33 CreatePatternMap(); 34 CreatePatternMap();
34 ExtractType(source); 35 ExtractType(source);
35 if (!ParseValues()) { 36 if (!ParseValues()) {
36 type_ = ui::TEXT_INPUT_TYPE_NONE; 37 type_ = ui::TEXT_INPUT_TYPE_NONE;
37 ClearAll(); 38 ClearAll();
38 LOG(WARNING) << "Problems parsing input <" << formatted_string_ << ">"; 39 LOG(WARNING) << "Problems parsing input <" << formatted_string_ << ">";
39 } 40 }
40 } 41 }
41 42
42 DateTimeFormatter::DateTimeFormatter( 43 DateTimeFormatter::DateTimeFormatter(
43 ui::TextInputType type, 44 ui::TextInputType type,
44 int year, int month, int day, int hour, int minute, int second) 45 int year, int month, int day, int hour, int minute, int second,
46 int week_year, int week)
45 : type_(type), 47 : type_(type),
46 year_(year), 48 year_(year),
47 month_(month), 49 month_(month),
48 day_(day), 50 day_(day),
49 hour_(hour), 51 hour_(hour),
50 minute_(minute), 52 minute_(minute),
51 second_(second) { 53 second_(second),
54 week_year_(week_year),
55 week_(week) {
52 CreatePatternMap(); 56 CreatePatternMap();
53 pattern_ = type_ > 0 && type_ <= ui::TEXT_INPUT_TYPE_MAX ? 57 pattern_ = type_ > 0 && type_ <= ui::TEXT_INPUT_TYPE_MAX ?
54 &patterns_[type_] : &patterns_[ui::TEXT_INPUT_TYPE_NONE]; 58 &patterns_[type_] : &patterns_[ui::TEXT_INPUT_TYPE_NONE];
55 59
56 formatted_string_ = FormatString(); 60 formatted_string_ = FormatString();
57 } 61 }
58 62
59 DateTimeFormatter::~DateTimeFormatter() { 63 DateTimeFormatter::~DateTimeFormatter() {
60 } 64 }
61 65
(...skipping 14 matching lines...) Expand all
76 } 80 }
77 81
78 int DateTimeFormatter::GetMinute() const { 82 int DateTimeFormatter::GetMinute() const {
79 return minute_; 83 return minute_;
80 } 84 }
81 85
82 int DateTimeFormatter::GetSecond() const { 86 int DateTimeFormatter::GetSecond() const {
83 return second_; 87 return second_;
84 } 88 }
85 89
90 int DateTimeFormatter::GetWeekYear() const {
91 return week_year_;
92 }
93
94 int DateTimeFormatter::GetWeek() const {
95 return week_;
96 }
97
86 ui::TextInputType DateTimeFormatter::GetType() const { 98 ui::TextInputType DateTimeFormatter::GetType() const {
87 return type_; 99 return type_;
88 } 100 }
89 101
90 const std::string& DateTimeFormatter::GetFormattedValue() const { 102 const std::string& DateTimeFormatter::GetFormattedValue() const {
91 return formatted_string_; 103 return formatted_string_;
92 } 104 }
93 105
94 const std::string DateTimeFormatter::FormatString() const { 106 const std::string DateTimeFormatter::FormatString() const {
95 UErrorCode success = U_ZERO_ERROR; 107 UErrorCode success = U_ZERO_ERROR;
96 if (year_ == 0 && month_ == 0 && day_ == 0 && 108 if (year_ == 0 && month_ == 0 && day_ == 0 &&
97 hour_ == 0 && minute_ == 0 && second_ == 0) { 109 hour_ == 0 && minute_ == 0 && second_ == 0 &&
110 week_year_ == 0 && week_ == 0) {
98 return std::string(); 111 return std::string();
99 } 112 }
100 113
101 std::string result; 114 std::string result;
102 const icu::GregorianCalendar calendar( 115 icu::GregorianCalendar calendar(success);
103 year_, month_, day_, hour_, minute_, second_, success); 116 if (type_ == ui::TEXT_INPUT_TYPE_WEEK) {
Miguel Garcia 2013/05/10 15:17:59 you probably want to check that success is still t
keishi 2013/05/14 13:47:47 Done.
117 calendar.setMinimalDaysInFirstWeek(4);
Miguel Garcia 2013/05/10 15:17:59 I think some comment on why this is needed would b
keishi 2013/05/14 13:47:47 Done.
118 calendar.setFirstDayOfWeek(UCAL_MONDAY);
Miguel Garcia 2013/05/10 15:17:59 Doesn't this depend on the locale?
keishi 2013/05/14 13:47:47 ISO weeks always start from Monday regardless of l
119 calendar.set(UCAL_YEAR_WOY, week_year_);
120 calendar.set(UCAL_WEEK_OF_YEAR, week_);
121 } else {
Miguel Garcia 2013/05/10 15:17:59 it seems you can anyway set year, month... in all
keishi 2013/05/14 13:47:47 I may not be understanding you correctly. Are you
Miguel Garcia 2013/05/17 11:23:38 I'm still hesitant to special case week here, can
keishi 2013/05/22 07:33:26 calendar.getTime() will return milliseconds since
Miguel Garcia 2013/05/23 18:38:49 Ok, I think you've convinced me :) On 2013/05/22
122 calendar.set(UCAL_YEAR, year_);
123 calendar.set(UCAL_MONTH, month_);
124 calendar.set(UCAL_DATE, day_);
125 calendar.set(UCAL_HOUR_OF_DAY, hour_);
126 calendar.set(UCAL_MINUTE, minute_);
127 calendar.set(UCAL_SECOND, second_);
128 }
104 if (success <= U_ZERO_ERROR) { 129 if (success <= U_ZERO_ERROR) {
105 UDate time = calendar.getTime(success); 130 UDate time = calendar.getTime(success);
106 icu::SimpleDateFormat formatter(*pattern_, success); 131 icu::SimpleDateFormat formatter(*pattern_, success);
107 icu::UnicodeString formatted_time; 132 icu::UnicodeString formatted_time;
108 formatter.format(time, formatted_time, success); 133 formatter.format(time, formatted_time, success);
109 UTF16ToUTF8(formatted_time.getBuffer(), 134 UTF16ToUTF8(formatted_time.getBuffer(),
110 static_cast<size_t>(formatted_time.length()), 135 static_cast<size_t>(formatted_time.length()),
111 &result); 136 &result);
112 if (success <= U_ZERO_ERROR) 137 if (success <= U_ZERO_ERROR)
113 return result; 138 return result;
(...skipping 14 matching lines...) Expand all
128 case WebKit::WebDateTimeInputTypeDateTimeLocal: 153 case WebKit::WebDateTimeInputTypeDateTimeLocal:
129 type_ = ui::TEXT_INPUT_TYPE_DATE_TIME_LOCAL; 154 type_ = ui::TEXT_INPUT_TYPE_DATE_TIME_LOCAL;
130 break; 155 break;
131 case WebKit::WebDateTimeInputTypeMonth: 156 case WebKit::WebDateTimeInputTypeMonth:
132 type_ = ui::TEXT_INPUT_TYPE_MONTH; 157 type_ = ui::TEXT_INPUT_TYPE_MONTH;
133 break; 158 break;
134 case WebKit::WebDateTimeInputTypeTime: 159 case WebKit::WebDateTimeInputTypeTime:
135 type_ = ui::TEXT_INPUT_TYPE_TIME; 160 type_ = ui::TEXT_INPUT_TYPE_TIME;
136 break; 161 break;
137 case WebKit::WebDateTimeInputTypeWeek: // Not implemented 162 case WebKit::WebDateTimeInputTypeWeek: // Not implemented
163 type_ = ui::TEXT_INPUT_TYPE_WEEK;
164 break;
138 case WebKit::WebDateTimeInputTypeNone: 165 case WebKit::WebDateTimeInputTypeNone:
139 default: 166 default:
140 type_ = ui::TEXT_INPUT_TYPE_NONE; 167 type_ = ui::TEXT_INPUT_TYPE_NONE;
141 } 168 }
142 } 169 }
143 170
144 // Not all fields are defined in all configurations and ICU might store 171 // Not all fields are defined in all configurations and ICU might store
145 // garbage if success <= U_ZERO_ERROR so the output is sanitized here. 172 // garbage if success <= U_ZERO_ERROR so the output is sanitized here.
146 int DateTimeFormatter::ExtractValue( 173 int DateTimeFormatter::ExtractValue(
147 const icu::Calendar* calendar, UCalendarDateFields value) const { 174 const icu::Calendar* calendar, UCalendarDateFields value) const {
(...skipping 21 matching lines...) Expand all
169 icu::SimpleDateFormat formatter(pattern, success); 196 icu::SimpleDateFormat formatter(pattern, success);
170 formatter.parse(icu_value, success); 197 formatter.parse(icu_value, success);
171 if (success <= U_ZERO_ERROR) { 198 if (success <= U_ZERO_ERROR) {
172 const icu::Calendar* cal = formatter.getCalendar(); 199 const icu::Calendar* cal = formatter.getCalendar();
173 year_ = ExtractValue(cal, UCAL_YEAR); 200 year_ = ExtractValue(cal, UCAL_YEAR);
174 month_ = ExtractValue(cal, UCAL_MONTH); 201 month_ = ExtractValue(cal, UCAL_MONTH);
175 day_ = ExtractValue(cal, UCAL_DATE); 202 day_ = ExtractValue(cal, UCAL_DATE);
176 hour_ = ExtractValue(cal, UCAL_HOUR_OF_DAY); // 24h format 203 hour_ = ExtractValue(cal, UCAL_HOUR_OF_DAY); // 24h format
177 minute_ = ExtractValue(cal, UCAL_MINUTE); 204 minute_ = ExtractValue(cal, UCAL_MINUTE);
178 second_ = ExtractValue(cal, UCAL_SECOND); 205 second_ = ExtractValue(cal, UCAL_SECOND);
206 week_year_ = ExtractValue(cal, UCAL_YEAR_WOY);
207 week_ = ExtractValue(cal, UCAL_WEEK_OF_YEAR);
179 } 208 }
180 } 209 }
181 210
182 return (success <= U_ZERO_ERROR); 211 return (success <= U_ZERO_ERROR);
183 } 212 }
184 213
185 void DateTimeFormatter::ClearAll() { 214 void DateTimeFormatter::ClearAll() {
186 year_ = 0; 215 year_ = 0;
187 month_ = 0; 216 month_ = 0;
188 day_ = 0; 217 day_ = 0;
189 hour_ = 0; 218 hour_ = 0;
190 minute_ = 0; 219 minute_ = 0;
191 second_ = 0; 220 second_ = 0;
221 week_year_ = 0;
222 week_ = 0;
192 } 223 }
193 224
194 } // namespace content 225 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698