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 <limits> | 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" |
(...skipping 14 matching lines...) Expand all Loading... |
25 UI_BASE_EXPORT base::LazyInstance<FormatterContainer>::Leaky g_container = | 25 UI_BASE_EXPORT base::LazyInstance<FormatterContainer>::Leaky g_container = |
26 LAZY_INSTANCE_INITIALIZER; | 26 LAZY_INSTANCE_INITIALIZER; |
27 | 27 |
28 // static | 28 // static |
29 base::string16 TimeFormat::Simple(TimeFormat::Format format, | 29 base::string16 TimeFormat::Simple(TimeFormat::Format format, |
30 TimeFormat::Length length, | 30 TimeFormat::Length length, |
31 const base::TimeDelta& delta) { | 31 const base::TimeDelta& delta) { |
32 return Detailed(format, length, 0, delta); | 32 return Detailed(format, length, 0, delta); |
33 } | 33 } |
34 | 34 |
| 35 base::string16 TimeFormat::SimpleWithMonthAndYear(TimeFormat::Format format, |
| 36 TimeFormat::Length length, |
| 37 const base::TimeDelta& delta, |
| 38 bool with_month_and_year) { |
| 39 return DetailedWithMonthAndYear(format, length, 0, delta, |
| 40 with_month_and_year); |
| 41 } |
| 42 |
35 // static | 43 // static |
36 base::string16 TimeFormat::Detailed(TimeFormat::Format format, | 44 base::string16 TimeFormat::Detailed(TimeFormat::Format format, |
37 TimeFormat::Length length, | 45 TimeFormat::Length length, |
38 int cutoff, | 46 int cutoff, |
39 const base::TimeDelta& delta) { | 47 const base::TimeDelta& delta) { |
| 48 return DetailedWithMonthAndYear(format, length, cutoff, delta, false); |
| 49 } |
| 50 |
| 51 base::string16 TimeFormat::DetailedWithMonthAndYear( |
| 52 TimeFormat::Format format, |
| 53 TimeFormat::Length length, |
| 54 int cutoff, |
| 55 const base::TimeDelta& delta, |
| 56 bool with_month_and_year) { |
40 if (delta < TimeDelta::FromSeconds(0)) { | 57 if (delta < TimeDelta::FromSeconds(0)) { |
41 NOTREACHED() << "Negative duration"; | 58 NOTREACHED() << "Negative duration"; |
42 return base::string16(); | 59 return base::string16(); |
43 } | 60 } |
44 | 61 |
45 // Negative cutoff: always use two-value format. | 62 // Negative cutoff: always use two-value format. |
46 if (cutoff < 0) | 63 if (cutoff < 0) |
47 cutoff = std::numeric_limits<int>::max(); | 64 cutoff = std::numeric_limits<int>::max(); |
48 | 65 |
49 const TimeDelta one_minute(TimeDelta::FromMinutes(1)); | 66 const TimeDelta one_minute(TimeDelta::FromMinutes(1)); |
50 const TimeDelta one_hour(TimeDelta::FromHours(1)); | 67 const TimeDelta one_hour(TimeDelta::FromHours(1)); |
51 const TimeDelta one_day(TimeDelta::FromDays(1)); | 68 const TimeDelta one_day(TimeDelta::FromDays(1)); |
| 69 |
| 70 // An average month is a twelfth of a year. |
| 71 const TimeDelta one_month(TimeDelta::FromDays(365) / 12); |
| 72 |
| 73 // Simplify one year to be 365 days. |
| 74 const TimeDelta one_year(TimeDelta::FromDays(365)); |
| 75 |
52 const TimeDelta half_second(TimeDelta::FromMilliseconds(500)); | 76 const TimeDelta half_second(TimeDelta::FromMilliseconds(500)); |
53 const TimeDelta half_minute(TimeDelta::FromSeconds(30)); | 77 const TimeDelta half_minute(TimeDelta::FromSeconds(30)); |
54 const TimeDelta half_hour(TimeDelta::FromMinutes(30)); | 78 const TimeDelta half_hour(TimeDelta::FromMinutes(30)); |
55 const TimeDelta half_day(TimeDelta::FromHours(12)); | 79 const TimeDelta half_day(TimeDelta::FromHours(12)); |
56 | 80 |
57 // Rationale: Start by determining major (first) unit, then add minor (second) | 81 // Rationale: Start by determining major (first) unit, then add minor (second) |
58 // unit if mandated by |cutoff|. | 82 // unit if mandated by |cutoff|. |
59 icu::UnicodeString time_string; | 83 icu::UnicodeString time_string; |
60 const Formatter* formatter = g_container.Get().Get(format, length); | 84 const Formatter* formatter = g_container.Get().Get(format, length); |
61 if (delta < one_minute - half_second) { | 85 if (delta < one_minute - half_second) { |
(...skipping 22 matching lines...) Expand all Loading... |
84 // minutes). | 108 // minutes). |
85 if (delta >= cutoff * one_hour - half_minute) { | 109 if (delta >= cutoff * one_hour - half_minute) { |
86 const int hours = (delta + half_hour).InHours(); | 110 const int hours = (delta + half_hour).InHours(); |
87 formatter->Format(Formatter::UNIT_HOUR, hours, &time_string); | 111 formatter->Format(Formatter::UNIT_HOUR, hours, &time_string); |
88 } else { | 112 } else { |
89 const int hours = (delta + half_minute).InHours(); | 113 const int hours = (delta + half_minute).InHours(); |
90 const int minutes = (delta + half_minute).InMinutes() % 60; | 114 const int minutes = (delta + half_minute).InMinutes() % 60; |
91 formatter->Format(Formatter::TWO_UNITS_HOUR_MIN, | 115 formatter->Format(Formatter::TWO_UNITS_HOUR_MIN, |
92 hours, minutes, &time_string); | 116 hours, minutes, &time_string); |
93 } | 117 } |
94 | 118 } else if (!with_month_and_year || delta < one_month) { |
95 } else { | |
96 // Anything bigger is formatted as days (respectively days and hours). | 119 // Anything bigger is formatted as days (respectively days and hours). |
97 if (delta >= cutoff * one_day - half_hour) { | 120 if (delta >= cutoff * one_day - half_hour) { |
98 const int days = (delta + half_day).InDays(); | 121 const int days = (delta + half_day).InDays(); |
99 formatter->Format(Formatter::UNIT_DAY, days, &time_string); | 122 formatter->Format(Formatter::UNIT_DAY, days, &time_string); |
100 } else { | 123 } else { |
101 const int days = (delta + half_hour).InDays(); | 124 const int days = (delta + half_hour).InDays(); |
102 const int hours = (delta + half_hour).InHours() % 24; | 125 const int hours = (delta + half_hour).InHours() % 24; |
103 formatter->Format(Formatter::TWO_UNITS_DAY_HOUR, | 126 formatter->Format(Formatter::TWO_UNITS_DAY_HOUR, |
104 days, hours, &time_string); | 127 days, hours, &time_string); |
105 } | 128 } |
| 129 } else if (delta < one_year) { |
| 130 DCHECK(with_month_and_year); |
| 131 int month = delta / one_month; |
| 132 DCHECK(month >= 1 && month <= 12); |
| 133 formatter->Format(Formatter::UNIT_MONTH, month, &time_string); |
| 134 } else { |
| 135 DCHECK(with_month_and_year); |
| 136 int year = delta / one_year; |
| 137 formatter->Format(Formatter::UNIT_YEAR, year, &time_string); |
106 } | 138 } |
107 | 139 |
108 const int capacity = time_string.length() + 1; | 140 const int capacity = time_string.length() + 1; |
109 DCHECK_GT(capacity, 1); | 141 DCHECK_GT(capacity, 1); |
110 base::string16 result; | 142 base::string16 result; |
111 UErrorCode error = U_ZERO_ERROR; | 143 UErrorCode error = U_ZERO_ERROR; |
112 time_string.extract(static_cast<UChar*>(base::WriteInto(&result, capacity)), | 144 time_string.extract(static_cast<UChar*>(base::WriteInto(&result, capacity)), |
113 capacity, error); | 145 capacity, error); |
114 DCHECK(U_SUCCESS(error)); | 146 DCHECK(U_SUCCESS(error)); |
115 return result; | 147 return result; |
(...skipping 11 matching lines...) Expand all Loading... |
127 if (time >= tomorrow) | 159 if (time >= tomorrow) |
128 return base::string16(); | 160 return base::string16(); |
129 else if (time >= midnight_today) | 161 else if (time >= midnight_today) |
130 return l10n_util::GetStringUTF16(IDS_PAST_TIME_TODAY); | 162 return l10n_util::GetStringUTF16(IDS_PAST_TIME_TODAY); |
131 else if (time >= yesterday) | 163 else if (time >= yesterday) |
132 return l10n_util::GetStringUTF16(IDS_PAST_TIME_YESTERDAY); | 164 return l10n_util::GetStringUTF16(IDS_PAST_TIME_YESTERDAY); |
133 return base::string16(); | 165 return base::string16(); |
134 } | 166 } |
135 | 167 |
136 } // namespace ui | 168 } // namespace ui |
OLD | NEW |