Chromium Code Reviews| 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 "ui/base/l10n/time_format.h" | 5 #include "ui/base/l10n/time_format.h" |
| 6 | 6 |
| 7 #include <vector> | 7 #include <limits> |
| 8 | 8 |
| 9 #include "base/lazy_instance.h" | 9 #include "base/lazy_instance.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "base/memory/scoped_ptr.h" | 11 #include "base/strings/string_util.h" |
| 12 #include "base/memory/scoped_vector.h" | |
| 13 #include "base/stl_util.h" | |
| 14 #include "base/strings/string16.h" | |
| 15 #include "base/strings/utf_string_conversions.h" | |
| 16 #include "base/time/time.h" | 12 #include "base/time/time.h" |
| 17 #include "grit/ui_strings.h" | 13 #include "grit/ui_strings.h" |
| 18 #include "third_party/icu/source/common/unicode/locid.h" | 14 #include "third_party/icu/source/common/unicode/unistr.h" |
| 19 #include "third_party/icu/source/i18n/unicode/datefmt.h" | 15 #include "ui/base/l10n/formatter.h" |
| 20 #include "third_party/icu/source/i18n/unicode/plurfmt.h" | |
| 21 #include "third_party/icu/source/i18n/unicode/plurrule.h" | |
| 22 #include "third_party/icu/source/i18n/unicode/smpdtfmt.h" | |
| 23 #include "ui/base/l10n/l10n_util.h" | 16 #include "ui/base/l10n/l10n_util.h" |
| 24 #include "ui/base/l10n/l10n_util_plurals.h" | |
| 25 | 17 |
| 26 using base::Time; | 18 using base::Time; |
| 27 using base::TimeDelta; | 19 using base::TimeDelta; |
| 28 | 20 |
| 29 namespace { | 21 using ui::TimeFormat; |
| 30 | 22 |
| 31 static const char kFallbackFormatSuffixAgo[] = " ago}"; | 23 namespace ui { |
| 32 static const char kFallbackFormatSuffixLeft[] = " left}"; | |
| 33 static const char kFallbackFormatSuffixDuration[] = "}"; | |
| 34 | 24 |
| 35 // Contains message IDs for various time units and pluralities. | 25 UI_BASE_EXPORT base::LazyInstance<FormatterContainer> g_container = |
|
bartfab (slow)
2014/02/26 12:45:20
Nit: #include "ui/base/ui_base_export.h"
Thiemo Nagel
2014/02/26 13:45:38
Done.
| |
| 36 struct MessageIDs { | |
| 37 // There are 4 different time units and 6 different pluralities. | |
| 38 int ids[4][6]; | |
| 39 }; | |
| 40 | |
| 41 // Message IDs for different time formats. | |
| 42 static const MessageIDs kTimeElapsedMessageIDs = { { | |
| 43 { | |
| 44 IDS_TIME_ELAPSED_SECS_DEFAULT, IDS_TIME_ELAPSED_SEC_SINGULAR, | |
| 45 IDS_TIME_ELAPSED_SECS_ZERO, IDS_TIME_ELAPSED_SECS_TWO, | |
| 46 IDS_TIME_ELAPSED_SECS_FEW, IDS_TIME_ELAPSED_SECS_MANY | |
| 47 }, | |
| 48 { | |
| 49 IDS_TIME_ELAPSED_MINS_DEFAULT, IDS_TIME_ELAPSED_MIN_SINGULAR, | |
| 50 IDS_TIME_ELAPSED_MINS_ZERO, IDS_TIME_ELAPSED_MINS_TWO, | |
| 51 IDS_TIME_ELAPSED_MINS_FEW, IDS_TIME_ELAPSED_MINS_MANY | |
| 52 }, | |
| 53 { | |
| 54 IDS_TIME_ELAPSED_HOURS_DEFAULT, IDS_TIME_ELAPSED_HOUR_SINGULAR, | |
| 55 IDS_TIME_ELAPSED_HOURS_ZERO, IDS_TIME_ELAPSED_HOURS_TWO, | |
| 56 IDS_TIME_ELAPSED_HOURS_FEW, IDS_TIME_ELAPSED_HOURS_MANY | |
| 57 }, | |
| 58 { | |
| 59 IDS_TIME_ELAPSED_DAYS_DEFAULT, IDS_TIME_ELAPSED_DAY_SINGULAR, | |
| 60 IDS_TIME_ELAPSED_DAYS_ZERO, IDS_TIME_ELAPSED_DAYS_TWO, | |
| 61 IDS_TIME_ELAPSED_DAYS_FEW, IDS_TIME_ELAPSED_DAYS_MANY | |
| 62 } | |
| 63 } }; | |
| 64 | |
| 65 static const MessageIDs kTimeRemainingMessageIDs = { { | |
| 66 { | |
| 67 IDS_TIME_REMAINING_SECS_DEFAULT, IDS_TIME_REMAINING_SEC_SINGULAR, | |
| 68 IDS_TIME_REMAINING_SECS_ZERO, IDS_TIME_REMAINING_SECS_TWO, | |
| 69 IDS_TIME_REMAINING_SECS_FEW, IDS_TIME_REMAINING_SECS_MANY | |
| 70 }, | |
| 71 { | |
| 72 IDS_TIME_REMAINING_MINS_DEFAULT, IDS_TIME_REMAINING_MIN_SINGULAR, | |
| 73 IDS_TIME_REMAINING_MINS_ZERO, IDS_TIME_REMAINING_MINS_TWO, | |
| 74 IDS_TIME_REMAINING_MINS_FEW, IDS_TIME_REMAINING_MINS_MANY | |
| 75 }, | |
| 76 { | |
| 77 IDS_TIME_REMAINING_HOURS_DEFAULT, IDS_TIME_REMAINING_HOUR_SINGULAR, | |
| 78 IDS_TIME_REMAINING_HOURS_ZERO, IDS_TIME_REMAINING_HOURS_TWO, | |
| 79 IDS_TIME_REMAINING_HOURS_FEW, IDS_TIME_REMAINING_HOURS_MANY | |
| 80 }, | |
| 81 { | |
| 82 IDS_TIME_REMAINING_DAYS_DEFAULT, IDS_TIME_REMAINING_DAY_SINGULAR, | |
| 83 IDS_TIME_REMAINING_DAYS_ZERO, IDS_TIME_REMAINING_DAYS_TWO, | |
| 84 IDS_TIME_REMAINING_DAYS_FEW, IDS_TIME_REMAINING_DAYS_MANY | |
| 85 } | |
| 86 } }; | |
| 87 | |
| 88 static const MessageIDs kTimeRemainingLongMessageIDs = { { | |
| 89 { | |
| 90 IDS_TIME_REMAINING_LONG_SECS_DEFAULT, IDS_TIME_REMAINING_LONG_SEC_SINGULAR, | |
| 91 IDS_TIME_REMAINING_LONG_SECS_ZERO, IDS_TIME_REMAINING_LONG_SECS_TWO, | |
| 92 IDS_TIME_REMAINING_LONG_SECS_FEW, IDS_TIME_REMAINING_LONG_SECS_MANY | |
| 93 }, | |
| 94 { | |
| 95 IDS_TIME_REMAINING_LONG_MINS_DEFAULT, IDS_TIME_REMAINING_LONG_MIN_SINGULAR, | |
| 96 IDS_TIME_REMAINING_LONG_MINS_ZERO, IDS_TIME_REMAINING_LONG_MINS_TWO, | |
| 97 IDS_TIME_REMAINING_LONG_MINS_FEW, IDS_TIME_REMAINING_LONG_MINS_MANY | |
| 98 }, | |
| 99 { | |
| 100 IDS_TIME_REMAINING_HOURS_DEFAULT, IDS_TIME_REMAINING_HOUR_SINGULAR, | |
| 101 IDS_TIME_REMAINING_HOURS_ZERO, IDS_TIME_REMAINING_HOURS_TWO, | |
| 102 IDS_TIME_REMAINING_HOURS_FEW, IDS_TIME_REMAINING_HOURS_MANY | |
| 103 }, | |
| 104 { | |
| 105 IDS_TIME_REMAINING_DAYS_DEFAULT, IDS_TIME_REMAINING_DAY_SINGULAR, | |
| 106 IDS_TIME_REMAINING_DAYS_ZERO, IDS_TIME_REMAINING_DAYS_TWO, | |
| 107 IDS_TIME_REMAINING_DAYS_FEW, IDS_TIME_REMAINING_DAYS_MANY | |
| 108 } | |
| 109 } }; | |
| 110 | |
| 111 static const MessageIDs kTimeDurationShortMessageIDs = { { | |
| 112 { | |
| 113 IDS_TIME_SECS_DEFAULT, IDS_TIME_SEC_SINGULAR, IDS_TIME_SECS_ZERO, | |
| 114 IDS_TIME_SECS_TWO, IDS_TIME_SECS_FEW, IDS_TIME_SECS_MANY | |
| 115 }, | |
| 116 { | |
| 117 IDS_TIME_MINS_DEFAULT, IDS_TIME_MIN_SINGULAR, IDS_TIME_MINS_ZERO, | |
| 118 IDS_TIME_MINS_TWO, IDS_TIME_MINS_FEW, IDS_TIME_MINS_MANY | |
| 119 }, | |
| 120 { | |
| 121 IDS_TIME_HOURS_DEFAULT, IDS_TIME_HOUR_SINGULAR, IDS_TIME_HOURS_ZERO, | |
| 122 IDS_TIME_HOURS_TWO, IDS_TIME_HOURS_FEW, IDS_TIME_HOURS_MANY | |
| 123 }, | |
| 124 { | |
| 125 IDS_TIME_DAYS_DEFAULT, IDS_TIME_DAY_SINGULAR, IDS_TIME_DAYS_ZERO, | |
| 126 IDS_TIME_DAYS_TWO, IDS_TIME_DAYS_FEW, IDS_TIME_DAYS_MANY | |
| 127 } | |
| 128 } }; | |
| 129 | |
| 130 static const MessageIDs kTimeDurationLongMessageIDs = { { | |
| 131 { | |
| 132 IDS_TIME_DURATION_LONG_SECS_DEFAULT, IDS_TIME_DURATION_LONG_SEC_SINGULAR, | |
| 133 IDS_TIME_DURATION_LONG_SECS_ZERO, IDS_TIME_DURATION_LONG_SECS_TWO, | |
| 134 IDS_TIME_DURATION_LONG_SECS_FEW, IDS_TIME_DURATION_LONG_SECS_MANY | |
| 135 }, | |
| 136 { | |
| 137 IDS_TIME_DURATION_LONG_MINS_DEFAULT, IDS_TIME_DURATION_LONG_MIN_SINGULAR, | |
| 138 IDS_TIME_DURATION_LONG_MINS_ZERO, IDS_TIME_DURATION_LONG_MINS_TWO, | |
| 139 IDS_TIME_DURATION_LONG_MINS_FEW, IDS_TIME_DURATION_LONG_MINS_MANY | |
| 140 }, | |
| 141 { | |
| 142 IDS_TIME_HOURS_DEFAULT, IDS_TIME_HOUR_SINGULAR, | |
| 143 IDS_TIME_HOURS_ZERO, IDS_TIME_HOURS_TWO, | |
| 144 IDS_TIME_HOURS_FEW, IDS_TIME_HOURS_MANY | |
| 145 }, | |
| 146 { | |
| 147 IDS_TIME_DAYS_DEFAULT, IDS_TIME_DAY_SINGULAR, | |
| 148 IDS_TIME_DAYS_ZERO, IDS_TIME_DAYS_TWO, | |
| 149 IDS_TIME_DAYS_FEW, IDS_TIME_DAYS_MANY | |
| 150 } | |
| 151 } }; | |
| 152 | |
| 153 // Different format types. | |
| 154 enum FormatType { | |
| 155 FORMAT_ELAPSED, | |
| 156 FORMAT_REMAINING, | |
| 157 FORMAT_REMAINING_LONG, | |
| 158 FORMAT_DURATION_SHORT, | |
| 159 FORMAT_DURATION_LONG, | |
| 160 }; | |
| 161 | |
| 162 class TimeFormatter { | |
| 163 public: | |
| 164 const std::vector<icu::PluralFormat*>& formatter(FormatType format_type) { | |
| 165 switch (format_type) { | |
| 166 case FORMAT_ELAPSED: | |
| 167 return time_elapsed_formatter_.get(); | |
| 168 case FORMAT_REMAINING: | |
| 169 return time_left_formatter_.get(); | |
| 170 case FORMAT_REMAINING_LONG: | |
| 171 return time_left_long_formatter_.get(); | |
| 172 case FORMAT_DURATION_SHORT: | |
| 173 return time_duration_short_formatter_.get(); | |
| 174 case FORMAT_DURATION_LONG: | |
| 175 return time_duration_long_formatter_.get(); | |
| 176 default: | |
| 177 NOTREACHED(); | |
| 178 return time_duration_short_formatter_.get(); | |
| 179 } | |
| 180 } | |
| 181 private: | |
| 182 static const MessageIDs& GetMessageIDs(FormatType format_type) { | |
| 183 switch (format_type) { | |
| 184 case FORMAT_ELAPSED: | |
| 185 return kTimeElapsedMessageIDs; | |
| 186 case FORMAT_REMAINING: | |
| 187 return kTimeRemainingMessageIDs; | |
| 188 case FORMAT_REMAINING_LONG: | |
| 189 return kTimeRemainingLongMessageIDs; | |
| 190 case FORMAT_DURATION_SHORT: | |
| 191 return kTimeDurationShortMessageIDs; | |
| 192 case FORMAT_DURATION_LONG: | |
| 193 return kTimeDurationLongMessageIDs; | |
| 194 default: | |
| 195 NOTREACHED(); | |
| 196 return kTimeDurationShortMessageIDs; | |
| 197 } | |
| 198 } | |
| 199 | |
| 200 static const char* GetFallbackFormatSuffix(FormatType format_type) { | |
| 201 switch (format_type) { | |
| 202 case FORMAT_ELAPSED: | |
| 203 return kFallbackFormatSuffixAgo; | |
| 204 case FORMAT_REMAINING: | |
| 205 case FORMAT_REMAINING_LONG: | |
| 206 return kFallbackFormatSuffixLeft; | |
| 207 case FORMAT_DURATION_SHORT: | |
| 208 case FORMAT_DURATION_LONG: | |
| 209 return kFallbackFormatSuffixDuration; | |
| 210 default: | |
| 211 NOTREACHED(); | |
| 212 return kFallbackFormatSuffixDuration; | |
| 213 } | |
| 214 } | |
| 215 | |
| 216 TimeFormatter() { | |
| 217 BuildFormats(FORMAT_ELAPSED, &time_elapsed_formatter_); | |
| 218 BuildFormats(FORMAT_REMAINING, &time_left_formatter_); | |
| 219 BuildFormats(FORMAT_REMAINING_LONG, &time_left_long_formatter_); | |
| 220 BuildFormats(FORMAT_DURATION_SHORT, &time_duration_short_formatter_); | |
| 221 BuildFormats(FORMAT_DURATION_LONG, &time_duration_long_formatter_); | |
| 222 } | |
| 223 ~TimeFormatter() { | |
| 224 } | |
| 225 friend struct base::DefaultLazyInstanceTraits<TimeFormatter>; | |
| 226 | |
| 227 ScopedVector<icu::PluralFormat> time_elapsed_formatter_; | |
| 228 ScopedVector<icu::PluralFormat> time_left_formatter_; | |
| 229 ScopedVector<icu::PluralFormat> time_left_long_formatter_; | |
| 230 ScopedVector<icu::PluralFormat> time_duration_short_formatter_; | |
| 231 ScopedVector<icu::PluralFormat> time_duration_long_formatter_; | |
| 232 static void BuildFormats(FormatType format_type, | |
| 233 ScopedVector<icu::PluralFormat>* time_formats); | |
| 234 static icu::PluralFormat* createFallbackFormat( | |
| 235 const icu::PluralRules& rules, int index, FormatType format_type); | |
| 236 | |
| 237 DISALLOW_COPY_AND_ASSIGN(TimeFormatter); | |
| 238 }; | |
| 239 | |
| 240 static base::LazyInstance<TimeFormatter> g_time_formatter = | |
| 241 LAZY_INSTANCE_INITIALIZER; | 26 LAZY_INSTANCE_INITIALIZER; |
| 242 | 27 |
| 243 void TimeFormatter::BuildFormats( | 28 // static |
| 244 FormatType format_type, ScopedVector<icu::PluralFormat>* time_formats) { | 29 base::string16 TimeFormat::Simple(TimeFormat::Format format, |
| 245 const MessageIDs& message_ids = GetMessageIDs(format_type); | 30 TimeFormat::Length length, |
| 246 | 31 const base::TimeDelta& delta) { |
| 247 for (int i = 0; i < 4; ++i) { | 32 return Detailed(format, length, 0, delta); |
| 248 icu::UnicodeString pattern; | |
| 249 std::vector<int> ids; | |
| 250 for (size_t j = 0; j < arraysize(message_ids.ids[i]); ++j) { | |
| 251 ids.push_back(message_ids.ids[i][j]); | |
| 252 } | |
| 253 scoped_ptr<icu::PluralFormat> format = l10n_util::BuildPluralFormat(ids); | |
| 254 if (format) { | |
| 255 time_formats->push_back(format.release()); | |
| 256 } else { | |
| 257 scoped_ptr<icu::PluralRules> rules(l10n_util::BuildPluralRules()); | |
| 258 time_formats->push_back(createFallbackFormat(*rules, i, format_type)); | |
| 259 } | |
| 260 } | |
| 261 } | 33 } |
| 262 | 34 |
| 263 // Create a hard-coded fallback plural format. This will never be called | 35 // static |
| 264 // unless translators make a mistake. | 36 base::string16 TimeFormat::Detailed(TimeFormat::Format format, |
| 265 icu::PluralFormat* TimeFormatter::createFallbackFormat( | 37 TimeFormat::Length length, |
| 266 const icu::PluralRules& rules, int index, FormatType format_type) { | 38 int cutoff, |
| 267 const icu::UnicodeString kUnits[4][2] = { | 39 const base::TimeDelta& delta) { |
| 268 { UNICODE_STRING_SIMPLE("sec"), UNICODE_STRING_SIMPLE("secs") }, | |
| 269 { UNICODE_STRING_SIMPLE("min"), UNICODE_STRING_SIMPLE("mins") }, | |
| 270 { UNICODE_STRING_SIMPLE("hour"), UNICODE_STRING_SIMPLE("hours") }, | |
| 271 { UNICODE_STRING_SIMPLE("day"), UNICODE_STRING_SIMPLE("days") } | |
| 272 }; | |
| 273 icu::UnicodeString suffix(GetFallbackFormatSuffix(format_type), -1, US_INV); | |
| 274 icu::UnicodeString pattern; | |
| 275 if (rules.isKeyword(UNICODE_STRING_SIMPLE("one"))) { | |
| 276 pattern += UNICODE_STRING_SIMPLE("one{# ") + kUnits[index][0] + suffix; | |
| 277 } | |
| 278 pattern += UNICODE_STRING_SIMPLE(" other{# ") + kUnits[index][1] + suffix; | |
| 279 UErrorCode err = U_ZERO_ERROR; | |
| 280 icu::PluralFormat* format = new icu::PluralFormat(rules, pattern, err); | |
| 281 DCHECK(U_SUCCESS(err)); | |
| 282 return format; | |
| 283 } | |
| 284 | |
| 285 base::string16 FormatTimeImpl(const TimeDelta& delta, FormatType format_type) { | |
| 286 if (delta < TimeDelta::FromSeconds(0)) { | 40 if (delta < TimeDelta::FromSeconds(0)) { |
| 287 NOTREACHED() << "Negative duration"; | 41 NOTREACHED() << "Negative duration"; |
| 288 return base::string16(); | 42 return base::string16(); |
| 289 } | 43 } |
| 290 | 44 |
| 291 const std::vector<icu::PluralFormat*>& formatters = | 45 // Negative cutoff: always use two-value format. |
| 292 g_time_formatter.Get().formatter(format_type); | 46 if (cutoff < 0) |
| 293 | 47 cutoff = std::numeric_limits<int>::max(); |
| 294 UErrorCode error = U_ZERO_ERROR; | |
| 295 icu::UnicodeString time_string; | |
| 296 | 48 |
| 297 const TimeDelta one_minute(TimeDelta::FromMinutes(1)); | 49 const TimeDelta one_minute(TimeDelta::FromMinutes(1)); |
| 298 const TimeDelta one_hour(TimeDelta::FromHours(1)); | 50 const TimeDelta one_hour(TimeDelta::FromHours(1)); |
| 299 const TimeDelta one_day(TimeDelta::FromDays(1)); | 51 const TimeDelta one_day(TimeDelta::FromDays(1)); |
| 300 | |
| 301 const TimeDelta half_second(TimeDelta::FromMilliseconds(500)); | 52 const TimeDelta half_second(TimeDelta::FromMilliseconds(500)); |
| 302 const TimeDelta half_minute(TimeDelta::FromSeconds(30)); | 53 const TimeDelta half_minute(TimeDelta::FromSeconds(30)); |
| 303 const TimeDelta half_hour(TimeDelta::FromMinutes(30)); | 54 const TimeDelta half_hour(TimeDelta::FromMinutes(30)); |
| 304 const TimeDelta half_day(TimeDelta::FromHours(12)); | 55 const TimeDelta half_day(TimeDelta::FromHours(12)); |
| 305 | 56 |
| 306 // Less than 59.5 seconds gets "X seconds left", anything larger is | 57 // Rationale: Start by determining major (first) unit, then add minor (second) |
| 307 // rounded to minutes. | 58 // unit if mandated by |cutoff|. |
| 59 icu::UnicodeString time_string; | |
| 60 const Formatter* formatter = g_container.Get().Get(format, length); | |
| 308 if (delta < one_minute - half_second) { | 61 if (delta < one_minute - half_second) { |
| 62 // Anything up to 59.500 seconds is formatted as seconds. | |
| 309 const int seconds = static_cast<int>((delta + half_second).InSeconds()); | 63 const int seconds = static_cast<int>((delta + half_second).InSeconds()); |
| 310 time_string = formatters[0]->format(seconds, error); | 64 formatter->Format(Formatter::UNIT_SEC, seconds, time_string); |
| 311 | 65 |
| 312 // Less than 59.5 minutes gets "X minutes left", anything larger is | 66 } else if (delta < one_hour - (cutoff < 60 ? half_minute : half_second)) { |
| 313 // rounded to hours. | 67 // Anything up to 59.5 minutes (respectively 59:59.500 when |cutoff| permits |
| 314 } else if (delta < one_hour - half_minute) { | 68 // two-value output) is formatted as minutes (respectively minutes and |
| 315 const int minutes = (delta + half_minute).InMinutes(); | 69 // seconds). |
| 316 time_string = formatters[1]->format(minutes, error); | 70 if (delta >= cutoff * one_minute - half_second) { |
| 71 const int minutes = (delta + half_minute).InMinutes(); | |
| 72 formatter->Format(Formatter::UNIT_MIN, minutes, time_string); | |
| 73 } else { | |
| 74 const int minutes = (delta + half_second).InMinutes(); | |
| 75 const int seconds = static_cast<int>( | |
| 76 (delta + half_second).InSeconds() % 60); | |
| 77 formatter->Format(Formatter::TWO_UNITS_MIN_SEC, | |
| 78 minutes, seconds, time_string); | |
| 79 } | |
| 317 | 80 |
| 318 // Less than 23.5 hours gets "X hours left", anything larger is | 81 } else if (delta < one_day - (cutoff < 24 ? half_hour : half_minute)) { |
| 319 // rounded to days. | 82 // Anything up to 23.5 hours (respectively 23:59:30.000 when |cutoff| |
| 320 } else if (delta < one_day - half_hour) { | 83 // permits two-value output) is formatted as hours (respectively hours and |
| 321 const int hours = (delta + half_hour).InHours(); | 84 // minutes). |
| 322 time_string = formatters[2]->format(hours, error); | 85 if (delta >= cutoff * one_hour - half_minute) { |
| 86 const int hours = (delta + half_hour).InHours(); | |
| 87 formatter->Format(Formatter::UNIT_HOUR, hours, time_string); | |
| 88 } else { | |
| 89 const int hours = (delta + half_minute).InHours(); | |
| 90 const int minutes = (delta + half_minute).InMinutes() % 60; | |
| 91 formatter->Format(Formatter::TWO_UNITS_HOUR_MIN, | |
| 92 hours, minutes, time_string); | |
| 93 } | |
| 323 | 94 |
| 324 // Anything bigger gets "X days left". | |
| 325 } else { | 95 } else { |
| 326 const int days = (delta + half_day).InDays(); | 96 // Anything bigger is formatted as days (respectively days and hours). |
| 327 time_string = formatters[3]->format(days, error); | 97 if (delta >= cutoff * one_day - half_hour) { |
| 98 const int days = (delta + half_day).InDays(); | |
| 99 formatter->Format(Formatter::UNIT_DAY, days, time_string); | |
| 100 } else { | |
| 101 const int days = (delta + half_hour).InDays(); | |
| 102 const int hours = (delta + half_hour).InHours() % 24; | |
| 103 formatter->Format(Formatter::TWO_UNITS_DAY_HOUR, | |
| 104 days, hours, time_string); | |
| 105 } | |
| 328 } | 106 } |
| 329 | 107 |
| 330 // With the fallback added, this should never fail. | 108 const int capacity = time_string.length() + 1; |
| 331 DCHECK(U_SUCCESS(error)); | |
| 332 int capacity = time_string.length() + 1; | |
| 333 DCHECK_GT(capacity, 1); | 109 DCHECK_GT(capacity, 1); |
| 334 base::string16 result; | 110 base::string16 result; |
| 111 UErrorCode error = U_ZERO_ERROR; | |
| 335 time_string.extract(static_cast<UChar*>(WriteInto(&result, capacity)), | 112 time_string.extract(static_cast<UChar*>(WriteInto(&result, capacity)), |
| 336 capacity, error); | 113 capacity, error); |
| 337 DCHECK(U_SUCCESS(error)); | 114 DCHECK(U_SUCCESS(error)); |
| 338 return result; | 115 return result; |
| 339 } | 116 } |
| 340 | 117 |
| 341 } // namespace | |
| 342 | |
| 343 namespace ui { | |
| 344 | |
| 345 // static | |
| 346 base::string16 TimeFormat::TimeElapsed(const TimeDelta& delta) { | |
| 347 return FormatTimeImpl(delta, FORMAT_ELAPSED); | |
| 348 } | |
| 349 | |
| 350 // static | |
| 351 base::string16 TimeFormat::TimeRemaining(const TimeDelta& delta) { | |
| 352 return FormatTimeImpl(delta, FORMAT_REMAINING); | |
| 353 } | |
| 354 | |
| 355 // static | |
| 356 base::string16 TimeFormat::TimeRemainingLong(const TimeDelta& delta) { | |
| 357 return FormatTimeImpl(delta, FORMAT_REMAINING_LONG); | |
| 358 } | |
| 359 | |
| 360 // static | |
| 361 base::string16 TimeFormat::TimeDurationShort(const TimeDelta& delta) { | |
| 362 return FormatTimeImpl(delta, FORMAT_DURATION_SHORT); | |
| 363 } | |
| 364 | |
| 365 // static | |
| 366 base::string16 TimeFormat::TimeDurationLong(const TimeDelta& delta) { | |
| 367 return FormatTimeImpl(delta, FORMAT_DURATION_LONG); | |
| 368 } | |
| 369 | |
| 370 // static | 118 // static |
| 371 base::string16 TimeFormat::RelativeDate( | 119 base::string16 TimeFormat::RelativeDate( |
| 372 const Time& time, | 120 const Time& time, |
| 373 const Time* optional_midnight_today) { | 121 const Time* optional_midnight_today) { |
| 374 Time midnight_today = optional_midnight_today ? *optional_midnight_today : | 122 Time midnight_today = optional_midnight_today ? *optional_midnight_today : |
| 375 Time::Now().LocalMidnight(); | 123 Time::Now().LocalMidnight(); |
| 376 TimeDelta day = TimeDelta::FromMicroseconds(Time::kMicrosecondsPerDay); | 124 TimeDelta day = TimeDelta::FromMicroseconds(Time::kMicrosecondsPerDay); |
| 377 Time tomorrow = midnight_today + day; | 125 Time tomorrow = midnight_today + day; |
| 378 Time yesterday = midnight_today - day; | 126 Time yesterday = midnight_today - day; |
| 379 if (time >= tomorrow) | 127 if (time >= tomorrow) |
| 380 return base::string16(); | 128 return base::string16(); |
| 381 else if (time >= midnight_today) | 129 else if (time >= midnight_today) |
| 382 return l10n_util::GetStringUTF16(IDS_PAST_TIME_TODAY); | 130 return l10n_util::GetStringUTF16(IDS_PAST_TIME_TODAY); |
| 383 else if (time >= yesterday) | 131 else if (time >= yesterday) |
| 384 return l10n_util::GetStringUTF16(IDS_PAST_TIME_YESTERDAY); | 132 return l10n_util::GetStringUTF16(IDS_PAST_TIME_YESTERDAY); |
| 385 return base::string16(); | 133 return base::string16(); |
| 386 } | 134 } |
| 387 | 135 |
| 388 } // namespace ui | 136 } // namespace ui |
| OLD | NEW |