time_formatting.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. // Basic time formatting methods. These methods use the current locale
  5. // formatting for displaying the time.
  6. #ifndef BASE_I18N_TIME_FORMATTING_H_
  7. #define BASE_I18N_TIME_FORMATTING_H_
  8. #include "base/compiler_specific.h"
  9. #include "base/i18n/base_i18n_export.h"
  10. #include "base/strings/string16.h"
  11. namespace base {
  12. class Time;
  13. class TimeDelta;
  14. // Argument type used to specify the hour clock type.
  15. enum HourClockType {
  16. k12HourClock, // Uses 1-12. e.g., "3:07 PM"
  17. k24HourClock, // Uses 0-23. e.g., "15:07"
  18. };
  19. // Argument type used to specify whether or not to include AM/PM sign.
  20. enum AmPmClockType {
  21. kDropAmPm, // Drops AM/PM sign. e.g., "3:07"
  22. kKeepAmPm, // Keeps AM/PM sign. e.g., "3:07 PM"
  23. };
  24. // Should match UMeasureFormatWidth in measfmt.h; replicated here to avoid
  25. // requiring third_party/icu dependencies with this file.
  26. enum DurationFormatWidth {
  27. DURATION_WIDTH_WIDE, // "3 hours, 7 minutes"
  28. DURATION_WIDTH_SHORT, // "3 hr, 7 min"
  29. DURATION_WIDTH_NARROW, // "3h 7m"
  30. DURATION_WIDTH_NUMERIC // "3:07"
  31. };
  32. // Date formats from third_party/icu/source/i18n/unicode/udat.h. Add more as
  33. // necessary.
  34. enum DateFormat {
  35. // November 2007
  36. DATE_FORMAT_YEAR_MONTH,
  37. // Tuesday, 7 November
  38. DATE_FORMAT_MONTH_WEEKDAY_DAY,
  39. };
  40. // Returns the time of day, e.g., "3:07 PM".
  41. BASE_I18N_EXPORT string16 TimeFormatTimeOfDay(const Time& time);
  42. // Returns the time of day in 24-hour clock format with millisecond accuracy,
  43. // e.g., "15:07:30.568"
  44. BASE_I18N_EXPORT string16 TimeFormatTimeOfDayWithMilliseconds(const Time& time);
  45. // Returns the time of day in the specified hour clock type. e.g.
  46. // "3:07 PM" (type == k12HourClock, ampm == kKeepAmPm).
  47. // "3:07" (type == k12HourClock, ampm == kDropAmPm).
  48. // "15:07" (type == k24HourClock).
  49. BASE_I18N_EXPORT string16 TimeFormatTimeOfDayWithHourClockType(
  50. const Time& time,
  51. HourClockType type,
  52. AmPmClockType ampm);
  53. // Returns a shortened date, e.g. "Nov 7, 2007"
  54. BASE_I18N_EXPORT string16 TimeFormatShortDate(const Time& time);
  55. // Returns a numeric date such as 12/13/52.
  56. BASE_I18N_EXPORT string16 TimeFormatShortDateNumeric(const Time& time);
  57. // Returns a numeric date and time such as "12/13/52 2:44:30 PM".
  58. BASE_I18N_EXPORT string16 TimeFormatShortDateAndTime(const Time& time);
  59. // Returns a month and year, e.g. "November 2007"
  60. BASE_I18N_EXPORT string16 TimeFormatMonthAndYear(const Time& time);
  61. // Returns a numeric date and time with time zone such as
  62. // "12/13/52 2:44:30 PM PST".
  63. BASE_I18N_EXPORT string16
  64. TimeFormatShortDateAndTimeWithTimeZone(const Time& time);
  65. // Formats a time in a friendly sentence format, e.g.
  66. // "Monday, March 6, 2008 2:44:30 PM".
  67. BASE_I18N_EXPORT string16 TimeFormatFriendlyDateAndTime(const Time& time);
  68. // Formats a time in a friendly sentence format, e.g.
  69. // "Monday, March 6, 2008".
  70. BASE_I18N_EXPORT string16 TimeFormatFriendlyDate(const Time& time);
  71. // Formats a time using a skeleton to produce a format for different locales
  72. // when an unusual time format is needed, e.g. "Feb. 2, 18:00".
  73. //
  74. // See http://userguide.icu-project.org/formatparse/datetime for details.
  75. BASE_I18N_EXPORT string16 TimeFormatWithPattern(const Time& time,
  76. const char* pattern);
  77. // Formats a time duration of hours and minutes into various formats, e.g.,
  78. // "3:07" or "3 hours, 7 minutes", and returns true on success. See
  79. // DurationFormatWidth for details.
  80. //
  81. // Please don't use width = DURATION_WIDTH_NUMERIC when the time duration
  82. // can possibly be larger than 24h, as the hour value will be cut below 24
  83. // after formatting.
  84. // TODO(crbug.com/675791): fix function output when width =
  85. // DURATION_WIDTH_NUMERIC.
  86. BASE_I18N_EXPORT bool TimeDurationFormat(const TimeDelta time,
  87. const DurationFormatWidth width,
  88. string16* out) WARN_UNUSED_RESULT;
  89. // Formats a time duration of hours, minutes and seconds into various formats,
  90. // e.g., "3:07:30" or "3 hours, 7 minutes, 30 seconds", and returns true on
  91. // success. See DurationFormatWidth for details.
  92. //
  93. // Please don't use width = DURATION_WIDTH_NUMERIC when the time duration
  94. // can possibly be larger than 24h, as the hour value will be cut below 24
  95. // after formatting.
  96. // TODO(crbug.com/675791): fix function output when width =
  97. // DURATION_WIDTH_NUMERIC.
  98. BASE_I18N_EXPORT bool TimeDurationFormatWithSeconds(
  99. const TimeDelta time,
  100. const DurationFormatWidth width,
  101. string16* out) WARN_UNUSED_RESULT;
  102. // Formats a date interval into various formats, e.g. "2 December - 4 December"
  103. // or "March 2016 - December 2016". See DateFormat for details.
  104. BASE_I18N_EXPORT string16 DateIntervalFormat(const Time& begin_time,
  105. const Time& end_time,
  106. DateFormat format);
  107. // Gets the hour clock type of the current locale. e.g.
  108. // k12HourClock (en-US).
  109. // k24HourClock (en-GB).
  110. BASE_I18N_EXPORT HourClockType GetHourClockType();
  111. } // namespace base
  112. #endif // BASE_I18N_TIME_FORMATTING_H_