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 #ifndef UI_BASE_L10N_TIME_FORMAT_H_ | 5 #ifndef UI_BASE_L10N_TIME_FORMAT_H_ |
6 #define UI_BASE_L10N_TIME_FORMAT_H_ | 6 #define UI_BASE_L10N_TIME_FORMAT_H_ |
7 | 7 |
8 #include "base/macros.h" | 8 #include "base/macros.h" |
9 #include "base/strings/string16.h" | 9 #include "base/strings/string16.h" |
10 #include "ui/base/ui_base_export.h" | 10 #include "ui/base/ui_base_export.h" |
(...skipping 14 matching lines...) Expand all Loading... |
25 FORMAT_ELAPSED, // Elapsed time, e.g. in English: "2 minutes ago". | 25 FORMAT_ELAPSED, // Elapsed time, e.g. in English: "2 minutes ago". |
26 FORMAT_COUNT // Enum size counter, not a format. Must be last. | 26 FORMAT_COUNT // Enum size counter, not a format. Must be last. |
27 }; | 27 }; |
28 | 28 |
29 enum Length { | 29 enum Length { |
30 LENGTH_SHORT, // Short format, e.g. in English: sec/min/hour/day. | 30 LENGTH_SHORT, // Short format, e.g. in English: sec/min/hour/day. |
31 LENGTH_LONG, // Long format, e.g. in English: second/minute/hour/day. | 31 LENGTH_LONG, // Long format, e.g. in English: second/minute/hour/day. |
32 LENGTH_COUNT // Enum size counter, not a length. Must be last. | 32 LENGTH_COUNT // Enum size counter, not a length. Must be last. |
33 }; | 33 }; |
34 | 34 |
| 35 // Equivalent to SimpleWithMonthAndYear(format, length, delta, false); |
| 36 static base::string16 Simple(Format format, |
| 37 Length length, |
| 38 const base::TimeDelta& delta); |
35 // Return a localized string of approximate time duration, formatted as a | 39 // Return a localized string of approximate time duration, formatted as a |
36 // single number, e.g. in English "2 hours ago". Currently, all combinations | 40 // single number, e.g. in English "2 hours ago". Currently, all combinations |
37 // of format and length are implemented. | 41 // of format and length are implemented. |
38 static base::string16 Simple(Format format, | 42 // If |use_month_and_year| is false. biggest unit is the day. If it is true, |
39 Length length, | 43 // "month" and "year" are also used. |
40 const base::TimeDelta& delta); | 44 static base::string16 SimpleWithMonthAndYear(Format format, |
| 45 Length length, |
| 46 const base::TimeDelta& delta, |
| 47 bool use_month_and_year); |
| 48 |
| 49 // Equivalent to |
| 50 // DetailedWithMonthAndYear(format, length, cutoff, delta, false); |
| 51 static base::string16 Detailed(Format format, |
| 52 Length length, |
| 53 int cutoff, |
| 54 const base::TimeDelta& delta); |
41 | 55 |
42 // Return a localized string of more precise time duration, either formatted | 56 // Return a localized string of more precise time duration, either formatted |
43 // as a single value or as two values: for a time delta of e.g. 2h19m either | 57 // as a single value or as two values: for a time delta of e.g. 2h19m either |
44 // "2 hours" or "2 hours and 19 minutes" could be returned in English. | 58 // "2 hours" or "2 hours and 19 minutes" could be returned in English. |
45 // Two-value output can be forced by setting |cutoff| to -1. Single-value | 59 // Two-value output can be forced by setting |cutoff| to -1. Single-value |
46 // output can be forced by using Simple() or setting |cutoff| to 0. | 60 // output can be forced by using Simple() or setting |cutoff| to 0. |
47 // Otherwise, choice of format happens automatically and the value of |cutoff| | 61 // Otherwise, choice of format happens automatically and the value of |cutoff| |
48 // determines the largest numeric value that may appear in a single-value | 62 // determines the largest numeric value that may appear in a single-value |
49 // format -- for lower numeric values, a second unit is added to increase | 63 // format -- for lower numeric values, a second unit is added to increase |
50 // precision. (Applied to the examples above, a |cutoff| of 2 or smaller | 64 // precision. (Applied to the examples above, a |cutoff| of 2 or smaller |
51 // would yield the first string and a |cutoff| of 3 or larger would return the | 65 // would yield the first string and a |cutoff| of 3 or larger would return the |
52 // second string.) | 66 // second string.) |
53 // | 67 // |
54 // The aim of this logic is to reduce rounding errors (that in single-value | 68 // The aim of this logic is to reduce rounding errors (that in single-value |
55 // representation can amount up to 33% of the true time duration) while at the | 69 // representation can amount up to 33% of the true time duration) while at the |
56 // same time avoiding over-precise time outputs such as e.g. "56 minutes 29 | 70 // same time avoiding over-precise time outputs such as e.g. "56 minutes 29 |
57 // seconds". The relative rounding error is guaranteed to be less than 0.5 / | 71 // seconds". The relative rounding error is guaranteed to be less than 0.5 / |
58 // |cutoff| (e.g. 5% for a |cutoff| of 10) and a second unit is only used when | 72 // |cutoff| (e.g. 5% for a |cutoff| of 10) and a second unit is only used when |
59 // necessary to achieve the precision guarantee. | 73 // necessary to achieve the precision guarantee. |
60 // | 74 // |
| 75 // If |use_month_and_year| is true, also display longer time in number of |
| 76 // month and year. A year is defined as 365 days. A month is defined as a |
| 77 // twelfth of the year. Due to the length of these time, and the approximate |
| 78 // definition, no cutoff is allowed with these values and showing two values |
| 79 // is not supported (1 month 2 days is ambiguous due to the definition of a |
| 80 // month). |
| 81 // |
61 // Currently, the only combination of format and length that is implemented is | 82 // Currently, the only combination of format and length that is implemented is |
62 // (FORMAT_DURATION, LENGTH_LONG), but it's easy to add others if required. | 83 // (FORMAT_DURATION, LENGTH_LONG), but it's easy to add others if required. |
63 // | 84 // |
64 // Note: To allow pre-, post- and infixes which can be inflected depending on | 85 // Note: To allow pre-, post- and infixes which can be inflected depending on |
65 // either the first or the second value, two separate translation strings | 86 // either the first or the second value, two separate translation strings |
66 // (IDS_TIME_*_1ST and IDS_TIME_*_2ND) are used per [plurality] times [pair of | 87 // (IDS_TIME_*_1ST and IDS_TIME_*_2ND) are used per [plurality] times [pair of |
67 // units] and are concatenated after having been formatted individually. The | 88 // units] and are concatenated after having been formatted individually. The |
68 // separator between first unit and second unit (a blank in English) is | 89 // separator between first unit and second unit (a blank in English) is |
69 // included in IDS_TIME_*_1ST. | 90 // included in IDS_TIME_*_1ST. |
70 static base::string16 Detailed(Format format, | 91 static base::string16 DetailedWithMonthAndYear(Format format, |
71 Length length, | 92 Length length, |
72 int cutoff, | 93 int cutoff, |
73 const base::TimeDelta& delta); | 94 const base::TimeDelta& delta, |
| 95 bool use_month_and_year); |
74 | 96 |
75 // For displaying a relative time in the past. This method returns either | 97 // For displaying a relative time in the past. This method returns either |
76 // "Today", "Yesterday", or an empty string if it's older than that. Returns | 98 // "Today", "Yesterday", or an empty string if it's older than that. Returns |
77 // the empty string for days in the future. | 99 // the empty string for days in the future. |
78 // | 100 // |
79 // TODO(brettw): This should be able to handle days in the future like | 101 // TODO(brettw): This should be able to handle days in the future like |
80 // "Tomorrow". | 102 // "Tomorrow". |
81 // TODO(tc): This should be able to do things like "Last week". This | 103 // TODO(tc): This should be able to do things like "Last week". This |
82 // requires handling singluar/plural for all languages. | 104 // requires handling singluar/plural for all languages. |
83 // | 105 // |
84 // The second parameter is optional, it is midnight of "Now" for relative day | 106 // The second parameter is optional, it is midnight of "Now" for relative day |
85 // computations: Time::Now().LocalMidnight() | 107 // computations: Time::Now().LocalMidnight() |
86 // If NULL, the current day's midnight will be retrieved, which can be | 108 // If NULL, the current day's midnight will be retrieved, which can be |
87 // slow. If many items are being processed, it is best to get the current | 109 // slow. If many items are being processed, it is best to get the current |
88 // time once at the beginning and pass it for each computation. | 110 // time once at the beginning and pass it for each computation. |
89 static base::string16 RelativeDate(const base::Time& time, | 111 static base::string16 RelativeDate(const base::Time& time, |
90 const base::Time* optional_midnight_today); | 112 const base::Time* optional_midnight_today); |
91 | 113 |
92 private: | 114 private: |
93 DISALLOW_IMPLICIT_CONSTRUCTORS(TimeFormat); | 115 DISALLOW_IMPLICIT_CONSTRUCTORS(TimeFormat); |
94 }; | 116 }; |
95 | 117 |
96 } // namespace ui | 118 } // namespace ui |
97 | 119 |
98 #endif // UI_BASE_L10N_TIME_FORMAT_H_ | 120 #endif // UI_BASE_L10N_TIME_FORMAT_H_ |
OLD | NEW |