time_formatting.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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 <string>
  9. #include "base/i18n/base_i18n_export.h"
  10. #include "build/build_config.h"
  11. #include "build/chromeos_buildflags.h"
  12. #if BUILDFLAG(IS_CHROMEOS_ASH)
  13. #include "third_party/icu/source/i18n/unicode/timezone.h"
  14. #endif // BUILDFLAG(IS_CHROMEOS_ASH)
  15. namespace base {
  16. class Time;
  17. class TimeDelta;
  18. // Argument type used to specify the hour clock type.
  19. enum HourClockType {
  20. k12HourClock, // Uses 1-12. e.g., "3:07 PM"
  21. k24HourClock, // Uses 0-23. e.g., "15:07"
  22. };
  23. // Argument type used to specify whether or not to include AM/PM sign.
  24. enum AmPmClockType {
  25. kDropAmPm, // Drops AM/PM sign. e.g., "3:07"
  26. kKeepAmPm, // Keeps AM/PM sign. e.g., "3:07 PM"
  27. };
  28. // Should match UMeasureFormatWidth in measfmt.h; replicated here to avoid
  29. // requiring third_party/icu dependencies with this file.
  30. enum DurationFormatWidth {
  31. DURATION_WIDTH_WIDE, // "3 hours, 7 minutes"
  32. DURATION_WIDTH_SHORT, // "3 hr, 7 min"
  33. DURATION_WIDTH_NARROW, // "3h 7m"
  34. DURATION_WIDTH_NUMERIC // "3:07"
  35. };
  36. // Date formats from third_party/icu/source/i18n/unicode/udat.h. Add more as
  37. // necessary.
  38. enum DateFormat {
  39. // November 2007
  40. DATE_FORMAT_YEAR_MONTH,
  41. // Tuesday, 7 November
  42. DATE_FORMAT_MONTH_WEEKDAY_DAY,
  43. };
  44. // Returns the time of day, e.g., "3:07 PM".
  45. BASE_I18N_EXPORT std::u16string TimeFormatTimeOfDay(const Time& time);
  46. // Returns the time of day in 24-hour clock format with millisecond accuracy,
  47. // e.g., "15:07:30.568"
  48. BASE_I18N_EXPORT std::u16string TimeFormatTimeOfDayWithMilliseconds(
  49. const Time& time);
  50. // Returns the time of day in the specified hour clock type. e.g.
  51. // "3:07 PM" (type == k12HourClock, ampm == kKeepAmPm).
  52. // "3:07" (type == k12HourClock, ampm == kDropAmPm).
  53. // "15:07" (type == k24HourClock).
  54. BASE_I18N_EXPORT std::u16string TimeFormatTimeOfDayWithHourClockType(
  55. const Time& time,
  56. HourClockType type,
  57. AmPmClockType ampm);
  58. // Returns a shortened date, e.g. "Nov 7, 2007"
  59. BASE_I18N_EXPORT std::u16string TimeFormatShortDate(const Time& time);
  60. // Returns a numeric date such as 12/13/52.
  61. BASE_I18N_EXPORT std::u16string TimeFormatShortDateNumeric(const Time& time);
  62. // Returns a numeric date and time such as "12/13/52 2:44:30 PM".
  63. BASE_I18N_EXPORT std::u16string TimeFormatShortDateAndTime(const Time& time);
  64. #if BUILDFLAG(IS_CHROMEOS_ASH)
  65. // Returns a month and year, e.g. "November 2007" for the specified time zone.
  66. BASE_I18N_EXPORT std::u16string TimeFormatMonthAndYearForTimeZone(
  67. const Time& time,
  68. const icu::TimeZone* time_zone);
  69. #endif // BUILDFLAG(IS_CHROMEOS_ASH)
  70. // Returns a month and year, e.g. "November 2007"
  71. BASE_I18N_EXPORT std::u16string TimeFormatMonthAndYear(const Time& time);
  72. // Returns a numeric date and time with time zone such as
  73. // "12/13/52 2:44:30 PM PST".
  74. BASE_I18N_EXPORT std::u16string TimeFormatShortDateAndTimeWithTimeZone(
  75. const Time& time);
  76. // Formats a time in a friendly sentence format, e.g.
  77. // "Monday, March 6, 2008 2:44:30 PM".
  78. BASE_I18N_EXPORT std::u16string TimeFormatFriendlyDateAndTime(const Time& time);
  79. // Formats a time in a friendly sentence format, e.g.
  80. // "Monday, March 6, 2008".
  81. BASE_I18N_EXPORT std::u16string TimeFormatFriendlyDate(const Time& time);
  82. // Formats a time using a skeleton to produce a format for different locales
  83. // when an unusual time format is needed, e.g. "Feb. 2, 18:00".
  84. //
  85. // See http://userguide.icu-project.org/formatparse/datetime for details.
  86. BASE_I18N_EXPORT std::u16string TimeFormatWithPattern(const Time& time,
  87. const char* pattern);
  88. // Formats a time duration of hours and minutes into various formats, e.g.,
  89. // "3:07" or "3 hours, 7 minutes", and returns true on success. See
  90. // DurationFormatWidth for details.
  91. [[nodiscard]] BASE_I18N_EXPORT bool TimeDurationFormat(
  92. const TimeDelta time,
  93. const DurationFormatWidth width,
  94. std::u16string* out);
  95. // Formats a time duration of hours, minutes and seconds into various formats,
  96. // e.g., "3:07:30" or "3 hours, 7 minutes, 30 seconds", and returns true on
  97. // success. See DurationFormatWidth for details.
  98. [[nodiscard]] BASE_I18N_EXPORT bool TimeDurationFormatWithSeconds(
  99. const TimeDelta time,
  100. const DurationFormatWidth width,
  101. std::u16string* out);
  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 std::u16string 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_