1 // Copyright 2011 The Chromium Authors 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 // Basic time formatting methods. These methods use the current locale 6 // formatting for displaying the time. 7 8 #ifndef BASE_I18N_TIME_FORMATTING_H_ 9 #define BASE_I18N_TIME_FORMATTING_H_ 10 11 #include <string> 12 13 #include "base/i18n/base_i18n_export.h" 14 #include "build/build_config.h" 15 #include "build/chromeos_buildflags.h" 16 17 #if BUILDFLAG(IS_CHROMEOS_ASH) 18 #include "third_party/icu/source/i18n/unicode/timezone.h" 19 #endif // BUILDFLAG(IS_CHROMEOS_ASH) 20 21 namespace base { 22 23 class Time; 24 class TimeDelta; 25 26 // Argument type used to specify the hour clock type. 27 enum HourClockType { 28 k12HourClock, // Uses 1-12. e.g., "3:07 PM" 29 k24HourClock, // Uses 0-23. e.g., "15:07" 30 }; 31 32 // Argument type used to specify whether or not to include AM/PM sign. 33 enum AmPmClockType { 34 kDropAmPm, // Drops AM/PM sign. e.g., "3:07" 35 kKeepAmPm, // Keeps AM/PM sign. e.g., "3:07 PM" 36 }; 37 38 // Should match UMeasureFormatWidth in measfmt.h; replicated here to avoid 39 // requiring third_party/icu dependencies with this file. 40 enum DurationFormatWidth { 41 DURATION_WIDTH_WIDE, // "3 hours, 7 minutes" 42 DURATION_WIDTH_SHORT, // "3 hr, 7 min" 43 DURATION_WIDTH_NARROW, // "3h 7m" 44 DURATION_WIDTH_NUMERIC // "3:07" 45 }; 46 47 // Date formats from third_party/icu/source/i18n/unicode/udat.h. Add more as 48 // necessary. 49 enum DateFormat { 50 // November 2007 51 DATE_FORMAT_YEAR_MONTH, 52 // Tuesday, 7 November 53 DATE_FORMAT_MONTH_WEEKDAY_DAY, 54 }; 55 56 // Returns the time of day, e.g., "3:07 PM". 57 BASE_I18N_EXPORT std::u16string TimeFormatTimeOfDay(const Time& time); 58 59 // Returns the time of day in 24-hour clock format with millisecond accuracy, 60 // e.g., "15:07:30.568" 61 BASE_I18N_EXPORT std::u16string TimeFormatTimeOfDayWithMilliseconds( 62 const Time& time); 63 64 // Returns the time of day in the specified hour clock type. e.g. 65 // "3:07 PM" (type == k12HourClock, ampm == kKeepAmPm). 66 // "3:07" (type == k12HourClock, ampm == kDropAmPm). 67 // "15:07" (type == k24HourClock). 68 BASE_I18N_EXPORT std::u16string TimeFormatTimeOfDayWithHourClockType( 69 const Time& time, 70 HourClockType type, 71 AmPmClockType ampm); 72 73 // Returns a shortened date, e.g. "Nov 7, 2007" 74 BASE_I18N_EXPORT std::u16string TimeFormatShortDate(const Time& time); 75 76 // Returns a numeric date such as 12/13/52. 77 BASE_I18N_EXPORT std::u16string TimeFormatShortDateNumeric(const Time& time); 78 79 // Returns a numeric date and time such as "12/13/52 2:44:30 PM". 80 BASE_I18N_EXPORT std::u16string TimeFormatShortDateAndTime(const Time& time); 81 82 #if BUILDFLAG(IS_CHROMEOS_ASH) 83 // Returns a month and year, e.g. "November 2007" for the specified time zone. 84 BASE_I18N_EXPORT std::u16string TimeFormatMonthAndYearForTimeZone( 85 const Time& time, 86 const icu::TimeZone* time_zone); 87 #endif // BUILDFLAG(IS_CHROMEOS_ASH) 88 89 // Returns a month and year, e.g. "November 2007" 90 BASE_I18N_EXPORT std::u16string TimeFormatMonthAndYear(const Time& time); 91 92 // Returns a numeric date and time with time zone such as 93 // "12/13/52 2:44:30 PM PST". 94 BASE_I18N_EXPORT std::u16string TimeFormatShortDateAndTimeWithTimeZone( 95 const Time& time); 96 97 // Formats a time in a friendly sentence format, e.g. 98 // "Monday, March 6, 2008 2:44:30 PM". 99 BASE_I18N_EXPORT std::u16string TimeFormatFriendlyDateAndTime(const Time& time); 100 101 // Formats a time in a friendly sentence format, e.g. 102 // "Monday, March 6, 2008". 103 BASE_I18N_EXPORT std::u16string TimeFormatFriendlyDate(const Time& time); 104 105 // Formats a time using a skeleton to produce a format for different locales 106 // when an unusual time format is needed, e.g. "Feb. 2, 18:00". 107 // 108 // See http://userguide.icu-project.org/formatparse/datetime for details. 109 BASE_I18N_EXPORT std::u16string TimeFormatWithPattern(const Time& time, 110 const char* pattern); 111 112 // Formats a time duration of hours and minutes into various formats, e.g., 113 // "3:07" or "3 hours, 7 minutes", and returns true on success. See 114 // DurationFormatWidth for details. 115 [[nodiscard]] BASE_I18N_EXPORT bool TimeDurationFormat( 116 TimeDelta time, 117 DurationFormatWidth width, 118 std::u16string* out); 119 120 // Formats a time duration of hours, minutes and seconds into various formats, 121 // e.g., "3:07:30" or "3 hours, 7 minutes, 30 seconds", and returns true on 122 // success. See DurationFormatWidth for details. 123 [[nodiscard]] BASE_I18N_EXPORT bool TimeDurationFormatWithSeconds( 124 TimeDelta time, 125 DurationFormatWidth width, 126 std::u16string* out); 127 128 // Formats a date interval into various formats, e.g. "2 December - 4 December" 129 // or "March 2016 - December 2016". See DateFormat for details. 130 BASE_I18N_EXPORT std::u16string DateIntervalFormat(const Time& begin_time, 131 const Time& end_time, 132 DateFormat format); 133 134 // Gets the hour clock type of the current locale. e.g. 135 // k12HourClock (en-US). 136 // k24HourClock (en-GB). 137 BASE_I18N_EXPORT HourClockType GetHourClockType(); 138 139 } // namespace base 140 141 #endif // BASE_I18N_TIME_FORMATTING_H_ 142