| 1 |
/* This Source Code Form is subject to the terms of the Mozilla Public |
| 2 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 4 |
#ifndef intl_components_TimeZone_h_ |
| 5 |
#define intl_components_TimeZone_h_ |
| 6 |
|
| 7 |
// ICU doesn't provide a separate C API for time zone functions, but instead |
| 8 |
// requires to use UCalendar. This adds a measurable overhead when compared to |
| 9 |
// using ICU's C++ TimeZone API, therefore we prefer to use the C++ API when |
| 10 |
// possible. Due to the lack of a stable ABI in C++, it's only possible to use |
| 11 |
// the C++ API when we use our in-tree ICU copy. |
| 12 |
#if !MOZ_SYSTEM_ICU |
| 13 |
# define MOZ_INTL_USE_ICU_CPP_TIMEZONE 1 |
| 14 |
#else |
| 15 |
# define MOZ_INTL_USE_ICU_CPP_TIMEZONE 0 |
| 16 |
#endif |
| 17 |
|
| 18 |
#include <stdint.h> |
| 19 |
#include <utility> |
| 20 |
|
| 21 |
#include "unicode/ucal.h" |
| 22 |
#include "unicode/utypes.h" |
| 23 |
#if MOZ_INTL_USE_ICU_CPP_TIMEZONE |
| 24 |
# include "unicode/locid.h" |
| 25 |
# include "unicode/timezone.h" |
| 26 |
# include "unicode/unistr.h" |
| 27 |
#endif |
| 28 |
|
| 29 |
#include "mozilla/Assertions.h" |
| 30 |
#include "mozilla/Casting.h" |
| 31 |
#include "mozilla/intl/ICU4CGlue.h" |
| 32 |
#include "mozilla/intl/ICUError.h" |
| 33 |
#include "mozilla/Maybe.h" |
| 34 |
#include "mozilla/Result.h" |
| 35 |
#include "mozilla/Span.h" |
| 36 |
#include "mozilla/Try.h" |
| 37 |
#include "mozilla/UniquePtr.h" |
| 38 |
|
| 39 |
namespace mozilla::intl { |
| 40 |
|
| 41 |
/** |
| 42 |
* This component is a Mozilla-focused API for working with time zones in |
| 43 |
* internationalization code. It is used in coordination with other operations |
| 44 |
* such as datetime formatting. |
| 45 |
*/ |
| 46 |
class TimeZone final { |
| 47 |
public: |
| 48 |
#if MOZ_INTL_USE_ICU_CPP_TIMEZONE |
| 49 |
explicit TimeZone(UniquePtr<icu::TimeZone> aTimeZone) |
| 50 |
: mTimeZone(std::move(aTimeZone)) { |
| 51 |
MOZ_ASSERT(mTimeZone); |
| 52 |
} |
| 53 |
#else |
| 54 |
explicit TimeZone(UCalendar* aCalendar) : mCalendar(aCalendar) { |
| 55 |
MOZ_ASSERT(mCalendar); |
| 56 |
} |
| 57 |
#endif |
| 58 |
|
| 59 |
// Do not allow copy as this class owns the ICU resource. Move is not |
| 60 |
// currently implemented, but a custom move operator could be created if |
| 61 |
// needed. |
| 62 |
TimeZone(const TimeZone&) = delete; |
| 63 |
TimeZone& operator=(const TimeZone&) = delete; |
| 64 |
|
| 65 |
#if MOZ_INTL_USE_ICU_CPP_TIMEZONE |
| 66 |
~TimeZone() = default; |
| 67 |
#else |
| 68 |
~TimeZone(); |
| 69 |
#endif |
| 70 |
|
| 71 |
/** |
| 72 |
* Create a TimeZone. |
| 73 |
*/ |
| 74 |
static Result<UniquePtr<TimeZone>, ICUError> TryCreate( |
| 75 |
Maybe<Span<const char16_t>> aTimeZoneOverride = Nothing{}); |
| 76 |
|
| 77 |
/** |
| 78 |
* A number indicating the raw offset from GMT in milliseconds. |
| 79 |
*/ |
| 80 |
Result<int32_t, ICUError> GetRawOffsetMs(); |
| 81 |
|
| 82 |
/** |
| 83 |
* Return the daylight saving offset in milliseconds at the given UTC time. |
| 84 |
*/ |
| 85 |
Result<int32_t, ICUError> GetDSTOffsetMs(int64_t aUTCMilliseconds); |
| 86 |
|
| 87 |
/** |
| 88 |
* Return the local offset in milliseconds at the given UTC time. |
| 89 |
*/ |
| 90 |
Result<int32_t, ICUError> GetOffsetMs(int64_t aUTCMilliseconds); |
| 91 |
|
| 92 |
/** |
| 93 |
* Return the UTC offset in milliseconds at the given local time. |
| 94 |
*/ |
| 95 |
Result<int32_t, ICUError> GetUTCOffsetMs(int64_t aLocalMilliseconds); |
| 96 |
|
| 97 |
enum class LocalOption { |
| 98 |
/** |
| 99 |
* The input is interpreted as local time before a time zone transition. |
| 100 |
*/ |
| 101 |
Former, |
| 102 |
|
| 103 |
/** |
| 104 |
* The input is interpreted as local time after a time zone transition. |
| 105 |
*/ |
| 106 |
Latter, |
| 107 |
}; |
| 108 |
|
| 109 |
/** |
| 110 |
* Return the UTC offset in milliseconds at the given local time. |
| 111 |
* |
| 112 |
* `aSkippedTime` and `aRepeatedTime` select how to interpret skipped and |
| 113 |
* repeated local times. |
| 114 |
*/ |
| 115 |
Result<int32_t, ICUError> GetUTCOffsetMs(int64_t aLocalMilliseconds, |
| 116 |
LocalOption aSkippedTime, |
| 117 |
LocalOption aRepeatedTime); |
| 118 |
|
| 119 |
/** |
| 120 |
* Return the previous time zone transition before the given UTC time. If no |
| 121 |
* transition was found, return Nothing. |
| 122 |
*/ |
| 123 |
Result<Maybe<int64_t>, ICUError> GetPreviousTransition( |
| 124 |
int64_t aUTCMilliseconds); |
| 125 |
|
| 126 |
/** |
| 127 |
* Return the next time zone transition after the given UTC time. If no |
| 128 |
* transition was found, return Nothing. |
| 129 |
*/ |
| 130 |
Result<Maybe<int64_t>, ICUError> GetNextTransition(int64_t aUTCMilliseconds); |
| 131 |
|
| 132 |
enum class DaylightSavings : bool { No, Yes }; |
| 133 |
|
| 134 |
/** |
| 135 |
* Return the display name for this time zone. |
| 136 |
*/ |
| 137 |
template <typename B> |
| 138 |
ICUResult GetDisplayName(const char* aLocale, |
| 139 |
DaylightSavings aDaylightSavings, B& aBuffer) { |
| 140 |
#if MOZ_INTL_USE_ICU_CPP_TIMEZONE |
| 141 |
icu::UnicodeString displayName; |
| 142 |
mTimeZone->getDisplayName(static_cast<bool>(aDaylightSavings), |
| 143 |
icu::TimeZone::LONG, icu::Locale(aLocale), |
| 144 |
displayName); |
| 145 |
return FillBuffer(displayName, aBuffer); |
| 146 |
#else |
| 147 |
return FillBufferWithICUCall( |
| 148 |
aBuffer, [&](UChar* target, int32_t length, UErrorCode* status) { |
| 149 |
UCalendarDisplayNameType type = |
| 150 |
static_cast<bool>(aDaylightSavings) ? UCAL_DST : UCAL_STANDARD; |
| 151 |
return ucal_getTimeZoneDisplayName(mCalendar, type, aLocale, target, |
| 152 |
length, status); |
| 153 |
}); |
| 154 |
#endif |
| 155 |
} |
| 156 |
|
| 157 |
/** |
| 158 |
* Return the identifier for this time zone. |
| 159 |
*/ |
| 160 |
template <typename B> |
| 161 |
ICUResult GetId(B& aBuffer) { |
| 162 |
#if MOZ_INTL_USE_ICU_CPP_TIMEZONE |
| 163 |
icu::UnicodeString id; |
| 164 |
mTimeZone->getID(id); |
| 165 |
return FillBuffer(id, aBuffer); |
| 166 |
#else |
| 167 |
return FillBufferWithICUCall( |
| 168 |
aBuffer, [&](UChar* target, int32_t length, UErrorCode* status) { |
| 169 |
return ucal_getTimeZoneID(mCalendar, target, length, status); |
| 170 |
}); |
| 171 |
#endif |
| 172 |
} |
| 173 |
|
| 174 |
/** |
| 175 |
* Fill the buffer with the system's default IANA time zone identifier, e.g. |
| 176 |
* "America/Chicago". |
| 177 |
*/ |
| 178 |
template <typename B> |
| 179 |
static ICUResult GetDefaultTimeZone(B& aBuffer) { |
| 180 |
return FillBufferWithICUCall(aBuffer, ucal_getDefaultTimeZone); |
| 181 |
} |
| 182 |
|
| 183 |
/** |
| 184 |
* Fill the buffer with the host system's default IANA time zone identifier, |
| 185 |
* e.g. "America/Chicago". |
| 186 |
* |
| 187 |
* NOTE: This function is not thread-safe. |
| 188 |
*/ |
| 189 |
template <typename B> |
| 190 |
static ICUResult GetHostTimeZone(B& aBuffer) { |
| 191 |
return FillBufferWithICUCall(aBuffer, ucal_getHostTimeZone); |
| 192 |
} |
| 193 |
|
| 194 |
/** |
| 195 |
* Set the default time zone. |
| 196 |
*/ |
| 197 |
static Result<bool, ICUError> SetDefaultTimeZone(Span<const char> aTimeZone); |
| 198 |
|
| 199 |
/** |
| 200 |
* Set the default time zone using the host system's time zone. |
| 201 |
* |
| 202 |
* NOTE: This function is not thread-safe. |
| 203 |
*/ |
| 204 |
static ICUResult SetDefaultTimeZoneFromHostTimeZone(); |
| 205 |
|
| 206 |
/** |
| 207 |
* Return the tzdata version. |
| 208 |
* |
| 209 |
* The tzdata version is a string of the form "<year><release>", e.g. "2021a". |
| 210 |
*/ |
| 211 |
static Result<Span<const char>, ICUError> GetTZDataVersion(); |
| 212 |
|
| 213 |
/** |
| 214 |
* Constant for the typical maximal length of a time zone identifier. |
| 215 |
* |
| 216 |
* At the time of this writing 32 characters fits every supported time zone: |
| 217 |
* |
| 218 |
* Intl.supportedValuesOf("timeZone") |
| 219 |
* .reduce((acc, v) => Math.max(acc, v.length), 0) |
| 220 |
*/ |
| 221 |
static constexpr size_t TimeZoneIdentifierLength = 32; |
| 222 |
|
| 223 |
/** |
| 224 |
* Returns the canonical system time zone ID or the normalized custom time |
| 225 |
* zone ID for the given time zone ID. |
| 226 |
*/ |
| 227 |
template <typename B> |
| 228 |
static ICUResult GetCanonicalTimeZoneID(Span<const char16_t> inputTimeZone, |
| 229 |
B& aBuffer) { |
| 230 |
static_assert(std::is_same_v<typename B::CharType, char16_t>, |
| 231 |
"Currently only UTF-16 buffers are supported."); |
| 232 |
|
| 233 |
if (aBuffer.capacity() == 0) { |
| 234 |
// ucal_getCanonicalTimeZoneID differs from other API calls and fails when |
| 235 |
// passed a nullptr or 0 length result. Reserve some space initially so |
| 236 |
// that a real pointer will be used in the API. |
| 237 |
if (!aBuffer.reserve(TimeZoneIdentifierLength)) { |
| 238 |
return Err(ICUError::OutOfMemory); |
| 239 |
} |
| 240 |
} |
| 241 |
|
| 242 |
return FillBufferWithICUCall( |
| 243 |
aBuffer, |
| 244 |
[&inputTimeZone](UChar* target, int32_t length, UErrorCode* status) { |
| 245 |
return ucal_getCanonicalTimeZoneID( |
| 246 |
inputTimeZone.Elements(), |
| 247 |
static_cast<int32_t>(inputTimeZone.Length()), target, length, |
| 248 |
/* isSystemID */ nullptr, status); |
| 249 |
}); |
| 250 |
} |
| 251 |
|
| 252 |
/** |
| 253 |
* Return an enumeration over all time zones commonly used in the given |
| 254 |
* region. |
| 255 |
*/ |
| 256 |
static Result<SpanEnumeration<char>, ICUError> GetAvailableTimeZones( |
| 257 |
const char* aRegion); |
| 258 |
|
| 259 |
/** |
| 260 |
* Return an enumeration over all available time zones. |
| 261 |
*/ |
| 262 |
static Result<SpanEnumeration<char>, ICUError> GetAvailableTimeZones(); |
| 263 |
|
| 264 |
private: |
| 265 |
#if MOZ_INTL_USE_ICU_CPP_TIMEZONE |
| 266 |
template <typename B> |
| 267 |
static ICUResult FillBuffer(const icu::UnicodeString& aString, B& aBuffer) { |
| 268 |
int32_t length = aString.length(); |
| 269 |
if (!aBuffer.reserve(AssertedCast<size_t>(length))) { |
| 270 |
return Err(ICUError::OutOfMemory); |
| 271 |
} |
| 272 |
|
| 273 |
UErrorCode status = U_ZERO_ERROR; |
| 274 |
int32_t written = aString.extract(aBuffer.data(), length, status); |
| 275 |
if (!ICUSuccessForStringSpan(status)) { |
| 276 |
return Err(ToICUError(status)); |
| 277 |
} |
| 278 |
MOZ_ASSERT(written == length); |
| 279 |
|
| 280 |
aBuffer.written(written); |
| 281 |
|
| 282 |
return Ok{}; |
| 283 |
} |
| 284 |
|
| 285 |
UniquePtr<icu::TimeZone> mTimeZone = nullptr; |
| 286 |
#else |
| 287 |
UCalendar* mCalendar = nullptr; |
| 288 |
#endif |
| 289 |
}; |
| 290 |
|
| 291 |
} // namespace mozilla::intl |
| 292 |
|
| 293 |
#endif |