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