| 1 | // auto-generated by: jiff-cli generate shared |
| 2 | |
| 3 | use core::fmt::Debug; |
| 4 | |
| 5 | use super::{ |
| 6 | util::{ |
| 7 | array_str::Abbreviation, |
| 8 | error::{err, Error}, |
| 9 | escape::{Byte, Bytes}, |
| 10 | itime::{ |
| 11 | IAmbiguousOffset, IDate, IDateTime, IOffset, ITime, ITimeSecond, |
| 12 | ITimestamp, IWeekday, |
| 13 | }, |
| 14 | }, |
| 15 | PosixDay, PosixDayTime, PosixDst, PosixOffset, PosixRule, PosixTime, |
| 16 | PosixTimeZone, |
| 17 | }; |
| 18 | |
| 19 | impl PosixTimeZone<Abbreviation> { |
| 20 | /// Parse a POSIX `TZ` environment variable, assuming it's a rule and not |
| 21 | /// an implementation defined value, from the given bytes. |
| 22 | pub fn parse(bytes: &[u8]) -> Result<PosixTimeZone<Abbreviation>, Error> { |
| 23 | // We enable the IANA v3+ extensions here. (Namely, that the time |
| 24 | // specification hour value has the range `-167..=167` instead of |
| 25 | // `0..=24`.) Requiring strict POSIX rules doesn't seem necessary |
| 26 | // since the extension is a strict superset. Plus, GNU tooling |
| 27 | // seems to accept the extension. |
| 28 | let parser = Parser { ianav3plus: true, ..Parser::new(bytes) }; |
| 29 | parser.parse() |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | impl<ABBREV: AsRef<str> + Debug> PosixTimeZone<ABBREV> { |
| 34 | /// Returns the appropriate time zone offset to use for the given |
| 35 | /// timestamp. |
| 36 | /// |
| 37 | /// If you need information like whether the offset is in DST or not, or |
| 38 | /// the time zone abbreviation, then use `PosixTimeZone::to_offset_info`. |
| 39 | /// But that API may be more expensive to use, so only use it if you need |
| 40 | /// the additional data. |
| 41 | pub(crate) fn to_offset(&self, timestamp: ITimestamp) -> IOffset { |
| 42 | let std_offset = self.std_offset.to_ioffset(); |
| 43 | if self.dst.is_none() { |
| 44 | return std_offset; |
| 45 | } |
| 46 | |
| 47 | let dt = timestamp.to_datetime(IOffset::UTC); |
| 48 | self.dst_info_utc(dt.date.year) |
| 49 | .filter(|dst_info| dst_info.in_dst(dt)) |
| 50 | .map(|dst_info| dst_info.offset().to_ioffset()) |
| 51 | .unwrap_or_else(|| std_offset) |
| 52 | } |
| 53 | |
| 54 | /// Returns the appropriate time zone offset to use for the given |
| 55 | /// timestamp. |
| 56 | /// |
| 57 | /// This also includes whether the offset returned should be considered |
| 58 | /// to be "DST" or not, along with the time zone abbreviation (e.g., EST |
| 59 | /// for standard time in New York, and EDT for DST in New York). |
| 60 | pub(crate) fn to_offset_info( |
| 61 | &self, |
| 62 | timestamp: ITimestamp, |
| 63 | ) -> (IOffset, &'_ str, bool) { |
| 64 | let std_offset = self.std_offset.to_ioffset(); |
| 65 | if self.dst.is_none() { |
| 66 | return (std_offset, self.std_abbrev.as_ref(), false); |
| 67 | } |
| 68 | |
| 69 | let dt = timestamp.to_datetime(IOffset::UTC); |
| 70 | self.dst_info_utc(dt.date.year) |
| 71 | .filter(|dst_info| dst_info.in_dst(dt)) |
| 72 | .map(|dst_info| { |
| 73 | ( |
| 74 | dst_info.offset().to_ioffset(), |
| 75 | dst_info.dst.abbrev.as_ref(), |
| 76 | true, |
| 77 | ) |
| 78 | }) |
| 79 | .unwrap_or_else(|| (std_offset, self.std_abbrev.as_ref(), false)) |
| 80 | } |
| 81 | |
| 82 | /// Returns a possibly ambiguous timestamp for the given civil datetime. |
| 83 | /// |
| 84 | /// The given datetime should correspond to the "wall" clock time of what |
| 85 | /// humans use to tell time for this time zone. |
| 86 | /// |
| 87 | /// Note that "ambiguous timestamp" is represented by the possible |
| 88 | /// selection of offsets that could be applied to the given datetime. In |
| 89 | /// general, it is only ambiguous around transitions to-and-from DST. The |
| 90 | /// ambiguity can arise as a "fold" (when a particular wall clock time is |
| 91 | /// repeated) or as a "gap" (when a particular wall clock time is skipped |
| 92 | /// entirely). |
| 93 | pub(crate) fn to_ambiguous_kind(&self, dt: IDateTime) -> IAmbiguousOffset { |
| 94 | let year = dt.date.year; |
| 95 | let std_offset = self.std_offset.to_ioffset(); |
| 96 | let Some(dst_info) = self.dst_info_wall(year) else { |
| 97 | return IAmbiguousOffset::Unambiguous { offset: std_offset }; |
| 98 | }; |
| 99 | let dst_offset = dst_info.offset().to_ioffset(); |
| 100 | let diff = dst_offset.second - std_offset.second; |
| 101 | // When the difference between DST and standard is positive, that means |
| 102 | // STD->DST results in a gap while DST->STD results in a fold. However, |
| 103 | // when the difference is negative, that means STD->DST results in a |
| 104 | // fold while DST->STD results in a gap. The former is by far the most |
| 105 | // common. The latter is a bit weird, but real cases do exist. For |
| 106 | // example, Dublin has DST in winter (UTC+01) and STD in the summer |
| 107 | // (UTC+00). |
| 108 | // |
| 109 | // When the difference is zero, then we have a weird POSIX time zone |
| 110 | // where a DST transition rule was specified, but was set to explicitly |
| 111 | // be the same as STD. In this case, there can be no ambiguity. (The |
| 112 | // zero case is strictly redundant. Both the diff < 0 and diff > 0 |
| 113 | // cases handle the zero case correctly. But we write it out for |
| 114 | // clarity.) |
| 115 | if diff == 0 { |
| 116 | debug_assert_eq!(std_offset, dst_offset); |
| 117 | IAmbiguousOffset::Unambiguous { offset: std_offset } |
| 118 | } else if diff.is_negative() { |
| 119 | // For DST transitions that always move behind one hour, ambiguous |
| 120 | // timestamps only occur when the given civil datetime falls in the |
| 121 | // standard time range. |
| 122 | if dst_info.in_dst(dt) { |
| 123 | IAmbiguousOffset::Unambiguous { offset: dst_offset } |
| 124 | } else { |
| 125 | let fold_start = dst_info.start.saturating_add_seconds(diff); |
| 126 | let gap_end = |
| 127 | dst_info.end.saturating_add_seconds(diff.saturating_neg()); |
| 128 | if fold_start <= dt && dt < dst_info.start { |
| 129 | IAmbiguousOffset::Fold { |
| 130 | before: std_offset, |
| 131 | after: dst_offset, |
| 132 | } |
| 133 | } else if dst_info.end <= dt && dt < gap_end { |
| 134 | IAmbiguousOffset::Gap { |
| 135 | before: dst_offset, |
| 136 | after: std_offset, |
| 137 | } |
| 138 | } else { |
| 139 | IAmbiguousOffset::Unambiguous { offset: std_offset } |
| 140 | } |
| 141 | } |
| 142 | } else { |
| 143 | // For DST transitions that always move ahead one hour, ambiguous |
| 144 | // timestamps only occur when the given civil datetime falls in the |
| 145 | // DST range. |
| 146 | if !dst_info.in_dst(dt) { |
| 147 | IAmbiguousOffset::Unambiguous { offset: std_offset } |
| 148 | } else { |
| 149 | // PERF: I wonder if it makes sense to pre-compute these? |
| 150 | // Probably not, because we have to do it based on year of |
| 151 | // datetime given. But if we ever add a "caching" layer for |
| 152 | // POSIX time zones, then it might be worth adding these to it. |
| 153 | let gap_end = dst_info.start.saturating_add_seconds(diff); |
| 154 | let fold_start = |
| 155 | dst_info.end.saturating_add_seconds(diff.saturating_neg()); |
| 156 | if dst_info.start <= dt && dt < gap_end { |
| 157 | IAmbiguousOffset::Gap { |
| 158 | before: std_offset, |
| 159 | after: dst_offset, |
| 160 | } |
| 161 | } else if fold_start <= dt && dt < dst_info.end { |
| 162 | IAmbiguousOffset::Fold { |
| 163 | before: dst_offset, |
| 164 | after: std_offset, |
| 165 | } |
| 166 | } else { |
| 167 | IAmbiguousOffset::Unambiguous { offset: dst_offset } |
| 168 | } |
| 169 | } |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | /// Returns the timestamp of the most recent time zone transition prior |
| 174 | /// to the timestamp given. If one doesn't exist, `None` is returned. |
| 175 | pub(crate) fn previous_transition( |
| 176 | &self, |
| 177 | timestamp: ITimestamp, |
| 178 | ) -> Option<(ITimestamp, IOffset, &'_ str, bool)> { |
| 179 | let dt = timestamp.to_datetime(IOffset::UTC); |
| 180 | let dst_info = self.dst_info_utc(dt.date.year)?; |
| 181 | let (earlier, later) = dst_info.ordered(); |
| 182 | let (prev, dst_info) = if dt > later { |
| 183 | (later, dst_info) |
| 184 | } else if dt > earlier { |
| 185 | (earlier, dst_info) |
| 186 | } else { |
| 187 | let prev_year = dt.date.prev_year().ok()?; |
| 188 | let dst_info = self.dst_info_utc(prev_year)?; |
| 189 | let (_, later) = dst_info.ordered(); |
| 190 | (later, dst_info) |
| 191 | }; |
| 192 | |
| 193 | let timestamp = prev.to_timestamp_checked(IOffset::UTC)?; |
| 194 | let dt = timestamp.to_datetime(IOffset::UTC); |
| 195 | let (offset, abbrev, dst) = if dst_info.in_dst(dt) { |
| 196 | (dst_info.offset(), dst_info.dst.abbrev.as_ref(), true) |
| 197 | } else { |
| 198 | (&self.std_offset, self.std_abbrev.as_ref(), false) |
| 199 | }; |
| 200 | Some((timestamp, offset.to_ioffset(), abbrev, dst)) |
| 201 | } |
| 202 | |
| 203 | /// Returns the timestamp of the soonest time zone transition after the |
| 204 | /// timestamp given. If one doesn't exist, `None` is returned. |
| 205 | pub(crate) fn next_transition( |
| 206 | &self, |
| 207 | timestamp: ITimestamp, |
| 208 | ) -> Option<(ITimestamp, IOffset, &'_ str, bool)> { |
| 209 | let dt = timestamp.to_datetime(IOffset::UTC); |
| 210 | let dst_info = self.dst_info_utc(dt.date.year)?; |
| 211 | let (earlier, later) = dst_info.ordered(); |
| 212 | let (next, dst_info) = if dt < earlier { |
| 213 | (earlier, dst_info) |
| 214 | } else if dt < later { |
| 215 | (later, dst_info) |
| 216 | } else { |
| 217 | let next_year = dt.date.next_year().ok()?; |
| 218 | let dst_info = self.dst_info_utc(next_year)?; |
| 219 | let (earlier, _) = dst_info.ordered(); |
| 220 | (earlier, dst_info) |
| 221 | }; |
| 222 | |
| 223 | let timestamp = next.to_timestamp_checked(IOffset::UTC)?; |
| 224 | let dt = timestamp.to_datetime(IOffset::UTC); |
| 225 | let (offset, abbrev, dst) = if dst_info.in_dst(dt) { |
| 226 | (dst_info.offset(), dst_info.dst.abbrev.as_ref(), true) |
| 227 | } else { |
| 228 | (&self.std_offset, self.std_abbrev.as_ref(), false) |
| 229 | }; |
| 230 | Some((timestamp, offset.to_ioffset(), abbrev, dst)) |
| 231 | } |
| 232 | |
| 233 | /// Returns the range in which DST occurs. |
| 234 | /// |
| 235 | /// The civil datetimes returned are in UTC. This is useful for determining |
| 236 | /// whether a timestamp is in DST or not. |
| 237 | fn dst_info_utc(&self, year: i16) -> Option<DstInfo<'_, ABBREV>> { |
| 238 | let dst = self.dst.as_ref()?; |
| 239 | // DST time starts with respect to standard time, so offset it by the |
| 240 | // standard offset. |
| 241 | let start = |
| 242 | dst.rule.start.to_datetime(year, self.std_offset.to_ioffset()); |
| 243 | // DST time ends with respect to DST time, so offset it by the DST |
| 244 | // offset. |
| 245 | let mut end = dst.rule.end.to_datetime(year, dst.offset.to_ioffset()); |
| 246 | // This is a whacky special case when DST is permanent, but the math |
| 247 | // using to calculate the start/end datetimes ends up leaving a gap |
| 248 | // for standard time to appear. In which case, it's possible for a |
| 249 | // timestamp at the end of a calendar year to get standard time when |
| 250 | // it really should be DST. |
| 251 | // |
| 252 | // We detect this case by re-interpreting the end of the boundary using |
| 253 | // the standard offset. If we get a datetime that is in a different |
| 254 | // year, then it follows that standard time is actually impossible to |
| 255 | // occur. |
| 256 | // |
| 257 | // These weird POSIX time zones can occur as the TZ strings in |
| 258 | // a TZif file compiled using rearguard semantics. For example, |
| 259 | // `Africa/Casablanca` has: |
| 260 | // |
| 261 | // XXX-2<+01>-1,0/0,J365/23 |
| 262 | // |
| 263 | // Notice here that DST is actually one hour *behind* (it is usually |
| 264 | // one hour *ahead*) _and_ it ends at 23:00:00 on the last day of the |
| 265 | // year. But if it ends at 23:00, then jumping to standard time moves |
| 266 | // the clocks *forward*. Which would bring us to 00:00:00 on the first |
| 267 | // of the next year... but that is when DST begins! Hence, DST is |
| 268 | // permanent. |
| 269 | // |
| 270 | // Ideally, this could just be handled by our math automatically. But |
| 271 | // I couldn't figure out how to make it work. In particular, in the |
| 272 | // above example for year 2087, we get |
| 273 | // |
| 274 | // start == 2087-01-01T00:00:00Z |
| 275 | // end == 2087-12-31T22:00:00Z |
| 276 | // |
| 277 | // Which leaves a two hour gap for a timestamp to get erroneously |
| 278 | // categorized as standard time. |
| 279 | // |
| 280 | // ... so we special case this. We could pre-compute whether a POSIX |
| 281 | // time zone is in permanent DST at construction time, but it's not |
| 282 | // obvious to me that it's worth it. Especially since this is an |
| 283 | // exceptionally rare case. |
| 284 | // |
| 285 | // Note that I did try to consult tzcode's (incredibly inscrutable) |
| 286 | // `localtime` implementation to figure out how they deal with it. At |
| 287 | // first, it looks like they don't have any special handling for this |
| 288 | // case. But looking more closely, they skip any time zone transitions |
| 289 | // generated by POSIX time zones whose rule spans more than 1 year: |
| 290 | // |
| 291 | // https://github.com/eggert/tz/blob/8d65db9786753f3b263087e31c59d191561d63e3/localtime.c#L1717-L1735 |
| 292 | // |
| 293 | // By just ignoring them, I think it achieves the desired effect of |
| 294 | // permanent DST. But I'm not 100% confident in my understanding of |
| 295 | // the code. |
| 296 | if start.date.month == 1 |
| 297 | && start.date.day == 1 |
| 298 | && start.time == ITime::MIN |
| 299 | // NOTE: This should come last because it is potentially expensive. |
| 300 | && year |
| 301 | != end.saturating_add_seconds(self.std_offset.second).date.year |
| 302 | { |
| 303 | end = IDateTime { |
| 304 | date: IDate { year, month: 12, day: 31 }, |
| 305 | time: ITime::MAX, |
| 306 | }; |
| 307 | } |
| 308 | Some(DstInfo { dst, start, end }) |
| 309 | } |
| 310 | |
| 311 | /// Returns the range in which DST occurs. |
| 312 | /// |
| 313 | /// The civil datetimes returned are in "wall clock time." That is, they |
| 314 | /// represent the transitions as they are seen from humans reading a clock |
| 315 | /// within the geographic location of that time zone. |
| 316 | fn dst_info_wall(&self, year: i16) -> Option<DstInfo<'_, ABBREV>> { |
| 317 | let dst = self.dst.as_ref()?; |
| 318 | // POSIX time zones express their DST transitions in terms of wall |
| 319 | // clock time. Since this method specifically is returning wall |
| 320 | // clock times, we don't want to offset our datetimes at all. |
| 321 | let start = dst.rule.start.to_datetime(year, IOffset::UTC); |
| 322 | let end = dst.rule.end.to_datetime(year, IOffset::UTC); |
| 323 | Some(DstInfo { dst, start, end }) |
| 324 | } |
| 325 | |
| 326 | /// Returns the DST transition rule. This panics if this time zone doesn't |
| 327 | /// have DST. |
| 328 | #[cfg(test)] |
| 329 | fn rule(&self) -> &PosixRule { |
| 330 | &self.dst.as_ref().unwrap().rule |
| 331 | } |
| 332 | } |
| 333 | |
| 334 | impl<ABBREV: AsRef<str>> core::fmt::Display for PosixTimeZone<ABBREV> { |
| 335 | fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result { |
| 336 | write!( |
| 337 | f, |
| 338 | "{}{}", |
| 339 | AbbreviationDisplay(self.std_abbrev.as_ref()), |
| 340 | self.std_offset |
| 341 | )?; |
| 342 | if let Some(ref dst) = self.dst { |
| 343 | dst.display(&self.std_offset, f)?; |
| 344 | } |
| 345 | Ok(()) |
| 346 | } |
| 347 | } |
| 348 | |
| 349 | impl<ABBREV: AsRef<str>> PosixDst<ABBREV> { |
| 350 | fn display( |
| 351 | &self, |
| 352 | std_offset: &PosixOffset, |
| 353 | f: &mut core::fmt::Formatter, |
| 354 | ) -> core::fmt::Result { |
| 355 | write!(f, "{}", AbbreviationDisplay(self.abbrev.as_ref()))?; |
| 356 | // The overwhelming common case is that DST is exactly one hour ahead |
| 357 | // of standard time. So common that this is the default. So don't write |
| 358 | // the offset if we don't need to. |
| 359 | let default = PosixOffset { second: std_offset.second + 3600 }; |
| 360 | if self.offset != default { |
| 361 | write!(f, "{}", self.offset)?; |
| 362 | } |
| 363 | write!(f, ",{}", self.rule)?; |
| 364 | Ok(()) |
| 365 | } |
| 366 | } |
| 367 | |
| 368 | impl core::fmt::Display for PosixRule { |
| 369 | fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result { |
| 370 | write!(f, "{},{}", self.start, self.end) |
| 371 | } |
| 372 | } |
| 373 | |
| 374 | impl PosixDayTime { |
| 375 | /// Turns this POSIX datetime spec into a civil datetime in the year given |
| 376 | /// with the given offset. The datetimes returned are offset by the given |
| 377 | /// offset. For wall clock time, an offset of `0` should be given. For |
| 378 | /// UTC time, the offset (standard or DST) corresponding to this time |
| 379 | /// spec should be given. |
| 380 | /// |
| 381 | /// The datetime returned is guaranteed to have a year component equal |
| 382 | /// to the year given. This guarantee is upheld even when the datetime |
| 383 | /// specification (combined with the offset) would extend past the end of |
| 384 | /// the year (or before the start of the year). In this case, the maximal |
| 385 | /// (or minimal) datetime for the given year is returned. |
| 386 | pub(crate) fn to_datetime(&self, year: i16, offset: IOffset) -> IDateTime { |
| 387 | let mkmin = || IDateTime { |
| 388 | date: IDate { year, month: 1, day: 1 }, |
| 389 | time: ITime::MIN, |
| 390 | }; |
| 391 | let mkmax = || IDateTime { |
| 392 | date: IDate { year, month: 12, day: 31 }, |
| 393 | time: ITime::MAX, |
| 394 | }; |
| 395 | let Some(date) = self.date.to_date(year) else { return mkmax() }; |
| 396 | // The range on `self.time` is `-604799..=604799`, and the range |
| 397 | // on `offset.second` is `-93599..=93599`. Therefore, subtracting |
| 398 | // them can never overflow an `i32`. |
| 399 | let offset = self.time.second - offset.second; |
| 400 | // If the time goes negative or above 86400, then we might have |
| 401 | // to adjust our date. |
| 402 | let days = offset.div_euclid(86400); |
| 403 | let second = offset.rem_euclid(86400); |
| 404 | |
| 405 | let Ok(date) = date.checked_add_days(days) else { |
| 406 | return if offset < 0 { mkmin() } else { mkmax() }; |
| 407 | }; |
| 408 | if date.year < year { |
| 409 | mkmin() |
| 410 | } else if date.year > year { |
| 411 | mkmax() |
| 412 | } else { |
| 413 | let time = ITimeSecond { second }.to_time(); |
| 414 | IDateTime { date, time } |
| 415 | } |
| 416 | } |
| 417 | } |
| 418 | |
| 419 | impl core::fmt::Display for PosixDayTime { |
| 420 | fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result { |
| 421 | write!(f, "{}", self.date)?; |
| 422 | // This is the default time, so don't write it if we |
| 423 | // don't need to. |
| 424 | if self.time != PosixTime::DEFAULT { |
| 425 | write!(f, "/{}", self.time)?; |
| 426 | } |
| 427 | Ok(()) |
| 428 | } |
| 429 | } |
| 430 | |
| 431 | impl PosixDay { |
| 432 | /// Convert this date specification to a civil date in the year given. |
| 433 | /// |
| 434 | /// If this date specification couldn't be turned into a date in the year |
| 435 | /// given, then `None` is returned. This happens when `366` is given as |
| 436 | /// a day, but the year given is not a leap year. In this case, callers may |
| 437 | /// want to assume a datetime that is maximal for the year given. |
| 438 | fn to_date(&self, year: i16) -> Option<IDate> { |
| 439 | match *self { |
| 440 | PosixDay::JulianOne(day) => { |
| 441 | // Parsing validates that our day is 1-365 which will always |
| 442 | // succeed for all possible year values. That is, every valid |
| 443 | // year has a December 31. |
| 444 | Some( |
| 445 | IDate::from_day_of_year_no_leap(year, day) |
| 446 | .expect("Julian `J day` should be in bounds"), |
| 447 | ) |
| 448 | } |
| 449 | PosixDay::JulianZero(day) => { |
| 450 | // OK because our value for `day` is validated to be `0..=365`, |
| 451 | // and since it is an `i16`, it is always valid to add 1. |
| 452 | // |
| 453 | // Also, while `day+1` is guaranteed to be in `1..=366`, it is |
| 454 | // possible that `366` is invalid, for when `year` is not a |
| 455 | // leap year. In this case, we throw our hands up, and ask the |
| 456 | // caller to make a decision for how to deal with it. Why does |
| 457 | // POSIX go out of its way to specifically not specify behavior |
| 458 | // in error cases? |
| 459 | IDate::from_day_of_year(year, day + 1).ok() |
| 460 | } |
| 461 | PosixDay::WeekdayOfMonth { month, week, weekday } => { |
| 462 | let weekday = IWeekday::from_sunday_zero_offset(weekday); |
| 463 | let first = IDate { year, month, day: 1 }; |
| 464 | let week = if week == 5 { -1 } else { week }; |
| 465 | debug_assert!(week == -1 || (1..=4).contains(&week)); |
| 466 | // This is maybe non-obvious, but this will always succeed |
| 467 | // because it can only fail when the week number is one of |
| 468 | // {-5, 0, 5}. Since we've validated that 'week' is in 1..=5, |
| 469 | // we know it can't be 0. Moreover, because of the conditional |
| 470 | // above and since `5` actually means "last weekday of month," |
| 471 | // that case will always translate to `-1`. |
| 472 | // |
| 473 | // Also, I looked at how other libraries deal with this case, |
| 474 | // and almost all of them just do a bunch of inline hairy |
| 475 | // arithmetic here. I suppose I could be reduced to such |
| 476 | // things if perf called for it, but we have a nice civil date |
| 477 | // abstraction. So use it, god damn it. (Well, we did, and now |
| 478 | // we have a lower level IDate abstraction. But it's still |
| 479 | // an abstraction!) |
| 480 | Some( |
| 481 | first |
| 482 | .nth_weekday_of_month(week, weekday) |
| 483 | .expect("nth weekday always exists"), |
| 484 | ) |
| 485 | } |
| 486 | } |
| 487 | } |
| 488 | } |
| 489 | |
| 490 | impl core::fmt::Display for PosixDay { |
| 491 | fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result { |
| 492 | match *self { |
| 493 | PosixDay::JulianOne(n) => write!(f, "J{n}"), |
| 494 | PosixDay::JulianZero(n) => write!(f, "{n}"), |
| 495 | PosixDay::WeekdayOfMonth { month, week, weekday } => { |
| 496 | write!(f, "M{month}.{week}.{weekday}") |
| 497 | } |
| 498 | } |
| 499 | } |
| 500 | } |
| 501 | |
| 502 | impl PosixTime { |
| 503 | const DEFAULT: PosixTime = PosixTime { second: 2 * 60 * 60 }; |
| 504 | } |
| 505 | |
| 506 | impl core::fmt::Display for PosixTime { |
| 507 | fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result { |
| 508 | if self.second.is_negative() { |
| 509 | write!(f, "-")?; |
| 510 | // The default is positive, so when |
| 511 | // positive, we write nothing. |
| 512 | } |
| 513 | let second = self.second.unsigned_abs(); |
| 514 | let h = second / 3600; |
| 515 | let m = (second / 60) % 60; |
| 516 | let s = second % 60; |
| 517 | write!(f, "{h}")?; |
| 518 | if m != 0 || s != 0 { |
| 519 | write!(f, ":{m:02}")?; |
| 520 | if s != 0 { |
| 521 | write!(f, ":{s:02}")?; |
| 522 | } |
| 523 | } |
| 524 | Ok(()) |
| 525 | } |
| 526 | } |
| 527 | |
| 528 | impl PosixOffset { |
| 529 | fn to_ioffset(&self) -> IOffset { |
| 530 | IOffset { second: self.second } |
| 531 | } |
| 532 | } |
| 533 | |
| 534 | impl core::fmt::Display for PosixOffset { |
| 535 | fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result { |
| 536 | // Yes, this is backwards. Blame POSIX. |
| 537 | // N.B. `+` is the default, so we don't |
| 538 | // need to write that out. |
| 539 | if self.second > 0 { |
| 540 | write!(f, "-")?; |
| 541 | } |
| 542 | let second = self.second.unsigned_abs(); |
| 543 | let h = second / 3600; |
| 544 | let m = (second / 60) % 60; |
| 545 | let s = second % 60; |
| 546 | write!(f, "{h}")?; |
| 547 | if m != 0 || s != 0 { |
| 548 | write!(f, ":{m:02}")?; |
| 549 | if s != 0 { |
| 550 | write!(f, ":{s:02}")?; |
| 551 | } |
| 552 | } |
| 553 | Ok(()) |
| 554 | } |
| 555 | } |
| 556 | |
| 557 | /// A helper type for formatting a time zone abbreviation. |
| 558 | /// |
| 559 | /// Basically, this will write the `<` and `>` quotes if necessary, and |
| 560 | /// otherwise write out the abbreviation in its unquoted form. |
| 561 | #[derive(Debug)] |
| 562 | struct AbbreviationDisplay<S>(S); |
| 563 | |
| 564 | impl<S: AsRef<str>> core::fmt::Display for AbbreviationDisplay<S> { |
| 565 | fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result { |
| 566 | let s = self.0.as_ref(); |
| 567 | if s.chars().any(|ch| ch == '+' || ch == '-') { |
| 568 | write!(f, "<{s}>") |
| 569 | } else { |
| 570 | write!(f, "{s}") |
| 571 | } |
| 572 | } |
| 573 | } |
| 574 | |
| 575 | /// The daylight saving time (DST) info for a POSIX time zone in a particular |
| 576 | /// year. |
| 577 | #[derive(Debug, Eq, PartialEq)] |
| 578 | struct DstInfo<'a, ABBREV> { |
| 579 | /// The DST transition rule that generated this info. |
| 580 | dst: &'a PosixDst<ABBREV>, |
| 581 | /// The start time (inclusive) that DST begins. |
| 582 | /// |
| 583 | /// Note that this may be greater than `end`. This tends to happen in the |
| 584 | /// southern hemisphere. |
| 585 | /// |
| 586 | /// Note also that this may be in UTC or in wall clock civil |
| 587 | /// time. It depends on whether `PosixTimeZone::dst_info_utc` or |
| 588 | /// `PosixTimeZone::dst_info_wall` was used. |
| 589 | start: IDateTime, |
| 590 | /// The end time (exclusive) that DST ends. |
| 591 | /// |
| 592 | /// Note that this may be less than `start`. This tends to happen in the |
| 593 | /// southern hemisphere. |
| 594 | /// |
| 595 | /// Note also that this may be in UTC or in wall clock civil |
| 596 | /// time. It depends on whether `PosixTimeZone::dst_info_utc` or |
| 597 | /// `PosixTimeZone::dst_info_wall` was used. |
| 598 | end: IDateTime, |
| 599 | } |
| 600 | |
| 601 | impl<'a, ABBREV> DstInfo<'a, ABBREV> { |
| 602 | /// Returns true if and only if the given civil datetime ought to be |
| 603 | /// considered in DST. |
| 604 | fn in_dst(&self, utc_dt: IDateTime) -> bool { |
| 605 | if self.start <= self.end { |
| 606 | self.start <= utc_dt && utc_dt < self.end |
| 607 | } else { |
| 608 | !(self.end <= utc_dt && utc_dt < self.start) |
| 609 | } |
| 610 | } |
| 611 | |
| 612 | /// Returns the earlier and later times for this DST info. |
| 613 | fn ordered(&self) -> (IDateTime, IDateTime) { |
| 614 | if self.start <= self.end { |
| 615 | (self.start, self.end) |
| 616 | } else { |
| 617 | (self.end, self.start) |
| 618 | } |
| 619 | } |
| 620 | |
| 621 | /// Returns the DST offset. |
| 622 | fn offset(&self) -> &PosixOffset { |
| 623 | &self.dst.offset |
| 624 | } |
| 625 | } |
| 626 | |
| 627 | /// A parser for POSIX time zones. |
| 628 | #[derive(Debug)] |
| 629 | struct Parser<'s> { |
| 630 | /// The `TZ` string that we're parsing. |
| 631 | tz: &'s [u8], |
| 632 | /// The parser's current position in `tz`. |
| 633 | pos: core::cell::Cell<usize>, |
| 634 | /// Whether to use IANA rules, i.e., when parsing a TZ string in a TZif |
| 635 | /// file of version 3 or greater. From `tzfile(5)`: |
| 636 | /// |
| 637 | /// > First, the hours part of its transition times may be signed and range |
| 638 | /// > from `-167` through `167` instead of the POSIX-required unsigned |
| 639 | /// > values from `0` through `24`. Second, DST is in effect all year if |
| 640 | /// > it starts January 1 at 00:00 and ends December 31 at 24:00 plus the |
| 641 | /// > difference between daylight saving and standard time. |
| 642 | /// |
| 643 | /// At time of writing, I don't think I understand the significance of |
| 644 | /// the second part above. (RFC 8536 elaborates that it is meant to be an |
| 645 | /// explicit clarification of something that POSIX itself implies.) But the |
| 646 | /// first part is clear: it permits the hours to be a bigger range. |
| 647 | ianav3plus: bool, |
| 648 | } |
| 649 | |
| 650 | impl<'s> Parser<'s> { |
| 651 | /// Create a new parser for extracting a POSIX time zone from the given |
| 652 | /// bytes. |
| 653 | fn new<B: ?Sized + AsRef<[u8]>>(tz: &'s B) -> Parser<'s> { |
| 654 | Parser { |
| 655 | tz: tz.as_ref(), |
| 656 | pos: core::cell::Cell::new(0), |
| 657 | ianav3plus: false, |
| 658 | } |
| 659 | } |
| 660 | |
| 661 | /// Parses a POSIX time zone from the current position of the parser and |
| 662 | /// ensures that the entire TZ string corresponds to a single valid POSIX |
| 663 | /// time zone. |
| 664 | fn parse(&self) -> Result<PosixTimeZone<Abbreviation>, Error> { |
| 665 | let (time_zone, remaining) = self.parse_prefix()?; |
| 666 | if !remaining.is_empty() { |
| 667 | return Err(err!( |
| 668 | "expected entire TZ string to be a valid POSIX \ |
| 669 | time zone, but found `{}` after what would otherwise \ |
| 670 | be a valid POSIX TZ string", |
| 671 | Bytes(remaining), |
| 672 | )); |
| 673 | } |
| 674 | Ok(time_zone) |
| 675 | } |
| 676 | |
| 677 | /// Parses a POSIX time zone from the current position of the parser and |
| 678 | /// returns the remaining input. |
| 679 | fn parse_prefix( |
| 680 | &self, |
| 681 | ) -> Result<(PosixTimeZone<Abbreviation>, &'s [u8]), Error> { |
| 682 | let time_zone = self.parse_posix_time_zone()?; |
| 683 | Ok((time_zone, self.remaining())) |
| 684 | } |
| 685 | |
| 686 | /// Parse a POSIX time zone from the current position of the parser. |
| 687 | /// |
| 688 | /// Upon success, the parser will be positioned immediately following the |
| 689 | /// TZ string. |
| 690 | fn parse_posix_time_zone( |
| 691 | &self, |
| 692 | ) -> Result<PosixTimeZone<Abbreviation>, Error> { |
| 693 | if self.is_done() { |
| 694 | return Err(err!( |
| 695 | "an empty string is not a valid POSIX time zone" |
| 696 | )); |
| 697 | } |
| 698 | let std_abbrev = self |
| 699 | .parse_abbreviation() |
| 700 | .map_err(|e| err!("failed to parse standard abbreviation: {e}"))?; |
| 701 | let std_offset = self |
| 702 | .parse_posix_offset() |
| 703 | .map_err(|e| err!("failed to parse standard offset: {e}"))?; |
| 704 | let mut dst = None; |
| 705 | if !self.is_done() |
| 706 | && (self.byte().is_ascii_alphabetic() || self.byte() == b'<') |
| 707 | { |
| 708 | dst = Some(self.parse_posix_dst(&std_offset)?); |
| 709 | } |
| 710 | Ok(PosixTimeZone { std_abbrev, std_offset, dst }) |
| 711 | } |
| 712 | |
| 713 | /// Parse a DST zone with an optional explicit transition rule. |
| 714 | /// |
| 715 | /// This assumes the parser is positioned at the first byte of the DST |
| 716 | /// abbreviation. |
| 717 | /// |
| 718 | /// Upon success, the parser will be positioned immediately after the end |
| 719 | /// of the DST transition rule (which might just be the abbreviation, but |
| 720 | /// might also include explicit start/end datetime specifications). |
| 721 | fn parse_posix_dst( |
| 722 | &self, |
| 723 | std_offset: &PosixOffset, |
| 724 | ) -> Result<PosixDst<Abbreviation>, Error> { |
| 725 | let abbrev = self |
| 726 | .parse_abbreviation() |
| 727 | .map_err(|e| err!("failed to parse DST abbreviation: {e}"))?; |
| 728 | if self.is_done() { |
| 729 | return Err(err!( |
| 730 | "found DST abbreviation `{abbrev}`, but no transition \ |
| 731 | rule (this is technically allowed by POSIX, but has \ |
| 732 | unspecified behavior)", |
| 733 | )); |
| 734 | } |
| 735 | // This is the default: one hour ahead of standard time. We may |
| 736 | // override this if the DST portion specifies an offset. (But it |
| 737 | // usually doesn't.) |
| 738 | let mut offset = PosixOffset { second: std_offset.second + 3600 }; |
| 739 | if self.byte() != b',' { |
| 740 | offset = self |
| 741 | .parse_posix_offset() |
| 742 | .map_err(|e| err!("failed to parse DST offset: {e}"))?; |
| 743 | if self.is_done() { |
| 744 | return Err(err!( |
| 745 | "found DST abbreviation `{abbrev}` and offset \ |
| 746 | `{offset}s`, but no transition rule (this is \ |
| 747 | technically allowed by POSIX, but has \ |
| 748 | unspecified behavior)", |
| 749 | offset = offset.second, |
| 750 | )); |
| 751 | } |
| 752 | } |
| 753 | if self.byte() != b',' { |
| 754 | return Err(err!( |
| 755 | "after parsing DST offset in POSIX time zone string, \ |
| 756 | found `{}` but expected a ','", |
| 757 | Byte(self.byte()), |
| 758 | )); |
| 759 | } |
| 760 | if !self.bump() { |
| 761 | return Err(err!( |
| 762 | "after parsing DST offset in POSIX time zone string, \ |
| 763 | found end of string after a trailing ','", |
| 764 | )); |
| 765 | } |
| 766 | let rule = self.parse_rule()?; |
| 767 | Ok(PosixDst { abbrev, offset, rule }) |
| 768 | } |
| 769 | |
| 770 | /// Parse a time zone abbreviation. |
| 771 | /// |
| 772 | /// This assumes the parser is positioned at the first byte of |
| 773 | /// the abbreviation. This is either the first character in the |
| 774 | /// abbreviation, or the opening quote of a quoted abbreviation. |
| 775 | /// |
| 776 | /// Upon success, the parser will be positioned immediately following |
| 777 | /// the abbreviation name. |
| 778 | /// |
| 779 | /// The string returned is guaranteed to be no more than 30 bytes. |
| 780 | /// (This restriction is somewhat arbitrary, but it's so we can put |
| 781 | /// the abbreviation in a fixed capacity array.) |
| 782 | fn parse_abbreviation(&self) -> Result<Abbreviation, Error> { |
| 783 | if self.byte() == b'<' { |
| 784 | if !self.bump() { |
| 785 | return Err(err!( |
| 786 | "found opening '<' quote for abbreviation in \ |
| 787 | POSIX time zone string, and expected a name \ |
| 788 | following it, but found the end of string instead" |
| 789 | )); |
| 790 | } |
| 791 | self.parse_quoted_abbreviation() |
| 792 | } else { |
| 793 | self.parse_unquoted_abbreviation() |
| 794 | } |
| 795 | } |
| 796 | |
| 797 | /// Parses an unquoted time zone abbreviation. |
| 798 | /// |
| 799 | /// This assumes the parser is position at the first byte in the |
| 800 | /// abbreviation. |
| 801 | /// |
| 802 | /// Upon success, the parser will be positioned immediately after the |
| 803 | /// last byte in the abbreviation. |
| 804 | /// |
| 805 | /// The string returned is guaranteed to be no more than 30 bytes. |
| 806 | /// (This restriction is somewhat arbitrary, but it's so we can put |
| 807 | /// the abbreviation in a fixed capacity array.) |
| 808 | fn parse_unquoted_abbreviation(&self) -> Result<Abbreviation, Error> { |
| 809 | let start = self.pos(); |
| 810 | for i in 0.. { |
| 811 | if !self.byte().is_ascii_alphabetic() { |
| 812 | break; |
| 813 | } |
| 814 | if i >= Abbreviation::capacity() { |
| 815 | return Err(err!( |
| 816 | "expected abbreviation with at most {} bytes, \ |
| 817 | but found a longer abbreviation beginning with `{}`", |
| 818 | Abbreviation::capacity(), |
| 819 | Bytes(&self.tz[start..][..i]), |
| 820 | )); |
| 821 | } |
| 822 | if !self.bump() { |
| 823 | break; |
| 824 | } |
| 825 | } |
| 826 | let end = self.pos(); |
| 827 | let abbrev = |
| 828 | core::str::from_utf8(&self.tz[start..end]).map_err(|_| { |
| 829 | // NOTE: I believe this error is technically impossible |
| 830 | // since the loop above restricts letters in an |
| 831 | // abbreviation to ASCII. So everything from `start` to |
| 832 | // `end` is ASCII and thus should be UTF-8. But it doesn't |
| 833 | // cost us anything to report an error here in case the |
| 834 | // code above evolves somehow. |
| 835 | err!( |
| 836 | "found abbreviation `{}`, but it is not valid UTF-8", |
| 837 | Bytes(&self.tz[start..end]), |
| 838 | ) |
| 839 | })?; |
| 840 | if abbrev.len() < 3 { |
| 841 | return Err(err!( |
| 842 | "expected abbreviation with 3 or more bytes, but found \ |
| 843 | abbreviation {:?} with {} bytes", |
| 844 | abbrev, |
| 845 | abbrev.len(), |
| 846 | )); |
| 847 | } |
| 848 | // OK because we verified above that the abbreviation |
| 849 | // does not exceed `Abbreviation::capacity`. |
| 850 | Ok(Abbreviation::new(abbrev).unwrap()) |
| 851 | } |
| 852 | |
| 853 | /// Parses a quoted time zone abbreviation. |
| 854 | /// |
| 855 | /// This assumes the parser is positioned immediately after the opening |
| 856 | /// `<` quote. That is, at the first byte in the abbreviation. |
| 857 | /// |
| 858 | /// Upon success, the parser will be positioned immediately after the |
| 859 | /// closing `>` quote. |
| 860 | /// |
| 861 | /// The string returned is guaranteed to be no more than 30 bytes. |
| 862 | /// (This restriction is somewhat arbitrary, but it's so we can put |
| 863 | /// the abbreviation in a fixed capacity array.) |
| 864 | fn parse_quoted_abbreviation(&self) -> Result<Abbreviation, Error> { |
| 865 | let start = self.pos(); |
| 866 | for i in 0.. { |
| 867 | if !self.byte().is_ascii_alphanumeric() |
| 868 | && self.byte() != b'+' |
| 869 | && self.byte() != b'-' |
| 870 | { |
| 871 | break; |
| 872 | } |
| 873 | if i >= Abbreviation::capacity() { |
| 874 | return Err(err!( |
| 875 | "expected abbreviation with at most {} bytes, \ |
| 876 | but found a longer abbreviation beginning with `{}`", |
| 877 | Abbreviation::capacity(), |
| 878 | Bytes(&self.tz[start..][..i]), |
| 879 | )); |
| 880 | } |
| 881 | if !self.bump() { |
| 882 | break; |
| 883 | } |
| 884 | } |
| 885 | let end = self.pos(); |
| 886 | let abbrev = |
| 887 | core::str::from_utf8(&self.tz[start..end]).map_err(|_| { |
| 888 | // NOTE: I believe this error is technically impossible |
| 889 | // since the loop above restricts letters in an |
| 890 | // abbreviation to ASCII. So everything from `start` to |
| 891 | // `end` is ASCII and thus should be UTF-8. But it doesn't |
| 892 | // cost us anything to report an error here in case the |
| 893 | // code above evolves somehow. |
| 894 | err!( |
| 895 | "found abbreviation `{}`, but it is not valid UTF-8", |
| 896 | Bytes(&self.tz[start..end]), |
| 897 | ) |
| 898 | })?; |
| 899 | if self.is_done() { |
| 900 | return Err(err!( |
| 901 | "found non-empty quoted abbreviation {abbrev:?}, but \ |
| 902 | did not find expected end-of-quoted abbreviation \ |
| 903 | '>' character", |
| 904 | )); |
| 905 | } |
| 906 | if self.byte() != b'>' { |
| 907 | return Err(err!( |
| 908 | "found non-empty quoted abbreviation {abbrev:?}, but \ |
| 909 | found `{}` instead of end-of-quoted abbreviation '>' \ |
| 910 | character", |
| 911 | Byte(self.byte()), |
| 912 | )); |
| 913 | } |
| 914 | self.bump(); |
| 915 | if abbrev.len() < 3 { |
| 916 | return Err(err!( |
| 917 | "expected abbreviation with 3 or more bytes, but found \ |
| 918 | abbreviation {abbrev:?} with {} bytes", |
| 919 | abbrev.len(), |
| 920 | )); |
| 921 | } |
| 922 | // OK because we verified above that the abbreviation |
| 923 | // does not exceed `Abbreviation::capacity`. |
| 924 | Ok(Abbreviation::new(abbrev).unwrap()) |
| 925 | } |
| 926 | |
| 927 | /// Parse a POSIX time offset. |
| 928 | /// |
| 929 | /// This assumes the parser is positioned at the first byte of the |
| 930 | /// offset. This can either be a digit (for a positive offset) or the |
| 931 | /// sign of the offset (which must be either `-` or `+`). |
| 932 | /// |
| 933 | /// Upon success, the parser will be positioned immediately after the |
| 934 | /// end of the offset. |
| 935 | fn parse_posix_offset(&self) -> Result<PosixOffset, Error> { |
| 936 | let sign = self |
| 937 | .parse_optional_sign() |
| 938 | .map_err(|e| { |
| 939 | err!( |
| 940 | "failed to parse sign for time offset \ |
| 941 | in POSIX time zone string: {e}", |
| 942 | ) |
| 943 | })? |
| 944 | .unwrap_or(1); |
| 945 | let hour = self.parse_hour_posix()?; |
| 946 | let (mut minute, mut second) = (0, 0); |
| 947 | if self.maybe_byte() == Some(b':') { |
| 948 | if !self.bump() { |
| 949 | return Err(err!( |
| 950 | "incomplete time in POSIX timezone (missing minutes)", |
| 951 | )); |
| 952 | } |
| 953 | minute = self.parse_minute()?; |
| 954 | if self.maybe_byte() == Some(b':') { |
| 955 | if !self.bump() { |
| 956 | return Err(err!( |
| 957 | "incomplete time in POSIX timezone (missing seconds)", |
| 958 | )); |
| 959 | } |
| 960 | second = self.parse_second()?; |
| 961 | } |
| 962 | } |
| 963 | let mut offset = PosixOffset { second: i32::from(hour) * 3600 }; |
| 964 | offset.second += i32::from(minute) * 60; |
| 965 | offset.second += i32::from(second); |
| 966 | // Yes, we flip the sign, because POSIX is backwards. |
| 967 | // For example, `EST5` corresponds to `-05:00`. |
| 968 | offset.second *= i32::from(-sign); |
| 969 | // Must be true because the parsing routines for hours, minutes |
| 970 | // and seconds enforce they are in the ranges -24..=24, 0..=59 |
| 971 | // and 0..=59, respectively. |
| 972 | assert!( |
| 973 | -89999 <= offset.second && offset.second <= 89999, |
| 974 | "POSIX offset seconds {} is out of range", |
| 975 | offset.second |
| 976 | ); |
| 977 | Ok(offset) |
| 978 | } |
| 979 | |
| 980 | /// Parses a POSIX DST transition rule. |
| 981 | /// |
| 982 | /// This assumes the parser is positioned at the first byte in the |
| 983 | /// rule. That is, it comes immediately after the DST abbreviation or |
| 984 | /// its optional offset. |
| 985 | /// |
| 986 | /// Upon success, the parser will be positioned immediately after the |
| 987 | /// DST transition rule. In typical cases, this corresponds to the end |
| 988 | /// of the TZ string. |
| 989 | fn parse_rule(&self) -> Result<PosixRule, Error> { |
| 990 | let start = self.parse_posix_datetime().map_err(|e| { |
| 991 | err!("failed to parse start of DST transition rule: {e}") |
| 992 | })?; |
| 993 | if self.maybe_byte() != Some(b',') || !self.bump() { |
| 994 | return Err(err!( |
| 995 | "expected end of DST rule after parsing the start \ |
| 996 | of the DST rule" |
| 997 | )); |
| 998 | } |
| 999 | let end = self.parse_posix_datetime().map_err(|e| { |
| 1000 | err!("failed to parse end of DST transition rule: {e}") |
| 1001 | })?; |
| 1002 | Ok(PosixRule { start, end }) |
| 1003 | } |
| 1004 | |
| 1005 | /// Parses a POSIX datetime specification. |
| 1006 | /// |
| 1007 | /// This assumes the parser is position at the first byte where a |
| 1008 | /// datetime specification is expected to occur. |
| 1009 | /// |
| 1010 | /// Upon success, the parser will be positioned after the datetime |
| 1011 | /// specification. This will either be immediately after the date, or |
| 1012 | /// if it's present, the time part of the specification. |
| 1013 | fn parse_posix_datetime(&self) -> Result<PosixDayTime, Error> { |
| 1014 | let mut daytime = PosixDayTime { |
| 1015 | date: self.parse_posix_date()?, |
| 1016 | time: PosixTime::DEFAULT, |
| 1017 | }; |
| 1018 | if self.maybe_byte() != Some(b'/') { |
| 1019 | return Ok(daytime); |
| 1020 | } |
| 1021 | if !self.bump() { |
| 1022 | return Err(err!( |
| 1023 | "expected time specification after '/' following a date |
| 1024 | specification in a POSIX time zone DST transition rule", |
| 1025 | )); |
| 1026 | } |
| 1027 | daytime.time = self.parse_posix_time()?; |
| 1028 | Ok(daytime) |
| 1029 | } |
| 1030 | |
| 1031 | /// Parses a POSIX date specification. |
| 1032 | /// |
| 1033 | /// This assumes the parser is positioned at the first byte of the date |
| 1034 | /// specification. This can be `J` (for one based Julian day without |
| 1035 | /// leap days), `M` (for "weekday of month") or a digit starting the |
| 1036 | /// zero based Julian day with leap days. This routine will validate |
| 1037 | /// that the position points to one of these possible values. That is, |
| 1038 | /// the caller doesn't need to parse the `M` or the `J` or the leading |
| 1039 | /// digit. The caller should just call this routine when it *expect* a |
| 1040 | /// date specification to follow. |
| 1041 | /// |
| 1042 | /// Upon success, the parser will be positioned immediately after the |
| 1043 | /// date specification. |
| 1044 | fn parse_posix_date(&self) -> Result<PosixDay, Error> { |
| 1045 | match self.byte() { |
| 1046 | b'J' => { |
| 1047 | if !self.bump() { |
| 1048 | return Err(err!( |
| 1049 | "expected one-based Julian day after 'J' in date \ |
| 1050 | specification of a POSIX time zone DST \ |
| 1051 | transition rule, but got the end of the string \ |
| 1052 | instead" |
| 1053 | )); |
| 1054 | } |
| 1055 | Ok(PosixDay::JulianOne(self.parse_posix_julian_day_no_leap()?)) |
| 1056 | } |
| 1057 | b'0'..=b'9' => Ok(PosixDay::JulianZero( |
| 1058 | self.parse_posix_julian_day_with_leap()?, |
| 1059 | )), |
| 1060 | b'M' => { |
| 1061 | if !self.bump() { |
| 1062 | return Err(err!( |
| 1063 | "expected month-week-weekday after 'M' in date \ |
| 1064 | specification of a POSIX time zone DST \ |
| 1065 | transition rule, but got the end of the string \ |
| 1066 | instead" |
| 1067 | )); |
| 1068 | } |
| 1069 | let (month, week, weekday) = self.parse_weekday_of_month()?; |
| 1070 | Ok(PosixDay::WeekdayOfMonth { month, week, weekday }) |
| 1071 | } |
| 1072 | _ => Err(err!( |
| 1073 | "expected 'J', a digit or 'M' at the beginning of a date \ |
| 1074 | specification of a POSIX time zone DST transition rule, \ |
| 1075 | but got `{}` instead", |
| 1076 | Byte(self.byte()), |
| 1077 | )), |
| 1078 | } |
| 1079 | } |
| 1080 | |
| 1081 | /// Parses a POSIX Julian day that does not include leap days |
| 1082 | /// (`1 <= n <= 365`). |
| 1083 | /// |
| 1084 | /// This assumes the parser is positioned just after the `J` and at the |
| 1085 | /// first digit of the Julian day. Upon success, the parser will be |
| 1086 | /// positioned immediately following the day number. |
| 1087 | fn parse_posix_julian_day_no_leap(&self) -> Result<i16, Error> { |
| 1088 | let number = self |
| 1089 | .parse_number_with_upto_n_digits(3) |
| 1090 | .map_err(|e| err!("invalid one based Julian day: {e}"))?; |
| 1091 | let number = i16::try_from(number).map_err(|_| { |
| 1092 | err!( |
| 1093 | "one based Julian day `{number}` in POSIX time zone \ |
| 1094 | does not fit into 16-bit integer" |
| 1095 | ) |
| 1096 | })?; |
| 1097 | if !(1 <= number && number <= 365) { |
| 1098 | return Err(err!( |
| 1099 | "parsed one based Julian day `{number}`, \ |
| 1100 | but one based Julian day in POSIX time zone \ |
| 1101 | must be in range 1..=365", |
| 1102 | )); |
| 1103 | } |
| 1104 | Ok(number) |
| 1105 | } |
| 1106 | |
| 1107 | /// Parses a POSIX Julian day that includes leap days (`0 <= n <= |
| 1108 | /// 365`). |
| 1109 | /// |
| 1110 | /// This assumes the parser is positioned at the first digit of the |
| 1111 | /// Julian day. Upon success, the parser will be positioned immediately |
| 1112 | /// following the day number. |
| 1113 | fn parse_posix_julian_day_with_leap(&self) -> Result<i16, Error> { |
| 1114 | let number = self |
| 1115 | .parse_number_with_upto_n_digits(3) |
| 1116 | .map_err(|e| err!("invalid zero based Julian day: {e}"))?; |
| 1117 | let number = i16::try_from(number).map_err(|_| { |
| 1118 | err!( |
| 1119 | "zero based Julian day `{number}` in POSIX time zone \ |
| 1120 | does not fit into 16-bit integer" |
| 1121 | ) |
| 1122 | })?; |
| 1123 | if !(0 <= number && number <= 365) { |
| 1124 | return Err(err!( |
| 1125 | "parsed zero based Julian day `{number}`, \ |
| 1126 | but zero based Julian day in POSIX time zone \ |
| 1127 | must be in range 0..=365", |
| 1128 | )); |
| 1129 | } |
| 1130 | Ok(number) |
| 1131 | } |
| 1132 | |
| 1133 | /// Parses a POSIX "weekday of month" specification. |
| 1134 | /// |
| 1135 | /// This assumes the parser is positioned just after the `M` byte and |
| 1136 | /// at the first digit of the month. Upon success, the parser will be |
| 1137 | /// positioned immediately following the "weekday of the month" that |
| 1138 | /// was parsed. |
| 1139 | /// |
| 1140 | /// The tuple returned is month (1..=12), week (1..=5) and weekday |
| 1141 | /// (0..=6 with 0=Sunday). |
| 1142 | fn parse_weekday_of_month(&self) -> Result<(i8, i8, i8), Error> { |
| 1143 | let month = self.parse_month()?; |
| 1144 | if self.maybe_byte() != Some(b'.') { |
| 1145 | return Err(err!( |
| 1146 | "expected '.' after month `{month}` in \ |
| 1147 | POSIX time zone rule" |
| 1148 | )); |
| 1149 | } |
| 1150 | if !self.bump() { |
| 1151 | return Err(err!( |
| 1152 | "expected week after month `{month}` in \ |
| 1153 | POSIX time zone rule" |
| 1154 | )); |
| 1155 | } |
| 1156 | let week = self.parse_week()?; |
| 1157 | if self.maybe_byte() != Some(b'.') { |
| 1158 | return Err(err!( |
| 1159 | "expected '.' after week `{week}` in POSIX time zone rule" |
| 1160 | )); |
| 1161 | } |
| 1162 | if !self.bump() { |
| 1163 | return Err(err!( |
| 1164 | "expected day-of-week after week `{week}` in \ |
| 1165 | POSIX time zone rule" |
| 1166 | )); |
| 1167 | } |
| 1168 | let weekday = self.parse_weekday()?; |
| 1169 | Ok((month, week, weekday)) |
| 1170 | } |
| 1171 | |
| 1172 | /// This parses a POSIX time specification in the format |
| 1173 | /// `[+/-]hh?[:mm[:ss]]`. |
| 1174 | /// |
| 1175 | /// This assumes the parser is positioned at the first `h` (or the |
| 1176 | /// sign, if present). Upon success, the parser will be positioned |
| 1177 | /// immediately following the end of the time specification. |
| 1178 | fn parse_posix_time(&self) -> Result<PosixTime, Error> { |
| 1179 | let (sign, hour) = if self.ianav3plus { |
| 1180 | let sign = self |
| 1181 | .parse_optional_sign() |
| 1182 | .map_err(|e| { |
| 1183 | err!( |
| 1184 | "failed to parse sign for transition time \ |
| 1185 | in POSIX time zone string: {e}", |
| 1186 | ) |
| 1187 | })? |
| 1188 | .unwrap_or(1); |
| 1189 | let hour = self.parse_hour_ianav3plus()?; |
| 1190 | (sign, hour) |
| 1191 | } else { |
| 1192 | (1, i16::from(self.parse_hour_posix()?)) |
| 1193 | }; |
| 1194 | let (mut minute, mut second) = (0, 0); |
| 1195 | if self.maybe_byte() == Some(b':') { |
| 1196 | if !self.bump() { |
| 1197 | return Err(err!( |
| 1198 | "incomplete transition time in \ |
| 1199 | POSIX time zone string (missing minutes)", |
| 1200 | )); |
| 1201 | } |
| 1202 | minute = self.parse_minute()?; |
| 1203 | if self.maybe_byte() == Some(b':') { |
| 1204 | if !self.bump() { |
| 1205 | return Err(err!( |
| 1206 | "incomplete transition time in \ |
| 1207 | POSIX time zone string (missing seconds)", |
| 1208 | )); |
| 1209 | } |
| 1210 | second = self.parse_second()?; |
| 1211 | } |
| 1212 | } |
| 1213 | let mut time = PosixTime { second: i32::from(hour) * 3600 }; |
| 1214 | time.second += i32::from(minute) * 60; |
| 1215 | time.second += i32::from(second); |
| 1216 | time.second *= i32::from(sign); |
| 1217 | // Must be true because the parsing routines for hours, minutes |
| 1218 | // and seconds enforce they are in the ranges -167..=167, 0..=59 |
| 1219 | // and 0..=59, respectively. |
| 1220 | assert!( |
| 1221 | -604799 <= time.second && time.second <= 604799, |
| 1222 | "POSIX time seconds {} is out of range", |
| 1223 | time.second |
| 1224 | ); |
| 1225 | Ok(time) |
| 1226 | } |
| 1227 | |
| 1228 | /// Parses a month. |
| 1229 | /// |
| 1230 | /// This is expected to be positioned at the first digit. Upon success, |
| 1231 | /// the parser will be positioned after the month (which may contain |
| 1232 | /// two digits). |
| 1233 | fn parse_month(&self) -> Result<i8, Error> { |
| 1234 | let number = self.parse_number_with_upto_n_digits(2)?; |
| 1235 | let number = i8::try_from(number).map_err(|_| { |
| 1236 | err!( |
| 1237 | "month `{number}` in POSIX time zone \ |
| 1238 | does not fit into 8-bit integer" |
| 1239 | ) |
| 1240 | })?; |
| 1241 | if !(1 <= number && number <= 12) { |
| 1242 | return Err(err!( |
| 1243 | "parsed month `{number}`, but month in \ |
| 1244 | POSIX time zone must be in range 1..=12", |
| 1245 | )); |
| 1246 | } |
| 1247 | Ok(number) |
| 1248 | } |
| 1249 | |
| 1250 | /// Parses a week-of-month number. |
| 1251 | /// |
| 1252 | /// This is expected to be positioned at the first digit. Upon success, |
| 1253 | /// the parser will be positioned after the week digit. |
| 1254 | fn parse_week(&self) -> Result<i8, Error> { |
| 1255 | let number = self.parse_number_with_exactly_n_digits(1)?; |
| 1256 | let number = i8::try_from(number).map_err(|_| { |
| 1257 | err!( |
| 1258 | "week `{number}` in POSIX time zone \ |
| 1259 | does not fit into 8-bit integer" |
| 1260 | ) |
| 1261 | })?; |
| 1262 | if !(1 <= number && number <= 5) { |
| 1263 | return Err(err!( |
| 1264 | "parsed week `{number}`, but week in \ |
| 1265 | POSIX time zone must be in range 1..=5" |
| 1266 | )); |
| 1267 | } |
| 1268 | Ok(number) |
| 1269 | } |
| 1270 | |
| 1271 | /// Parses a weekday number. |
| 1272 | /// |
| 1273 | /// This is expected to be positioned at the first digit. Upon success, |
| 1274 | /// the parser will be positioned after the week digit. |
| 1275 | /// |
| 1276 | /// The weekday returned is guaranteed to be in the range `0..=6`, with |
| 1277 | /// `0` corresponding to Sunday. |
| 1278 | fn parse_weekday(&self) -> Result<i8, Error> { |
| 1279 | let number = self.parse_number_with_exactly_n_digits(1)?; |
| 1280 | let number = i8::try_from(number).map_err(|_| { |
| 1281 | err!( |
| 1282 | "weekday `{number}` in POSIX time zone \ |
| 1283 | does not fit into 8-bit integer" |
| 1284 | ) |
| 1285 | })?; |
| 1286 | if !(0 <= number && number <= 6) { |
| 1287 | return Err(err!( |
| 1288 | "parsed weekday `{number}`, but weekday in \ |
| 1289 | POSIX time zone must be in range `0..=6` \ |
| 1290 | (with `0` corresponding to Sunday)", |
| 1291 | )); |
| 1292 | } |
| 1293 | Ok(number) |
| 1294 | } |
| 1295 | |
| 1296 | /// Parses an hour from a POSIX time specification with the IANA |
| 1297 | /// v3+ extension. That is, the hour may be in the range `0..=167`. |
| 1298 | /// (Callers should parse an optional sign preceding the hour digits |
| 1299 | /// when IANA V3+ parsing is enabled.) |
| 1300 | /// |
| 1301 | /// The hour is allowed to be a single digit (unlike minutes or |
| 1302 | /// seconds). |
| 1303 | /// |
| 1304 | /// This assumes the parser is positioned at the position where the |
| 1305 | /// first hour digit should occur. Upon success, the parser will be |
| 1306 | /// positioned immediately after the last hour digit. |
| 1307 | fn parse_hour_ianav3plus(&self) -> Result<i16, Error> { |
| 1308 | // Callers should only be using this method when IANA v3+ parsing |
| 1309 | // is enabled. |
| 1310 | assert!(self.ianav3plus); |
| 1311 | let number = self |
| 1312 | .parse_number_with_upto_n_digits(3) |
| 1313 | .map_err(|e| err!("invalid hour digits: {e}"))?; |
| 1314 | let number = i16::try_from(number).map_err(|_| { |
| 1315 | err!( |
| 1316 | "hour `{number}` in POSIX time zone \ |
| 1317 | does not fit into 16-bit integer" |
| 1318 | ) |
| 1319 | })?; |
| 1320 | if !(0 <= number && number <= 167) { |
| 1321 | // The error message says -167 but the check above uses 0. |
| 1322 | // This is because the caller is responsible for parsing |
| 1323 | // the sign. |
| 1324 | return Err(err!( |
| 1325 | "parsed hour `{number}`, but hour in IANA v3+ \ |
| 1326 | POSIX time zone must be in range `-167..=167`", |
| 1327 | )); |
| 1328 | } |
| 1329 | Ok(number) |
| 1330 | } |
| 1331 | |
| 1332 | /// Parses an hour from a POSIX time specification, with the allowed |
| 1333 | /// range being `0..=24`. |
| 1334 | /// |
| 1335 | /// The hour is allowed to be a single digit (unlike minutes or |
| 1336 | /// seconds). |
| 1337 | /// |
| 1338 | /// This assumes the parser is positioned at the position where the |
| 1339 | /// first hour digit should occur. Upon success, the parser will be |
| 1340 | /// positioned immediately after the last hour digit. |
| 1341 | fn parse_hour_posix(&self) -> Result<i8, Error> { |
| 1342 | let number = self |
| 1343 | .parse_number_with_upto_n_digits(2) |
| 1344 | .map_err(|e| err!("invalid hour digits: {e}"))?; |
| 1345 | let number = i8::try_from(number).map_err(|_| { |
| 1346 | err!( |
| 1347 | "hour `{number}` in POSIX time zone \ |
| 1348 | does not fit into 8-bit integer" |
| 1349 | ) |
| 1350 | })?; |
| 1351 | if !(0 <= number && number <= 24) { |
| 1352 | return Err(err!( |
| 1353 | "parsed hour `{number}`, but hour in \ |
| 1354 | POSIX time zone must be in range `0..=24`", |
| 1355 | )); |
| 1356 | } |
| 1357 | Ok(number) |
| 1358 | } |
| 1359 | |
| 1360 | /// Parses a minute from a POSIX time specification. |
| 1361 | /// |
| 1362 | /// The minute must be exactly two digits. |
| 1363 | /// |
| 1364 | /// This assumes the parser is positioned at the position where the |
| 1365 | /// first minute digit should occur. Upon success, the parser will be |
| 1366 | /// positioned immediately after the second minute digit. |
| 1367 | fn parse_minute(&self) -> Result<i8, Error> { |
| 1368 | let number = self |
| 1369 | .parse_number_with_exactly_n_digits(2) |
| 1370 | .map_err(|e| err!("invalid minute digits: {e}"))?; |
| 1371 | let number = i8::try_from(number).map_err(|_| { |
| 1372 | err!( |
| 1373 | "minute `{number}` in POSIX time zone \ |
| 1374 | does not fit into 8-bit integer" |
| 1375 | ) |
| 1376 | })?; |
| 1377 | if !(0 <= number && number <= 59) { |
| 1378 | return Err(err!( |
| 1379 | "parsed minute `{number}`, but minute in \ |
| 1380 | POSIX time zone must be in range `0..=59`", |
| 1381 | )); |
| 1382 | } |
| 1383 | Ok(number) |
| 1384 | } |
| 1385 | |
| 1386 | /// Parses a second from a POSIX time specification. |
| 1387 | /// |
| 1388 | /// The second must be exactly two digits. |
| 1389 | /// |
| 1390 | /// This assumes the parser is positioned at the position where the |
| 1391 | /// first second digit should occur. Upon success, the parser will be |
| 1392 | /// positioned immediately after the second second digit. |
| 1393 | fn parse_second(&self) -> Result<i8, Error> { |
| 1394 | let number = self |
| 1395 | .parse_number_with_exactly_n_digits(2) |
| 1396 | .map_err(|e| err!("invalid second digits: {e}"))?; |
| 1397 | let number = i8::try_from(number).map_err(|_| { |
| 1398 | err!( |
| 1399 | "second `{number}` in POSIX time zone \ |
| 1400 | does not fit into 8-bit integer" |
| 1401 | ) |
| 1402 | })?; |
| 1403 | if !(0 <= number && number <= 59) { |
| 1404 | return Err(err!( |
| 1405 | "parsed second `{number}`, but second in \ |
| 1406 | POSIX time zone must be in range `0..=59`", |
| 1407 | )); |
| 1408 | } |
| 1409 | Ok(number) |
| 1410 | } |
| 1411 | |
| 1412 | /// Parses a signed 64-bit integer expressed in exactly `n` digits. |
| 1413 | /// |
| 1414 | /// If `n` digits could not be found (or if the `TZ` string ends before |
| 1415 | /// `n` digits could be found), then this returns an error. |
| 1416 | /// |
| 1417 | /// This assumes that `n >= 1` and that the parser is positioned at the |
| 1418 | /// first digit. Upon success, the parser is positioned immediately |
| 1419 | /// after the `n`th digit. |
| 1420 | fn parse_number_with_exactly_n_digits( |
| 1421 | &self, |
| 1422 | n: usize, |
| 1423 | ) -> Result<i32, Error> { |
| 1424 | assert!(n >= 1, "numbers must have at least 1 digit"); |
| 1425 | let start = self.pos(); |
| 1426 | let mut number: i32 = 0; |
| 1427 | for i in 0..n { |
| 1428 | if self.is_done() { |
| 1429 | return Err(err!("expected {n} digits, but found {i}")); |
| 1430 | } |
| 1431 | let byte = self.byte(); |
| 1432 | let digit = match byte.checked_sub(b'0') { |
| 1433 | None => { |
| 1434 | return Err(err!( |
| 1435 | "invalid digit, expected 0-9 but got {}", |
| 1436 | Byte(byte), |
| 1437 | )); |
| 1438 | } |
| 1439 | Some(digit) if digit > 9 => { |
| 1440 | return Err(err!( |
| 1441 | "invalid digit, expected 0-9 but got {}", |
| 1442 | Byte(byte), |
| 1443 | )) |
| 1444 | } |
| 1445 | Some(digit) => { |
| 1446 | debug_assert!((0..=9).contains(&digit)); |
| 1447 | i32::from(digit) |
| 1448 | } |
| 1449 | }; |
| 1450 | number = number |
| 1451 | .checked_mul(10) |
| 1452 | .and_then(|n| n.checked_add(digit)) |
| 1453 | .ok_or_else(|| { |
| 1454 | err!( |
| 1455 | "number `{}` too big to parse into 64-bit integer", |
| 1456 | Bytes(&self.tz[start..][..i]), |
| 1457 | ) |
| 1458 | })?; |
| 1459 | self.bump(); |
| 1460 | } |
| 1461 | Ok(number) |
| 1462 | } |
| 1463 | |
| 1464 | /// Parses a signed 64-bit integer expressed with up to `n` digits and |
| 1465 | /// at least 1 digit. |
| 1466 | /// |
| 1467 | /// This assumes that `n >= 1` and that the parser is positioned at the |
| 1468 | /// first digit. Upon success, the parser is position immediately after |
| 1469 | /// the last digit (which can be at most `n`). |
| 1470 | fn parse_number_with_upto_n_digits(&self, n: usize) -> Result<i32, Error> { |
| 1471 | assert!(n >= 1, "numbers must have at least 1 digit"); |
| 1472 | let start = self.pos(); |
| 1473 | let mut number: i32 = 0; |
| 1474 | for i in 0..n { |
| 1475 | if self.is_done() || !self.byte().is_ascii_digit() { |
| 1476 | if i == 0 { |
| 1477 | return Err(err!("invalid number, no digits found")); |
| 1478 | } |
| 1479 | break; |
| 1480 | } |
| 1481 | let digit = i32::from(self.byte() - b'0'); |
| 1482 | number = number |
| 1483 | .checked_mul(10) |
| 1484 | .and_then(|n| n.checked_add(digit)) |
| 1485 | .ok_or_else(|| { |
| 1486 | err!( |
| 1487 | "number `{}` too big to parse into 64-bit integer", |
| 1488 | Bytes(&self.tz[start..][..i]), |
| 1489 | ) |
| 1490 | })?; |
| 1491 | self.bump(); |
| 1492 | } |
| 1493 | Ok(number) |
| 1494 | } |
| 1495 | |
| 1496 | /// Parses an optional sign. |
| 1497 | /// |
| 1498 | /// This assumes the parser is positioned at the position where a |
| 1499 | /// positive or negative sign is permitted. If one exists, then it |
| 1500 | /// is consumed and returned. Moreover, if one exists, then this |
| 1501 | /// guarantees that it is not the last byte in the input. That is, upon |
| 1502 | /// success, it is valid to call `self.byte()`. |
| 1503 | fn parse_optional_sign(&self) -> Result<Option<i8>, Error> { |
| 1504 | if self.is_done() { |
| 1505 | return Ok(None); |
| 1506 | } |
| 1507 | Ok(match self.byte() { |
| 1508 | b'-' => { |
| 1509 | if !self.bump() { |
| 1510 | return Err(err!( |
| 1511 | "expected digit after '-' sign, \ |
| 1512 | but got end of input", |
| 1513 | )); |
| 1514 | } |
| 1515 | Some(-1) |
| 1516 | } |
| 1517 | b'+' => { |
| 1518 | if !self.bump() { |
| 1519 | return Err(err!( |
| 1520 | "expected digit after '+' sign, \ |
| 1521 | but got end of input", |
| 1522 | )); |
| 1523 | } |
| 1524 | Some(1) |
| 1525 | } |
| 1526 | _ => None, |
| 1527 | }) |
| 1528 | } |
| 1529 | } |
| 1530 | |
| 1531 | /// Helper routines for parsing a POSIX `TZ` string. |
| 1532 | impl<'s> Parser<'s> { |
| 1533 | /// Bump the parser to the next byte. |
| 1534 | /// |
| 1535 | /// If the end of the input has been reached, then `false` is returned. |
| 1536 | fn bump(&self) -> bool { |
| 1537 | if self.is_done() { |
| 1538 | return false; |
| 1539 | } |
| 1540 | self.pos.set( |
| 1541 | self.pos().checked_add(1).expect("pos cannot overflow usize"), |
| 1542 | ); |
| 1543 | !self.is_done() |
| 1544 | } |
| 1545 | |
| 1546 | /// Returns true if the next call to `bump` would return false. |
| 1547 | fn is_done(&self) -> bool { |
| 1548 | self.pos() == self.tz.len() |
| 1549 | } |
| 1550 | |
| 1551 | /// Return the byte at the current position of the parser. |
| 1552 | /// |
| 1553 | /// This panics if the parser is positioned at the end of the TZ |
| 1554 | /// string. |
| 1555 | fn byte(&self) -> u8 { |
| 1556 | self.tz[self.pos()] |
| 1557 | } |
| 1558 | |
| 1559 | /// Return the byte at the current position of the parser. If the TZ |
| 1560 | /// string has been exhausted, then this returns `None`. |
| 1561 | fn maybe_byte(&self) -> Option<u8> { |
| 1562 | self.tz.get(self.pos()).copied() |
| 1563 | } |
| 1564 | |
| 1565 | /// Return the current byte offset of the parser. |
| 1566 | /// |
| 1567 | /// The offset starts at `0` from the beginning of the TZ string. |
| 1568 | fn pos(&self) -> usize { |
| 1569 | self.pos.get() |
| 1570 | } |
| 1571 | |
| 1572 | /// Returns the remaining bytes of the TZ string. |
| 1573 | /// |
| 1574 | /// This includes `self.byte()`. It may be empty. |
| 1575 | fn remaining(&self) -> &'s [u8] { |
| 1576 | &self.tz[self.pos()..] |
| 1577 | } |
| 1578 | } |
| 1579 | |
| 1580 | // Tests require parsing, and parsing requires alloc. |
| 1581 | #[cfg(test)] |
| 1582 | mod tests { |
| 1583 | use alloc::string::ToString; |
| 1584 | |
| 1585 | use super::*; |
| 1586 | |
| 1587 | fn posix_time_zone( |
| 1588 | input: impl AsRef<[u8]>, |
| 1589 | ) -> PosixTimeZone<Abbreviation> { |
| 1590 | let input = input.as_ref(); |
| 1591 | let tz = PosixTimeZone::parse(input).unwrap(); |
| 1592 | // While we're here, assert that converting the TZ back |
| 1593 | // to a string matches what we got. In the original version |
| 1594 | // of the POSIX TZ parser, we were very meticulous about |
| 1595 | // capturing the exact AST of the time zone. But I've |
| 1596 | // since simplified the data structure considerably such |
| 1597 | // that it is lossy in terms of what was actually parsed |
| 1598 | // (but of course, not lossy in terms of the semantic |
| 1599 | // meaning of the time zone). |
| 1600 | // |
| 1601 | // So to account for this, we serialize to a string and |
| 1602 | // then parse it back. We should get what we started with. |
| 1603 | let reparsed = |
| 1604 | PosixTimeZone::parse(tz.to_string().as_bytes()).unwrap(); |
| 1605 | assert_eq!(tz, reparsed); |
| 1606 | assert_eq!(tz.to_string(), reparsed.to_string()); |
| 1607 | tz |
| 1608 | } |
| 1609 | |
| 1610 | fn parser(s: &str) -> Parser<'_> { |
| 1611 | Parser::new(s.as_bytes()) |
| 1612 | } |
| 1613 | |
| 1614 | fn date(year: i16, month: i8, day: i8) -> IDate { |
| 1615 | IDate { year, month, day } |
| 1616 | } |
| 1617 | |
| 1618 | #[test] |
| 1619 | fn parse() { |
| 1620 | let p = parser("NZST-12NZDT,J60,J300"); |
| 1621 | assert_eq!( |
| 1622 | p.parse().unwrap(), |
| 1623 | PosixTimeZone { |
| 1624 | std_abbrev: "NZST".into(), |
| 1625 | std_offset: PosixOffset { second: 12 * 60 * 60 }, |
| 1626 | dst: Some(PosixDst { |
| 1627 | abbrev: "NZDT".into(), |
| 1628 | offset: PosixOffset { second: 13 * 60 * 60 }, |
| 1629 | rule: PosixRule { |
| 1630 | start: PosixDayTime { |
| 1631 | date: PosixDay::JulianOne(60), |
| 1632 | time: PosixTime { second: 2 * 60 * 60 }, |
| 1633 | }, |
| 1634 | end: PosixDayTime { |
| 1635 | date: PosixDay::JulianOne(300), |
| 1636 | time: PosixTime { second: 2 * 60 * 60 }, |
| 1637 | }, |
| 1638 | }, |
| 1639 | }), |
| 1640 | }, |
| 1641 | ); |
| 1642 | |
| 1643 | let p = Parser::new("NZST-12NZDT,J60,J300WAT"); |
| 1644 | assert!(p.parse().is_err()); |
| 1645 | } |
| 1646 | |
| 1647 | #[test] |
| 1648 | fn parse_posix_time_zone() { |
| 1649 | let p = Parser::new("NZST-12NZDT,M9.5.0,M4.1.0/3"); |
| 1650 | assert_eq!( |
| 1651 | p.parse_posix_time_zone().unwrap(), |
| 1652 | PosixTimeZone { |
| 1653 | std_abbrev: "NZST".into(), |
| 1654 | std_offset: PosixOffset { second: 12 * 60 * 60 }, |
| 1655 | dst: Some(PosixDst { |
| 1656 | abbrev: "NZDT".into(), |
| 1657 | offset: PosixOffset { second: 13 * 60 * 60 }, |
| 1658 | rule: PosixRule { |
| 1659 | start: PosixDayTime { |
| 1660 | date: PosixDay::WeekdayOfMonth { |
| 1661 | month: 9, |
| 1662 | week: 5, |
| 1663 | weekday: 0, |
| 1664 | }, |
| 1665 | time: PosixTime { second: 2 * 60 * 60 }, |
| 1666 | }, |
| 1667 | end: PosixDayTime { |
| 1668 | date: PosixDay::WeekdayOfMonth { |
| 1669 | month: 4, |
| 1670 | week: 1, |
| 1671 | weekday: 0, |
| 1672 | }, |
| 1673 | time: PosixTime { second: 3 * 60 * 60 }, |
| 1674 | }, |
| 1675 | }, |
| 1676 | }), |
| 1677 | }, |
| 1678 | ); |
| 1679 | |
| 1680 | let p = Parser::new("NZST-12NZDT,M9.5.0,M4.1.0/3WAT"); |
| 1681 | assert_eq!( |
| 1682 | p.parse_posix_time_zone().unwrap(), |
| 1683 | PosixTimeZone { |
| 1684 | std_abbrev: "NZST".into(), |
| 1685 | std_offset: PosixOffset { second: 12 * 60 * 60 }, |
| 1686 | dst: Some(PosixDst { |
| 1687 | abbrev: "NZDT".into(), |
| 1688 | offset: PosixOffset { second: 13 * 60 * 60 }, |
| 1689 | rule: PosixRule { |
| 1690 | start: PosixDayTime { |
| 1691 | date: PosixDay::WeekdayOfMonth { |
| 1692 | month: 9, |
| 1693 | week: 5, |
| 1694 | weekday: 0, |
| 1695 | }, |
| 1696 | time: PosixTime { second: 2 * 60 * 60 }, |
| 1697 | }, |
| 1698 | end: PosixDayTime { |
| 1699 | date: PosixDay::WeekdayOfMonth { |
| 1700 | month: 4, |
| 1701 | week: 1, |
| 1702 | weekday: 0, |
| 1703 | }, |
| 1704 | time: PosixTime { second: 3 * 60 * 60 }, |
| 1705 | }, |
| 1706 | }, |
| 1707 | }), |
| 1708 | }, |
| 1709 | ); |
| 1710 | |
| 1711 | let p = Parser::new("NZST-12NZDT,J60,J300"); |
| 1712 | assert_eq!( |
| 1713 | p.parse_posix_time_zone().unwrap(), |
| 1714 | PosixTimeZone { |
| 1715 | std_abbrev: "NZST".into(), |
| 1716 | std_offset: PosixOffset { second: 12 * 60 * 60 }, |
| 1717 | dst: Some(PosixDst { |
| 1718 | abbrev: "NZDT".into(), |
| 1719 | offset: PosixOffset { second: 13 * 60 * 60 }, |
| 1720 | rule: PosixRule { |
| 1721 | start: PosixDayTime { |
| 1722 | date: PosixDay::JulianOne(60), |
| 1723 | time: PosixTime { second: 2 * 60 * 60 }, |
| 1724 | }, |
| 1725 | end: PosixDayTime { |
| 1726 | date: PosixDay::JulianOne(300), |
| 1727 | time: PosixTime { second: 2 * 60 * 60 }, |
| 1728 | }, |
| 1729 | }, |
| 1730 | }), |
| 1731 | }, |
| 1732 | ); |
| 1733 | |
| 1734 | let p = Parser::new("NZST-12NZDT,J60,J300WAT"); |
| 1735 | assert_eq!( |
| 1736 | p.parse_posix_time_zone().unwrap(), |
| 1737 | PosixTimeZone { |
| 1738 | std_abbrev: "NZST".into(), |
| 1739 | std_offset: PosixOffset { second: 12 * 60 * 60 }, |
| 1740 | dst: Some(PosixDst { |
| 1741 | abbrev: "NZDT".into(), |
| 1742 | offset: PosixOffset { second: 13 * 60 * 60 }, |
| 1743 | rule: PosixRule { |
| 1744 | start: PosixDayTime { |
| 1745 | date: PosixDay::JulianOne(60), |
| 1746 | time: PosixTime { second: 2 * 60 * 60 }, |
| 1747 | }, |
| 1748 | end: PosixDayTime { |
| 1749 | date: PosixDay::JulianOne(300), |
| 1750 | time: PosixTime { second: 2 * 60 * 60 }, |
| 1751 | }, |
| 1752 | }, |
| 1753 | }), |
| 1754 | }, |
| 1755 | ); |
| 1756 | } |
| 1757 | |
| 1758 | #[test] |
| 1759 | fn parse_posix_dst() { |
| 1760 | let p = Parser::new("NZDT,M9.5.0,M4.1.0/3"); |
| 1761 | assert_eq!( |
| 1762 | p.parse_posix_dst(&PosixOffset { second: 12 * 60 * 60 }).unwrap(), |
| 1763 | PosixDst { |
| 1764 | abbrev: "NZDT".into(), |
| 1765 | offset: PosixOffset { second: 13 * 60 * 60 }, |
| 1766 | rule: PosixRule { |
| 1767 | start: PosixDayTime { |
| 1768 | date: PosixDay::WeekdayOfMonth { |
| 1769 | month: 9, |
| 1770 | week: 5, |
| 1771 | weekday: 0, |
| 1772 | }, |
| 1773 | time: PosixTime { second: 2 * 60 * 60 }, |
| 1774 | }, |
| 1775 | end: PosixDayTime { |
| 1776 | date: PosixDay::WeekdayOfMonth { |
| 1777 | month: 4, |
| 1778 | week: 1, |
| 1779 | weekday: 0, |
| 1780 | }, |
| 1781 | time: PosixTime { second: 3 * 60 * 60 }, |
| 1782 | }, |
| 1783 | }, |
| 1784 | }, |
| 1785 | ); |
| 1786 | |
| 1787 | let p = Parser::new("NZDT,J60,J300"); |
| 1788 | assert_eq!( |
| 1789 | p.parse_posix_dst(&PosixOffset { second: 12 * 60 * 60 }).unwrap(), |
| 1790 | PosixDst { |
| 1791 | abbrev: "NZDT".into(), |
| 1792 | offset: PosixOffset { second: 13 * 60 * 60 }, |
| 1793 | rule: PosixRule { |
| 1794 | start: PosixDayTime { |
| 1795 | date: PosixDay::JulianOne(60), |
| 1796 | time: PosixTime { second: 2 * 60 * 60 }, |
| 1797 | }, |
| 1798 | end: PosixDayTime { |
| 1799 | date: PosixDay::JulianOne(300), |
| 1800 | time: PosixTime { second: 2 * 60 * 60 }, |
| 1801 | }, |
| 1802 | }, |
| 1803 | }, |
| 1804 | ); |
| 1805 | |
| 1806 | let p = Parser::new("NZDT-7,J60,J300"); |
| 1807 | assert_eq!( |
| 1808 | p.parse_posix_dst(&PosixOffset { second: 12 * 60 * 60 }).unwrap(), |
| 1809 | PosixDst { |
| 1810 | abbrev: "NZDT".into(), |
| 1811 | offset: PosixOffset { second: 7 * 60 * 60 }, |
| 1812 | rule: PosixRule { |
| 1813 | start: PosixDayTime { |
| 1814 | date: PosixDay::JulianOne(60), |
| 1815 | time: PosixTime { second: 2 * 60 * 60 }, |
| 1816 | }, |
| 1817 | end: PosixDayTime { |
| 1818 | date: PosixDay::JulianOne(300), |
| 1819 | time: PosixTime { second: 2 * 60 * 60 }, |
| 1820 | }, |
| 1821 | }, |
| 1822 | }, |
| 1823 | ); |
| 1824 | |
| 1825 | let p = Parser::new("NZDT+7,J60,J300"); |
| 1826 | assert_eq!( |
| 1827 | p.parse_posix_dst(&PosixOffset { second: 12 * 60 * 60 }).unwrap(), |
| 1828 | PosixDst { |
| 1829 | abbrev: "NZDT".into(), |
| 1830 | offset: PosixOffset { second: -7 * 60 * 60 }, |
| 1831 | rule: PosixRule { |
| 1832 | start: PosixDayTime { |
| 1833 | date: PosixDay::JulianOne(60), |
| 1834 | time: PosixTime { second: 2 * 60 * 60 }, |
| 1835 | }, |
| 1836 | end: PosixDayTime { |
| 1837 | date: PosixDay::JulianOne(300), |
| 1838 | time: PosixTime { second: 2 * 60 * 60 }, |
| 1839 | }, |
| 1840 | }, |
| 1841 | }, |
| 1842 | ); |
| 1843 | |
| 1844 | let p = Parser::new("NZDT7,J60,J300"); |
| 1845 | assert_eq!( |
| 1846 | p.parse_posix_dst(&PosixOffset { second: 12 * 60 * 60 }).unwrap(), |
| 1847 | PosixDst { |
| 1848 | abbrev: "NZDT".into(), |
| 1849 | offset: PosixOffset { second: -7 * 60 * 60 }, |
| 1850 | rule: PosixRule { |
| 1851 | start: PosixDayTime { |
| 1852 | date: PosixDay::JulianOne(60), |
| 1853 | time: PosixTime { second: 2 * 60 * 60 }, |
| 1854 | }, |
| 1855 | end: PosixDayTime { |
| 1856 | date: PosixDay::JulianOne(300), |
| 1857 | time: PosixTime { second: 2 * 60 * 60 }, |
| 1858 | }, |
| 1859 | }, |
| 1860 | }, |
| 1861 | ); |
| 1862 | |
| 1863 | let p = Parser::new("NZDT7,"); |
| 1864 | assert!(p |
| 1865 | .parse_posix_dst(&PosixOffset { second: 12 * 60 * 60 }) |
| 1866 | .is_err()); |
| 1867 | |
| 1868 | let p = Parser::new("NZDT7!"); |
| 1869 | assert!(p |
| 1870 | .parse_posix_dst(&PosixOffset { second: 12 * 60 * 60 }) |
| 1871 | .is_err()); |
| 1872 | } |
| 1873 | |
| 1874 | #[test] |
| 1875 | fn parse_abbreviation() { |
| 1876 | let p = Parser::new("ABC"); |
| 1877 | assert_eq!(p.parse_abbreviation().unwrap(), "ABC"); |
| 1878 | |
| 1879 | let p = Parser::new("<ABC>"); |
| 1880 | assert_eq!(p.parse_abbreviation().unwrap(), "ABC"); |
| 1881 | |
| 1882 | let p = Parser::new("<+09>"); |
| 1883 | assert_eq!(p.parse_abbreviation().unwrap(), "+09"); |
| 1884 | |
| 1885 | let p = Parser::new("+09"); |
| 1886 | assert!(p.parse_abbreviation().is_err()); |
| 1887 | } |
| 1888 | |
| 1889 | #[test] |
| 1890 | fn parse_unquoted_abbreviation() { |
| 1891 | let p = Parser::new("ABC"); |
| 1892 | assert_eq!(p.parse_unquoted_abbreviation().unwrap(), "ABC"); |
| 1893 | |
| 1894 | let p = Parser::new("ABCXYZ"); |
| 1895 | assert_eq!(p.parse_unquoted_abbreviation().unwrap(), "ABCXYZ"); |
| 1896 | |
| 1897 | let p = Parser::new("ABC123"); |
| 1898 | assert_eq!(p.parse_unquoted_abbreviation().unwrap(), "ABC"); |
| 1899 | |
| 1900 | let tz = "a".repeat(30); |
| 1901 | let p = Parser::new(&tz); |
| 1902 | assert_eq!(p.parse_unquoted_abbreviation().unwrap(), &*tz); |
| 1903 | |
| 1904 | let p = Parser::new("a"); |
| 1905 | assert!(p.parse_unquoted_abbreviation().is_err()); |
| 1906 | |
| 1907 | let p = Parser::new("ab"); |
| 1908 | assert!(p.parse_unquoted_abbreviation().is_err()); |
| 1909 | |
| 1910 | let p = Parser::new("ab1"); |
| 1911 | assert!(p.parse_unquoted_abbreviation().is_err()); |
| 1912 | |
| 1913 | let tz = "a".repeat(31); |
| 1914 | let p = Parser::new(&tz); |
| 1915 | assert!(p.parse_unquoted_abbreviation().is_err()); |
| 1916 | |
| 1917 | let p = Parser::new(b"ab\xFFcd"); |
| 1918 | assert!(p.parse_unquoted_abbreviation().is_err()); |
| 1919 | } |
| 1920 | |
| 1921 | #[test] |
| 1922 | fn parse_quoted_abbreviation() { |
| 1923 | // The inputs look a little funny here, but that's because |
| 1924 | // 'parse_quoted_abbreviation' starts after the opening quote |
| 1925 | // has been parsed. |
| 1926 | |
| 1927 | let p = Parser::new("ABC>"); |
| 1928 | assert_eq!(p.parse_quoted_abbreviation().unwrap(), "ABC"); |
| 1929 | |
| 1930 | let p = Parser::new("ABCXYZ>"); |
| 1931 | assert_eq!(p.parse_quoted_abbreviation().unwrap(), "ABCXYZ"); |
| 1932 | |
| 1933 | let p = Parser::new("ABC>123"); |
| 1934 | assert_eq!(p.parse_quoted_abbreviation().unwrap(), "ABC"); |
| 1935 | |
| 1936 | let p = Parser::new("ABC123>"); |
| 1937 | assert_eq!(p.parse_quoted_abbreviation().unwrap(), "ABC123"); |
| 1938 | |
| 1939 | let p = Parser::new("ab1>"); |
| 1940 | assert_eq!(p.parse_quoted_abbreviation().unwrap(), "ab1"); |
| 1941 | |
| 1942 | let p = Parser::new("+09>"); |
| 1943 | assert_eq!(p.parse_quoted_abbreviation().unwrap(), "+09"); |
| 1944 | |
| 1945 | let p = Parser::new("-09>"); |
| 1946 | assert_eq!(p.parse_quoted_abbreviation().unwrap(), "-09"); |
| 1947 | |
| 1948 | let tz = alloc::format!("{}>", "a".repeat(30)); |
| 1949 | let p = Parser::new(&tz); |
| 1950 | assert_eq!( |
| 1951 | p.parse_quoted_abbreviation().unwrap(), |
| 1952 | tz.trim_end_matches(">") |
| 1953 | ); |
| 1954 | |
| 1955 | let p = Parser::new("a>"); |
| 1956 | assert!(p.parse_quoted_abbreviation().is_err()); |
| 1957 | |
| 1958 | let p = Parser::new("ab>"); |
| 1959 | assert!(p.parse_quoted_abbreviation().is_err()); |
| 1960 | |
| 1961 | let tz = alloc::format!("{}>", "a".repeat(31)); |
| 1962 | let p = Parser::new(&tz); |
| 1963 | assert!(p.parse_quoted_abbreviation().is_err()); |
| 1964 | |
| 1965 | let p = Parser::new(b"ab\xFFcd>"); |
| 1966 | assert!(p.parse_quoted_abbreviation().is_err()); |
| 1967 | |
| 1968 | let p = Parser::new("ABC"); |
| 1969 | assert!(p.parse_quoted_abbreviation().is_err()); |
| 1970 | |
| 1971 | let p = Parser::new("ABC!>"); |
| 1972 | assert!(p.parse_quoted_abbreviation().is_err()); |
| 1973 | } |
| 1974 | |
| 1975 | #[test] |
| 1976 | fn parse_posix_offset() { |
| 1977 | let p = Parser::new("5"); |
| 1978 | assert_eq!(p.parse_posix_offset().unwrap().second, -5 * 60 * 60); |
| 1979 | |
| 1980 | let p = Parser::new("+5"); |
| 1981 | assert_eq!(p.parse_posix_offset().unwrap().second, -5 * 60 * 60); |
| 1982 | |
| 1983 | let p = Parser::new("-5"); |
| 1984 | assert_eq!(p.parse_posix_offset().unwrap().second, 5 * 60 * 60); |
| 1985 | |
| 1986 | let p = Parser::new("-12:34:56"); |
| 1987 | assert_eq!( |
| 1988 | p.parse_posix_offset().unwrap().second, |
| 1989 | 12 * 60 * 60 + 34 * 60 + 56, |
| 1990 | ); |
| 1991 | |
| 1992 | let p = Parser::new("a"); |
| 1993 | assert!(p.parse_posix_offset().is_err()); |
| 1994 | |
| 1995 | let p = Parser::new("-"); |
| 1996 | assert!(p.parse_posix_offset().is_err()); |
| 1997 | |
| 1998 | let p = Parser::new("+"); |
| 1999 | assert!(p.parse_posix_offset().is_err()); |
| 2000 | |
| 2001 | let p = Parser::new("-a"); |
| 2002 | assert!(p.parse_posix_offset().is_err()); |
| 2003 | |
| 2004 | let p = Parser::new("+a"); |
| 2005 | assert!(p.parse_posix_offset().is_err()); |
| 2006 | |
| 2007 | let p = Parser::new("-25"); |
| 2008 | assert!(p.parse_posix_offset().is_err()); |
| 2009 | |
| 2010 | let p = Parser::new("+25"); |
| 2011 | assert!(p.parse_posix_offset().is_err()); |
| 2012 | |
| 2013 | // This checks that we don't accidentally permit IANA rules for |
| 2014 | // offset parsing. Namely, the IANA tzfile v3+ extension only applies |
| 2015 | // to transition times. But since POSIX says that the "time" for the |
| 2016 | // offset and transition is the same format, it would be an easy |
| 2017 | // implementation mistake to implement the more flexible rule for |
| 2018 | // IANA and have it accidentally also apply to the offset. So we check |
| 2019 | // that it doesn't here. |
| 2020 | let p = Parser { ianav3plus: true, ..Parser::new("25") }; |
| 2021 | assert!(p.parse_posix_offset().is_err()); |
| 2022 | let p = Parser { ianav3plus: true, ..Parser::new("+25") }; |
| 2023 | assert!(p.parse_posix_offset().is_err()); |
| 2024 | let p = Parser { ianav3plus: true, ..Parser::new("-25") }; |
| 2025 | assert!(p.parse_posix_offset().is_err()); |
| 2026 | } |
| 2027 | |
| 2028 | #[test] |
| 2029 | fn parse_rule() { |
| 2030 | let p = Parser::new("M9.5.0,M4.1.0/3"); |
| 2031 | assert_eq!( |
| 2032 | p.parse_rule().unwrap(), |
| 2033 | PosixRule { |
| 2034 | start: PosixDayTime { |
| 2035 | date: PosixDay::WeekdayOfMonth { |
| 2036 | month: 9, |
| 2037 | week: 5, |
| 2038 | weekday: 0, |
| 2039 | }, |
| 2040 | time: PosixTime { second: 2 * 60 * 60 }, |
| 2041 | }, |
| 2042 | end: PosixDayTime { |
| 2043 | date: PosixDay::WeekdayOfMonth { |
| 2044 | month: 4, |
| 2045 | week: 1, |
| 2046 | weekday: 0, |
| 2047 | }, |
| 2048 | time: PosixTime { second: 3 * 60 * 60 }, |
| 2049 | }, |
| 2050 | }, |
| 2051 | ); |
| 2052 | |
| 2053 | let p = Parser::new("M9.5.0"); |
| 2054 | assert!(p.parse_rule().is_err()); |
| 2055 | |
| 2056 | let p = Parser::new(",M9.5.0,M4.1.0/3"); |
| 2057 | assert!(p.parse_rule().is_err()); |
| 2058 | |
| 2059 | let p = Parser::new("M9.5.0/"); |
| 2060 | assert!(p.parse_rule().is_err()); |
| 2061 | |
| 2062 | let p = Parser::new("M9.5.0,M4.1.0/"); |
| 2063 | assert!(p.parse_rule().is_err()); |
| 2064 | } |
| 2065 | |
| 2066 | #[test] |
| 2067 | fn parse_posix_datetime() { |
| 2068 | let p = Parser::new("J1"); |
| 2069 | assert_eq!( |
| 2070 | p.parse_posix_datetime().unwrap(), |
| 2071 | PosixDayTime { |
| 2072 | date: PosixDay::JulianOne(1), |
| 2073 | time: PosixTime { second: 2 * 60 * 60 } |
| 2074 | }, |
| 2075 | ); |
| 2076 | |
| 2077 | let p = Parser::new("J1/3"); |
| 2078 | assert_eq!( |
| 2079 | p.parse_posix_datetime().unwrap(), |
| 2080 | PosixDayTime { |
| 2081 | date: PosixDay::JulianOne(1), |
| 2082 | time: PosixTime { second: 3 * 60 * 60 } |
| 2083 | }, |
| 2084 | ); |
| 2085 | |
| 2086 | let p = Parser::new("M4.1.0/3"); |
| 2087 | assert_eq!( |
| 2088 | p.parse_posix_datetime().unwrap(), |
| 2089 | PosixDayTime { |
| 2090 | date: PosixDay::WeekdayOfMonth { |
| 2091 | month: 4, |
| 2092 | week: 1, |
| 2093 | weekday: 0, |
| 2094 | }, |
| 2095 | time: PosixTime { second: 3 * 60 * 60 }, |
| 2096 | }, |
| 2097 | ); |
| 2098 | |
| 2099 | let p = Parser::new("1/3:45:05"); |
| 2100 | assert_eq!( |
| 2101 | p.parse_posix_datetime().unwrap(), |
| 2102 | PosixDayTime { |
| 2103 | date: PosixDay::JulianZero(1), |
| 2104 | time: PosixTime { second: 3 * 60 * 60 + 45 * 60 + 5 }, |
| 2105 | }, |
| 2106 | ); |
| 2107 | |
| 2108 | let p = Parser::new("a"); |
| 2109 | assert!(p.parse_posix_datetime().is_err()); |
| 2110 | |
| 2111 | let p = Parser::new("J1/"); |
| 2112 | assert!(p.parse_posix_datetime().is_err()); |
| 2113 | |
| 2114 | let p = Parser::new("1/"); |
| 2115 | assert!(p.parse_posix_datetime().is_err()); |
| 2116 | |
| 2117 | let p = Parser::new("M4.1.0/"); |
| 2118 | assert!(p.parse_posix_datetime().is_err()); |
| 2119 | } |
| 2120 | |
| 2121 | #[test] |
| 2122 | fn parse_posix_date() { |
| 2123 | let p = Parser::new("J1"); |
| 2124 | assert_eq!(p.parse_posix_date().unwrap(), PosixDay::JulianOne(1)); |
| 2125 | let p = Parser::new("J365"); |
| 2126 | assert_eq!(p.parse_posix_date().unwrap(), PosixDay::JulianOne(365)); |
| 2127 | |
| 2128 | let p = Parser::new("0"); |
| 2129 | assert_eq!(p.parse_posix_date().unwrap(), PosixDay::JulianZero(0)); |
| 2130 | let p = Parser::new("1"); |
| 2131 | assert_eq!(p.parse_posix_date().unwrap(), PosixDay::JulianZero(1)); |
| 2132 | let p = Parser::new("365"); |
| 2133 | assert_eq!(p.parse_posix_date().unwrap(), PosixDay::JulianZero(365)); |
| 2134 | |
| 2135 | let p = Parser::new("M9.5.0"); |
| 2136 | assert_eq!( |
| 2137 | p.parse_posix_date().unwrap(), |
| 2138 | PosixDay::WeekdayOfMonth { month: 9, week: 5, weekday: 0 }, |
| 2139 | ); |
| 2140 | let p = Parser::new("M9.5.6"); |
| 2141 | assert_eq!( |
| 2142 | p.parse_posix_date().unwrap(), |
| 2143 | PosixDay::WeekdayOfMonth { month: 9, week: 5, weekday: 6 }, |
| 2144 | ); |
| 2145 | let p = Parser::new("M09.5.6"); |
| 2146 | assert_eq!( |
| 2147 | p.parse_posix_date().unwrap(), |
| 2148 | PosixDay::WeekdayOfMonth { month: 9, week: 5, weekday: 6 }, |
| 2149 | ); |
| 2150 | let p = Parser::new("M12.1.1"); |
| 2151 | assert_eq!( |
| 2152 | p.parse_posix_date().unwrap(), |
| 2153 | PosixDay::WeekdayOfMonth { month: 12, week: 1, weekday: 1 }, |
| 2154 | ); |
| 2155 | |
| 2156 | let p = Parser::new("a"); |
| 2157 | assert!(p.parse_posix_date().is_err()); |
| 2158 | |
| 2159 | let p = Parser::new("j"); |
| 2160 | assert!(p.parse_posix_date().is_err()); |
| 2161 | |
| 2162 | let p = Parser::new("m"); |
| 2163 | assert!(p.parse_posix_date().is_err()); |
| 2164 | |
| 2165 | let p = Parser::new("n"); |
| 2166 | assert!(p.parse_posix_date().is_err()); |
| 2167 | |
| 2168 | let p = Parser::new("J366"); |
| 2169 | assert!(p.parse_posix_date().is_err()); |
| 2170 | |
| 2171 | let p = Parser::new("366"); |
| 2172 | assert!(p.parse_posix_date().is_err()); |
| 2173 | } |
| 2174 | |
| 2175 | #[test] |
| 2176 | fn parse_posix_julian_day_no_leap() { |
| 2177 | let p = Parser::new("1"); |
| 2178 | assert_eq!(p.parse_posix_julian_day_no_leap().unwrap(), 1); |
| 2179 | |
| 2180 | let p = Parser::new("001"); |
| 2181 | assert_eq!(p.parse_posix_julian_day_no_leap().unwrap(), 1); |
| 2182 | |
| 2183 | let p = Parser::new("365"); |
| 2184 | assert_eq!(p.parse_posix_julian_day_no_leap().unwrap(), 365); |
| 2185 | |
| 2186 | let p = Parser::new("3655"); |
| 2187 | assert_eq!(p.parse_posix_julian_day_no_leap().unwrap(), 365); |
| 2188 | |
| 2189 | let p = Parser::new("0"); |
| 2190 | assert!(p.parse_posix_julian_day_no_leap().is_err()); |
| 2191 | |
| 2192 | let p = Parser::new("366"); |
| 2193 | assert!(p.parse_posix_julian_day_no_leap().is_err()); |
| 2194 | } |
| 2195 | |
| 2196 | #[test] |
| 2197 | fn parse_posix_julian_day_with_leap() { |
| 2198 | let p = Parser::new("0"); |
| 2199 | assert_eq!(p.parse_posix_julian_day_with_leap().unwrap(), 0); |
| 2200 | |
| 2201 | let p = Parser::new("1"); |
| 2202 | assert_eq!(p.parse_posix_julian_day_with_leap().unwrap(), 1); |
| 2203 | |
| 2204 | let p = Parser::new("001"); |
| 2205 | assert_eq!(p.parse_posix_julian_day_with_leap().unwrap(), 1); |
| 2206 | |
| 2207 | let p = Parser::new("365"); |
| 2208 | assert_eq!(p.parse_posix_julian_day_with_leap().unwrap(), 365); |
| 2209 | |
| 2210 | let p = Parser::new("3655"); |
| 2211 | assert_eq!(p.parse_posix_julian_day_with_leap().unwrap(), 365); |
| 2212 | |
| 2213 | let p = Parser::new("366"); |
| 2214 | assert!(p.parse_posix_julian_day_with_leap().is_err()); |
| 2215 | } |
| 2216 | |
| 2217 | #[test] |
| 2218 | fn parse_weekday_of_month() { |
| 2219 | let p = Parser::new("9.5.0"); |
| 2220 | assert_eq!(p.parse_weekday_of_month().unwrap(), (9, 5, 0)); |
| 2221 | |
| 2222 | let p = Parser::new("9.1.6"); |
| 2223 | assert_eq!(p.parse_weekday_of_month().unwrap(), (9, 1, 6)); |
| 2224 | |
| 2225 | let p = Parser::new("09.1.6"); |
| 2226 | assert_eq!(p.parse_weekday_of_month().unwrap(), (9, 1, 6)); |
| 2227 | |
| 2228 | let p = Parser::new("9"); |
| 2229 | assert!(p.parse_weekday_of_month().is_err()); |
| 2230 | |
| 2231 | let p = Parser::new("9."); |
| 2232 | assert!(p.parse_weekday_of_month().is_err()); |
| 2233 | |
| 2234 | let p = Parser::new("9.5"); |
| 2235 | assert!(p.parse_weekday_of_month().is_err()); |
| 2236 | |
| 2237 | let p = Parser::new("9.5."); |
| 2238 | assert!(p.parse_weekday_of_month().is_err()); |
| 2239 | |
| 2240 | let p = Parser::new("0.5.0"); |
| 2241 | assert!(p.parse_weekday_of_month().is_err()); |
| 2242 | |
| 2243 | let p = Parser::new("13.5.0"); |
| 2244 | assert!(p.parse_weekday_of_month().is_err()); |
| 2245 | |
| 2246 | let p = Parser::new("9.0.0"); |
| 2247 | assert!(p.parse_weekday_of_month().is_err()); |
| 2248 | |
| 2249 | let p = Parser::new("9.6.0"); |
| 2250 | assert!(p.parse_weekday_of_month().is_err()); |
| 2251 | |
| 2252 | let p = Parser::new("9.5.7"); |
| 2253 | assert!(p.parse_weekday_of_month().is_err()); |
| 2254 | } |
| 2255 | |
| 2256 | #[test] |
| 2257 | fn parse_posix_time() { |
| 2258 | let p = Parser::new("5"); |
| 2259 | assert_eq!(p.parse_posix_time().unwrap().second, 5 * 60 * 60); |
| 2260 | |
| 2261 | let p = Parser::new("22"); |
| 2262 | assert_eq!(p.parse_posix_time().unwrap().second, 22 * 60 * 60); |
| 2263 | |
| 2264 | let p = Parser::new("02"); |
| 2265 | assert_eq!(p.parse_posix_time().unwrap().second, 2 * 60 * 60); |
| 2266 | |
| 2267 | let p = Parser::new("5:45"); |
| 2268 | assert_eq!( |
| 2269 | p.parse_posix_time().unwrap().second, |
| 2270 | 5 * 60 * 60 + 45 * 60 |
| 2271 | ); |
| 2272 | |
| 2273 | let p = Parser::new("5:45:12"); |
| 2274 | assert_eq!( |
| 2275 | p.parse_posix_time().unwrap().second, |
| 2276 | 5 * 60 * 60 + 45 * 60 + 12 |
| 2277 | ); |
| 2278 | |
| 2279 | let p = Parser::new("5:45:129"); |
| 2280 | assert_eq!( |
| 2281 | p.parse_posix_time().unwrap().second, |
| 2282 | 5 * 60 * 60 + 45 * 60 + 12 |
| 2283 | ); |
| 2284 | |
| 2285 | let p = Parser::new("5:45:12:"); |
| 2286 | assert_eq!( |
| 2287 | p.parse_posix_time().unwrap().second, |
| 2288 | 5 * 60 * 60 + 45 * 60 + 12 |
| 2289 | ); |
| 2290 | |
| 2291 | let p = Parser { ianav3plus: true, ..Parser::new("+5:45:12") }; |
| 2292 | assert_eq!( |
| 2293 | p.parse_posix_time().unwrap().second, |
| 2294 | 5 * 60 * 60 + 45 * 60 + 12 |
| 2295 | ); |
| 2296 | |
| 2297 | let p = Parser { ianav3plus: true, ..Parser::new("-5:45:12") }; |
| 2298 | assert_eq!( |
| 2299 | p.parse_posix_time().unwrap().second, |
| 2300 | -(5 * 60 * 60 + 45 * 60 + 12) |
| 2301 | ); |
| 2302 | |
| 2303 | let p = Parser { ianav3plus: true, ..Parser::new("-167:45:12") }; |
| 2304 | assert_eq!( |
| 2305 | p.parse_posix_time().unwrap().second, |
| 2306 | -(167 * 60 * 60 + 45 * 60 + 12), |
| 2307 | ); |
| 2308 | |
| 2309 | let p = Parser::new("25"); |
| 2310 | assert!(p.parse_posix_time().is_err()); |
| 2311 | |
| 2312 | let p = Parser::new("12:2"); |
| 2313 | assert!(p.parse_posix_time().is_err()); |
| 2314 | |
| 2315 | let p = Parser::new("12:"); |
| 2316 | assert!(p.parse_posix_time().is_err()); |
| 2317 | |
| 2318 | let p = Parser::new("12:23:5"); |
| 2319 | assert!(p.parse_posix_time().is_err()); |
| 2320 | |
| 2321 | let p = Parser::new("12:23:"); |
| 2322 | assert!(p.parse_posix_time().is_err()); |
| 2323 | |
| 2324 | let p = Parser { ianav3plus: true, ..Parser::new("168") }; |
| 2325 | assert!(p.parse_posix_time().is_err()); |
| 2326 | |
| 2327 | let p = Parser { ianav3plus: true, ..Parser::new("-168") }; |
| 2328 | assert!(p.parse_posix_time().is_err()); |
| 2329 | |
| 2330 | let p = Parser { ianav3plus: true, ..Parser::new("+168") }; |
| 2331 | assert!(p.parse_posix_time().is_err()); |
| 2332 | } |
| 2333 | |
| 2334 | #[test] |
| 2335 | fn parse_month() { |
| 2336 | let p = Parser::new("1"); |
| 2337 | assert_eq!(p.parse_month().unwrap(), 1); |
| 2338 | |
| 2339 | // Should this be allowed? POSIX spec is unclear. |
| 2340 | // We allow it because our parse does stop at 2 |
| 2341 | // digits, so this seems harmless. Namely, '001' |
| 2342 | // results in an error. |
| 2343 | let p = Parser::new("01"); |
| 2344 | assert_eq!(p.parse_month().unwrap(), 1); |
| 2345 | |
| 2346 | let p = Parser::new("12"); |
| 2347 | assert_eq!(p.parse_month().unwrap(), 12); |
| 2348 | |
| 2349 | let p = Parser::new("0"); |
| 2350 | assert!(p.parse_month().is_err()); |
| 2351 | |
| 2352 | let p = Parser::new("00"); |
| 2353 | assert!(p.parse_month().is_err()); |
| 2354 | |
| 2355 | let p = Parser::new("001"); |
| 2356 | assert!(p.parse_month().is_err()); |
| 2357 | |
| 2358 | let p = Parser::new("13"); |
| 2359 | assert!(p.parse_month().is_err()); |
| 2360 | } |
| 2361 | |
| 2362 | #[test] |
| 2363 | fn parse_week() { |
| 2364 | let p = Parser::new("1"); |
| 2365 | assert_eq!(p.parse_week().unwrap(), 1); |
| 2366 | |
| 2367 | let p = Parser::new("5"); |
| 2368 | assert_eq!(p.parse_week().unwrap(), 5); |
| 2369 | |
| 2370 | let p = Parser::new("55"); |
| 2371 | assert_eq!(p.parse_week().unwrap(), 5); |
| 2372 | |
| 2373 | let p = Parser::new("0"); |
| 2374 | assert!(p.parse_week().is_err()); |
| 2375 | |
| 2376 | let p = Parser::new("6"); |
| 2377 | assert!(p.parse_week().is_err()); |
| 2378 | |
| 2379 | let p = Parser::new("00"); |
| 2380 | assert!(p.parse_week().is_err()); |
| 2381 | |
| 2382 | let p = Parser::new("01"); |
| 2383 | assert!(p.parse_week().is_err()); |
| 2384 | |
| 2385 | let p = Parser::new("05"); |
| 2386 | assert!(p.parse_week().is_err()); |
| 2387 | } |
| 2388 | |
| 2389 | #[test] |
| 2390 | fn parse_weekday() { |
| 2391 | let p = Parser::new("0"); |
| 2392 | assert_eq!(p.parse_weekday().unwrap(), 0); |
| 2393 | |
| 2394 | let p = Parser::new("1"); |
| 2395 | assert_eq!(p.parse_weekday().unwrap(), 1); |
| 2396 | |
| 2397 | let p = Parser::new("6"); |
| 2398 | assert_eq!(p.parse_weekday().unwrap(), 6); |
| 2399 | |
| 2400 | let p = Parser::new("00"); |
| 2401 | assert_eq!(p.parse_weekday().unwrap(), 0); |
| 2402 | |
| 2403 | let p = Parser::new("06"); |
| 2404 | assert_eq!(p.parse_weekday().unwrap(), 0); |
| 2405 | |
| 2406 | let p = Parser::new("60"); |
| 2407 | assert_eq!(p.parse_weekday().unwrap(), 6); |
| 2408 | |
| 2409 | let p = Parser::new("7"); |
| 2410 | assert!(p.parse_weekday().is_err()); |
| 2411 | } |
| 2412 | |
| 2413 | #[test] |
| 2414 | fn parse_hour_posix() { |
| 2415 | let p = Parser::new("5"); |
| 2416 | assert_eq!(p.parse_hour_posix().unwrap(), 5); |
| 2417 | |
| 2418 | let p = Parser::new("0"); |
| 2419 | assert_eq!(p.parse_hour_posix().unwrap(), 0); |
| 2420 | |
| 2421 | let p = Parser::new("00"); |
| 2422 | assert_eq!(p.parse_hour_posix().unwrap(), 0); |
| 2423 | |
| 2424 | let p = Parser::new("24"); |
| 2425 | assert_eq!(p.parse_hour_posix().unwrap(), 24); |
| 2426 | |
| 2427 | let p = Parser::new("100"); |
| 2428 | assert_eq!(p.parse_hour_posix().unwrap(), 10); |
| 2429 | |
| 2430 | let p = Parser::new("25"); |
| 2431 | assert!(p.parse_hour_posix().is_err()); |
| 2432 | |
| 2433 | let p = Parser::new("99"); |
| 2434 | assert!(p.parse_hour_posix().is_err()); |
| 2435 | } |
| 2436 | |
| 2437 | #[test] |
| 2438 | fn parse_hour_ianav3plus() { |
| 2439 | let new = |input| Parser { ianav3plus: true, ..Parser::new(input) }; |
| 2440 | |
| 2441 | let p = new("5"); |
| 2442 | assert_eq!(p.parse_hour_ianav3plus().unwrap(), 5); |
| 2443 | |
| 2444 | let p = new("0"); |
| 2445 | assert_eq!(p.parse_hour_ianav3plus().unwrap(), 0); |
| 2446 | |
| 2447 | let p = new("00"); |
| 2448 | assert_eq!(p.parse_hour_ianav3plus().unwrap(), 0); |
| 2449 | |
| 2450 | let p = new("000"); |
| 2451 | assert_eq!(p.parse_hour_ianav3plus().unwrap(), 0); |
| 2452 | |
| 2453 | let p = new("24"); |
| 2454 | assert_eq!(p.parse_hour_ianav3plus().unwrap(), 24); |
| 2455 | |
| 2456 | let p = new("100"); |
| 2457 | assert_eq!(p.parse_hour_ianav3plus().unwrap(), 100); |
| 2458 | |
| 2459 | let p = new("1000"); |
| 2460 | assert_eq!(p.parse_hour_ianav3plus().unwrap(), 100); |
| 2461 | |
| 2462 | let p = new("167"); |
| 2463 | assert_eq!(p.parse_hour_ianav3plus().unwrap(), 167); |
| 2464 | |
| 2465 | let p = new("168"); |
| 2466 | assert!(p.parse_hour_ianav3plus().is_err()); |
| 2467 | |
| 2468 | let p = new("999"); |
| 2469 | assert!(p.parse_hour_ianav3plus().is_err()); |
| 2470 | } |
| 2471 | |
| 2472 | #[test] |
| 2473 | fn parse_minute() { |
| 2474 | let p = Parser::new("00"); |
| 2475 | assert_eq!(p.parse_minute().unwrap(), 0); |
| 2476 | |
| 2477 | let p = Parser::new("24"); |
| 2478 | assert_eq!(p.parse_minute().unwrap(), 24); |
| 2479 | |
| 2480 | let p = Parser::new("59"); |
| 2481 | assert_eq!(p.parse_minute().unwrap(), 59); |
| 2482 | |
| 2483 | let p = Parser::new("599"); |
| 2484 | assert_eq!(p.parse_minute().unwrap(), 59); |
| 2485 | |
| 2486 | let p = Parser::new("0"); |
| 2487 | assert!(p.parse_minute().is_err()); |
| 2488 | |
| 2489 | let p = Parser::new("1"); |
| 2490 | assert!(p.parse_minute().is_err()); |
| 2491 | |
| 2492 | let p = Parser::new("9"); |
| 2493 | assert!(p.parse_minute().is_err()); |
| 2494 | |
| 2495 | let p = Parser::new("60"); |
| 2496 | assert!(p.parse_minute().is_err()); |
| 2497 | } |
| 2498 | |
| 2499 | #[test] |
| 2500 | fn parse_second() { |
| 2501 | let p = Parser::new("00"); |
| 2502 | assert_eq!(p.parse_second().unwrap(), 0); |
| 2503 | |
| 2504 | let p = Parser::new("24"); |
| 2505 | assert_eq!(p.parse_second().unwrap(), 24); |
| 2506 | |
| 2507 | let p = Parser::new("59"); |
| 2508 | assert_eq!(p.parse_second().unwrap(), 59); |
| 2509 | |
| 2510 | let p = Parser::new("599"); |
| 2511 | assert_eq!(p.parse_second().unwrap(), 59); |
| 2512 | |
| 2513 | let p = Parser::new("0"); |
| 2514 | assert!(p.parse_second().is_err()); |
| 2515 | |
| 2516 | let p = Parser::new("1"); |
| 2517 | assert!(p.parse_second().is_err()); |
| 2518 | |
| 2519 | let p = Parser::new("9"); |
| 2520 | assert!(p.parse_second().is_err()); |
| 2521 | |
| 2522 | let p = Parser::new("60"); |
| 2523 | assert!(p.parse_second().is_err()); |
| 2524 | } |
| 2525 | |
| 2526 | #[test] |
| 2527 | fn parse_number_with_exactly_n_digits() { |
| 2528 | let p = Parser::new("1"); |
| 2529 | assert_eq!(p.parse_number_with_exactly_n_digits(1).unwrap(), 1); |
| 2530 | |
| 2531 | let p = Parser::new("12"); |
| 2532 | assert_eq!(p.parse_number_with_exactly_n_digits(2).unwrap(), 12); |
| 2533 | |
| 2534 | let p = Parser::new("123"); |
| 2535 | assert_eq!(p.parse_number_with_exactly_n_digits(2).unwrap(), 12); |
| 2536 | |
| 2537 | let p = Parser::new(""); |
| 2538 | assert!(p.parse_number_with_exactly_n_digits(1).is_err()); |
| 2539 | |
| 2540 | let p = Parser::new("1"); |
| 2541 | assert!(p.parse_number_with_exactly_n_digits(2).is_err()); |
| 2542 | |
| 2543 | let p = Parser::new("12"); |
| 2544 | assert!(p.parse_number_with_exactly_n_digits(3).is_err()); |
| 2545 | } |
| 2546 | |
| 2547 | #[test] |
| 2548 | fn parse_number_with_upto_n_digits() { |
| 2549 | let p = Parser::new("1"); |
| 2550 | assert_eq!(p.parse_number_with_upto_n_digits(1).unwrap(), 1); |
| 2551 | |
| 2552 | let p = Parser::new("1"); |
| 2553 | assert_eq!(p.parse_number_with_upto_n_digits(2).unwrap(), 1); |
| 2554 | |
| 2555 | let p = Parser::new("12"); |
| 2556 | assert_eq!(p.parse_number_with_upto_n_digits(2).unwrap(), 12); |
| 2557 | |
| 2558 | let p = Parser::new("12"); |
| 2559 | assert_eq!(p.parse_number_with_upto_n_digits(3).unwrap(), 12); |
| 2560 | |
| 2561 | let p = Parser::new("123"); |
| 2562 | assert_eq!(p.parse_number_with_upto_n_digits(2).unwrap(), 12); |
| 2563 | |
| 2564 | let p = Parser::new(""); |
| 2565 | assert!(p.parse_number_with_upto_n_digits(1).is_err()); |
| 2566 | |
| 2567 | let p = Parser::new("a"); |
| 2568 | assert!(p.parse_number_with_upto_n_digits(1).is_err()); |
| 2569 | } |
| 2570 | |
| 2571 | #[test] |
| 2572 | fn to_dst_civil_datetime_utc_range() { |
| 2573 | let tz = posix_time_zone("WART4WARST,J1/-3,J365/20"); |
| 2574 | let dst_info = DstInfo { |
| 2575 | // We test this in other places. It's too annoying to write this |
| 2576 | // out here, and I didn't adopt snapshot testing until I had |
| 2577 | // written out these tests by hand. ¯\_(ツ)_/¯ |
| 2578 | dst: tz.dst.as_ref().unwrap(), |
| 2579 | start: date(2024, 1, 1).at(1, 0, 0, 0), |
| 2580 | end: date(2024, 12, 31).at(23, 0, 0, 0), |
| 2581 | }; |
| 2582 | assert_eq!(tz.dst_info_utc(2024), Some(dst_info)); |
| 2583 | |
| 2584 | let tz = posix_time_zone("WART4WARST,J1/-4,J365/21"); |
| 2585 | let dst_info = DstInfo { |
| 2586 | dst: tz.dst.as_ref().unwrap(), |
| 2587 | start: date(2024, 1, 1).at(0, 0, 0, 0), |
| 2588 | end: date(2024, 12, 31).at(23, 59, 59, 999_999_999), |
| 2589 | }; |
| 2590 | assert_eq!(tz.dst_info_utc(2024), Some(dst_info)); |
| 2591 | |
| 2592 | let tz = posix_time_zone("EST5EDT,M3.2.0,M11.1.0"); |
| 2593 | let dst_info = DstInfo { |
| 2594 | dst: tz.dst.as_ref().unwrap(), |
| 2595 | start: date(2024, 3, 10).at(7, 0, 0, 0), |
| 2596 | end: date(2024, 11, 3).at(6, 0, 0, 0), |
| 2597 | }; |
| 2598 | assert_eq!(tz.dst_info_utc(2024), Some(dst_info)); |
| 2599 | } |
| 2600 | |
| 2601 | // See: https://github.com/BurntSushi/jiff/issues/386 |
| 2602 | #[test] |
| 2603 | fn regression_permanent_dst() { |
| 2604 | let tz = posix_time_zone("XXX-2<+01>-1,0/0,J365/23"); |
| 2605 | let dst_info = DstInfo { |
| 2606 | dst: tz.dst.as_ref().unwrap(), |
| 2607 | start: date(2087, 1, 1).at(0, 0, 0, 0), |
| 2608 | end: date(2087, 12, 31).at(23, 59, 59, 999_999_999), |
| 2609 | }; |
| 2610 | assert_eq!(tz.dst_info_utc(2087), Some(dst_info)); |
| 2611 | } |
| 2612 | |
| 2613 | #[test] |
| 2614 | fn reasonable() { |
| 2615 | assert!(PosixTimeZone::parse(b"EST5").is_ok()); |
| 2616 | assert!(PosixTimeZone::parse(b"EST5EDT").is_err()); |
| 2617 | assert!(PosixTimeZone::parse(b"EST5EDT,J1,J365").is_ok()); |
| 2618 | |
| 2619 | let tz = posix_time_zone("EST24EDT,J1,J365"); |
| 2620 | assert_eq!( |
| 2621 | tz, |
| 2622 | PosixTimeZone { |
| 2623 | std_abbrev: "EST".into(), |
| 2624 | std_offset: PosixOffset { second: -24 * 60 * 60 }, |
| 2625 | dst: Some(PosixDst { |
| 2626 | abbrev: "EDT".into(), |
| 2627 | offset: PosixOffset { second: -23 * 60 * 60 }, |
| 2628 | rule: PosixRule { |
| 2629 | start: PosixDayTime { |
| 2630 | date: PosixDay::JulianOne(1), |
| 2631 | time: PosixTime::DEFAULT, |
| 2632 | }, |
| 2633 | end: PosixDayTime { |
| 2634 | date: PosixDay::JulianOne(365), |
| 2635 | time: PosixTime::DEFAULT, |
| 2636 | }, |
| 2637 | }, |
| 2638 | }), |
| 2639 | }, |
| 2640 | ); |
| 2641 | |
| 2642 | let tz = posix_time_zone("EST-24EDT,J1,J365"); |
| 2643 | assert_eq!( |
| 2644 | tz, |
| 2645 | PosixTimeZone { |
| 2646 | std_abbrev: "EST".into(), |
| 2647 | std_offset: PosixOffset { second: 24 * 60 * 60 }, |
| 2648 | dst: Some(PosixDst { |
| 2649 | abbrev: "EDT".into(), |
| 2650 | offset: PosixOffset { second: 25 * 60 * 60 }, |
| 2651 | rule: PosixRule { |
| 2652 | start: PosixDayTime { |
| 2653 | date: PosixDay::JulianOne(1), |
| 2654 | time: PosixTime::DEFAULT, |
| 2655 | }, |
| 2656 | end: PosixDayTime { |
| 2657 | date: PosixDay::JulianOne(365), |
| 2658 | time: PosixTime::DEFAULT, |
| 2659 | }, |
| 2660 | }, |
| 2661 | }), |
| 2662 | }, |
| 2663 | ); |
| 2664 | } |
| 2665 | |
| 2666 | #[test] |
| 2667 | fn posix_date_time_spec_to_datetime() { |
| 2668 | // For this test, we just keep the offset to zero to simplify things |
| 2669 | // a bit. We get coverage for non-zero offsets in higher level tests. |
| 2670 | let to_datetime = |daytime: &PosixDayTime, year: i16| { |
| 2671 | daytime.to_datetime(year, IOffset::UTC) |
| 2672 | }; |
| 2673 | |
| 2674 | let tz = posix_time_zone("EST5EDT,J1,J365/5:12:34"); |
| 2675 | assert_eq!( |
| 2676 | to_datetime(&tz.rule().start, 2023), |
| 2677 | date(2023, 1, 1).at(2, 0, 0, 0), |
| 2678 | ); |
| 2679 | assert_eq!( |
| 2680 | to_datetime(&tz.rule().end, 2023), |
| 2681 | date(2023, 12, 31).at(5, 12, 34, 0), |
| 2682 | ); |
| 2683 | |
| 2684 | let tz = posix_time_zone("EST+5EDT,M3.2.0/2,M11.1.0/2"); |
| 2685 | assert_eq!( |
| 2686 | to_datetime(&tz.rule().start, 2024), |
| 2687 | date(2024, 3, 10).at(2, 0, 0, 0), |
| 2688 | ); |
| 2689 | assert_eq!( |
| 2690 | to_datetime(&tz.rule().end, 2024), |
| 2691 | date(2024, 11, 3).at(2, 0, 0, 0), |
| 2692 | ); |
| 2693 | |
| 2694 | let tz = posix_time_zone("EST+5EDT,M1.1.1,M12.5.2"); |
| 2695 | assert_eq!( |
| 2696 | to_datetime(&tz.rule().start, 2024), |
| 2697 | date(2024, 1, 1).at(2, 0, 0, 0), |
| 2698 | ); |
| 2699 | assert_eq!( |
| 2700 | to_datetime(&tz.rule().end, 2024), |
| 2701 | date(2024, 12, 31).at(2, 0, 0, 0), |
| 2702 | ); |
| 2703 | |
| 2704 | let tz = posix_time_zone("EST5EDT,0/0,J365/25"); |
| 2705 | assert_eq!( |
| 2706 | to_datetime(&tz.rule().start, 2024), |
| 2707 | date(2024, 1, 1).at(0, 0, 0, 0), |
| 2708 | ); |
| 2709 | assert_eq!( |
| 2710 | to_datetime(&tz.rule().end, 2024), |
| 2711 | date(2024, 12, 31).at(23, 59, 59, 999_999_999), |
| 2712 | ); |
| 2713 | |
| 2714 | let tz = posix_time_zone("XXX3EDT4,0/0,J365/23"); |
| 2715 | assert_eq!( |
| 2716 | to_datetime(&tz.rule().start, 2024), |
| 2717 | date(2024, 1, 1).at(0, 0, 0, 0), |
| 2718 | ); |
| 2719 | assert_eq!( |
| 2720 | to_datetime(&tz.rule().end, 2024), |
| 2721 | date(2024, 12, 31).at(23, 0, 0, 0), |
| 2722 | ); |
| 2723 | |
| 2724 | let tz = posix_time_zone("XXX3EDT4,0/0,365"); |
| 2725 | assert_eq!( |
| 2726 | to_datetime(&tz.rule().end, 2023), |
| 2727 | date(2023, 12, 31).at(23, 59, 59, 999_999_999), |
| 2728 | ); |
| 2729 | assert_eq!( |
| 2730 | to_datetime(&tz.rule().end, 2024), |
| 2731 | date(2024, 12, 31).at(2, 0, 0, 0), |
| 2732 | ); |
| 2733 | |
| 2734 | let tz = posix_time_zone("XXX3EDT4,J1/-167:59:59,J365/167:59:59"); |
| 2735 | assert_eq!( |
| 2736 | to_datetime(&tz.rule().start, 2024), |
| 2737 | date(2024, 1, 1).at(0, 0, 0, 0), |
| 2738 | ); |
| 2739 | assert_eq!( |
| 2740 | to_datetime(&tz.rule().end, 2024), |
| 2741 | date(2024, 12, 31).at(23, 59, 59, 999_999_999), |
| 2742 | ); |
| 2743 | } |
| 2744 | |
| 2745 | #[test] |
| 2746 | fn posix_date_time_spec_time() { |
| 2747 | let tz = posix_time_zone("EST5EDT,J1,J365/5:12:34"); |
| 2748 | assert_eq!(tz.rule().start.time, PosixTime::DEFAULT); |
| 2749 | assert_eq!( |
| 2750 | tz.rule().end.time, |
| 2751 | PosixTime { second: 5 * 60 * 60 + 12 * 60 + 34 }, |
| 2752 | ); |
| 2753 | } |
| 2754 | |
| 2755 | #[test] |
| 2756 | fn posix_date_spec_to_date() { |
| 2757 | let tz = posix_time_zone("EST+5EDT,M3.2.0/2,M11.1.0/2"); |
| 2758 | let start = tz.rule().start.date.to_date(2023); |
| 2759 | assert_eq!(start, Some(date(2023, 3, 12))); |
| 2760 | let end = tz.rule().end.date.to_date(2023); |
| 2761 | assert_eq!(end, Some(date(2023, 11, 5))); |
| 2762 | let start = tz.rule().start.date.to_date(2024); |
| 2763 | assert_eq!(start, Some(date(2024, 3, 10))); |
| 2764 | let end = tz.rule().end.date.to_date(2024); |
| 2765 | assert_eq!(end, Some(date(2024, 11, 3))); |
| 2766 | |
| 2767 | let tz = posix_time_zone("EST+5EDT,J60,J365"); |
| 2768 | let start = tz.rule().start.date.to_date(2023); |
| 2769 | assert_eq!(start, Some(date(2023, 3, 1))); |
| 2770 | let end = tz.rule().end.date.to_date(2023); |
| 2771 | assert_eq!(end, Some(date(2023, 12, 31))); |
| 2772 | let start = tz.rule().start.date.to_date(2024); |
| 2773 | assert_eq!(start, Some(date(2024, 3, 1))); |
| 2774 | let end = tz.rule().end.date.to_date(2024); |
| 2775 | assert_eq!(end, Some(date(2024, 12, 31))); |
| 2776 | |
| 2777 | let tz = posix_time_zone("EST+5EDT,59,365"); |
| 2778 | let start = tz.rule().start.date.to_date(2023); |
| 2779 | assert_eq!(start, Some(date(2023, 3, 1))); |
| 2780 | let end = tz.rule().end.date.to_date(2023); |
| 2781 | assert_eq!(end, None); |
| 2782 | let start = tz.rule().start.date.to_date(2024); |
| 2783 | assert_eq!(start, Some(date(2024, 2, 29))); |
| 2784 | let end = tz.rule().end.date.to_date(2024); |
| 2785 | assert_eq!(end, Some(date(2024, 12, 31))); |
| 2786 | |
| 2787 | let tz = posix_time_zone("EST+5EDT,M1.1.1,M12.5.2"); |
| 2788 | let start = tz.rule().start.date.to_date(2024); |
| 2789 | assert_eq!(start, Some(date(2024, 1, 1))); |
| 2790 | let end = tz.rule().end.date.to_date(2024); |
| 2791 | assert_eq!(end, Some(date(2024, 12, 31))); |
| 2792 | } |
| 2793 | |
| 2794 | #[test] |
| 2795 | fn posix_time_spec_to_civil_time() { |
| 2796 | let tz = posix_time_zone("EST5EDT,J1,J365/5:12:34"); |
| 2797 | assert_eq!( |
| 2798 | tz.dst.as_ref().unwrap().rule.start.time.second, |
| 2799 | 2 * 60 * 60, |
| 2800 | ); |
| 2801 | assert_eq!( |
| 2802 | tz.dst.as_ref().unwrap().rule.end.time.second, |
| 2803 | 5 * 60 * 60 + 12 * 60 + 34, |
| 2804 | ); |
| 2805 | |
| 2806 | let tz = posix_time_zone("EST5EDT,J1/23:59:59,J365/24:00:00"); |
| 2807 | assert_eq!( |
| 2808 | tz.dst.as_ref().unwrap().rule.start.time.second, |
| 2809 | 23 * 60 * 60 + 59 * 60 + 59, |
| 2810 | ); |
| 2811 | assert_eq!( |
| 2812 | tz.dst.as_ref().unwrap().rule.end.time.second, |
| 2813 | 24 * 60 * 60, |
| 2814 | ); |
| 2815 | |
| 2816 | let tz = posix_time_zone("EST5EDT,J1/-1,J365/167:00:00"); |
| 2817 | assert_eq!( |
| 2818 | tz.dst.as_ref().unwrap().rule.start.time.second, |
| 2819 | -1 * 60 * 60, |
| 2820 | ); |
| 2821 | assert_eq!( |
| 2822 | tz.dst.as_ref().unwrap().rule.end.time.second, |
| 2823 | 167 * 60 * 60, |
| 2824 | ); |
| 2825 | } |
| 2826 | |
| 2827 | #[test] |
| 2828 | fn parse_iana() { |
| 2829 | // Ref: https://github.com/chronotope/chrono/issues/1153 |
| 2830 | let p = PosixTimeZone::parse(b"CRAZY5SHORT,M12.5.0/50,0/2").unwrap(); |
| 2831 | assert_eq!( |
| 2832 | p, |
| 2833 | PosixTimeZone { |
| 2834 | std_abbrev: "CRAZY".into(), |
| 2835 | std_offset: PosixOffset { second: -5 * 60 * 60 }, |
| 2836 | dst: Some(PosixDst { |
| 2837 | abbrev: "SHORT".into(), |
| 2838 | offset: PosixOffset { second: -4 * 60 * 60 }, |
| 2839 | rule: PosixRule { |
| 2840 | start: PosixDayTime { |
| 2841 | date: PosixDay::WeekdayOfMonth { |
| 2842 | month: 12, |
| 2843 | week: 5, |
| 2844 | weekday: 0, |
| 2845 | }, |
| 2846 | time: PosixTime { second: 50 * 60 * 60 }, |
| 2847 | }, |
| 2848 | end: PosixDayTime { |
| 2849 | date: PosixDay::JulianZero(0), |
| 2850 | time: PosixTime { second: 2 * 60 * 60 }, |
| 2851 | }, |
| 2852 | }, |
| 2853 | }), |
| 2854 | }, |
| 2855 | ); |
| 2856 | |
| 2857 | assert!(PosixTimeZone::parse(b"America/New_York").is_err()); |
| 2858 | assert!(PosixTimeZone::parse(b":America/New_York").is_err()); |
| 2859 | } |
| 2860 | |
| 2861 | // See: https://github.com/BurntSushi/jiff/issues/407 |
| 2862 | #[test] |
| 2863 | fn parse_empty_is_err() { |
| 2864 | assert!(PosixTimeZone::parse(b"").is_err()); |
| 2865 | } |
| 2866 | |
| 2867 | // See: https://github.com/BurntSushi/jiff/issues/407 |
| 2868 | #[test] |
| 2869 | fn parse_weird_is_err() { |
| 2870 | let s = |
| 2871 | b"AAAAAAAAAAAAAAACAAAAAAAAAAAAQA8AACAAAAAAAAAAAAAAAAACAAAAAAAAAAA"; |
| 2872 | assert!(PosixTimeZone::parse(s).is_err()); |
| 2873 | |
| 2874 | let s = |
| 2875 | b"<AAAAAAAAAAAAAAACAAAAAAAAAAAAQA>8<AACAAAAAAAAAAAAAAAAACAAAAAAAAAAA>"; |
| 2876 | assert!(PosixTimeZone::parse(s).is_err()); |
| 2877 | |
| 2878 | let s = b"PPPPPPPPPPPPPPPPPPPPnoofPPPAAA6DaPPPPPPPPPPPPPPPPPPPPPnoofPPPPP,n"; |
| 2879 | assert!(PosixTimeZone::parse(s).is_err()); |
| 2880 | |
| 2881 | let s = b"oooooooooovooooooooooooooooool9<ooooo2o-o-oooooookoorooooooooroo8"; |
| 2882 | assert!(PosixTimeZone::parse(s).is_err()); |
| 2883 | } |
| 2884 | } |