OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "chrome/common/time_format.h" | 5 #include "chrome/common/time_format.h" |
6 | 6 |
7 #include <vector> | 7 #include <vector> |
8 | 8 |
9 #include "base/lazy_instance.h" | 9 #include "base/lazy_instance.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
72 IDS_TIME_REMAINING_HOURS_ZERO, IDS_TIME_REMAINING_HOURS_TWO, | 72 IDS_TIME_REMAINING_HOURS_ZERO, IDS_TIME_REMAINING_HOURS_TWO, |
73 IDS_TIME_REMAINING_HOURS_FEW, IDS_TIME_REMAINING_HOURS_MANY | 73 IDS_TIME_REMAINING_HOURS_FEW, IDS_TIME_REMAINING_HOURS_MANY |
74 }, | 74 }, |
75 { | 75 { |
76 IDS_TIME_REMAINING_DAYS_DEFAULT, IDS_TIME_REMAINING_DAY_SINGULAR, | 76 IDS_TIME_REMAINING_DAYS_DEFAULT, IDS_TIME_REMAINING_DAY_SINGULAR, |
77 IDS_TIME_REMAINING_DAYS_ZERO, IDS_TIME_REMAINING_DAYS_TWO, | 77 IDS_TIME_REMAINING_DAYS_ZERO, IDS_TIME_REMAINING_DAYS_TWO, |
78 IDS_TIME_REMAINING_DAYS_FEW, IDS_TIME_REMAINING_DAYS_MANY | 78 IDS_TIME_REMAINING_DAYS_FEW, IDS_TIME_REMAINING_DAYS_MANY |
79 } | 79 } |
80 } }; | 80 } }; |
81 | 81 |
| 82 static const MessageIDs kTimeRemainingLongMessageIDs = { { |
| 83 { |
| 84 IDS_TIME_REMAINING_SECS_DEFAULT, IDS_TIME_REMAINING_SEC_SINGULAR, |
| 85 IDS_TIME_REMAINING_SECS_ZERO, IDS_TIME_REMAINING_SECS_TWO, |
| 86 IDS_TIME_REMAINING_SECS_FEW, IDS_TIME_REMAINING_SECS_MANY |
| 87 }, |
| 88 { |
| 89 IDS_TIME_REMAINING_LONG_MINS_DEFAULT, IDS_TIME_REMAINING_LONG_MIN_SINGULAR, |
| 90 IDS_TIME_REMAINING_LONG_MINS_ZERO, IDS_TIME_REMAINING_LONG_MINS_TWO, |
| 91 IDS_TIME_REMAINING_LONG_MINS_FEW, IDS_TIME_REMAINING_LONG_MINS_MANY |
| 92 }, |
| 93 { |
| 94 IDS_TIME_REMAINING_HOURS_DEFAULT, IDS_TIME_REMAINING_HOUR_SINGULAR, |
| 95 IDS_TIME_REMAINING_HOURS_ZERO, IDS_TIME_REMAINING_HOURS_TWO, |
| 96 IDS_TIME_REMAINING_HOURS_FEW, IDS_TIME_REMAINING_HOURS_MANY |
| 97 }, |
| 98 { |
| 99 IDS_TIME_REMAINING_DAYS_DEFAULT, IDS_TIME_REMAINING_DAY_SINGULAR, |
| 100 IDS_TIME_REMAINING_DAYS_ZERO, IDS_TIME_REMAINING_DAYS_TWO, |
| 101 IDS_TIME_REMAINING_DAYS_FEW, IDS_TIME_REMAINING_DAYS_MANY |
| 102 } |
| 103 } }; |
| 104 |
82 static const MessageIDs kTimeElapsedMessageIDs = { { | 105 static const MessageIDs kTimeElapsedMessageIDs = { { |
83 { | 106 { |
84 IDS_TIME_ELAPSED_SECS_DEFAULT, IDS_TIME_ELAPSED_SEC_SINGULAR, | 107 IDS_TIME_ELAPSED_SECS_DEFAULT, IDS_TIME_ELAPSED_SEC_SINGULAR, |
85 IDS_TIME_ELAPSED_SECS_ZERO, IDS_TIME_ELAPSED_SECS_TWO, | 108 IDS_TIME_ELAPSED_SECS_ZERO, IDS_TIME_ELAPSED_SECS_TWO, |
86 IDS_TIME_ELAPSED_SECS_FEW, IDS_TIME_ELAPSED_SECS_MANY | 109 IDS_TIME_ELAPSED_SECS_FEW, IDS_TIME_ELAPSED_SECS_MANY |
87 }, | 110 }, |
88 { | 111 { |
89 IDS_TIME_ELAPSED_MINS_DEFAULT, IDS_TIME_ELAPSED_MIN_SINGULAR, | 112 IDS_TIME_ELAPSED_MINS_DEFAULT, IDS_TIME_ELAPSED_MIN_SINGULAR, |
90 IDS_TIME_ELAPSED_MINS_ZERO, IDS_TIME_ELAPSED_MINS_TWO, | 113 IDS_TIME_ELAPSED_MINS_ZERO, IDS_TIME_ELAPSED_MINS_TWO, |
91 IDS_TIME_ELAPSED_MINS_FEW, IDS_TIME_ELAPSED_MINS_MANY | 114 IDS_TIME_ELAPSED_MINS_FEW, IDS_TIME_ELAPSED_MINS_MANY |
92 }, | 115 }, |
93 { | 116 { |
94 IDS_TIME_ELAPSED_HOURS_DEFAULT, IDS_TIME_ELAPSED_HOUR_SINGULAR, | 117 IDS_TIME_ELAPSED_HOURS_DEFAULT, IDS_TIME_ELAPSED_HOUR_SINGULAR, |
95 IDS_TIME_ELAPSED_HOURS_ZERO, IDS_TIME_ELAPSED_HOURS_TWO, | 118 IDS_TIME_ELAPSED_HOURS_ZERO, IDS_TIME_ELAPSED_HOURS_TWO, |
96 IDS_TIME_ELAPSED_HOURS_FEW, IDS_TIME_ELAPSED_HOURS_MANY | 119 IDS_TIME_ELAPSED_HOURS_FEW, IDS_TIME_ELAPSED_HOURS_MANY |
97 }, | 120 }, |
98 { | 121 { |
99 IDS_TIME_ELAPSED_DAYS_DEFAULT, IDS_TIME_ELAPSED_DAY_SINGULAR, | 122 IDS_TIME_ELAPSED_DAYS_DEFAULT, IDS_TIME_ELAPSED_DAY_SINGULAR, |
100 IDS_TIME_ELAPSED_DAYS_ZERO, IDS_TIME_ELAPSED_DAYS_TWO, | 123 IDS_TIME_ELAPSED_DAYS_ZERO, IDS_TIME_ELAPSED_DAYS_TWO, |
101 IDS_TIME_ELAPSED_DAYS_FEW, IDS_TIME_ELAPSED_DAYS_MANY | 124 IDS_TIME_ELAPSED_DAYS_FEW, IDS_TIME_ELAPSED_DAYS_MANY |
102 } | 125 } |
103 } }; | 126 } }; |
104 | 127 |
105 // Different format types. | 128 // Different format types. |
106 enum FormatType { | 129 enum FormatType { |
107 FORMAT_SHORT, | 130 FORMAT_SHORT, |
108 FORMAT_REMAINING, | 131 FORMAT_REMAINING, |
| 132 FORMAT_REMAINING_LONG, |
109 FORMAT_ELAPSED, | 133 FORMAT_ELAPSED, |
110 }; | 134 }; |
111 | 135 |
112 } // namespace | 136 } // namespace |
113 | 137 |
114 class TimeFormatter { | 138 class TimeFormatter { |
115 public: | 139 public: |
116 const std::vector<icu::PluralFormat*>& formatter(FormatType format_type) { | 140 const std::vector<icu::PluralFormat*>& formatter(FormatType format_type) { |
117 switch (format_type) { | 141 switch (format_type) { |
118 case FORMAT_SHORT: | 142 case FORMAT_SHORT: |
119 return short_formatter_; | 143 return short_formatter_; |
120 case FORMAT_REMAINING: | 144 case FORMAT_REMAINING: |
121 return time_left_formatter_; | 145 return time_left_formatter_; |
| 146 case FORMAT_REMAINING_LONG: |
| 147 return time_left_long_formatter_; |
122 case FORMAT_ELAPSED: | 148 case FORMAT_ELAPSED: |
123 return time_elapsed_formatter_; | 149 return time_elapsed_formatter_; |
124 default: | 150 default: |
125 NOTREACHED(); | 151 NOTREACHED(); |
126 return short_formatter_; | 152 return short_formatter_; |
127 } | 153 } |
128 } | 154 } |
129 private: | 155 private: |
130 static const MessageIDs& GetMessageIDs(FormatType format_type) { | 156 static const MessageIDs& GetMessageIDs(FormatType format_type) { |
131 switch (format_type) { | 157 switch (format_type) { |
132 case FORMAT_SHORT: | 158 case FORMAT_SHORT: |
133 return kTimeShortMessageIDs; | 159 return kTimeShortMessageIDs; |
134 case FORMAT_REMAINING: | 160 case FORMAT_REMAINING: |
135 return kTimeRemainingMessageIDs; | 161 return kTimeRemainingMessageIDs; |
| 162 case FORMAT_REMAINING_LONG: |
| 163 return kTimeRemainingLongMessageIDs; |
136 case FORMAT_ELAPSED: | 164 case FORMAT_ELAPSED: |
137 return kTimeElapsedMessageIDs; | 165 return kTimeElapsedMessageIDs; |
138 default: | 166 default: |
139 NOTREACHED(); | 167 NOTREACHED(); |
140 return kTimeShortMessageIDs; | 168 return kTimeShortMessageIDs; |
141 } | 169 } |
142 } | 170 } |
143 | 171 |
144 static const char* GetFallbackFormatSuffix(FormatType format_type) { | 172 static const char* GetFallbackFormatSuffix(FormatType format_type) { |
145 switch (format_type) { | 173 switch (format_type) { |
146 case FORMAT_SHORT: | 174 case FORMAT_SHORT: |
147 return kFallbackFormatSuffixShort; | 175 return kFallbackFormatSuffixShort; |
148 case FORMAT_REMAINING: | 176 case FORMAT_REMAINING: |
| 177 case FORMAT_REMAINING_LONG: |
149 return kFallbackFormatSuffixLeft; | 178 return kFallbackFormatSuffixLeft; |
150 case FORMAT_ELAPSED: | 179 case FORMAT_ELAPSED: |
151 return kFallbackFormatSuffixAgo; | 180 return kFallbackFormatSuffixAgo; |
152 default: | 181 default: |
153 NOTREACHED(); | 182 NOTREACHED(); |
154 return kFallbackFormatSuffixShort; | 183 return kFallbackFormatSuffixShort; |
155 } | 184 } |
156 } | 185 } |
157 | 186 |
158 TimeFormatter() { | 187 TimeFormatter() { |
159 BuildFormats(FORMAT_SHORT, &short_formatter_); | 188 BuildFormats(FORMAT_SHORT, &short_formatter_); |
160 BuildFormats(FORMAT_REMAINING, &time_left_formatter_); | 189 BuildFormats(FORMAT_REMAINING, &time_left_formatter_); |
| 190 BuildFormats(FORMAT_REMAINING_LONG, &time_left_long_formatter_); |
161 BuildFormats(FORMAT_ELAPSED, &time_elapsed_formatter_); | 191 BuildFormats(FORMAT_ELAPSED, &time_elapsed_formatter_); |
162 } | 192 } |
163 ~TimeFormatter() { | 193 ~TimeFormatter() { |
164 STLDeleteContainerPointers(short_formatter_.begin(), | 194 STLDeleteContainerPointers(short_formatter_.begin(), |
165 short_formatter_.end()); | 195 short_formatter_.end()); |
166 STLDeleteContainerPointers(time_left_formatter_.begin(), | 196 STLDeleteContainerPointers(time_left_formatter_.begin(), |
167 time_left_formatter_.end()); | 197 time_left_formatter_.end()); |
| 198 STLDeleteContainerPointers(time_left_long_formatter_.begin(), |
| 199 time_left_long_formatter_.end()); |
168 STLDeleteContainerPointers(time_elapsed_formatter_.begin(), | 200 STLDeleteContainerPointers(time_elapsed_formatter_.begin(), |
169 time_elapsed_formatter_.end()); | 201 time_elapsed_formatter_.end()); |
170 } | 202 } |
171 friend struct base::DefaultLazyInstanceTraits<TimeFormatter>; | 203 friend struct base::DefaultLazyInstanceTraits<TimeFormatter>; |
172 | 204 |
173 std::vector<icu::PluralFormat*> short_formatter_; | 205 std::vector<icu::PluralFormat*> short_formatter_; |
174 std::vector<icu::PluralFormat*> time_left_formatter_; | 206 std::vector<icu::PluralFormat*> time_left_formatter_; |
| 207 std::vector<icu::PluralFormat*> time_left_long_formatter_; |
175 std::vector<icu::PluralFormat*> time_elapsed_formatter_; | 208 std::vector<icu::PluralFormat*> time_elapsed_formatter_; |
176 static void BuildFormats(FormatType format_type, | 209 static void BuildFormats(FormatType format_type, |
177 std::vector<icu::PluralFormat*>* time_formats); | 210 std::vector<icu::PluralFormat*>* time_formats); |
178 static icu::PluralFormat* createFallbackFormat( | 211 static icu::PluralFormat* createFallbackFormat( |
179 const icu::PluralRules& rules, int index, FormatType format_type); | 212 const icu::PluralRules& rules, int index, FormatType format_type); |
180 | 213 |
181 DISALLOW_COPY_AND_ASSIGN(TimeFormatter); | 214 DISALLOW_COPY_AND_ASSIGN(TimeFormatter); |
182 }; | 215 }; |
183 | 216 |
184 static base::LazyInstance<TimeFormatter> g_time_formatter = | 217 static base::LazyInstance<TimeFormatter> g_time_formatter = |
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
308 string16 TimeFormat::TimeElapsed(const TimeDelta& delta) { | 341 string16 TimeFormat::TimeElapsed(const TimeDelta& delta) { |
309 return FormatTimeImpl(delta, FORMAT_ELAPSED); | 342 return FormatTimeImpl(delta, FORMAT_ELAPSED); |
310 } | 343 } |
311 | 344 |
312 // static | 345 // static |
313 string16 TimeFormat::TimeRemaining(const TimeDelta& delta) { | 346 string16 TimeFormat::TimeRemaining(const TimeDelta& delta) { |
314 return FormatTimeImpl(delta, FORMAT_REMAINING); | 347 return FormatTimeImpl(delta, FORMAT_REMAINING); |
315 } | 348 } |
316 | 349 |
317 // static | 350 // static |
| 351 string16 TimeFormat::TimeRemainingLong(const TimeDelta& delta) { |
| 352 return FormatTimeImpl(delta, FORMAT_REMAINING_LONG); |
| 353 } |
| 354 |
| 355 // static |
318 string16 TimeFormat::TimeRemainingShort(const TimeDelta& delta) { | 356 string16 TimeFormat::TimeRemainingShort(const TimeDelta& delta) { |
319 return FormatTimeImpl(delta, FORMAT_SHORT); | 357 return FormatTimeImpl(delta, FORMAT_SHORT); |
320 } | 358 } |
321 | 359 |
322 // static | 360 // static |
323 string16 TimeFormat::RelativeDate( | 361 string16 TimeFormat::RelativeDate( |
324 const Time& time, | 362 const Time& time, |
325 const Time* optional_midnight_today) { | 363 const Time* optional_midnight_today) { |
326 Time midnight_today = optional_midnight_today ? *optional_midnight_today : | 364 Time midnight_today = optional_midnight_today ? *optional_midnight_today : |
327 Time::Now().LocalMidnight(); | 365 Time::Now().LocalMidnight(); |
328 TimeDelta day = TimeDelta::FromMicroseconds(Time::kMicrosecondsPerDay); | 366 TimeDelta day = TimeDelta::FromMicroseconds(Time::kMicrosecondsPerDay); |
329 Time tomorrow = midnight_today + day; | 367 Time tomorrow = midnight_today + day; |
330 Time yesterday = midnight_today - day; | 368 Time yesterday = midnight_today - day; |
331 if (time >= tomorrow) | 369 if (time >= tomorrow) |
332 return string16(); | 370 return string16(); |
333 else if (time >= midnight_today) | 371 else if (time >= midnight_today) |
334 return l10n_util::GetStringUTF16(IDS_PAST_TIME_TODAY); | 372 return l10n_util::GetStringUTF16(IDS_PAST_TIME_TODAY); |
335 else if (time >= yesterday) | 373 else if (time >= yesterday) |
336 return l10n_util::GetStringUTF16(IDS_PAST_TIME_YESTERDAY); | 374 return l10n_util::GetStringUTF16(IDS_PAST_TIME_YESTERDAY); |
337 return string16(); | 375 return string16(); |
338 } | 376 } |
OLD | NEW |