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/basictypes.h" | 8 #include "base/basictypes.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" |
11 | 11 |
12 namespace base { | 12 namespace base { |
13 class Time; | 13 class Time; |
14 class TimeDelta; | 14 class TimeDelta; |
15 } | 15 } |
16 | 16 |
17 namespace ui { | 17 namespace ui { |
18 class FormatterContainer; | |
18 | 19 |
19 // Methods to format time values as strings. | 20 // Methods to format time values as strings. |
20 class UI_BASE_EXPORT TimeFormat { | 21 class UI_BASE_EXPORT TimeFormat { |
21 public: | 22 public: |
22 // TimeElapsed, TimeRemaining* and TimeDuration*: | 23 enum { kNType = 3 }; |
bartfab (slow)
2014/02/18 12:04:04
This is not how we do enums in Chrome:
1. Enum en
Thiemo Nagel
2014/02/19 17:08:44
Done.
| |
23 // These functions return a localized string of approximate time duration. | 24 enum Type { |
25 kDuration, // plain duration, eg. in English: "2 minutes" | |
bartfab (slow)
2014/02/18 12:04:04
Nit: Capitalize the first letter of comments.
Thiemo Nagel
2014/02/19 17:08:44
Done.
| |
26 kRemaining, // remaining time, eg. in English: "2 minutes left" | |
27 kElapsed // elapsed time, eg. in English: "2 minutes ago" | |
28 }; | |
24 | 29 |
25 // Returns times in elapsed-format: "3 mins ago", "2 days ago". | 30 enum { kNLength = 2 }; |
bartfab (slow)
2014/02/18 12:04:04
See above. This is not how we do enums in Chrome.
Thiemo Nagel
2014/02/19 17:08:44
Done.
| |
26 static base::string16 TimeElapsed(const base::TimeDelta& delta); | 31 enum Length { |
32 kShort, // short format, eg. in English: sec/min/hour/day | |
bartfab (slow)
2014/02/18 12:04:04
Nit 1: Capitalize the first letter of comments.
Ni
Thiemo Nagel
2014/02/19 17:08:44
Done.
| |
33 kLong // long format, eg. in English: second/minute/hour/day | |
34 }; | |
27 | 35 |
28 // Returns times in remaining-format: "3 mins left", "2 days left". | 36 // Return a localized string of approximate time duration, formatted as a |
29 static base::string16 TimeRemaining(const base::TimeDelta& delta); | 37 // single number, eg. in English "2 hours ago". Currently, all combinations |
bartfab (slow)
2014/02/18 12:04:04
Nit: s/eg./e.g.,/
Thiemo Nagel
2014/02/19 17:08:44
Done.
| |
38 // of type and length are implemented except (kElapsed,kLong). | |
bartfab (slow)
2014/02/18 12:04:04
Nit 1: Add space after comma.
Nit 2: Reorder sente
Thiemo Nagel
2014/02/19 17:08:44
Done.
| |
39 static base::string16 Simple(Type type, | |
40 Length length, | |
41 const base::TimeDelta& delta); | |
30 | 42 |
31 // Returns times in remaining-long-format: "3 minutes left", "2 days left". | 43 // Return a localized string of approximate time duration, conditionally |
32 static base::string16 TimeRemainingLong(const base::TimeDelta& delta); | 44 // formatted as a single number, eg. in English "2 hours ago", or as two |
bartfab (slow)
2014/02/18 12:04:04
Nit: s/eg./e.g.,/
Thiemo Nagel
2014/02/19 17:08:44
Done.
| |
33 | 45 // numbers, eg. in English "2 hours and 13 minutes ago". The two-number |
bartfab (slow)
2014/02/18 12:04:04
Nit: s/eg./e.g.,/
Thiemo Nagel
2014/02/19 17:08:44
Done.
| |
34 // Returns times in short-format: "3 mins", "2 days". | 46 // format is chosen whenever the single-number format would yield a numeric |
35 static base::string16 TimeDurationShort(const base::TimeDelta& delta); | 47 // value less than cutoff. (Applied to the examples above, cutoff=2 would |
bartfab (slow)
2014/02/18 12:04:04
Nit 1: Put pipes around symbol names referenced in
Thiemo Nagel
2014/02/19 17:08:44
Done.
| |
36 | 48 // yield the first string and cutoff=3 would return the second string.) |
bartfab (slow)
2014/02/18 12:04:04
Your example is correct but it took me a lot of th
Thiemo Nagel
2014/02/19 17:08:44
I've tried to improve the comment.
| |
37 // Return times in long-format: "2 hours", "25 minutes". | 49 // Currently, the only combination of type and length that is implemented is |
38 static base::string16 TimeDurationLong(const base::TimeDelta& delta); | 50 // (kDuration,kLong). |
bartfab (slow)
2014/02/18 12:04:04
Nit: Add space after comma.
Thiemo Nagel
2014/02/19 17:08:44
Done.
| |
51 // | |
52 // Note: This is implemented with two translation strings (IDS_TIME_*_1ST and | |
53 // IDS_TIME_*_2ND) per [plurality x pair of units] which are contatenated | |
bartfab (slow)
2014/02/18 12:04:04
Nit: s/contatenated/concatenated/
Thiemo Nagel
2014/02/19 17:08:44
Done.
| |
54 // after having been formatted individually. The separator between first unit | |
55 // and second unit (presently a blank) is included in IDS_TIME_*_1ST. | |
bartfab (slow)
2014/02/18 12:04:04
I think "a blank in the case of English" would be
Thiemo Nagel
2014/02/19 17:08:44
Done.
(I wanted to express that someday someone m
| |
56 static base::string16 Detailed(Type type, | |
57 Length length, | |
58 int cutoff, | |
59 const base::TimeDelta& delta); | |
39 | 60 |
40 // For displaying a relative time in the past. This method returns either | 61 // For displaying a relative time in the past. This method returns either |
41 // "Today", "Yesterday", or an empty string if it's older than that. Returns | 62 // "Today", "Yesterday", or an empty string if it's older than that. Returns |
42 // the empty string for days in the future. | 63 // the empty string for days in the future. |
43 // | 64 // |
44 // TODO(brettw): This should be able to handle days in the future like | 65 // TODO(brettw): This should be able to handle days in the future like |
45 // "Tomorrow". | 66 // "Tomorrow". |
46 // TODO(tc): This should be able to do things like "Last week". This | 67 // TODO(tc): This should be able to do things like "Last week". This |
47 // requires handling singluar/plural for all languages. | 68 // requires handling singluar/plural for all languages. |
48 // | 69 // |
49 // The second parameter is optional, it is midnight of "Now" for relative day | 70 // The second parameter is optional, it is midnight of "Now" for relative day |
50 // computations: Time::Now().LocalMidnight() | 71 // computations: Time::Now().LocalMidnight() |
51 // If NULL, the current day's midnight will be retrieved, which can be | 72 // If NULL, the current day's midnight will be retrieved, which can be |
52 // slow. If many items are being processed, it is best to get the current | 73 // slow. If many items are being processed, it is best to get the current |
53 // time once at the beginning and pass it for each computation. | 74 // time once at the beginning and pass it for each computation. |
54 static base::string16 RelativeDate(const base::Time& time, | 75 static base::string16 RelativeDate(const base::Time& time, |
55 const base::Time* optional_midnight_today); | 76 const base::Time* optional_midnight_today); |
56 | 77 |
78 protected: | |
79 // Implemented in header file to avoid code generation in production builds. | |
80 static base::string16 SimpleForTesting(const FormatterContainer& container, | |
bartfab (slow)
2014/02/18 12:04:04
AFAICT, the only difference between these ForTesti
Thiemo Nagel
2014/02/19 17:08:44
Done.
| |
81 Type type, | |
82 Length length, | |
83 const base::TimeDelta& delta) { | |
84 return DetailedForTesting(container, type, length, 0, delta); | |
85 } | |
86 static base::string16 DetailedForTesting(const FormatterContainer& container, | |
bartfab (slow)
2014/02/18 12:04:04
Nit: Add blank line before this.
Thiemo Nagel
2014/02/19 17:08:44
Done.
| |
87 Type type, | |
88 Length length, | |
89 int cutoff, | |
90 const base::TimeDelta& delta) { | |
91 return FormatTimeImpl(container, type, length, cutoff, delta); | |
92 } | |
93 | |
57 private: | 94 private: |
95 static base::string16 FormatTimeImpl(const FormatterContainer& container, | |
bartfab (slow)
2014/02/18 12:04:04
Nit: The suffix |Impl| is used in Chrome for class
Thiemo Nagel
2014/02/19 17:08:44
I've removed the method anyways.
| |
96 Type type, | |
97 Length length, | |
98 int cutoff, | |
99 const base::TimeDelta& delta); | |
100 | |
101 #ifndef TIME_FORMAT_UNITTEST | |
bartfab (slow)
2014/02/18 12:04:04
There is no precedent for this kind of constructio
Thiemo Nagel
2014/02/19 17:08:44
Done.
| |
58 DISALLOW_IMPLICIT_CONSTRUCTORS(TimeFormat); | 102 DISALLOW_IMPLICIT_CONSTRUCTORS(TimeFormat); |
103 #endif | |
59 }; | 104 }; |
60 | 105 |
61 } // namespace ui | 106 } // namespace ui |
62 | 107 |
63 #endif // UI_BASE_L10N_TIME_FORMAT_H_ | 108 #endif // UI_BASE_L10N_TIME_FORMAT_H_ |
OLD | NEW |