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 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
54 const TimeDelta half_hour(TimeDelta::FromMinutes(30)); | 54 const TimeDelta half_hour(TimeDelta::FromMinutes(30)); |
55 const TimeDelta half_day(TimeDelta::FromHours(12)); | 55 const TimeDelta half_day(TimeDelta::FromHours(12)); |
56 | 56 |
57 // Rationale: Start by determining major (first) unit, then add minor (second) | 57 // Rationale: Start by determining major (first) unit, then add minor (second) |
58 // unit if mandated by |cutoff|. | 58 // unit if mandated by |cutoff|. |
59 icu::UnicodeString time_string; | 59 icu::UnicodeString time_string; |
60 const Formatter* formatter = g_container.Get().Get(format, length); | 60 const Formatter* formatter = g_container.Get().Get(format, length); |
61 if (delta < one_minute - half_second) { | 61 if (delta < one_minute - half_second) { |
62 // Anything up to 59.500 seconds is formatted as seconds. | 62 // Anything up to 59.500 seconds is formatted as seconds. |
63 const int seconds = static_cast<int>((delta + half_second).InSeconds()); | 63 const int seconds = static_cast<int>((delta + half_second).InSeconds()); |
64 formatter->Format(Formatter::UNIT_SEC, seconds, time_string); | 64 formatter->Format(Formatter::UNIT_SEC, seconds, &time_string); |
65 | 65 |
66 } else if (delta < one_hour - (cutoff < 60 ? half_minute : half_second)) { | 66 } else if (delta < one_hour - (cutoff < 60 ? half_minute : half_second)) { |
67 // Anything up to 59.5 minutes (respectively 59:59.500 when |cutoff| permits | 67 // Anything up to 59.5 minutes (respectively 59:59.500 when |cutoff| permits |
68 // two-value output) is formatted as minutes (respectively minutes and | 68 // two-value output) is formatted as minutes (respectively minutes and |
69 // seconds). | 69 // seconds). |
70 if (delta >= cutoff * one_minute - half_second) { | 70 if (delta >= cutoff * one_minute - half_second) { |
71 const int minutes = (delta + half_minute).InMinutes(); | 71 const int minutes = (delta + half_minute).InMinutes(); |
72 formatter->Format(Formatter::UNIT_MIN, minutes, time_string); | 72 formatter->Format(Formatter::UNIT_MIN, minutes, &time_string); |
73 } else { | 73 } else { |
74 const int minutes = (delta + half_second).InMinutes(); | 74 const int minutes = (delta + half_second).InMinutes(); |
75 const int seconds = static_cast<int>( | 75 const int seconds = static_cast<int>( |
76 (delta + half_second).InSeconds() % 60); | 76 (delta + half_second).InSeconds() % 60); |
77 formatter->Format(Formatter::TWO_UNITS_MIN_SEC, | 77 formatter->Format(Formatter::TWO_UNITS_MIN_SEC, |
78 minutes, seconds, time_string); | 78 minutes, seconds, &time_string); |
79 } | 79 } |
80 | 80 |
81 } else if (delta < one_day - (cutoff < 24 ? half_hour : half_minute)) { | 81 } else if (delta < one_day - (cutoff < 24 ? half_hour : half_minute)) { |
82 // Anything up to 23.5 hours (respectively 23:59:30.000 when |cutoff| | 82 // Anything up to 23.5 hours (respectively 23:59:30.000 when |cutoff| |
83 // permits two-value output) is formatted as hours (respectively hours and | 83 // permits two-value output) is formatted as hours (respectively hours and |
84 // minutes). | 84 // minutes). |
85 if (delta >= cutoff * one_hour - half_minute) { | 85 if (delta >= cutoff * one_hour - half_minute) { |
86 const int hours = (delta + half_hour).InHours(); | 86 const int hours = (delta + half_hour).InHours(); |
87 formatter->Format(Formatter::UNIT_HOUR, hours, time_string); | 87 formatter->Format(Formatter::UNIT_HOUR, hours, &time_string); |
88 } else { | 88 } else { |
89 const int hours = (delta + half_minute).InHours(); | 89 const int hours = (delta + half_minute).InHours(); |
90 const int minutes = (delta + half_minute).InMinutes() % 60; | 90 const int minutes = (delta + half_minute).InMinutes() % 60; |
91 formatter->Format(Formatter::TWO_UNITS_HOUR_MIN, | 91 formatter->Format(Formatter::TWO_UNITS_HOUR_MIN, |
92 hours, minutes, time_string); | 92 hours, minutes, &time_string); |
93 } | 93 } |
94 | 94 |
95 } else { | 95 } else { |
96 // Anything bigger is formatted as days (respectively days and hours). | 96 // Anything bigger is formatted as days (respectively days and hours). |
97 if (delta >= cutoff * one_day - half_hour) { | 97 if (delta >= cutoff * one_day - half_hour) { |
98 const int days = (delta + half_day).InDays(); | 98 const int days = (delta + half_day).InDays(); |
99 formatter->Format(Formatter::UNIT_DAY, days, time_string); | 99 formatter->Format(Formatter::UNIT_DAY, days, &time_string); |
100 } else { | 100 } else { |
101 const int days = (delta + half_hour).InDays(); | 101 const int days = (delta + half_hour).InDays(); |
102 const int hours = (delta + half_hour).InHours() % 24; | 102 const int hours = (delta + half_hour).InHours() % 24; |
103 formatter->Format(Formatter::TWO_UNITS_DAY_HOUR, | 103 formatter->Format(Formatter::TWO_UNITS_DAY_HOUR, |
104 days, hours, time_string); | 104 days, hours, &time_string); |
105 } | 105 } |
106 } | 106 } |
107 | 107 |
108 const int capacity = time_string.length() + 1; | 108 const int capacity = time_string.length() + 1; |
109 DCHECK_GT(capacity, 1); | 109 DCHECK_GT(capacity, 1); |
110 base::string16 result; | 110 base::string16 result; |
111 UErrorCode error = U_ZERO_ERROR; | 111 UErrorCode error = U_ZERO_ERROR; |
112 time_string.extract(static_cast<UChar*>(WriteInto(&result, capacity)), | 112 time_string.extract(static_cast<UChar*>(WriteInto(&result, capacity)), |
113 capacity, error); | 113 capacity, error); |
114 DCHECK(U_SUCCESS(error)); | 114 DCHECK(U_SUCCESS(error)); |
(...skipping 12 matching lines...) Expand all Loading... |
127 if (time >= tomorrow) | 127 if (time >= tomorrow) |
128 return base::string16(); | 128 return base::string16(); |
129 else if (time >= midnight_today) | 129 else if (time >= midnight_today) |
130 return l10n_util::GetStringUTF16(IDS_PAST_TIME_TODAY); | 130 return l10n_util::GetStringUTF16(IDS_PAST_TIME_TODAY); |
131 else if (time >= yesterday) | 131 else if (time >= yesterday) |
132 return l10n_util::GetStringUTF16(IDS_PAST_TIME_YESTERDAY); | 132 return l10n_util::GetStringUTF16(IDS_PAST_TIME_YESTERDAY); |
133 return base::string16(); | 133 return base::string16(); |
134 } | 134 } |
135 | 135 |
136 } // namespace ui | 136 } // namespace ui |
OLD | NEW |