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

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: Removed week_year. Added BaseDatePicker. 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);
104 if (success <= U_ZERO_ERROR) { 116 if (success <= U_ZERO_ERROR) {
117 if (type_ == ui::TEXT_INPUT_TYPE_WEEK) {
118 // An ISO week starts with Monday.
119 calendar.setFirstDayOfWeek(UCAL_MONDAY);
120 // ISO 8601 defines that the week with the year's first Thursday is the
121 // first week.
122 calendar.setMinimalDaysInFirstWeek(4);
123 calendar.set(UCAL_YEAR_WOY, week_year_);
124 calendar.set(UCAL_WEEK_OF_YEAR, week_);
125 } else {
126 calendar.set(UCAL_YEAR, year_);
127 calendar.set(UCAL_MONTH, month_);
128 calendar.set(UCAL_DATE, day_);
129 calendar.set(UCAL_HOUR_OF_DAY, hour_);
130 calendar.set(UCAL_MINUTE, minute_);
131 calendar.set(UCAL_SECOND, second_);
132 }
105 UDate time = calendar.getTime(success); 133 UDate time = calendar.getTime(success);
106 icu::SimpleDateFormat formatter(*pattern_, success); 134 icu::SimpleDateFormat formatter(*pattern_, success);
107 icu::UnicodeString formatted_time; 135 icu::UnicodeString formatted_time;
108 formatter.format(time, formatted_time, success); 136 formatter.format(time, formatted_time, success);
109 UTF16ToUTF8(formatted_time.getBuffer(), 137 UTF16ToUTF8(formatted_time.getBuffer(),
110 static_cast<size_t>(formatted_time.length()), 138 static_cast<size_t>(formatted_time.length()),
111 &result); 139 &result);
112 if (success <= U_ZERO_ERROR) 140 if (success <= U_ZERO_ERROR)
113 return result; 141 return result;
114 } 142 }
(...skipping 12 matching lines...) Expand all
127 break; 155 break;
128 case WebKit::WebDateTimeInputTypeDateTimeLocal: 156 case WebKit::WebDateTimeInputTypeDateTimeLocal:
129 type_ = ui::TEXT_INPUT_TYPE_DATE_TIME_LOCAL; 157 type_ = ui::TEXT_INPUT_TYPE_DATE_TIME_LOCAL;
130 break; 158 break;
131 case WebKit::WebDateTimeInputTypeMonth: 159 case WebKit::WebDateTimeInputTypeMonth:
132 type_ = ui::TEXT_INPUT_TYPE_MONTH; 160 type_ = ui::TEXT_INPUT_TYPE_MONTH;
133 break; 161 break;
134 case WebKit::WebDateTimeInputTypeTime: 162 case WebKit::WebDateTimeInputTypeTime:
135 type_ = ui::TEXT_INPUT_TYPE_TIME; 163 type_ = ui::TEXT_INPUT_TYPE_TIME;
136 break; 164 break;
137 case WebKit::WebDateTimeInputTypeWeek: // Not implemented 165 case WebKit::WebDateTimeInputTypeWeek: // Not implemented
Miguel Garcia 2013/05/17 15:59:40 nit: remove the // Not implemented comment
keishi 2013/05/22 07:33:26 Done.
166 type_ = ui::TEXT_INPUT_TYPE_WEEK;
167 break;
138 case WebKit::WebDateTimeInputTypeNone: 168 case WebKit::WebDateTimeInputTypeNone:
139 default: 169 default:
140 type_ = ui::TEXT_INPUT_TYPE_NONE; 170 type_ = ui::TEXT_INPUT_TYPE_NONE;
141 } 171 }
142 } 172 }
143 173
144 // Not all fields are defined in all configurations and ICU might store 174 // 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. 175 // garbage if success <= U_ZERO_ERROR so the output is sanitized here.
146 int DateTimeFormatter::ExtractValue( 176 int DateTimeFormatter::ExtractValue(
147 const icu::Calendar* calendar, UCalendarDateFields value) const { 177 const icu::Calendar* calendar, UCalendarDateFields value) const {
(...skipping 21 matching lines...) Expand all
169 icu::SimpleDateFormat formatter(pattern, success); 199 icu::SimpleDateFormat formatter(pattern, success);
170 formatter.parse(icu_value, success); 200 formatter.parse(icu_value, success);
171 if (success <= U_ZERO_ERROR) { 201 if (success <= U_ZERO_ERROR) {
172 const icu::Calendar* cal = formatter.getCalendar(); 202 const icu::Calendar* cal = formatter.getCalendar();
173 year_ = ExtractValue(cal, UCAL_YEAR); 203 year_ = ExtractValue(cal, UCAL_YEAR);
174 month_ = ExtractValue(cal, UCAL_MONTH); 204 month_ = ExtractValue(cal, UCAL_MONTH);
175 day_ = ExtractValue(cal, UCAL_DATE); 205 day_ = ExtractValue(cal, UCAL_DATE);
176 hour_ = ExtractValue(cal, UCAL_HOUR_OF_DAY); // 24h format 206 hour_ = ExtractValue(cal, UCAL_HOUR_OF_DAY); // 24h format
177 minute_ = ExtractValue(cal, UCAL_MINUTE); 207 minute_ = ExtractValue(cal, UCAL_MINUTE);
178 second_ = ExtractValue(cal, UCAL_SECOND); 208 second_ = ExtractValue(cal, UCAL_SECOND);
209 week_year_ = ExtractValue(cal, UCAL_YEAR_WOY);
210 week_ = ExtractValue(cal, UCAL_WEEK_OF_YEAR);
179 } 211 }
180 } 212 }
181 213
182 return (success <= U_ZERO_ERROR); 214 return (success <= U_ZERO_ERROR);
183 } 215 }
184 216
185 void DateTimeFormatter::ClearAll() { 217 void DateTimeFormatter::ClearAll() {
186 year_ = 0; 218 year_ = 0;
187 month_ = 0; 219 month_ = 0;
188 day_ = 0; 220 day_ = 0;
189 hour_ = 0; 221 hour_ = 0;
190 minute_ = 0; 222 minute_ = 0;
191 second_ = 0; 223 second_ = 0;
224 week_year_ = 0;
225 week_ = 0;
192 } 226 }
193 227
194 } // namespace content 228 } // namespace content
OLDNEW
« no previous file with comments | « content/renderer/date_time_formatter.h ('k') | content/renderer/date_time_formatter_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698