| 1 |
// For the full copyright and license information, please view the LICENSE |
| 2 |
// file that was distributed with this source code. |
| 3 |
|
| 4 |
// spell-checker:ignore multispace0 |
| 5 |
|
| 6 |
//! From the GNU docs: |
| 7 |
//! |
| 8 |
//! > A date is a string, possibly empty, containing many items separated by |
| 9 |
//! > whitespace. The whitespace may be omitted when no ambiguity arises. The |
| 10 |
//! > empty string means the beginning of today (i.e., midnight). Order of the |
| 11 |
//! > items is immaterial. A date string may contain many flavors of items: |
| 12 |
//! > - calendar date items |
| 13 |
//! > - time of day items |
| 14 |
//! > - time zone items |
| 15 |
//! > - combined date and time of day items |
| 16 |
//! > - day of the week items |
| 17 |
//! > - relative items |
| 18 |
//! > - pure numbers. |
| 19 |
//! |
| 20 |
//! We put all of those in separate modules: |
| 21 |
//! - [`combined`] |
| 22 |
//! - [`date`] |
| 23 |
//! - [`epoch`] |
| 24 |
//! - [`offset`] |
| 25 |
//! - [`pure`] |
| 26 |
//! - [`relative`] |
| 27 |
//! - [`time`] |
| 28 |
//! - [`timezone`] |
| 29 |
//! - [`weekday`] |
| 30 |
//! - [`year`] |
| 31 |
|
| 32 |
// date and time items |
| 33 |
mod combined; |
| 34 |
mod date; |
| 35 |
mod epoch; |
| 36 |
mod offset; |
| 37 |
mod pure; |
| 38 |
mod relative; |
| 39 |
mod time; |
| 40 |
mod timezone; |
| 41 |
mod weekday; |
| 42 |
mod year; |
| 43 |
|
| 44 |
// utility modules |
| 45 |
mod builder; |
| 46 |
mod ordinal; |
| 47 |
mod primitive; |
| 48 |
|
| 49 |
pub(crate) mod error; |
| 50 |
|
| 51 |
use crate::ParsedDateTime; |
| 52 |
use jiff::Zoned; |
| 53 |
use primitive::space; |
| 54 |
use winnow::{ |
| 55 |
combinator::{alt, eof, preceded, repeat_till, terminated, trace}, |
| 56 |
error::{AddContext, ContextError, ErrMode, StrContext, StrContextValue}, |
| 57 |
stream::Stream, |
| 58 |
ModalResult, Parser, |
| 59 |
}; |
| 60 |
|
| 61 |
use builder::DateTimeBuilder; |
| 62 |
use error::Error; |
| 63 |
|
| 64 |
#[derive(PartialEq, Debug)] |
| 65 |
enum Item { |
| 66 |
DateTime(combined::DateTime), |
| 67 |
Date(date::Date), |
| 68 |
Time(time::Time), |
| 69 |
Weekday(weekday::Weekday), |
| 70 |
Relative(relative::Relative), |
| 71 |
Offset(offset::Offset), |
| 72 |
/// The military timezone letter `j`, which GNU `date` defines as local |
| 73 |
/// time. It is a timezone item like [`Item::Offset`], but carries no |
| 74 |
/// offset, so it cannot be represented as one. |
| 75 |
LocalZone, |
| 76 |
TimeZone(jiff::tz::TimeZone), |
| 77 |
Pure(String), |
| 78 |
} |
| 79 |
|
| 80 |
/// Parse a date and time string and resolve it against the given base date and |
| 81 |
/// time, returning a [`ParsedDateTime`] result. |
| 82 |
pub(crate) fn parse_at_date<S: AsRef<str> + Clone>( |
| 83 |
base: Zoned, |
| 84 |
input: S, |
| 85 |
) -> Result<ParsedDateTime, Error> { |
| 86 |
match parse(&mut input.as_ref()) { |
| 87 |
Ok(builder) => builder.set_base(base).build(), |
| 88 |
Err(e) => Err(e.into()), |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
/// Parse a date and time string and resolve it against the current local date |
| 93 |
/// and time, returning a [`ParsedDateTime`] result. |
| 94 |
pub(crate) fn parse_at_local<S: AsRef<str> + Clone>(input: S) -> Result<ParsedDateTime, Error> { |
| 95 |
match parse(&mut input.as_ref()) { |
| 96 |
Ok(builder) => builder.build(), // the builder uses current local date and time if no base is given. |
| 97 |
Err(e) => Err(e.into()), |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
/// Parse a date and time string. |
| 102 |
/// |
| 103 |
/// Grammar: |
| 104 |
/// |
| 105 |
/// ```ebnf |
| 106 |
/// spec = [ tz_rule ] ( timestamp | items ) ; |
| 107 |
/// |
| 108 |
/// tz_rule = "TZ=" , "\"" , ( posix_tz | iana_tz ) , "\"" ; |
| 109 |
/// |
| 110 |
/// timestamp = "@" , float ; |
| 111 |
/// |
| 112 |
/// items = item , { item } ; |
| 113 |
/// item = datetime | date | time | relative | weekday | offset | pure ; |
| 114 |
/// |
| 115 |
/// datetime = date , [ "t" | whitespace ] , iso_time ; |
| 116 |
/// |
| 117 |
/// date = iso_date | us_date | literal1_date | literal2_date ; |
| 118 |
/// |
| 119 |
/// iso_date = year , [ iso_date_delim ] , month , [ iso_date_delim ] , day ; |
| 120 |
/// iso_date_delim = optional_whitespace , "-" , optional_whitespace ; |
| 121 |
/// |
| 122 |
/// us_date = month , [ us_date_delim ] , day , [ us_date_delim , year ]; |
| 123 |
/// us_date_delim = optional_whitespace , "/" , optional_whitespace ; |
| 124 |
/// |
| 125 |
/// literal1_date = day , [ literal1_date_delim ] , literal_month , [ literal1_date_delim , year ] ; |
| 126 |
/// literal1_date_delim = (optional_whitespace , "-" , optional_whitespace) | optional_whitespace ; |
| 127 |
/// |
| 128 |
/// literal2_date = literal_month , optional_whitespace , day , [ literal2_date_delim , year ] ; |
| 129 |
/// literal2_date_delim = (optional_whitespace , "," , optional_whitespace) | optional_whitespace ; |
| 130 |
/// |
| 131 |
/// year = dec_uint ; |
| 132 |
/// month = dec_uint ; |
| 133 |
/// day = dec_uint ; |
| 134 |
/// |
| 135 |
/// literal_month = "january" | "jan" |
| 136 |
/// | "february" | "feb" |
| 137 |
/// | "march" | "mar" |
| 138 |
/// | "april" | "apr" |
| 139 |
/// | "may" |
| 140 |
/// | "june" | "jun" |
| 141 |
/// | "july" | "jul" |
| 142 |
/// | "august" | "aug" |
| 143 |
/// | "september" | "sept" | "sep" |
| 144 |
/// | "october" | "oct" |
| 145 |
/// | "november" | "nov" |
| 146 |
/// | "december" | "dec" ; |
| 147 |
/// |
| 148 |
/// time = iso_time | meridiem_time ; |
| 149 |
/// |
| 150 |
/// iso_time = hour24 , [ ":" , minute , [ ":" , second ] ] , [ time_offset ] ; |
| 151 |
/// |
| 152 |
/// meridiem_time = hour12 , [ ":" , minute , [ ":" , second ] ] , meridiem ; |
| 153 |
/// meridiem = "am" | "pm" | "a.m." | "p.m." ; |
| 154 |
/// |
| 155 |
/// hour24 = dec_uint ; |
| 156 |
/// hour12 = dec_uint ; |
| 157 |
/// minute = dec_uint ; |
| 158 |
/// second = dec_uint ; |
| 159 |
/// |
| 160 |
/// time_offset = ( "+" | "-" ) , dec_uint , [ ":" , dec_uint ] ; |
| 161 |
/// |
| 162 |
/// relative = [ numeric_ordinal ] , unit , [ "ago" ] | day_shift ; |
| 163 |
/// |
| 164 |
/// unit = "year" | "years" |
| 165 |
/// | "month" | "months" |
| 166 |
/// | "fortnight" | "fortnights" |
| 167 |
/// | "week" | "weeks" |
| 168 |
/// | "day" | "days" |
| 169 |
/// | "hour" | "hours" |
| 170 |
/// | "minute" | "minutes" | "min" | "mins" |
| 171 |
/// | "second" | "seconds" | "sec" | "secs" ; |
| 172 |
/// |
| 173 |
/// day_shift = "tomorrow" | "yesterday" | "today" | "now" ; |
| 174 |
/// |
| 175 |
/// weekday = [ ordinal ] , day , [ "," ] ; |
| 176 |
/// |
| 177 |
/// ordinal = numeric_ordinal | text_ordinal ; |
| 178 |
/// numeric_ordinal = [ "+" | "-" ] , dec_uint ; |
| 179 |
/// text_ordinal = "last" | "this" | "next" | "first" |
| 180 |
/// | "third" | "fourth" | "fifth" | "sixth" |
| 181 |
/// | "seventh" | "eighth" | "ninth" | "tenth" |
| 182 |
/// | "eleventh" | "twelfth" ; |
| 183 |
/// |
| 184 |
/// day = "monday" | "mon" | "mon." |
| 185 |
/// | "tuesday" | "tue" | "tue." | "tues" |
| 186 |
/// | "wednesday" | "wed" | "wed." | "wednes" |
| 187 |
/// | "thursday" | "thu" | "thu." | "thur" | "thurs" |
| 188 |
/// | "friday" | "fri" | "fri." |
| 189 |
/// | "saturday" | "sat" | "sat." |
| 190 |
/// | "sunday" | "sun" | "sun." ; |
| 191 |
/// |
| 192 |
/// offset = named_zone , [ time_offset ] ; |
| 193 |
/// |
| 194 |
/// pure = { digit } |
| 195 |
/// |
| 196 |
/// optional_whitespace = { whitespace } ; |
| 197 |
/// ``` |
| 198 |
fn parse(input: &mut &str) -> ModalResult<DateTimeBuilder> { |
| 199 |
trace("parse", alt((parse_timestamp, parse_items))).parse_next(input) |
| 200 |
} |
| 201 |
|
| 202 |
/// Parse a standalone epoch timestamp (e.g., `@1758724019`). |
| 203 |
/// |
| 204 |
/// GNU `date` specifies that a timestamp item is *complete* and *must not* be |
| 205 |
/// combined with any other date/time item. |
| 206 |
/// |
| 207 |
/// Notes: |
| 208 |
/// |
| 209 |
/// - If a timezone rule (`TZ="..."`) appears at the beginning of the input, it |
| 210 |
/// has no effect on the epoch value. We intentionally parse and ignore it. |
| 211 |
/// - Trailing input (aside from optional whitespaces) is rejected. |
| 212 |
fn parse_timestamp(input: &mut &str) -> ModalResult<DateTimeBuilder> { |
| 213 |
// Parse and ignore an optional leading timezone rule. |
| 214 |
let _ = timezone::parse(input); |
| 215 |
|
| 216 |
trace( |
| 217 |
"parse_timestamp", |
| 218 |
// Expect exactly one timestamp and then EOF (allowing trailing spaces). |
| 219 |
terminated(epoch::parse, preceded(space, eof)), |
| 220 |
) |
| 221 |
.verify_map(|ts| DateTimeBuilder::new().set_timestamp(ts).ok()) |
| 222 |
.parse_next(input) |
| 223 |
} |
| 224 |
|
| 225 |
/// Parse a sequence of date/time items, honoring an optional leading TZ rule. |
| 226 |
/// |
| 227 |
/// Notes: |
| 228 |
/// |
| 229 |
/// - If a timezone rule (`TZ="..."`) appears at the beginning of the input, |
| 230 |
/// parse it first. The timezone rule is case-sensitive. |
| 231 |
/// - After the optional timezone rule is parsed, we convert the input to |
| 232 |
/// lowercase to allow case-insensitive parsing of the remaining items. |
| 233 |
/// - Trailing input (aside from optional whitespaces) is rejected. |
| 234 |
fn parse_items(input: &mut &str) -> ModalResult<DateTimeBuilder> { |
| 235 |
// Parse and consume an optional leading timezone rule. |
| 236 |
let tz = timezone::parse(input).map(Item::TimeZone); |
| 237 |
|
| 238 |
// Convert input to lowercase for case-insensitive parsing. |
| 239 |
let lower = input.to_ascii_lowercase(); |
| 240 |
let input = &mut lower.as_str(); |
| 241 |
|
| 242 |
let (mut items, _): (Vec<Item>, _) = trace( |
| 243 |
"parse_items", |
| 244 |
// Parse zero or more items until EOF (allowing trailing spaces). |
| 245 |
repeat_till(0.., parse_item, preceded(space, eof)), |
| 246 |
) |
| 247 |
.parse_next(input)?; |
| 248 |
|
| 249 |
if let Ok(tz) = tz { |
| 250 |
items.push(tz); |
| 251 |
} |
| 252 |
|
| 253 |
items.try_into().map_err(|e| expect_error(input, e)) |
| 254 |
} |
| 255 |
|
| 256 |
/// Parse an item. |
| 257 |
fn parse_item(input: &mut &str) -> ModalResult<Item> { |
| 258 |
trace( |
| 259 |
"parse_item", |
| 260 |
alt(( |
| 261 |
combined::parse.map(Item::DateTime), |
| 262 |
date::parse.map(Item::Date), |
| 263 |
time::parse.map(Item::Time), |
| 264 |
relative::parse.map(Item::Relative), |
| 265 |
weekday::parse.map(Item::Weekday), |
| 266 |
offset::parse.map(Item::Offset), |
| 267 |
offset::parse_local.map(|()| Item::LocalZone), |
| 268 |
pure::parse.map(Item::Pure), |
| 269 |
)), |
| 270 |
) |
| 271 |
.parse_next(input) |
| 272 |
} |
| 273 |
|
| 274 |
/// Create an error with context for unexpected input. |
| 275 |
fn expect_error(input: &mut &str, reason: &'static str) -> ErrMode<ContextError> { |
| 276 |
ErrMode::Cut(ContextError::new()).add_context( |
| 277 |
input, |
| 278 |
&input.checkpoint(), |
| 279 |
StrContext::Expected(StrContextValue::Description(reason)), |
| 280 |
) |
| 281 |
} |
| 282 |
|
| 283 |
#[cfg(test)] |
| 284 |
mod tests { |
| 285 |
use crate::ParsedDateTime; |
| 286 |
use jiff::{civil::DateTime, tz::TimeZone, ToSpan, Zoned}; |
| 287 |
|
| 288 |
use super::*; |
| 289 |
|
| 290 |
fn at_date(builder: DateTimeBuilder, base: Zoned) -> Zoned { |
| 291 |
builder.set_base(base).build().unwrap().expect_in_range() |
| 292 |
} |
| 293 |
|
| 294 |
fn at_utc(builder: DateTimeBuilder) -> Zoned { |
| 295 |
at_date(builder, Zoned::now().with_time_zone(TimeZone::UTC)) |
| 296 |
} |
| 297 |
|
| 298 |
fn test_eq_fmt(fmt: &str, input: &str) -> String { |
| 299 |
let input = input.to_ascii_lowercase(); |
| 300 |
parse(&mut input.as_str()) |
| 301 |
.map(at_utc) |
| 302 |
.map_err(|e| eprintln!("TEST FAILED AT:\n{e}")) |
| 303 |
.expect("parsing failed during tests") |
| 304 |
.strftime(fmt) |
| 305 |
.to_string() |
| 306 |
} |
| 307 |
|
| 308 |
fn assert_extended_datetime(input: &str, base: Zoned, expected: &str) { |
| 309 |
let parsed = parse_at_date(base, input).unwrap(); |
| 310 |
assert!( |
| 311 |
matches!(parsed, ParsedDateTime::Extended(_)), |
| 312 |
"expected extended datetime, got in-range for: {input}" |
| 313 |
); |
| 314 |
assert_eq!(parsed.to_string(), expected, "{input}"); |
| 315 |
} |
| 316 |
|
| 317 |
fn expect_extended_datetime(parsed: ParsedDateTime) -> crate::ExtendedDateTime { |
| 318 |
match parsed { |
| 319 |
ParsedDateTime::Extended(dt) => dt, |
| 320 |
ParsedDateTime::InRange(z) => panic!("expected extended datetime, got in-range: {z}"), |
| 321 |
} |
| 322 |
} |
| 323 |
|
| 324 |
#[test] |
| 325 |
fn date_and_time() { |
| 326 |
assert_eq!( |
| 327 |
"2022-12-12", |
| 328 |
test_eq_fmt("%Y-%m-%d", " 10:10 2022-12-12 ") |
| 329 |
); |
| 330 |
|
| 331 |
// format, expected output, input |
| 332 |
assert_eq!("2024-01-02", test_eq_fmt("%Y-%m-%d", "2024-01-02")); |
| 333 |
|
| 334 |
// https://github.com/uutils/coreutils/issues/6662 |
| 335 |
assert_eq!("2005-01-02", test_eq_fmt("%Y-%m-%d", "2005-01-01 +1 day")); |
| 336 |
|
| 337 |
// https://github.com/uutils/coreutils/issues/6644 |
| 338 |
assert_eq!("Jul 16", test_eq_fmt("%b %d", "Jul 16")); |
| 339 |
assert_eq!("0718061449", test_eq_fmt("%m%d%H%M%S", "Jul 18 06:14:49")); |
| 340 |
assert_eq!( |
| 341 |
"07182024061449", |
| 342 |
test_eq_fmt("%m%d%Y%H%M%S", "Jul 18, 2024 06:14:49") |
| 343 |
); |
| 344 |
assert_eq!( |
| 345 |
"07182024061449", |
| 346 |
test_eq_fmt("%m%d%Y%H%M%S", "Jul 18 06:14:49 2024") |
| 347 |
); |
| 348 |
|
| 349 |
// https://github.com/uutils/coreutils/issues/5177 |
| 350 |
assert_eq!( |
| 351 |
"2023-07-27T13:53:54+00:00", |
| 352 |
test_eq_fmt("%Y-%m-%dT%H:%M:%S%:z", "@1690466034") |
| 353 |
); |
| 354 |
|
| 355 |
assert_eq!( |
| 356 |
"2023-07-27T13:53:54+00:00", |
| 357 |
test_eq_fmt("%Y-%m-%dT%H:%M:%S%:z", " @1690466034 ") |
| 358 |
); |
| 359 |
|
| 360 |
// https://github.com/uutils/coreutils/issues/6398 |
| 361 |
// TODO: make this work |
| 362 |
// assert_eq!("1111 1111 00", test_eq_fmt("%m%d %H%M %S", "11111111")); |
| 363 |
|
| 364 |
assert_eq!( |
| 365 |
"2024-07-17 06:14:49 +00:00", |
| 366 |
test_eq_fmt("%Y-%m-%d %H:%M:%S %:z", "Jul 17 06:14:49 2024 GMT"), |
| 367 |
); |
| 368 |
|
| 369 |
assert_eq!( |
| 370 |
"2024-07-17 06:14:49.567 +00:00", |
| 371 |
test_eq_fmt("%Y-%m-%d %H:%M:%S%.f %:z", "Jul 17 06:14:49.567 2024 GMT"), |
| 372 |
); |
| 373 |
|
| 374 |
assert_eq!( |
| 375 |
"2024-07-17 06:14:49.567 +00:00", |
| 376 |
test_eq_fmt("%Y-%m-%d %H:%M:%S%.f %:z", "Jul 17 06:14:49,567 2024 GMT"), |
| 377 |
); |
| 378 |
|
| 379 |
assert_eq!( |
| 380 |
"2024-07-17 06:14:49 -03:00", |
| 381 |
test_eq_fmt("%Y-%m-%d %H:%M:%S %:z", "Jul 17 06:14:49 2024 BRT"), |
| 382 |
); |
| 383 |
} |
| 384 |
|
| 385 |
#[test] |
| 386 |
fn date_and_time_ampm() { |
| 387 |
// https://github.com/uutils/parse_datetime/issues/282 |
| 388 |
assert_eq!("12:00", test_eq_fmt("%H:%M", "2024-06-15 12:00 PM")); |
| 389 |
assert_eq!("11:30", test_eq_fmt("%H:%M", "2024-06-15 11:30am")); |
| 390 |
assert_eq!("15:00", test_eq_fmt("%H:%M", "2024-06-15 3:00 PM")); |
| 391 |
} |
| 392 |
|
| 393 |
#[test] |
| 394 |
fn empty() { |
| 395 |
let result = parse(&mut ""); |
| 396 |
assert!(result.is_ok()); |
| 397 |
} |
| 398 |
|
| 399 |
#[test] |
| 400 |
fn whitespace_only() { |
| 401 |
let result = parse(&mut "\x0B \x0C"); |
| 402 |
assert!(result.is_ok()); |
| 403 |
} |
| 404 |
|
| 405 |
#[test] |
| 406 |
fn invalid() { |
| 407 |
let result = parse(&mut "2025-05-19 2024-05-20 06:14:49"); |
| 408 |
assert!(result.is_err()); |
| 409 |
assert!(result |
| 410 |
.unwrap_err() |
| 411 |
.to_string() |
| 412 |
.contains("date cannot appear more than once")); |
| 413 |
|
| 414 |
let result = parse(&mut "2025-05-19 2024-05-20"); |
| 415 |
assert!(result.is_err()); |
| 416 |
assert!(result |
| 417 |
.unwrap_err() |
| 418 |
.to_string() |
| 419 |
.contains("date cannot appear more than once")); |
| 420 |
|
| 421 |
let result = parse(&mut "06:14:49 06:14:49"); |
| 422 |
assert!(result.is_err()); |
| 423 |
assert!(result |
| 424 |
.unwrap_err() |
| 425 |
.to_string() |
| 426 |
.contains("time cannot appear more than once")); |
| 427 |
|
| 428 |
let result = parse(&mut "2025-05-19 +00:00 +01:00"); |
| 429 |
assert!(result.is_err()); |
| 430 |
|
| 431 |
let result = parse(&mut "m1y"); |
| 432 |
assert!(result.is_err()); |
| 433 |
assert!(result |
| 434 |
.unwrap_err() |
| 435 |
.to_string() |
| 436 |
.contains("time offset cannot appear more than once")); |
| 437 |
|
| 438 |
let result = parse(&mut "2025-05-19 abcdef"); |
| 439 |
assert!(result.is_err()); |
| 440 |
|
| 441 |
let result = parse(&mut "@1690466034 2025-05-19"); |
| 442 |
assert!(result.is_err()); |
| 443 |
|
| 444 |
let result = parse(&mut "2025-05-19 @1690466034"); |
| 445 |
assert!(result.is_err()); |
| 446 |
|
| 447 |
// Pure number as year (large years are accepted). |
| 448 |
let result = parse(&mut "jul 18 12:30 10000"); |
| 449 |
assert!(result.is_ok()); |
| 450 |
let built = result.unwrap().build().unwrap(); |
| 451 |
let dt = expect_extended_datetime(built); |
| 452 |
assert_eq!(dt.year, 10000); |
| 453 |
|
| 454 |
// Pure number as time (too long). |
| 455 |
let result = parse(&mut "01:02 12345"); |
| 456 |
assert!(result.is_err()); |
| 457 |
assert!(result |
| 458 |
.unwrap_err() |
| 459 |
.to_string() |
| 460 |
.contains("pure number must be 1-4 digits when interpreted as time")); |
| 461 |
|
| 462 |
// Pure number as time (repeated time). |
| 463 |
let result = parse(&mut "01:02 1234"); |
| 464 |
assert!(result.is_err()); |
| 465 |
assert!(result |
| 466 |
.unwrap_err() |
| 467 |
.to_string() |
| 468 |
.contains("time cannot appear more than once")); |
| 469 |
|
| 470 |
// Pure number as time (invalid hour). |
| 471 |
let result = parse(&mut "jul 18 2025 2400"); |
| 472 |
assert!(result.is_err()); |
| 473 |
assert!(result |
| 474 |
.unwrap_err() |
| 475 |
.to_string() |
| 476 |
.contains("invalid hour in pure number")); |
| 477 |
|
| 478 |
// Pure number as time (invalid minute). |
| 479 |
let result = parse(&mut "jul 18 2025 2360"); |
| 480 |
assert!(result.is_err()); |
| 481 |
assert!(result |
| 482 |
.unwrap_err() |
| 483 |
.to_string() |
| 484 |
.contains("invalid minute in pure number")); |
| 485 |
} |
| 486 |
|
| 487 |
#[test] |
| 488 |
fn negative_base_year_with_yearless_date_errors() { |
| 489 |
let base = DateTime::new(-1, 1, 1, 0, 0, 0, 0) |
| 490 |
.unwrap() |
| 491 |
.to_zoned(TimeZone::UTC) |
| 492 |
.unwrap(); |
| 493 |
let result = parse_at_date(base, "11/14"); |
| 494 |
assert!(result.is_err()); |
| 495 |
assert!(result |
| 496 |
.unwrap_err() |
| 497 |
.to_string() |
| 498 |
.contains("base year must be non-negative")); |
| 499 |
} |
| 500 |
|
| 501 |
#[test] |
| 502 |
fn boundary_rollover_from_9999_falls_back_to_extended() { |
| 503 |
let base = "2000-01-01 00:00:00" |
| 504 |
.parse::<DateTime>() |
| 505 |
.unwrap() |
| 506 |
.to_zoned(TimeZone::UTC) |
| 507 |
.unwrap(); |
| 508 |
assert_extended_datetime("9999-12-31 +1 day", base, "10000-01-01 00:00:00+00:00"); |
| 509 |
} |
| 510 |
|
| 511 |
#[test] |
| 512 |
fn boundary_year_9999_absolute_date_parses() { |
| 513 |
let base = "2000-01-01 00:00:00" |
| 514 |
.parse::<DateTime>() |
| 515 |
.unwrap() |
| 516 |
.to_zoned(TimeZone::UTC) |
| 517 |
.unwrap(); |
| 518 |
assert_extended_datetime("9999-12-31", base, "9999-12-31 00:00:00+00:00"); |
| 519 |
} |
| 520 |
|
| 521 |
#[test] |
| 522 |
fn boundary_year_9999_with_time_parses() { |
| 523 |
let base = "2000-01-01 00:00:00" |
| 524 |
.parse::<DateTime>() |
| 525 |
.unwrap() |
| 526 |
.to_zoned(TimeZone::UTC) |
| 527 |
.unwrap(); |
| 528 |
assert_extended_datetime("9999-12-31 12:00", base, "9999-12-31 12:00:00+00:00"); |
| 529 |
} |
| 530 |
|
| 531 |
#[test] |
| 532 |
fn boundary_year_9999_with_utc_timezone_parses() { |
| 533 |
let base = "2000-01-01 00:00:00" |
| 534 |
.parse::<DateTime>() |
| 535 |
.unwrap() |
| 536 |
.to_zoned(TimeZone::UTC) |
| 537 |
.unwrap(); |
| 538 |
assert_extended_datetime("9999-12-31 12:00 UTC", base, "9999-12-31 12:00:00+00:00"); |
| 539 |
} |
| 540 |
|
| 541 |
#[test] |
| 542 |
fn boundary_year_9999_with_explicit_offset_parses() { |
| 543 |
let base = "2000-01-01 00:00:00" |
| 544 |
.parse::<DateTime>() |
| 545 |
.unwrap() |
| 546 |
.to_zoned(TimeZone::UTC) |
| 547 |
.unwrap(); |
| 548 |
assert_extended_datetime("9999-12-31 12:00 +01:00", base, "9999-12-31 12:00:00+01:00"); |
| 549 |
} |
| 550 |
|
| 551 |
#[test] |
| 552 |
fn large_year_relative_parity_months_and_years() { |
| 553 |
let base = "2000-01-01 00:00:00" |
| 554 |
.parse::<DateTime>() |
| 555 |
.unwrap() |
| 556 |
.to_zoned(TimeZone::UTC) |
| 557 |
.unwrap(); |
| 558 |
|
| 559 |
assert_extended_datetime( |
| 560 |
"10000-01-31 +2 months", |
| 561 |
base.clone(), |
| 562 |
"10000-03-31 00:00:00+00:00", |
| 563 |
); |
| 564 |
assert_extended_datetime( |
| 565 |
"10000-01-31 +3 months", |
| 566 |
base.clone(), |
| 567 |
"10000-05-01 00:00:00+00:00", |
| 568 |
); |
| 569 |
assert_extended_datetime( |
| 570 |
"10000-08-31 +6 months", |
| 571 |
base.clone(), |
| 572 |
"10001-03-03 00:00:00+00:00", |
| 573 |
); |
| 574 |
assert_extended_datetime( |
| 575 |
"10000-05-31 3 months ago", |
| 576 |
base.clone(), |
| 577 |
"10000-03-02 00:00:00+00:00", |
| 578 |
); |
| 579 |
assert_extended_datetime("10000-02-29 +1 year", base, "10001-03-01 00:00:00+00:00"); |
| 580 |
} |
| 581 |
|
| 582 |
#[test] |
| 583 |
fn rejects_year_above_gnu_max() { |
| 584 |
assert!(parse_at_local("2147485548-01-01").is_err()); |
| 585 |
} |
| 586 |
|
| 587 |
#[test] |
| 588 |
fn large_year_can_return_in_range_after_relative() { |
| 589 |
let base = "2000-01-01 00:00:00" |
| 590 |
.parse::<DateTime>() |
| 591 |
.unwrap() |
| 592 |
.to_zoned(TimeZone::UTC) |
| 593 |
.unwrap(); |
| 594 |
let result = parse_at_date(base, "10000-01-01 -1000 years").unwrap(); |
| 595 |
let z = result.expect_in_range(); |
| 596 |
assert_eq!( |
| 597 |
z.strftime("%Y-%m-%d %H:%M:%S%:z").to_string(), |
| 598 |
"9000-01-01 00:00:00+00:00" |
| 599 |
); |
| 600 |
} |
| 601 |
|
| 602 |
#[test] |
| 603 |
fn large_year_can_return_in_range_after_relative_with_named_timezone_rule() { |
| 604 |
let base = "2000-01-01 00:00:00" |
| 605 |
.parse::<DateTime>() |
| 606 |
.unwrap() |
| 607 |
.to_zoned(TimeZone::UTC) |
| 608 |
.unwrap(); |
| 609 |
let result = parse_at_date(base, "TZ=\"Europe/Paris\" 10000-01-01 -1000 years").unwrap(); |
| 610 |
let z = result.expect_in_range(); |
| 611 |
assert_eq!( |
| 612 |
z.strftime("%Y-%m-%d %H:%M:%S%:z").to_string(), |
| 613 |
"9000-01-01 00:00:00+01:00" |
| 614 |
); |
| 615 |
} |
| 616 |
|
| 617 |
#[test] |
| 618 |
fn large_year_time_with_explicit_offset_is_extended() { |
| 619 |
let base = "2000-01-01 00:00:00" |
| 620 |
.parse::<DateTime>() |
| 621 |
.unwrap() |
| 622 |
.to_zoned(TimeZone::UTC) |
| 623 |
.unwrap(); |
| 624 |
let dt = |
| 625 |
expect_extended_datetime(parse_at_date(base, "10000-01-01 12:34:56+02:00").unwrap()); |
| 626 |
assert_eq!((dt.year, dt.month, dt.day), (10000, 1, 1)); |
| 627 |
assert_eq!((dt.hour, dt.minute, dt.second), (12, 34, 56)); |
| 628 |
assert_eq!(dt.offset_seconds, 2 * 3600); |
| 629 |
} |
| 630 |
|
| 631 |
#[test] |
| 632 |
fn parse_at_date_returns_error_for_invalid_input() { |
| 633 |
let base = "2000-01-01 00:00:00" |
| 634 |
.parse::<DateTime>() |
| 635 |
.unwrap() |
| 636 |
.to_zoned(TimeZone::UTC) |
| 637 |
.unwrap(); |
| 638 |
let result = parse_at_date(base, "not-a-date"); |
| 639 |
assert!(result.is_err()); |
| 640 |
} |
| 641 |
|
| 642 |
#[test] |
| 643 |
#[should_panic(expected = "expected extended datetime")] |
| 644 |
fn assert_extended_datetime_panics_for_in_range_input() { |
| 645 |
let base = "2000-01-01 00:00:00" |
| 646 |
.parse::<DateTime>() |
| 647 |
.unwrap() |
| 648 |
.to_zoned(TimeZone::UTC) |
| 649 |
.unwrap(); |
| 650 |
assert_extended_datetime("2001-01-01", base, "2001-01-01 00:00:00+00:00"); |
| 651 |
} |
| 652 |
|
| 653 |
#[test] |
| 654 |
#[should_panic(expected = "ParsedDateTime is not representable as jiff::Zoned")] |
| 655 |
fn expect_in_range_panics_for_extended_input() { |
| 656 |
let base = "2000-01-01 00:00:00" |
| 657 |
.parse::<DateTime>() |
| 658 |
.unwrap() |
| 659 |
.to_zoned(TimeZone::UTC) |
| 660 |
.unwrap(); |
| 661 |
let parsed = parse_at_date(base, "10000-01-01").unwrap(); |
| 662 |
let _ = parsed.expect_in_range(); |
| 663 |
} |
| 664 |
|
| 665 |
#[test] |
| 666 |
fn relative_weekday() { |
| 667 |
// Jan 1 2025 is a Wed |
| 668 |
let now = "2025-01-01 00:00:00" |
| 669 |
.parse::<DateTime>() |
| 670 |
.unwrap() |
| 671 |
.to_zoned(TimeZone::UTC) |
| 672 |
.unwrap(); |
| 673 |
|
| 674 |
assert_eq!( |
| 675 |
at_date(parse(&mut "last wed").unwrap(), now.clone()), |
| 676 |
now.checked_sub(7.days()).unwrap() |
| 677 |
); |
| 678 |
assert_eq!(at_date(parse(&mut "this wed").unwrap(), now.clone()), now); |
| 679 |
assert_eq!( |
| 680 |
at_date(parse(&mut "next wed").unwrap(), now.clone()), |
| 681 |
now.checked_add(7.days()).unwrap() |
| 682 |
); |
| 683 |
assert_eq!( |
| 684 |
at_date(parse(&mut "last thu").unwrap(), now.clone()), |
| 685 |
now.checked_sub(6.days()).unwrap() |
| 686 |
); |
| 687 |
assert_eq!( |
| 688 |
at_date(parse(&mut "this thu").unwrap(), now.clone()), |
| 689 |
now.checked_add(1.days()).unwrap() |
| 690 |
); |
| 691 |
assert_eq!( |
| 692 |
at_date(parse(&mut "next thu").unwrap(), now.clone()), |
| 693 |
now.checked_add(1.days()).unwrap() |
| 694 |
); |
| 695 |
assert_eq!( |
| 696 |
at_date(parse(&mut "1 wed").unwrap(), now.clone()), |
| 697 |
now.checked_add(7.days()).unwrap() |
| 698 |
); |
| 699 |
assert_eq!( |
| 700 |
at_date(parse(&mut "1 thu").unwrap(), now.clone()), |
| 701 |
now.checked_add(1.days()).unwrap() |
| 702 |
); |
| 703 |
assert_eq!( |
| 704 |
at_date(parse(&mut "2 wed").unwrap(), now.clone()), |
| 705 |
now.checked_add(14.days()).unwrap() |
| 706 |
); |
| 707 |
assert_eq!( |
| 708 |
at_date(parse(&mut "2 thu").unwrap(), now.clone()), |
| 709 |
now.checked_add(8.days()).unwrap() |
| 710 |
); |
| 711 |
} |
| 712 |
|
| 713 |
#[test] |
| 714 |
fn relative_date_time() { |
| 715 |
let now = Zoned::now().with_time_zone(TimeZone::UTC); |
| 716 |
|
| 717 |
let result = at_date(parse(&mut "2 days ago").unwrap(), now.clone()); |
| 718 |
assert_eq!(result, now.checked_sub(2.days()).unwrap()); |
| 719 |
assert_eq!(result.hour(), now.hour()); |
| 720 |
assert_eq!(result.minute(), now.minute()); |
| 721 |
assert_eq!(result.second(), now.second()); |
| 722 |
|
| 723 |
let result = at_date(parse(&mut "2 days 3 days ago").unwrap(), now.clone()); |
| 724 |
assert_eq!(result, now.checked_sub(1.days()).unwrap()); |
| 725 |
assert_eq!(result.hour(), now.hour()); |
| 726 |
assert_eq!(result.minute(), now.minute()); |
| 727 |
assert_eq!(result.second(), now.second()); |
| 728 |
|
| 729 |
let result = at_date(parse(&mut "2025-01-01 2 days ago").unwrap(), now.clone()); |
| 730 |
assert_eq!(result.hour(), 0); |
| 731 |
assert_eq!(result.minute(), 0); |
| 732 |
assert_eq!(result.second(), 0); |
| 733 |
|
| 734 |
let result = at_date(parse(&mut "3 weeks").unwrap(), now.clone()); |
| 735 |
assert_eq!(result, now.checked_add(21.days()).unwrap()); |
| 736 |
assert_eq!(result.hour(), now.hour()); |
| 737 |
assert_eq!(result.minute(), now.minute()); |
| 738 |
assert_eq!(result.second(), now.second()); |
| 739 |
|
| 740 |
let result = at_date(parse(&mut "2025-01-01 3 weeks").unwrap(), now); |
| 741 |
assert_eq!(result.hour(), 0); |
| 742 |
assert_eq!(result.minute(), 0); |
| 743 |
assert_eq!(result.second(), 0); |
| 744 |
} |
| 745 |
|
| 746 |
#[test] |
| 747 |
fn pure() { |
| 748 |
let now = Zoned::now().with_time_zone(TimeZone::UTC); |
| 749 |
|
| 750 |
// Pure number as year. |
| 751 |
let result = at_date(parse(&mut "jul 18 12:30 2025").unwrap(), now.clone()); |
| 752 |
assert_eq!(result.year(), 2025); |
| 753 |
|
| 754 |
// Pure number as time. |
| 755 |
let result = at_date(parse(&mut "1230").unwrap(), now.clone()); |
| 756 |
assert_eq!(result.hour(), 12); |
| 757 |
assert_eq!(result.minute(), 30); |
| 758 |
|
| 759 |
let result = at_date(parse(&mut "123").unwrap(), now.clone()); |
| 760 |
assert_eq!(result.hour(), 1); |
| 761 |
assert_eq!(result.minute(), 23); |
| 762 |
|
| 763 |
let result = at_date(parse(&mut "12").unwrap(), now.clone()); |
| 764 |
assert_eq!(result.hour(), 12); |
| 765 |
assert_eq!(result.minute(), 0); |
| 766 |
|
| 767 |
let result = at_date(parse(&mut "1").unwrap(), now.clone()); |
| 768 |
assert_eq!(result.hour(), 1); |
| 769 |
assert_eq!(result.minute(), 0); |
| 770 |
} |
| 771 |
|
| 772 |
#[test] |
| 773 |
fn timezone_rule() { |
| 774 |
let parse_build = |mut s| parse(&mut s).unwrap().build().unwrap(); |
| 775 |
|
| 776 |
for (input, expected) in [ |
| 777 |
( |
| 778 |
r#"TZ="Europe/Paris" 2025-01-02"#, |
| 779 |
"2025-01-02 00:00:00[Europe/Paris]" |
| 780 |
.parse::<Zoned>() |
| 781 |
.unwrap(), |
| 782 |
), |
| 783 |
( |
| 784 |
r#"TZ="Europe/Paris" 2025-01-02 03:04:05"#, |
| 785 |
"2025-01-02 03:04:05[Europe/Paris]" |
| 786 |
.parse::<Zoned>() |
| 787 |
.unwrap(), |
| 788 |
), |
| 789 |
] { |
| 790 |
assert_eq!(parse_build(input), expected, "{input}"); |
| 791 |
} |
| 792 |
} |
| 793 |
/// `j` is a time zone item, so it participates in the same duplicate check |
| 794 |
/// as a numeric offset. GNU rejects all three of these as a repeated zone. |
| 795 |
#[test] |
| 796 |
fn military_j_is_a_timezone_item() { |
| 797 |
// Parses on its own, attached or spaced, in either case. |
| 798 |
for input in ["j", "8j", "8 j", "8J", "j 8"] { |
| 799 |
assert!(parse(&mut &*input).is_ok(), "`{input}` should parse"); |
| 800 |
} |
| 801 |
|
| 802 |
for input in ["8j utc", "8 utc j", "8 j j"] { |
| 803 |
let result = parse(&mut &*input); |
| 804 |
assert!(result.is_err(), "`{input}` should be rejected"); |
| 805 |
assert!( |
| 806 |
result |
| 807 |
.unwrap_err() |
| 808 |
.to_string() |
| 809 |
.contains("time offset cannot appear more than once"), |
| 810 |
"`{input}` should report a repeated time offset" |
| 811 |
); |
| 812 |
} |
| 813 |
|
| 814 |
// A bare numeric offset is not an item on its own (the grammar requires |
| 815 |
// a named zone), so this is rejected by the parser rather than by the |
| 816 |
// duplicate check. GNU rejects it too. |
| 817 |
assert!(parse(&mut "j +05:00").is_err()); |
| 818 |
} |
| 819 |
} |