| 1 |
use alloc::{ |
| 2 |
string::{String, ToString}, |
| 3 |
vec::Vec, |
| 4 |
}; |
| 5 |
|
| 6 |
use jcore::util::ArrayStr; |
| 7 |
|
| 8 |
use crate::{ |
| 9 |
error::{ |
| 10 |
tz::concatenated::{Error as E, ALLOC_LIMIT}, |
| 11 |
Error, ErrorContext, |
| 12 |
}, |
| 13 |
tz::TimeZone, |
| 14 |
util::{escape, utf8}, |
| 15 |
}; |
| 16 |
|
| 17 |
/// An abstraction for reading data from Android's concatenated TZif data file. |
| 18 |
/// |
| 19 |
/// This abstraction is designed in a way that the data is reads from is |
| 20 |
/// largely untrusted. This means that, no matter what sequence of bytes is |
| 21 |
/// given, this should never panic (or else there is a bug). Moreover, there is |
| 22 |
/// some guarding against disproportionate allocation. While big allocations |
| 23 |
/// can still happen, they require a proportionally large data file. (Thus, |
| 24 |
/// callers can guard against this by considering the size of the data.) What |
| 25 |
/// this implementation prevents against is accidentally OOMing or panicking as |
| 26 |
/// a result of naively doing `Vec::with_capacity(rdr.decode_integer())`. |
| 27 |
/// |
| 28 |
/// This is also designed to work in alloc-only contexts mostly out of "good |
| 29 |
/// sense." Technically we don't (currently) use this outside of `std`, since |
| 30 |
/// it's only used for reading tzdb on Android from the file system. But we do |
| 31 |
/// things this way in case we end up wanting to use it for something else. |
| 32 |
/// If we needed this for no-alloc environments, then that's a much bigger |
| 33 |
/// change, if only because it would require making the TZif parser no-alloc |
| 34 |
/// compatible, and it's not quite clear what the best way to do that is. We |
| 35 |
/// achieve the alloc-only API be introducing a trait that abstracts over a |
| 36 |
/// `File` for random access to bytes. |
| 37 |
#[derive(Debug)] |
| 38 |
pub(crate) struct ConcatenatedTzif<R> { |
| 39 |
rdr: R, |
| 40 |
header: Header, |
| 41 |
} |
| 42 |
|
| 43 |
impl<R: Read> ConcatenatedTzif<R> { |
| 44 |
/// Open the concatenated TZif file using the reader given. |
| 45 |
/// |
| 46 |
/// This reads the header and will return an error if the header is |
| 47 |
/// invalid. |
| 48 |
pub(crate) fn open(rdr: R) -> Result<ConcatenatedTzif<R>, Error> { |
| 49 |
let header = Header::read(&rdr)?; |
| 50 |
Ok(ConcatenatedTzif { rdr, header }) |
| 51 |
} |
| 52 |
|
| 53 |
/// Returns the version of this `tzdata` database. |
| 54 |
pub(crate) fn version(&self) -> ArrayStr<5> { |
| 55 |
self.header.version |
| 56 |
} |
| 57 |
|
| 58 |
/// Returns a `TimeZone` extracted from this concatenated TZif data. |
| 59 |
/// |
| 60 |
/// This is only successful if an index entry with the corresponding |
| 61 |
/// IANA time zone identifier could be found. |
| 62 |
/// |
| 63 |
/// Callers must provide two scratch buffers that are used for temporary |
| 64 |
/// allocation internally. Callers can create a new buffer for each call, |
| 65 |
/// but it's likely faster to reuse them if possible. |
| 66 |
/// |
| 67 |
/// If a `TimeZone` is returned, it is guaranteed to have a present IANA |
| 68 |
/// name (accessible via `TimeZone::iana_name`). |
| 69 |
pub(crate) fn get( |
| 70 |
&self, |
| 71 |
query: &str, |
| 72 |
scratch1: &mut Vec<u8>, |
| 73 |
scratch2: &mut Vec<u8>, |
| 74 |
) -> Result<Option<TimeZone>, Error> { |
| 75 |
scratch1.clear(); |
| 76 |
alloc(scratch1, self.header.index_len())?; |
| 77 |
self.rdr |
| 78 |
.read_exact_at(scratch1, self.header.index_offset) |
| 79 |
.context(E::FailedReadIndex)?; |
| 80 |
|
| 81 |
let mut index = &**scratch1; |
| 82 |
while !index.is_empty() { |
| 83 |
let entry = IndexEntry::new(&index[..IndexEntry::LEN]); |
| 84 |
index = &index[IndexEntry::LEN..]; |
| 85 |
let ordering = utf8::cmp_ignore_ascii_case_bytes( |
| 86 |
entry.name_bytes(), |
| 87 |
query.as_bytes(), |
| 88 |
); |
| 89 |
if ordering.is_ne() { |
| 90 |
continue; |
| 91 |
} |
| 92 |
|
| 93 |
// OK because `entry.name_bytes()` is equal to `query`, |
| 94 |
// ignoring ASCII case. The only way this can be true is is |
| 95 |
// `entry.name_bytes()` is itself valid UTF-8. |
| 96 |
let name = entry.name().unwrap(); |
| 97 |
scratch2.clear(); |
| 98 |
alloc(scratch2, entry.len())?; |
| 99 |
let start = self.header.data_offset.saturating_add(entry.start()); |
| 100 |
self.rdr |
| 101 |
.read_exact_at(scratch2, start) |
| 102 |
.context(E::FailedReadData)?; |
| 103 |
return TimeZone::tzif(name, scratch2).map(Some); |
| 104 |
} |
| 105 |
Ok(None) |
| 106 |
} |
| 107 |
|
| 108 |
/// Returns a list of all IANA time zone identifiers in this concatenated |
| 109 |
/// TZif data. |
| 110 |
/// |
| 111 |
/// Callers must provide a scratch buffer that is used for temporary |
| 112 |
/// allocation internally. Callers can create a new buffer for each call, |
| 113 |
/// but it's likely faster to reuse them if possible. |
| 114 |
pub(crate) fn available( |
| 115 |
&self, |
| 116 |
scratch: &mut Vec<u8>, |
| 117 |
) -> Result<Vec<String>, Error> { |
| 118 |
scratch.clear(); |
| 119 |
alloc(scratch, self.header.index_len())?; |
| 120 |
self.rdr |
| 121 |
.read_exact_at(scratch, self.header.index_offset) |
| 122 |
.context(E::FailedReadIndex)?; |
| 123 |
|
| 124 |
let names_len = self.header.index_len() / IndexEntry::LEN; |
| 125 |
// Why are we careless with this alloc? Well, its size is proportional |
| 126 |
// to the actual amount of data in the file. So the only way to get a |
| 127 |
// big alloc is to create a huge file. This seems... fine... I guess. |
| 128 |
// Where as the `alloc` above is done on the basis of an arbitrary |
| 129 |
// 32-bit integer. |
| 130 |
let mut names = Vec::with_capacity(names_len); |
| 131 |
let mut index = &**scratch; |
| 132 |
while !index.is_empty() { |
| 133 |
let entry = IndexEntry::new(&index[..IndexEntry::LEN]); |
| 134 |
index = &index[IndexEntry::LEN..]; |
| 135 |
names.push(entry.name()?.to_string()); |
| 136 |
} |
| 137 |
Ok(names) |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
/// The header of Android concatenated TZif data. |
| 142 |
/// |
| 143 |
/// The header has the version and some offsets indicating the location of |
| 144 |
/// the index entry (a list of IANA time zone identifiers and offsets into |
| 145 |
/// the data block) and the actual TZif data. |
| 146 |
#[derive(Debug)] |
| 147 |
struct Header { |
| 148 |
version: ArrayStr<5>, |
| 149 |
index_offset: u64, |
| 150 |
data_offset: u64, |
| 151 |
} |
| 152 |
|
| 153 |
impl Header { |
| 154 |
/// Reads the header from Android's concatenated TZif concatenated data |
| 155 |
/// file. |
| 156 |
/// |
| 157 |
/// Basically, this gives us the version and some offsets for where to find |
| 158 |
/// data. |
| 159 |
fn read<R: Read + ?Sized>(rdr: &R) -> Result<Header, Error> { |
| 160 |
// 12 bytes plus 3 4-byte big endian integers. |
| 161 |
let mut buf = [0; 12 + 3 * 4]; |
| 162 |
rdr.read_exact_at(&mut buf, 0).context(E::FailedReadHeader)?; |
| 163 |
if &buf[..6] != b"tzdata" { |
| 164 |
return Err(Error::from(E::ExpectedFirstSixBytes)); |
| 165 |
} |
| 166 |
if buf[11] != 0 { |
| 167 |
return Err(Error::from(E::ExpectedLastByte)); |
| 168 |
} |
| 169 |
|
| 170 |
let version = { |
| 171 |
let version = core::str::from_utf8(&buf[6..11]) |
| 172 |
.map_err(|_| E::ExpectedVersion)?; |
| 173 |
// OK because `version` is exactly 5 bytes, by construction. |
| 174 |
ArrayStr::new(version).unwrap() |
| 175 |
}; |
| 176 |
// OK because the sub-slice is sized to exactly 4 bytes. |
| 177 |
let index_offset = u64::from(read_be32(&buf[12..16])); |
| 178 |
// OK because the sub-slice is sized to exactly 4 bytes. |
| 179 |
let data_offset = u64::from(read_be32(&buf[16..20])); |
| 180 |
if index_offset > data_offset { |
| 181 |
return Err(Error::from(E::InvalidIndexDataOffsets)); |
| 182 |
} |
| 183 |
// we don't read 20..24 since we don't care about zonetab (yet) |
| 184 |
let header = Header { version, index_offset, data_offset }; |
| 185 |
if header.index_len() % IndexEntry::LEN != 0 { |
| 186 |
return Err(Error::from(E::InvalidLengthIndexBlock)); |
| 187 |
} |
| 188 |
Ok(header) |
| 189 |
} |
| 190 |
|
| 191 |
/// Returns the length of the index section of the concatenated tzdb. |
| 192 |
/// |
| 193 |
/// Beware of using this to create allocations. In theory, this should be |
| 194 |
/// trusted data, but the length can be any 32-bit integer. If it's used to |
| 195 |
/// create an allocation, it could potentially be up to 4GB. |
| 196 |
fn index_len(&self) -> usize { |
| 197 |
// OK because `Header` parsing returns an error if this overflows. |
| 198 |
let len = self.data_offset.checked_sub(self.index_offset).unwrap(); |
| 199 |
// N.B. Overflow only occurs here on 16-bit (or smaller) platforms, |
| 200 |
// which at the time of writing, is not supported by Jiff. Instead, |
| 201 |
// a `usize::MAX` will trigger an allocation error. |
| 202 |
usize::try_from(len).unwrap_or(usize::MAX) |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
/// A view into a single index entry in the index block of concatenated TZif |
| 207 |
/// data. |
| 208 |
/// |
| 209 |
/// If we had safe transmute, it would be much nicer to define this as |
| 210 |
/// |
| 211 |
/// ```text |
| 212 |
/// #[derive(Clone, Copy)] |
| 213 |
/// #[repr(transparent, align(1))] |
| 214 |
/// struct IndexEntry { |
| 215 |
/// name: [u8; 40], |
| 216 |
/// start: u32, |
| 217 |
/// len: u32, |
| 218 |
/// _raw_utc_offset: u32, // we don't use this here |
| 219 |
/// } |
| 220 |
/// ``` |
| 221 |
/// |
| 222 |
/// And probably implement a trait asserting that this is plain old data (or |
| 223 |
/// derive it safely). And then we could cast `&[u8]` to `&[IndexEntry]` |
| 224 |
/// safely and access the individual fields as is. We could do this today, |
| 225 |
/// but not in safe code. And since this isn't performance critical, it's just |
| 226 |
/// not worth flagging this code as potentially containing undefined behavior. |
| 227 |
#[derive(Clone, Copy)] |
| 228 |
struct IndexEntry<'a>(&'a [u8]); |
| 229 |
|
| 230 |
impl<'a> IndexEntry<'a> { |
| 231 |
/// The length of an index entry. It's fixed size. 40 bytes for the IANA |
| 232 |
/// time zone identifier. 4 bytes for each of 3 big-endian integers. The |
| 233 |
/// first is the start of the corresponding TZif data within the data |
| 234 |
/// block. The second is the length of said TZif data. And the third is |
| 235 |
/// the "raw UTC offset" of the time zone. (I'm unclear on the semantics |
| 236 |
/// of this third, since some time zones have more than one because of |
| 237 |
/// DST. And of course, it can change over time. Since I don't know what |
| 238 |
/// Android uses this for, I'm not sure how I'm supposed to interpret it.) |
| 239 |
const LEN: usize = 40 + 3 * 4; |
| 240 |
|
| 241 |
/// Creates a new view into an entry in the concatenated TZif index. |
| 242 |
/// |
| 243 |
/// # Panics |
| 244 |
/// |
| 245 |
/// When `slice` does not have the expected length (`IndexEntry::LEN`). |
| 246 |
fn new(slice: &'a [u8]) -> IndexEntry<'a> { |
| 247 |
assert_eq!(slice.len(), IndexEntry::LEN, "invalid index entry length"); |
| 248 |
IndexEntry(slice) |
| 249 |
} |
| 250 |
|
| 251 |
/// Like `name_bytes`, but as a `&str`. |
| 252 |
/// |
| 253 |
/// This returns an error if the name isn't valid UTF-8. |
| 254 |
fn name(&self) -> Result<&str, Error> { |
| 255 |
core::str::from_utf8(self.name_bytes()) |
| 256 |
.map_err(|_| Error::from(E::ExpectedIanaName)) |
| 257 |
} |
| 258 |
|
| 259 |
/// Returns the IANA time zone identifier as a byte slice. |
| 260 |
/// |
| 261 |
/// In theory, an empty slice could be returned. But if that happens, |
| 262 |
/// then there is probably a bug in this code somewhere, the format |
| 263 |
/// changed or the source data is corrupt somehow. |
| 264 |
fn name_bytes(&self) -> &'a [u8] { |
| 265 |
let mut block = &self.0[..40]; |
| 266 |
while block.last().copied() == Some(0) { |
| 267 |
block = &block[..block.len() - 1]; |
| 268 |
} |
| 269 |
block |
| 270 |
} |
| 271 |
|
| 272 |
/// Returns the starting offset (relative to the beginning of the TZif |
| 273 |
/// data block) of the corresponding TZif data. |
| 274 |
fn start(&self) -> u64 { |
| 275 |
u64::from(read_be32(&self.0[40..44])) |
| 276 |
} |
| 277 |
|
| 278 |
/// Returns the length of the TZif data block. |
| 279 |
/// |
| 280 |
/// Beware of using this to create allocations. In theory, this should be |
| 281 |
/// trusted data, but the length can be any 32-bit integer. If it's used to |
| 282 |
/// create an allocation, it could potentially be up to 4GB. |
| 283 |
fn len(&self) -> usize { |
| 284 |
// N.B. Overflow only occurs here on 16-bit (or smaller) platforms, |
| 285 |
// which at the time of writing, is not supported by Jiff. Instead, |
| 286 |
// a `usize::MAX` will trigger an allocation error. |
| 287 |
usize::try_from(read_be32(&self.0[44..48])).unwrap_or(usize::MAX) |
| 288 |
} |
| 289 |
} |
| 290 |
|
| 291 |
impl<'a> core::fmt::Debug for IndexEntry<'a> { |
| 292 |
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result { |
| 293 |
f.debug_struct("IndexEntry") |
| 294 |
.field("name", &escape::Bytes(self.name_bytes())) |
| 295 |
.field("start", &self.start()) |
| 296 |
.field("len", &self.len()) |
| 297 |
.finish() |
| 298 |
} |
| 299 |
} |
| 300 |
|
| 301 |
/// A crate-internal trait defining the source of concatenated TZif data. |
| 302 |
/// |
| 303 |
/// Basically, this just provides a way to read a fixed amount of data at a |
| 304 |
/// particular offset. This is obviously trivial to implement on `&[u8]` (and |
| 305 |
/// indeed, we do so for testing), but we use it to abstract over platform |
| 306 |
/// differences when reading from a `File`. |
| 307 |
/// |
| 308 |
/// The intent is that on Unix, this will use `pread`, which avoids a file |
| 309 |
/// seek followed by a `read` call. |
| 310 |
pub(crate) trait Read { |
| 311 |
fn read_exact_at(&self, buf: &mut [u8], offset: u64) -> Result<(), Error>; |
| 312 |
} |
| 313 |
|
| 314 |
impl<'a, R: Read + ?Sized> Read for &'a R { |
| 315 |
fn read_exact_at(&self, buf: &mut [u8], offset: u64) -> Result<(), Error> { |
| 316 |
(**self).read_exact_at(buf, offset) |
| 317 |
} |
| 318 |
} |
| 319 |
|
| 320 |
/// Reads a 32-bit big endian encoded integer from `bytes`. |
| 321 |
/// |
| 322 |
/// # Panics |
| 323 |
/// |
| 324 |
/// If `bytes.len() != 4`. |
| 325 |
fn read_be32(bytes: &[u8]) -> u32 { |
| 326 |
u32::from_be_bytes(bytes.try_into().expect("slice of length 4")) |
| 327 |
} |
| 328 |
|
| 329 |
#[cfg(test)] |
| 330 |
impl Read for [u8] { |
| 331 |
fn read_exact_at(&self, buf: &mut [u8], offset: u64) -> Result<(), Error> { |
| 332 |
let offset = usize::try_from(offset) |
| 333 |
.map_err(|_| E::InvalidOffsetOverflowSlice)?; |
| 334 |
let Some(slice) = self.get(offset..) else { |
| 335 |
return Err(Error::from(E::InvalidOffsetTooBig)); |
| 336 |
}; |
| 337 |
if buf.len() > slice.len() { |
| 338 |
return Err(Error::from(E::ExpectedMoreData)); |
| 339 |
} |
| 340 |
buf.copy_from_slice(&slice[..buf.len()]); |
| 341 |
Ok(()) |
| 342 |
} |
| 343 |
} |
| 344 |
|
| 345 |
#[cfg(all(feature = "std", unix))] |
| 346 |
impl Read for std::fs::File { |
| 347 |
fn read_exact_at(&self, buf: &mut [u8], offset: u64) -> Result<(), Error> { |
| 348 |
use std::os::unix::fs::FileExt; |
| 349 |
FileExt::read_exact_at(self, buf, offset).map_err(Error::io) |
| 350 |
} |
| 351 |
} |
| 352 |
|
| 353 |
#[cfg(all(feature = "std", windows))] |
| 354 |
impl Read for std::fs::File { |
| 355 |
fn read_exact_at( |
| 356 |
&self, |
| 357 |
mut buf: &mut [u8], |
| 358 |
mut offset: u64, |
| 359 |
) -> Result<(), Error> { |
| 360 |
use std::{io, os::windows::fs::FileExt}; |
| 361 |
|
| 362 |
while !buf.is_empty() { |
| 363 |
match self.seek_read(buf, offset) { |
| 364 |
Ok(0) => break, |
| 365 |
Ok(n) => { |
| 366 |
buf = &mut buf[n..]; |
| 367 |
offset = u64::try_from(n) |
| 368 |
.ok() |
| 369 |
.and_then(|n| n.checked_add(offset)) |
| 370 |
.ok_or(E::InvalidOffsetOverflowFile)?; |
| 371 |
} |
| 372 |
Err(ref e) if e.kind() == io::ErrorKind::Interrupted => {} |
| 373 |
Err(e) => return Err(Error::io(e)), |
| 374 |
} |
| 375 |
} |
| 376 |
if !buf.is_empty() { |
| 377 |
Err(Error::io(io::Error::new( |
| 378 |
io::ErrorKind::UnexpectedEof, |
| 379 |
"failed to fill whole buffer", |
| 380 |
))) |
| 381 |
} else { |
| 382 |
Ok(()) |
| 383 |
} |
| 384 |
} |
| 385 |
} |
| 386 |
|
| 387 |
#[cfg(all(feature = "std", all(not(unix), not(windows))))] |
| 388 |
impl Read for std::fs::File { |
| 389 |
fn read_exact_at(&self, buf: &mut [u8], offset: u64) -> Result<(), Error> { |
| 390 |
use std::io::{Read as _, Seek as _, SeekFrom}; |
| 391 |
let mut file = self; |
| 392 |
file.seek(SeekFrom::Start(offset)) |
| 393 |
.map_err(Error::io) |
| 394 |
.context(E::FailedSeek)?; |
| 395 |
file.read_exact(buf).map_err(Error::io) |
| 396 |
} |
| 397 |
} |
| 398 |
|
| 399 |
/// Allocates `additional` extra bytes on the `Vec` given and set them to `0`. |
| 400 |
/// |
| 401 |
/// This specifically will never do an "OOM panic" and will instead return an |
| 402 |
/// error (courtesy of `Vec::try_reserve_exact`). It will also return an error |
| 403 |
/// without even trying the allocation if it's deemed to be "too big." |
| 404 |
/// |
| 405 |
/// This is used so that we are extra careful about creating allocations based |
| 406 |
/// on integers parsed from concatenated TZif data. Generally speaking, the |
| 407 |
/// data we parse should be "trusted" (since it's probably not writable by |
| 408 |
/// anyone other than `root`), but who knows where this code will ultimately be |
| 409 |
/// used. So we try pretty hard to avoid panicking (even for OOM). |
| 410 |
/// |
| 411 |
/// To be clear, we probably could panic on the error path. The goal here |
| 412 |
/// isn't to avoid OOM because you can't allocate 10 bytes---Jiff isn't robust |
| 413 |
/// enough in that kind of environment by far. The goal is to avoid OOM for |
| 414 |
/// exorbitantly large allocations through some kind of attack vector. |
| 415 |
fn alloc(bytes: &mut Vec<u8>, additional: usize) -> Result<(), Error> { |
| 416 |
if additional > ALLOC_LIMIT { |
| 417 |
return Err(Error::from(E::AllocRequestOverLimit)); |
| 418 |
} |
| 419 |
bytes.try_reserve_exact(additional).map_err(|_| E::AllocFailed)?; |
| 420 |
// This... can't actually happen right? |
| 421 |
let new_len = |
| 422 |
bytes.len().checked_add(additional).ok_or(E::AllocOverflow)?; |
| 423 |
bytes.resize(new_len, 0); |
| 424 |
Ok(()) |
| 425 |
} |
| 426 |
|
| 427 |
#[cfg(test)] |
| 428 |
mod tests { |
| 429 |
use crate::{ |
| 430 |
civil::date, |
| 431 |
tz::{ |
| 432 |
offset, testdata::ANDROID_CONCATENATED_TZIF, AmbiguousOffset, |
| 433 |
Offset, |
| 434 |
}, |
| 435 |
Timestamp, |
| 436 |
}; |
| 437 |
|
| 438 |
use super::*; |
| 439 |
|
| 440 |
fn unambiguous(offset_hours: i8) -> AmbiguousOffset { |
| 441 |
let offset = offset(offset_hours); |
| 442 |
o_unambiguous(offset) |
| 443 |
} |
| 444 |
|
| 445 |
fn gap( |
| 446 |
earlier_offset_hours: i8, |
| 447 |
later_offset_hours: i8, |
| 448 |
) -> AmbiguousOffset { |
| 449 |
let earlier = offset(earlier_offset_hours); |
| 450 |
let later = offset(later_offset_hours); |
| 451 |
o_gap(earlier, later) |
| 452 |
} |
| 453 |
|
| 454 |
fn fold( |
| 455 |
earlier_offset_hours: i8, |
| 456 |
later_offset_hours: i8, |
| 457 |
) -> AmbiguousOffset { |
| 458 |
let earlier = offset(earlier_offset_hours); |
| 459 |
let later = offset(later_offset_hours); |
| 460 |
o_fold(earlier, later) |
| 461 |
} |
| 462 |
|
| 463 |
fn o_unambiguous(offset: Offset) -> AmbiguousOffset { |
| 464 |
AmbiguousOffset::Unambiguous { offset } |
| 465 |
} |
| 466 |
|
| 467 |
fn o_gap(earlier: Offset, later: Offset) -> AmbiguousOffset { |
| 468 |
AmbiguousOffset::Gap { before: earlier, after: later } |
| 469 |
} |
| 470 |
|
| 471 |
fn o_fold(earlier: Offset, later: Offset) -> AmbiguousOffset { |
| 472 |
AmbiguousOffset::Fold { before: earlier, after: later } |
| 473 |
} |
| 474 |
|
| 475 |
// Copied from src/tz/mod.rs. |
| 476 |
#[test] |
| 477 |
fn time_zone_tzif_to_ambiguous_timestamp() { |
| 478 |
let tests: &[(&str, &[_])] = &[ |
| 479 |
( |
| 480 |
"America/New_York", |
| 481 |
&[ |
| 482 |
((1969, 12, 31, 19, 0, 0, 0), unambiguous(-5)), |
| 483 |
((2024, 3, 10, 1, 59, 59, 999_999_999), unambiguous(-5)), |
| 484 |
((2024, 3, 10, 2, 0, 0, 0), gap(-5, -4)), |
| 485 |
((2024, 3, 10, 2, 59, 59, 999_999_999), gap(-5, -4)), |
| 486 |
((2024, 3, 10, 3, 0, 0, 0), unambiguous(-4)), |
| 487 |
((2024, 11, 3, 0, 59, 59, 999_999_999), unambiguous(-4)), |
| 488 |
((2024, 11, 3, 1, 0, 0, 0), fold(-4, -5)), |
| 489 |
((2024, 11, 3, 1, 59, 59, 999_999_999), fold(-4, -5)), |
| 490 |
((2024, 11, 3, 2, 0, 0, 0), unambiguous(-5)), |
| 491 |
], |
| 492 |
), |
| 493 |
( |
| 494 |
"Europe/Dublin", |
| 495 |
&[ |
| 496 |
((1970, 1, 1, 0, 0, 0, 0), unambiguous(1)), |
| 497 |
((2024, 3, 31, 0, 59, 59, 999_999_999), unambiguous(0)), |
| 498 |
((2024, 3, 31, 1, 0, 0, 0), gap(0, 1)), |
| 499 |
((2024, 3, 31, 1, 59, 59, 999_999_999), gap(0, 1)), |
| 500 |
((2024, 3, 31, 2, 0, 0, 0), unambiguous(1)), |
| 501 |
((2024, 10, 27, 0, 59, 59, 999_999_999), unambiguous(1)), |
| 502 |
((2024, 10, 27, 1, 0, 0, 0), fold(1, 0)), |
| 503 |
((2024, 10, 27, 1, 59, 59, 999_999_999), fold(1, 0)), |
| 504 |
((2024, 10, 27, 2, 0, 0, 0), unambiguous(0)), |
| 505 |
], |
| 506 |
), |
| 507 |
( |
| 508 |
"Australia/Tasmania", |
| 509 |
&[ |
| 510 |
((1970, 1, 1, 11, 0, 0, 0), unambiguous(11)), |
| 511 |
((2024, 4, 7, 1, 59, 59, 999_999_999), unambiguous(11)), |
| 512 |
((2024, 4, 7, 2, 0, 0, 0), fold(11, 10)), |
| 513 |
((2024, 4, 7, 2, 59, 59, 999_999_999), fold(11, 10)), |
| 514 |
((2024, 4, 7, 3, 0, 0, 0), unambiguous(10)), |
| 515 |
((2024, 10, 6, 1, 59, 59, 999_999_999), unambiguous(10)), |
| 516 |
((2024, 10, 6, 2, 0, 0, 0), gap(10, 11)), |
| 517 |
((2024, 10, 6, 2, 59, 59, 999_999_999), gap(10, 11)), |
| 518 |
((2024, 10, 6, 3, 0, 0, 0), unambiguous(11)), |
| 519 |
], |
| 520 |
), |
| 521 |
( |
| 522 |
"Antarctica/Troll", |
| 523 |
&[ |
| 524 |
((1970, 1, 1, 0, 0, 0, 0), unambiguous(0)), |
| 525 |
// test the gap |
| 526 |
((2024, 3, 31, 0, 59, 59, 999_999_999), unambiguous(0)), |
| 527 |
((2024, 3, 31, 1, 0, 0, 0), gap(0, 2)), |
| 528 |
((2024, 3, 31, 1, 59, 59, 999_999_999), gap(0, 2)), |
| 529 |
// still in the gap! |
| 530 |
((2024, 3, 31, 2, 0, 0, 0), gap(0, 2)), |
| 531 |
((2024, 3, 31, 2, 59, 59, 999_999_999), gap(0, 2)), |
| 532 |
// finally out |
| 533 |
((2024, 3, 31, 3, 0, 0, 0), unambiguous(2)), |
| 534 |
// test the fold |
| 535 |
((2024, 10, 27, 0, 59, 59, 999_999_999), unambiguous(2)), |
| 536 |
((2024, 10, 27, 1, 0, 0, 0), fold(2, 0)), |
| 537 |
((2024, 10, 27, 1, 59, 59, 999_999_999), fold(2, 0)), |
| 538 |
// still in the fold! |
| 539 |
((2024, 10, 27, 2, 0, 0, 0), fold(2, 0)), |
| 540 |
((2024, 10, 27, 2, 59, 59, 999_999_999), fold(2, 0)), |
| 541 |
// finally out |
| 542 |
((2024, 10, 27, 3, 0, 0, 0), unambiguous(0)), |
| 543 |
], |
| 544 |
), |
| 545 |
( |
| 546 |
"America/St_Johns", |
| 547 |
&[ |
| 548 |
( |
| 549 |
(1969, 12, 31, 20, 30, 0, 0), |
| 550 |
o_unambiguous(-Offset::hms(3, 30, 0)), |
| 551 |
), |
| 552 |
( |
| 553 |
(2024, 3, 10, 1, 59, 59, 999_999_999), |
| 554 |
o_unambiguous(-Offset::hms(3, 30, 0)), |
| 555 |
), |
| 556 |
( |
| 557 |
(2024, 3, 10, 2, 0, 0, 0), |
| 558 |
o_gap(-Offset::hms(3, 30, 0), -Offset::hms(2, 30, 0)), |
| 559 |
), |
| 560 |
( |
| 561 |
(2024, 3, 10, 2, 59, 59, 999_999_999), |
| 562 |
o_gap(-Offset::hms(3, 30, 0), -Offset::hms(2, 30, 0)), |
| 563 |
), |
| 564 |
( |
| 565 |
(2024, 3, 10, 3, 0, 0, 0), |
| 566 |
o_unambiguous(-Offset::hms(2, 30, 0)), |
| 567 |
), |
| 568 |
( |
| 569 |
(2024, 11, 3, 0, 59, 59, 999_999_999), |
| 570 |
o_unambiguous(-Offset::hms(2, 30, 0)), |
| 571 |
), |
| 572 |
( |
| 573 |
(2024, 11, 3, 1, 0, 0, 0), |
| 574 |
o_fold(-Offset::hms(2, 30, 0), -Offset::hms(3, 30, 0)), |
| 575 |
), |
| 576 |
( |
| 577 |
(2024, 11, 3, 1, 59, 59, 999_999_999), |
| 578 |
o_fold(-Offset::hms(2, 30, 0), -Offset::hms(3, 30, 0)), |
| 579 |
), |
| 580 |
( |
| 581 |
(2024, 11, 3, 2, 0, 0, 0), |
| 582 |
o_unambiguous(-Offset::hms(3, 30, 0)), |
| 583 |
), |
| 584 |
], |
| 585 |
), |
| 586 |
// This time zone has an interesting transition where it jumps |
| 587 |
// backwards a full day at 1867-10-19T15:30:00. |
| 588 |
( |
| 589 |
"America/Sitka", |
| 590 |
&[ |
| 591 |
((1969, 12, 31, 16, 0, 0, 0), unambiguous(-8)), |
| 592 |
( |
| 593 |
(-9999, 1, 2, 16, 58, 46, 0), |
| 594 |
o_unambiguous(Offset::hms(14, 58, 47)), |
| 595 |
), |
| 596 |
( |
| 597 |
(1867, 10, 18, 15, 29, 59, 0), |
| 598 |
o_unambiguous(Offset::hms(14, 58, 47)), |
| 599 |
), |
| 600 |
( |
| 601 |
(1867, 10, 18, 15, 30, 0, 0), |
| 602 |
// A fold of 24 hours!!! |
| 603 |
o_fold( |
| 604 |
Offset::hms(14, 58, 47), |
| 605 |
-Offset::hms(9, 1, 13), |
| 606 |
), |
| 607 |
), |
| 608 |
( |
| 609 |
(1867, 10, 19, 15, 29, 59, 999_999_999), |
| 610 |
// Still in the fold... |
| 611 |
o_fold( |
| 612 |
Offset::hms(14, 58, 47), |
| 613 |
-Offset::hms(9, 1, 13), |
| 614 |
), |
| 615 |
), |
| 616 |
( |
| 617 |
(1867, 10, 19, 15, 30, 0, 0), |
| 618 |
// Finally out. |
| 619 |
o_unambiguous(-Offset::hms(9, 1, 13)), |
| 620 |
), |
| 621 |
], |
| 622 |
), |
| 623 |
// As with to_datetime, we test every possible transition |
| 624 |
// point here since this time zone has a small number of them. |
| 625 |
( |
| 626 |
"Pacific/Honolulu", |
| 627 |
&[ |
| 628 |
( |
| 629 |
(1896, 1, 13, 11, 59, 59, 0), |
| 630 |
o_unambiguous(-Offset::hms(10, 31, 26)), |
| 631 |
), |
| 632 |
( |
| 633 |
(1896, 1, 13, 12, 0, 0, 0), |
| 634 |
o_gap( |
| 635 |
-Offset::hms(10, 31, 26), |
| 636 |
-Offset::hms(10, 30, 0), |
| 637 |
), |
| 638 |
), |
| 639 |
( |
| 640 |
(1896, 1, 13, 12, 1, 25, 0), |
| 641 |
o_gap( |
| 642 |
-Offset::hms(10, 31, 26), |
| 643 |
-Offset::hms(10, 30, 0), |
| 644 |
), |
| 645 |
), |
| 646 |
( |
| 647 |
(1896, 1, 13, 12, 1, 26, 0), |
| 648 |
o_unambiguous(-Offset::hms(10, 30, 0)), |
| 649 |
), |
| 650 |
( |
| 651 |
(1933, 4, 30, 1, 59, 59, 0), |
| 652 |
o_unambiguous(-Offset::hms(10, 30, 0)), |
| 653 |
), |
| 654 |
( |
| 655 |
(1933, 4, 30, 2, 0, 0, 0), |
| 656 |
o_gap(-Offset::hms(10, 30, 0), -Offset::hms(9, 30, 0)), |
| 657 |
), |
| 658 |
( |
| 659 |
(1933, 4, 30, 2, 59, 59, 0), |
| 660 |
o_gap(-Offset::hms(10, 30, 0), -Offset::hms(9, 30, 0)), |
| 661 |
), |
| 662 |
( |
| 663 |
(1933, 4, 30, 3, 0, 0, 0), |
| 664 |
o_unambiguous(-Offset::hms(9, 30, 0)), |
| 665 |
), |
| 666 |
( |
| 667 |
(1933, 5, 21, 10, 59, 59, 0), |
| 668 |
o_unambiguous(-Offset::hms(9, 30, 0)), |
| 669 |
), |
| 670 |
( |
| 671 |
(1933, 5, 21, 11, 0, 0, 0), |
| 672 |
o_fold( |
| 673 |
-Offset::hms(9, 30, 0), |
| 674 |
-Offset::hms(10, 30, 0), |
| 675 |
), |
| 676 |
), |
| 677 |
( |
| 678 |
(1933, 5, 21, 11, 59, 59, 0), |
| 679 |
o_fold( |
| 680 |
-Offset::hms(9, 30, 0), |
| 681 |
-Offset::hms(10, 30, 0), |
| 682 |
), |
| 683 |
), |
| 684 |
( |
| 685 |
(1933, 5, 21, 12, 0, 0, 0), |
| 686 |
o_unambiguous(-Offset::hms(10, 30, 0)), |
| 687 |
), |
| 688 |
( |
| 689 |
(1942, 2, 9, 1, 59, 59, 0), |
| 690 |
o_unambiguous(-Offset::hms(10, 30, 0)), |
| 691 |
), |
| 692 |
( |
| 693 |
(1942, 2, 9, 2, 0, 0, 0), |
| 694 |
o_gap(-Offset::hms(10, 30, 0), -Offset::hms(9, 30, 0)), |
| 695 |
), |
| 696 |
( |
| 697 |
(1942, 2, 9, 2, 59, 59, 0), |
| 698 |
o_gap(-Offset::hms(10, 30, 0), -Offset::hms(9, 30, 0)), |
| 699 |
), |
| 700 |
( |
| 701 |
(1942, 2, 9, 3, 0, 0, 0), |
| 702 |
o_unambiguous(-Offset::hms(9, 30, 0)), |
| 703 |
), |
| 704 |
( |
| 705 |
(1945, 8, 14, 13, 29, 59, 0), |
| 706 |
o_unambiguous(-Offset::hms(9, 30, 0)), |
| 707 |
), |
| 708 |
( |
| 709 |
(1945, 8, 14, 13, 30, 0, 0), |
| 710 |
o_unambiguous(-Offset::hms(9, 30, 0)), |
| 711 |
), |
| 712 |
( |
| 713 |
(1945, 8, 14, 13, 30, 1, 0), |
| 714 |
o_unambiguous(-Offset::hms(9, 30, 0)), |
| 715 |
), |
| 716 |
( |
| 717 |
(1945, 9, 30, 0, 59, 59, 0), |
| 718 |
o_unambiguous(-Offset::hms(9, 30, 0)), |
| 719 |
), |
| 720 |
( |
| 721 |
(1945, 9, 30, 1, 0, 0, 0), |
| 722 |
o_fold( |
| 723 |
-Offset::hms(9, 30, 0), |
| 724 |
-Offset::hms(10, 30, 0), |
| 725 |
), |
| 726 |
), |
| 727 |
( |
| 728 |
(1945, 9, 30, 1, 59, 59, 0), |
| 729 |
o_fold( |
| 730 |
-Offset::hms(9, 30, 0), |
| 731 |
-Offset::hms(10, 30, 0), |
| 732 |
), |
| 733 |
), |
| 734 |
( |
| 735 |
(1945, 9, 30, 2, 0, 0, 0), |
| 736 |
o_unambiguous(-Offset::hms(10, 30, 0)), |
| 737 |
), |
| 738 |
( |
| 739 |
(1947, 6, 8, 1, 59, 59, 0), |
| 740 |
o_unambiguous(-Offset::hms(10, 30, 0)), |
| 741 |
), |
| 742 |
( |
| 743 |
(1947, 6, 8, 2, 0, 0, 0), |
| 744 |
o_gap(-Offset::hms(10, 30, 0), -offset(10)), |
| 745 |
), |
| 746 |
( |
| 747 |
(1947, 6, 8, 2, 29, 59, 0), |
| 748 |
o_gap(-Offset::hms(10, 30, 0), -offset(10)), |
| 749 |
), |
| 750 |
((1947, 6, 8, 2, 30, 0, 0), unambiguous(-10)), |
| 751 |
], |
| 752 |
), |
| 753 |
]; |
| 754 |
let db = ConcatenatedTzif::open(ANDROID_CONCATENATED_TZIF).unwrap(); |
| 755 |
let (mut buf1, mut buf2) = (alloc::vec![], alloc::vec![]); |
| 756 |
for &(tzname, datetimes_to_ambiguous) in tests { |
| 757 |
let tz = db.get(tzname, &mut buf1, &mut buf2).unwrap().unwrap(); |
| 758 |
for &(datetime, ambiguous_kind) in datetimes_to_ambiguous { |
| 759 |
let (year, month, day, hour, min, sec, nano) = datetime; |
| 760 |
let dt = date(year, month, day).at(hour, min, sec, nano); |
| 761 |
let got = tz.to_ambiguous_zoned(dt); |
| 762 |
assert_eq!( |
| 763 |
got.offset(), |
| 764 |
ambiguous_kind, |
| 765 |
"\nTZ: {tzname}\ndatetime: \ |
| 766 |
{year:04}-{month:02}-{day:02}T\ |
| 767 |
{hour:02}:{min:02}:{sec:02}.{nano:09}", |
| 768 |
); |
| 769 |
} |
| 770 |
} |
| 771 |
} |
| 772 |
|
| 773 |
// Copied from src/tz/mod.rs. |
| 774 |
#[test] |
| 775 |
fn time_zone_tzif_to_datetime() { |
| 776 |
let o = |hours| offset(hours); |
| 777 |
let tests: &[(&str, &[_])] = &[ |
| 778 |
( |
| 779 |
"America/New_York", |
| 780 |
&[ |
| 781 |
((0, 0), o(-5), "EST", (1969, 12, 31, 19, 0, 0, 0)), |
| 782 |
( |
| 783 |
(1710052200, 0), |
| 784 |
o(-5), |
| 785 |
"EST", |
| 786 |
(2024, 3, 10, 1, 30, 0, 0), |
| 787 |
), |
| 788 |
( |
| 789 |
(1710053999, 999_999_999), |
| 790 |
o(-5), |
| 791 |
"EST", |
| 792 |
(2024, 3, 10, 1, 59, 59, 999_999_999), |
| 793 |
), |
| 794 |
((1710054000, 0), o(-4), "EDT", (2024, 3, 10, 3, 0, 0, 0)), |
| 795 |
( |
| 796 |
(1710055800, 0), |
| 797 |
o(-4), |
| 798 |
"EDT", |
| 799 |
(2024, 3, 10, 3, 30, 0, 0), |
| 800 |
), |
| 801 |
((1730610000, 0), o(-4), "EDT", (2024, 11, 3, 1, 0, 0, 0)), |
| 802 |
( |
| 803 |
(1730611800, 0), |
| 804 |
o(-4), |
| 805 |
"EDT", |
| 806 |
(2024, 11, 3, 1, 30, 0, 0), |
| 807 |
), |
| 808 |
( |
| 809 |
(1730613599, 999_999_999), |
| 810 |
o(-4), |
| 811 |
"EDT", |
| 812 |
(2024, 11, 3, 1, 59, 59, 999_999_999), |
| 813 |
), |
| 814 |
((1730613600, 0), o(-5), "EST", (2024, 11, 3, 1, 0, 0, 0)), |
| 815 |
( |
| 816 |
(1730615400, 0), |
| 817 |
o(-5), |
| 818 |
"EST", |
| 819 |
(2024, 11, 3, 1, 30, 0, 0), |
| 820 |
), |
| 821 |
], |
| 822 |
), |
| 823 |
( |
| 824 |
"Australia/Tasmania", |
| 825 |
&[ |
| 826 |
((0, 0), o(11), "AEDT", (1970, 1, 1, 11, 0, 0, 0)), |
| 827 |
( |
| 828 |
(1728142200, 0), |
| 829 |
o(10), |
| 830 |
"AEST", |
| 831 |
(2024, 10, 6, 1, 30, 0, 0), |
| 832 |
), |
| 833 |
( |
| 834 |
(1728143999, 999_999_999), |
| 835 |
o(10), |
| 836 |
"AEST", |
| 837 |
(2024, 10, 6, 1, 59, 59, 999_999_999), |
| 838 |
), |
| 839 |
( |
| 840 |
(1728144000, 0), |
| 841 |
o(11), |
| 842 |
"AEDT", |
| 843 |
(2024, 10, 6, 3, 0, 0, 0), |
| 844 |
), |
| 845 |
( |
| 846 |
(1728145800, 0), |
| 847 |
o(11), |
| 848 |
"AEDT", |
| 849 |
(2024, 10, 6, 3, 30, 0, 0), |
| 850 |
), |
| 851 |
((1712415600, 0), o(11), "AEDT", (2024, 4, 7, 2, 0, 0, 0)), |
| 852 |
( |
| 853 |
(1712417400, 0), |
| 854 |
o(11), |
| 855 |
"AEDT", |
| 856 |
(2024, 4, 7, 2, 30, 0, 0), |
| 857 |
), |
| 858 |
( |
| 859 |
(1712419199, 999_999_999), |
| 860 |
o(11), |
| 861 |
"AEDT", |
| 862 |
(2024, 4, 7, 2, 59, 59, 999_999_999), |
| 863 |
), |
| 864 |
((1712419200, 0), o(10), "AEST", (2024, 4, 7, 2, 0, 0, 0)), |
| 865 |
( |
| 866 |
(1712421000, 0), |
| 867 |
o(10), |
| 868 |
"AEST", |
| 869 |
(2024, 4, 7, 2, 30, 0, 0), |
| 870 |
), |
| 871 |
], |
| 872 |
), |
| 873 |
// Pacific/Honolulu is small eough that we just test every |
| 874 |
// possible instant before, at and after each transition. |
| 875 |
( |
| 876 |
"Pacific/Honolulu", |
| 877 |
&[ |
| 878 |
( |
| 879 |
(-2334101315, 0), |
| 880 |
-Offset::hms(10, 31, 26), |
| 881 |
"LMT", |
| 882 |
(1896, 1, 13, 11, 59, 59, 0), |
| 883 |
), |
| 884 |
( |
| 885 |
(-2334101314, 0), |
| 886 |
-Offset::hms(10, 30, 0), |
| 887 |
"HST", |
| 888 |
(1896, 1, 13, 12, 1, 26, 0), |
| 889 |
), |
| 890 |
( |
| 891 |
(-2334101313, 0), |
| 892 |
-Offset::hms(10, 30, 0), |
| 893 |
"HST", |
| 894 |
(1896, 1, 13, 12, 1, 27, 0), |
| 895 |
), |
| 896 |
( |
| 897 |
(-1157283001, 0), |
| 898 |
-Offset::hms(10, 30, 0), |
| 899 |
"HST", |
| 900 |
(1933, 4, 30, 1, 59, 59, 0), |
| 901 |
), |
| 902 |
( |
| 903 |
(-1157283000, 0), |
| 904 |
-Offset::hms(9, 30, 0), |
| 905 |
"HDT", |
| 906 |
(1933, 4, 30, 3, 0, 0, 0), |
| 907 |
), |
| 908 |
( |
| 909 |
(-1157282999, 0), |
| 910 |
-Offset::hms(9, 30, 0), |
| 911 |
"HDT", |
| 912 |
(1933, 4, 30, 3, 0, 1, 0), |
| 913 |
), |
| 914 |
( |
| 915 |
(-1155436201, 0), |
| 916 |
-Offset::hms(9, 30, 0), |
| 917 |
"HDT", |
| 918 |
(1933, 5, 21, 11, 59, 59, 0), |
| 919 |
), |
| 920 |
( |
| 921 |
(-1155436200, 0), |
| 922 |
-Offset::hms(10, 30, 0), |
| 923 |
"HST", |
| 924 |
(1933, 5, 21, 11, 0, 0, 0), |
| 925 |
), |
| 926 |
( |
| 927 |
(-1155436199, 0), |
| 928 |
-Offset::hms(10, 30, 0), |
| 929 |
"HST", |
| 930 |
(1933, 5, 21, 11, 0, 1, 0), |
| 931 |
), |
| 932 |
( |
| 933 |
(-880198201, 0), |
| 934 |
-Offset::hms(10, 30, 0), |
| 935 |
"HST", |
| 936 |
(1942, 2, 9, 1, 59, 59, 0), |
| 937 |
), |
| 938 |
( |
| 939 |
(-880198200, 0), |
| 940 |
-Offset::hms(9, 30, 0), |
| 941 |
"HWT", |
| 942 |
(1942, 2, 9, 3, 0, 0, 0), |
| 943 |
), |
| 944 |
( |
| 945 |
(-880198199, 0), |
| 946 |
-Offset::hms(9, 30, 0), |
| 947 |
"HWT", |
| 948 |
(1942, 2, 9, 3, 0, 1, 0), |
| 949 |
), |
| 950 |
( |
| 951 |
(-769395601, 0), |
| 952 |
-Offset::hms(9, 30, 0), |
| 953 |
"HWT", |
| 954 |
(1945, 8, 14, 13, 29, 59, 0), |
| 955 |
), |
| 956 |
( |
| 957 |
(-769395600, 0), |
| 958 |
-Offset::hms(9, 30, 0), |
| 959 |
"HPT", |
| 960 |
(1945, 8, 14, 13, 30, 0, 0), |
| 961 |
), |
| 962 |
( |
| 963 |
(-769395599, 0), |
| 964 |
-Offset::hms(9, 30, 0), |
| 965 |
"HPT", |
| 966 |
(1945, 8, 14, 13, 30, 1, 0), |
| 967 |
), |
| 968 |
( |
| 969 |
(-765376201, 0), |
| 970 |
-Offset::hms(9, 30, 0), |
| 971 |
"HPT", |
| 972 |
(1945, 9, 30, 1, 59, 59, 0), |
| 973 |
), |
| 974 |
( |
| 975 |
(-765376200, 0), |
| 976 |
-Offset::hms(10, 30, 0), |
| 977 |
"HST", |
| 978 |
(1945, 9, 30, 1, 0, 0, 0), |
| 979 |
), |
| 980 |
( |
| 981 |
(-765376199, 0), |
| 982 |
-Offset::hms(10, 30, 0), |
| 983 |
"HST", |
| 984 |
(1945, 9, 30, 1, 0, 1, 0), |
| 985 |
), |
| 986 |
( |
| 987 |
(-712150201, 0), |
| 988 |
-Offset::hms(10, 30, 0), |
| 989 |
"HST", |
| 990 |
(1947, 6, 8, 1, 59, 59, 0), |
| 991 |
), |
| 992 |
// At this point, we hit the last transition and the POSIX |
| 993 |
// TZ string takes over. |
| 994 |
( |
| 995 |
(-712150200, 0), |
| 996 |
-Offset::hms(10, 0, 0), |
| 997 |
"HST", |
| 998 |
(1947, 6, 8, 2, 30, 0, 0), |
| 999 |
), |
| 1000 |
( |
| 1001 |
(-712150199, 0), |
| 1002 |
-Offset::hms(10, 0, 0), |
| 1003 |
"HST", |
| 1004 |
(1947, 6, 8, 2, 30, 1, 0), |
| 1005 |
), |
| 1006 |
], |
| 1007 |
), |
| 1008 |
// This time zone has an interesting transition where it jumps |
| 1009 |
// backwards a full day at 1867-10-19T15:30:00. |
| 1010 |
( |
| 1011 |
"America/Sitka", |
| 1012 |
&[ |
| 1013 |
((0, 0), o(-8), "PST", (1969, 12, 31, 16, 0, 0, 0)), |
| 1014 |
( |
| 1015 |
(-377705023201, 0), |
| 1016 |
Offset::hms(14, 58, 47), |
| 1017 |
"LMT", |
| 1018 |
(-9999, 1, 2, 16, 58, 46, 0), |
| 1019 |
), |
| 1020 |
( |
| 1021 |
(-3225223728, 0), |
| 1022 |
Offset::hms(14, 58, 47), |
| 1023 |
"LMT", |
| 1024 |
(1867, 10, 19, 15, 29, 59, 0), |
| 1025 |
), |
| 1026 |
// Notice the 24 hour time jump backwards a whole day! |
| 1027 |
( |
| 1028 |
(-3225223727, 0), |
| 1029 |
-Offset::hms(9, 1, 13), |
| 1030 |
"LMT", |
| 1031 |
(1867, 10, 18, 15, 30, 0, 0), |
| 1032 |
), |
| 1033 |
( |
| 1034 |
(-3225223726, 0), |
| 1035 |
-Offset::hms(9, 1, 13), |
| 1036 |
"LMT", |
| 1037 |
(1867, 10, 18, 15, 30, 1, 0), |
| 1038 |
), |
| 1039 |
], |
| 1040 |
), |
| 1041 |
]; |
| 1042 |
let db = ConcatenatedTzif::open(ANDROID_CONCATENATED_TZIF).unwrap(); |
| 1043 |
let (mut buf1, mut buf2) = (alloc::vec![], alloc::vec![]); |
| 1044 |
for &(tzname, timestamps_to_datetimes) in tests { |
| 1045 |
let tz = db.get(tzname, &mut buf1, &mut buf2).unwrap().unwrap(); |
| 1046 |
for &((unix_sec, unix_nano), offset, abbrev, datetime) in |
| 1047 |
timestamps_to_datetimes |
| 1048 |
{ |
| 1049 |
let (year, month, day, hour, min, sec, nano) = datetime; |
| 1050 |
let timestamp = Timestamp::new(unix_sec, unix_nano).unwrap(); |
| 1051 |
let info = tz.to_offset_info(timestamp); |
| 1052 |
assert_eq!( |
| 1053 |
info.offset(), |
| 1054 |
offset, |
| 1055 |
"\nTZ={tzname}, timestamp({unix_sec}, {unix_nano})", |
| 1056 |
); |
| 1057 |
assert_eq!( |
| 1058 |
info.abbreviation(), |
| 1059 |
abbrev, |
| 1060 |
"\nTZ={tzname}, timestamp({unix_sec}, {unix_nano})", |
| 1061 |
); |
| 1062 |
assert_eq!( |
| 1063 |
info.offset().to_datetime(timestamp), |
| 1064 |
date(year, month, day).at(hour, min, sec, nano), |
| 1065 |
"\nTZ={tzname}, timestamp({unix_sec}, {unix_nano})", |
| 1066 |
); |
| 1067 |
} |
| 1068 |
} |
| 1069 |
} |
| 1070 |
|
| 1071 |
#[test] |
| 1072 |
#[cfg(not(miri))] |
| 1073 |
fn read_all_time_zones() { |
| 1074 |
let db = ConcatenatedTzif::open(ANDROID_CONCATENATED_TZIF).unwrap(); |
| 1075 |
let available = db.available(&mut alloc::vec![]).unwrap(); |
| 1076 |
let (mut buf1, mut buf2) = (alloc::vec![], alloc::vec![]); |
| 1077 |
for tzname in available.iter() { |
| 1078 |
let tz = db.get(tzname, &mut buf1, &mut buf2).unwrap().unwrap(); |
| 1079 |
assert_eq!(tzname, tz.iana_name().unwrap()); |
| 1080 |
} |
| 1081 |
} |
| 1082 |
|
| 1083 |
#[test] |
| 1084 |
fn available_len() { |
| 1085 |
let db = ConcatenatedTzif::open(ANDROID_CONCATENATED_TZIF).unwrap(); |
| 1086 |
let available = db.available(&mut alloc::vec![]).unwrap(); |
| 1087 |
assert_eq!(596, available.len()); |
| 1088 |
for window in available.windows(2) { |
| 1089 |
let (x1, x2) = (&window[0], &window[1]); |
| 1090 |
assert!(x1 < x2, "{x1} is not less than {x2}"); |
| 1091 |
} |
| 1092 |
} |
| 1093 |
} |