| 1 |
use std::num::NonZero; |
| 2 |
|
| 3 |
use time::format_description::well_known::{Iso8601, Rfc2822, Rfc3339}; |
| 4 |
use time::format_description::{BorrowedFormatItem, Component, OwnedFormatItem, modifier}; |
| 5 |
use time::macros::{date, datetime, offset, time, utc_datetime}; |
| 6 |
use time::parsing::Parsed; |
| 7 |
use time::{ |
| 8 |
Date, Month, OffsetDateTime, PrimitiveDateTime, Time, UtcDateTime, UtcOffset, Weekday, error, |
| 9 |
format_description as fd, |
| 10 |
}; |
| 11 |
|
| 12 |
macro_rules! invalid_literal { |
| 13 |
() => { |
| 14 |
Err(error::Parse::ParseFromDescription( |
| 15 |
error::ParseFromDescription::InvalidLiteral { .. }, |
| 16 |
)) |
| 17 |
}; |
| 18 |
} |
| 19 |
macro_rules! invalid_component { |
| 20 |
($name:literal) => { |
| 21 |
Err(error::Parse::ParseFromDescription( |
| 22 |
error::ParseFromDescription::InvalidComponent($name), |
| 23 |
)) |
| 24 |
}; |
| 25 |
} |
| 26 |
|
| 27 |
#[test] |
| 28 |
#[expect(clippy::cognitive_complexity, reason = "all test the same thing")] |
| 29 |
fn rfc_2822() -> time::Result<()> { |
| 30 |
assert_eq!( |
| 31 |
OffsetDateTime::parse("Sat, 02 Jan 2021 03:04:05 GMT", &Rfc2822)?, |
| 32 |
datetime!(2021-01-02 03:04:05 UTC), |
| 33 |
); |
| 34 |
assert_eq!( |
| 35 |
OffsetDateTime::parse("Sat, 02 Jan 2021 03:04:05 UT", &Rfc2822)?, |
| 36 |
datetime!(2021-01-02 03:04:05 UTC), |
| 37 |
); |
| 38 |
assert_eq!( |
| 39 |
OffsetDateTime::parse("Sat, 02 Jan 2021 03:04:05 +0000", &Rfc2822)?, |
| 40 |
datetime!(2021-01-02 03:04:05 UTC), |
| 41 |
); |
| 42 |
assert_eq!( |
| 43 |
OffsetDateTime::parse("Sat, 02 Jan 2021 03:04:05 +0607", &Rfc2822)?, |
| 44 |
datetime!(2021-01-02 03:04:05 +06:07), |
| 45 |
); |
| 46 |
assert_eq!( |
| 47 |
OffsetDateTime::parse("Sat, 02 Jan 2021 03:04:05 -0607", &Rfc2822)?, |
| 48 |
datetime!(2021-01-02 03:04:05 -06:07), |
| 49 |
); |
| 50 |
assert_eq!( |
| 51 |
OffsetDateTime::parse("Fri, 31 Dec 2021 23:59:60 Z", &Rfc2822)?, |
| 52 |
datetime!(2021-12-31 23:59:59.999_999_999 UTC), |
| 53 |
); |
| 54 |
assert_eq!( |
| 55 |
OffsetDateTime::parse("Fri, 31 Dec 2021 23:59:60 z", &Rfc2822)?, |
| 56 |
datetime!(2021-12-31 23:59:59.999_999_999 UTC), |
| 57 |
); |
| 58 |
assert_eq!( |
| 59 |
OffsetDateTime::parse("Fri, 31 Dec 2021 23:59:60 a", &Rfc2822)?, |
| 60 |
datetime!(2021-12-31 23:59:59.999_999_999 UTC), |
| 61 |
); |
| 62 |
assert_eq!( |
| 63 |
OffsetDateTime::parse("Fri, 31 Dec 2021 23:59:60 A", &Rfc2822)?, |
| 64 |
datetime!(2021-12-31 23:59:59.999_999_999 UTC), |
| 65 |
); |
| 66 |
assert_eq!( |
| 67 |
OffsetDateTime::parse("Fri, 31 Dec 2021 17:52:60 -0607", &Rfc2822)?, |
| 68 |
datetime!(2021-12-31 17:52:59.999_999_999 -06:07), |
| 69 |
); |
| 70 |
assert_eq!( |
| 71 |
OffsetDateTime::parse("Sat, 01 Jan 2022 06:06:60 +0607", &Rfc2822)?, |
| 72 |
datetime!(2022-01-01 06:06:59.999_999_999 +06:07), |
| 73 |
); |
| 74 |
|
| 75 |
assert_eq!( |
| 76 |
UtcDateTime::parse("Sat, 02 Jan 2021 03:04:05 GMT", &Rfc2822)?, |
| 77 |
utc_datetime!(2021-01-02 03:04:05), |
| 78 |
); |
| 79 |
assert_eq!( |
| 80 |
UtcDateTime::parse("Sat, 02 Jan 2021 03:04:05 UT", &Rfc2822)?, |
| 81 |
utc_datetime!(2021-01-02 03:04:05), |
| 82 |
); |
| 83 |
assert_eq!( |
| 84 |
UtcDateTime::parse("Sat, 02 Jan 2021 03:04:05 +0000", &Rfc2822)?, |
| 85 |
utc_datetime!(2021-01-02 03:04:05), |
| 86 |
); |
| 87 |
assert_eq!( |
| 88 |
UtcDateTime::parse("Sat, 02 Jan 2021 03:04:05 +0607", &Rfc2822)?, |
| 89 |
datetime!(2021-01-02 03:04:05 +06:07).to_utc(), |
| 90 |
); |
| 91 |
assert_eq!( |
| 92 |
UtcDateTime::parse("Sat, 02 Jan 2021 03:04:05 -0607", &Rfc2822)?, |
| 93 |
datetime!(2021-01-02 03:04:05 -06:07).to_utc(), |
| 94 |
); |
| 95 |
assert_eq!( |
| 96 |
UtcDateTime::parse("Fri, 31 Dec 2021 23:59:60 Z", &Rfc2822)?, |
| 97 |
utc_datetime!(2021-12-31 23:59:59.999_999_999), |
| 98 |
); |
| 99 |
assert_eq!( |
| 100 |
UtcDateTime::parse("Fri, 31 Dec 2021 23:59:60 z", &Rfc2822)?, |
| 101 |
utc_datetime!(2021-12-31 23:59:59.999_999_999), |
| 102 |
); |
| 103 |
assert_eq!( |
| 104 |
UtcDateTime::parse("Fri, 31 Dec 2021 23:59:60 a", &Rfc2822)?, |
| 105 |
utc_datetime!(2021-12-31 23:59:59.999_999_999), |
| 106 |
); |
| 107 |
assert_eq!( |
| 108 |
UtcDateTime::parse("Fri, 31 Dec 2021 23:59:60 A", &Rfc2822)?, |
| 109 |
utc_datetime!(2021-12-31 23:59:59.999_999_999), |
| 110 |
); |
| 111 |
assert_eq!( |
| 112 |
UtcDateTime::parse("Fri, 31 Dec 2021 17:52:60 -0607", &Rfc2822)?, |
| 113 |
datetime!(2021-12-31 17:52:59.999_999_999 -06:07).to_utc(), |
| 114 |
); |
| 115 |
assert_eq!( |
| 116 |
UtcDateTime::parse("Sat, 01 Jan 2022 06:06:60 +0607", &Rfc2822)?, |
| 117 |
datetime!(2022-01-01 06:06:59.999_999_999 +06:07).to_utc(), |
| 118 |
); |
| 119 |
|
| 120 |
assert_eq!( |
| 121 |
Date::parse("Sat, 02 Jan 2021 03:04:05 GMT", &Rfc2822)?, |
| 122 |
date!(2021-01-02) |
| 123 |
); |
| 124 |
assert_eq!( |
| 125 |
Date::parse("Sat, 02 Jan 2021 03:04:05 +0607", &Rfc2822)?, |
| 126 |
date!(2021-01-02) |
| 127 |
); |
| 128 |
assert_eq!( |
| 129 |
Date::parse("Sat, 02 Jan 2021 03:04:05 -0607", &Rfc2822)?, |
| 130 |
date!(2021-01-02) |
| 131 |
); |
| 132 |
assert_eq!( |
| 133 |
Date::parse("Sat, 02 Jan 21 03:04:05 -0607", &Rfc2822)?, |
| 134 |
date!(2021-01-02) |
| 135 |
); |
| 136 |
assert_eq!( |
| 137 |
Date::parse("Sat, 02 Jan 71 03:04:05 -0607", &Rfc2822)?, |
| 138 |
date!(1971-01-02) |
| 139 |
); |
| 140 |
|
| 141 |
assert_eq!( |
| 142 |
OffsetDateTime::parse("Sat,(\\\u{a})02 Jan 2021 03:04:05 GMT", &Rfc2822)?, |
| 143 |
datetime!(2021-01-02 03:04:05 UTC), |
| 144 |
); |
| 145 |
#[rustfmt::skip] |
| 146 |
assert_eq!( |
| 147 |
Time::parse( |
| 148 |
" \t Sat,\r\n \ |
| 149 |
(\tfoo012FOO!)\ |
| 150 |
(\u{1}\u{b}\u{e}\u{7f})\ |
| 151 |
(\\\u{0})\ |
| 152 |
(\\\u{1}\\\u{9}\\\u{28}\\\u{29}\\\\u{5c}\\\u{7f})\ |
| 153 |
(\\\n\\\u{b})\ |
| 154 |
02 \r\n \r\n Jan 2021 03:04:05 GMT", |
| 155 |
&Rfc2822 |
| 156 |
)?, |
| 157 |
time!(03:04:05) |
| 158 |
); |
| 159 |
|
| 160 |
Ok(()) |
| 161 |
} |
| 162 |
|
| 163 |
#[test] |
| 164 |
fn issue_661() -> time::Result<()> { |
| 165 |
assert_eq!( |
| 166 |
OffsetDateTime::parse("02 Jan 2021 03:04:05 +0607", &Rfc2822)?, |
| 167 |
datetime!(2021-01-02 03:04:05 +06:07), |
| 168 |
); |
| 169 |
assert_eq!( |
| 170 |
Date::parse("02 Jan 2021 03:04:05 +0607", &Rfc2822)?, |
| 171 |
date!(2021-01-02) |
| 172 |
); |
| 173 |
|
| 174 |
Ok(()) |
| 175 |
} |
| 176 |
|
| 177 |
#[expect(clippy::cognitive_complexity, reason = "all test the same thing")] |
| 178 |
#[test] |
| 179 |
fn rfc_2822_err() { |
| 180 |
// In the first test, the "weekday" component is invalid, we're actually testing the whitespace |
| 181 |
// parser. The error is because the parser attempts and fails to parse the whitespace, but it's |
| 182 |
// optional so it backtracks and attempts to parse the weekday (while still having leading |
| 183 |
// whitespace). The weekday is also optional, so it backtracks and attempts to parse the day. |
| 184 |
// This component is required, so it fails at this point. |
| 185 |
assert!(matches!( |
| 186 |
OffsetDateTime::parse(" \r\nM", &Rfc2822), |
| 187 |
invalid_component!("day") |
| 188 |
)); |
| 189 |
|
| 190 |
assert!(matches!( |
| 191 |
OffsetDateTime::parse("Mon:", &Rfc2822), |
| 192 |
invalid_literal!() |
| 193 |
)); |
| 194 |
assert!(matches!( |
| 195 |
OffsetDateTime::parse("Mon, o2", &Rfc2822), |
| 196 |
invalid_component!("day") |
| 197 |
)); |
| 198 |
assert!(matches!( |
| 199 |
OffsetDateTime::parse("Mon, 02_", &Rfc2822), |
| 200 |
invalid_literal!() |
| 201 |
)); |
| 202 |
assert!(matches!( |
| 203 |
OffsetDateTime::parse("Mon, 02 jxn", &Rfc2822), |
| 204 |
invalid_component!("month") |
| 205 |
)); |
| 206 |
assert!(matches!( |
| 207 |
OffsetDateTime::parse("Mon, 02 Jan_", &Rfc2822), |
| 208 |
invalid_literal!() |
| 209 |
)); |
| 210 |
assert!(matches!( |
| 211 |
OffsetDateTime::parse("Mon, 02 Jan abcd", &Rfc2822), |
| 212 |
invalid_component!("year") |
| 213 |
)); |
| 214 |
assert!(matches!( |
| 215 |
OffsetDateTime::parse("Mon, 02 Jan 1899", &Rfc2822), |
| 216 |
invalid_component!("year") |
| 217 |
)); |
| 218 |
assert!(matches!( |
| 219 |
OffsetDateTime::parse("Mon, 02 Jan 2021_", &Rfc2822), |
| 220 |
invalid_literal!() |
| 221 |
)); |
| 222 |
assert!(matches!( |
| 223 |
OffsetDateTime::parse("Mon, 02 Jan 21_", &Rfc2822), |
| 224 |
invalid_literal!() |
| 225 |
)); |
| 226 |
assert!(matches!( |
| 227 |
OffsetDateTime::parse("Mon, 02 Jan 2021 ab", &Rfc2822), |
| 228 |
invalid_component!("hour") |
| 229 |
)); |
| 230 |
assert!(matches!( |
| 231 |
OffsetDateTime::parse("Mon, 02 Jan 2021 03_", &Rfc2822), |
| 232 |
invalid_literal!() |
| 233 |
)); |
| 234 |
assert!(matches!( |
| 235 |
OffsetDateTime::parse("Mon, 02 Jan 2021 03:ab", &Rfc2822), |
| 236 |
invalid_component!("minute") |
| 237 |
)); |
| 238 |
assert!(matches!( |
| 239 |
OffsetDateTime::parse("Mon, 02 Jan 2021 03:04_", &Rfc2822), |
| 240 |
invalid_literal!() |
| 241 |
)); |
| 242 |
assert!(matches!( |
| 243 |
OffsetDateTime::parse("Mon, 02 Jan 2021 03:04:ab", &Rfc2822), |
| 244 |
invalid_component!("second") |
| 245 |
)); |
| 246 |
assert!(matches!( |
| 247 |
OffsetDateTime::parse("Mon, 02 Jan 2021 03:04:05_", &Rfc2822), |
| 248 |
invalid_literal!() |
| 249 |
)); |
| 250 |
assert!(matches!( |
| 251 |
OffsetDateTime::parse("Mon, 02 Jan 2021 03:04 6", &Rfc2822), |
| 252 |
invalid_component!("offset hour") |
| 253 |
)); |
| 254 |
assert!(matches!( |
| 255 |
OffsetDateTime::parse("Mon, 02 Jan 2021 03:04:05 -6", &Rfc2822), |
| 256 |
invalid_component!("offset hour") |
| 257 |
)); |
| 258 |
assert!(matches!( |
| 259 |
OffsetDateTime::parse("Mon, 02 Jan 2021 03:04:05 -060", &Rfc2822), |
| 260 |
invalid_component!("offset minute") |
| 261 |
)); |
| 262 |
assert!(matches!( |
| 263 |
OffsetDateTime::parse("Fri, 31 Dec 2021 23:59:61 Z", &Rfc2822), |
| 264 |
Err(error::Parse::TryFromParsed(error::TryFromParsed::ComponentRange(component))) |
| 265 |
if component.name() == "second" && !component.is_conditional() |
| 266 |
)); |
| 267 |
assert!(matches!( |
| 268 |
OffsetDateTime::parse("Fri, 31 Dec 2021 03:04:60 Z", &Rfc2822), |
| 269 |
Err(error::Parse::TryFromParsed(error::TryFromParsed::ComponentRange(component))) |
| 270 |
if component.name() == "second" && component.is_conditional() |
| 271 |
)); |
| 272 |
assert!(matches!( |
| 273 |
OffsetDateTime::parse("Fri, 30 Dec 2021 23:59:60 Z", &Rfc2822), |
| 274 |
Err(error::Parse::TryFromParsed(error::TryFromParsed::ComponentRange(component))) |
| 275 |
if component.name() == "second" && component.is_conditional() |
| 276 |
)); |
| 277 |
} |
| 278 |
|
| 279 |
#[test] |
| 280 |
fn rfc_3339() -> time::Result<()> { |
| 281 |
assert_eq!( |
| 282 |
OffsetDateTime::parse("2021-01-02T03:04:05Z", &Rfc3339)?, |
| 283 |
datetime!(2021-01-02 03:04:05 UTC), |
| 284 |
); |
| 285 |
assert_eq!( |
| 286 |
OffsetDateTime::parse("2021-12-31T23:59:60Z", &Rfc3339)?, |
| 287 |
datetime!(2021-12-31 23:59:59.999_999_999 UTC), |
| 288 |
); |
| 289 |
assert_eq!( |
| 290 |
OffsetDateTime::parse("2015-07-01T00:59:60+01:00", &Rfc3339)?, |
| 291 |
datetime!(2015-06-30 23:59:59.999_999_999 UTC), |
| 292 |
); |
| 293 |
assert_eq!( |
| 294 |
OffsetDateTime::parse("2021-01-02T03:04:05.1Z", &Rfc3339)?, |
| 295 |
datetime!(2021-01-02 03:04:05.1 UTC), |
| 296 |
); |
| 297 |
assert_eq!( |
| 298 |
OffsetDateTime::parse("2021-01-02T03:04:05.12Z", &Rfc3339)?, |
| 299 |
datetime!(2021-01-02 03:04:05.12 UTC), |
| 300 |
); |
| 301 |
assert_eq!( |
| 302 |
OffsetDateTime::parse("2021-01-02T03:04:05.123Z", &Rfc3339)?, |
| 303 |
datetime!(2021-01-02 03:04:05.123 UTC), |
| 304 |
); |
| 305 |
assert_eq!( |
| 306 |
OffsetDateTime::parse("2021-01-02T03:04:05.1234Z", &Rfc3339)?, |
| 307 |
datetime!(2021-01-02 03:04:05.123_4 UTC), |
| 308 |
); |
| 309 |
assert_eq!( |
| 310 |
OffsetDateTime::parse("2021-01-02T03:04:05.12345Z", &Rfc3339)?, |
| 311 |
datetime!(2021-01-02 03:04:05.123_45 UTC), |
| 312 |
); |
| 313 |
assert_eq!( |
| 314 |
OffsetDateTime::parse("2021-01-02T03:04:05.123456Z", &Rfc3339)?, |
| 315 |
datetime!(2021-01-02 03:04:05.123_456 UTC), |
| 316 |
); |
| 317 |
assert_eq!( |
| 318 |
OffsetDateTime::parse("2021-01-02T03:04:05.1234567Z", &Rfc3339)?, |
| 319 |
datetime!(2021-01-02 03:04:05.123_456_7 UTC), |
| 320 |
); |
| 321 |
assert_eq!( |
| 322 |
OffsetDateTime::parse("2021-01-02T03:04:05.12345678Z", &Rfc3339)?, |
| 323 |
datetime!(2021-01-02 03:04:05.123_456_78 UTC), |
| 324 |
); |
| 325 |
assert_eq!( |
| 326 |
OffsetDateTime::parse("2021-01-02T03:04:05.123456789Z", &Rfc3339)?, |
| 327 |
datetime!(2021-01-02 03:04:05.123_456_789 UTC), |
| 328 |
); |
| 329 |
assert_eq!( |
| 330 |
OffsetDateTime::parse("2021-01-02T03:04:05.123456789-01:02", &Rfc3339)?, |
| 331 |
datetime!(2021-01-02 03:04:05.123_456_789 -01:02), |
| 332 |
); |
| 333 |
assert_eq!( |
| 334 |
OffsetDateTime::parse("2021-01-02T03:04:05.123456789+01:02", &Rfc3339)?, |
| 335 |
datetime!(2021-01-02 03:04:05.123_456_789 +01:02), |
| 336 |
); |
| 337 |
assert_eq!( |
| 338 |
OffsetDateTime::parse("2021-01-02T03:04:05.123-00:01", &Rfc3339)?, |
| 339 |
datetime!(2021-01-02 03:04:05.123 -00:01), |
| 340 |
); |
| 341 |
|
| 342 |
assert_eq!( |
| 343 |
Date::parse("2021-01-02T03:04:05Z", &Rfc3339)?, |
| 344 |
date!(2021-01-02), |
| 345 |
); |
| 346 |
assert_eq!( |
| 347 |
Date::parse("2021-01-02T03:04:05.123+01:02", &Rfc3339)?, |
| 348 |
date!(2021-01-02), |
| 349 |
); |
| 350 |
assert_eq!( |
| 351 |
Date::parse("2021-01-02T03:04:05.123-01:02", &Rfc3339)?, |
| 352 |
date!(2021-01-02), |
| 353 |
); |
| 354 |
|
| 355 |
assert_eq!( |
| 356 |
UtcOffset::parse("2021-01-02T03:04:05Z", &Rfc3339)?, |
| 357 |
offset!(UTC), |
| 358 |
); |
| 359 |
assert_eq!( |
| 360 |
UtcOffset::parse("2021-01-02T03:04:05.123+01:02", &Rfc3339)?, |
| 361 |
offset!(+01:02), |
| 362 |
); |
| 363 |
assert_eq!( |
| 364 |
UtcOffset::parse("2021-01-02T03:04:05.123-01:02", &Rfc3339)?, |
| 365 |
offset!(-01:02), |
| 366 |
); |
| 367 |
assert_eq!( |
| 368 |
UtcOffset::parse("2021-01-02T03:04:05.123-00:01", &Rfc3339)?, |
| 369 |
offset!(-00:01), |
| 370 |
); |
| 371 |
|
| 372 |
// Any separator is allowed by RFC 3339, not just `T`. |
| 373 |
assert_eq!( |
| 374 |
OffsetDateTime::parse("2021-01-02 03:04:05Z", &Rfc3339)?, |
| 375 |
datetime!(2021-01-02 03:04:05 UTC), |
| 376 |
); |
| 377 |
assert_eq!( |
| 378 |
OffsetDateTime::parse("2021-01-02$03:04:05Z", &Rfc3339)?, |
| 379 |
datetime!(2021-01-02 03:04:05 UTC), |
| 380 |
); |
| 381 |
|
| 382 |
Ok(()) |
| 383 |
} |
| 384 |
|
| 385 |
#[expect(clippy::cognitive_complexity, reason = "all test the same thing")] |
| 386 |
#[test] |
| 387 |
fn rfc_3339_err() { |
| 388 |
assert!(matches!( |
| 389 |
PrimitiveDateTime::parse("x", &Rfc3339), |
| 390 |
invalid_component!("year") |
| 391 |
)); |
| 392 |
assert!(matches!( |
| 393 |
PrimitiveDateTime::parse("2021x", &Rfc3339), |
| 394 |
invalid_literal!() |
| 395 |
)); |
| 396 |
assert!(matches!( |
| 397 |
PrimitiveDateTime::parse("2021-x", &Rfc3339), |
| 398 |
invalid_component!("month") |
| 399 |
)); |
| 400 |
assert!(matches!( |
| 401 |
PrimitiveDateTime::parse("2021-0", &Rfc3339), |
| 402 |
invalid_component!("month") |
| 403 |
)); |
| 404 |
assert!(matches!( |
| 405 |
PrimitiveDateTime::parse("2021-01x", &Rfc3339), |
| 406 |
invalid_literal!() |
| 407 |
)); |
| 408 |
assert!(matches!( |
| 409 |
PrimitiveDateTime::parse("2021-01-0", &Rfc3339), |
| 410 |
invalid_component!("day") |
| 411 |
)); |
| 412 |
assert!(matches!( |
| 413 |
PrimitiveDateTime::parse("2021-01-01", &Rfc3339), |
| 414 |
invalid_component!("separator") |
| 415 |
)); |
| 416 |
assert!(matches!( |
| 417 |
PrimitiveDateTime::parse("2021-01-01T0", &Rfc3339), |
| 418 |
invalid_component!("hour") |
| 419 |
)); |
| 420 |
assert!(matches!( |
| 421 |
PrimitiveDateTime::parse("2021-01-01T00x", &Rfc3339), |
| 422 |
invalid_literal!() |
| 423 |
)); |
| 424 |
assert!(matches!( |
| 425 |
PrimitiveDateTime::parse("2021-01-01T00:0", &Rfc3339), |
| 426 |
invalid_component!("minute") |
| 427 |
)); |
| 428 |
assert!(matches!( |
| 429 |
PrimitiveDateTime::parse("2021-01-01T00:00x", &Rfc3339), |
| 430 |
invalid_literal!() |
| 431 |
)); |
| 432 |
assert!(matches!( |
| 433 |
PrimitiveDateTime::parse("2021-01-01T00:00:0", &Rfc3339), |
| 434 |
invalid_component!("second") |
| 435 |
)); |
| 436 |
assert!(matches!( |
| 437 |
PrimitiveDateTime::parse("2021-01-01T00:00:00.x", &Rfc3339), |
| 438 |
invalid_component!("subsecond") |
| 439 |
)); |
| 440 |
assert!(matches!( |
| 441 |
PrimitiveDateTime::parse("2021-01-01T00:00:00x", &Rfc3339), |
| 442 |
invalid_component!("offset hour") |
| 443 |
)); |
| 444 |
assert!(matches!( |
| 445 |
PrimitiveDateTime::parse("2021-01-01T00:00:00+0", &Rfc3339), |
| 446 |
invalid_component!("offset hour") |
| 447 |
)); |
| 448 |
assert!(matches!( |
| 449 |
PrimitiveDateTime::parse("2021-01-01T00:00:00+00x", &Rfc3339), |
| 450 |
invalid_literal!() |
| 451 |
)); |
| 452 |
assert!(matches!( |
| 453 |
PrimitiveDateTime::parse("2021-01-01T00:00:00+00:0", &Rfc3339), |
| 454 |
invalid_component!("offset minute") |
| 455 |
)); |
| 456 |
assert!(matches!( |
| 457 |
PrimitiveDateTime::parse("2021-13-01T00:00:00Z", &Rfc3339), |
| 458 |
invalid_component!("month") |
| 459 |
)); |
| 460 |
assert!(matches!( |
| 461 |
PrimitiveDateTime::parse("2021-01-02T03:04:60Z", &Rfc3339), |
| 462 |
Err(error::Parse::TryFromParsed(error::TryFromParsed::ComponentRange(component))) if component.name() == "second" |
| 463 |
)); |
| 464 |
|
| 465 |
// Conversions to offset-unaware types do not perform special treatment for leap seconds |
| 466 |
// even if the input could refer to one. |
| 467 |
assert!(matches!( |
| 468 |
PrimitiveDateTime::parse("2022-01-01T00:59:60+01:00", &Rfc3339), |
| 469 |
Err(error::Parse::TryFromParsed(error::TryFromParsed::ComponentRange(component))) if component.name() == "second" |
| 470 |
)); |
| 471 |
assert!(matches!( |
| 472 |
Time::parse("2021-12-31T23:04:60Z", &Rfc3339), |
| 473 |
Err(error::Parse::TryFromParsed(error::TryFromParsed::ComponentRange(component))) if component.name() == "second" |
| 474 |
)); |
| 475 |
|
| 476 |
assert!(matches!( |
| 477 |
OffsetDateTime::parse("2021-01-02T03:04:05Z ", &Rfc3339), |
| 478 |
Err(error::Parse::ParseFromDescription( |
| 479 |
error::ParseFromDescription::UnexpectedTrailingCharacters { .. } |
| 480 |
)) |
| 481 |
)); |
| 482 |
assert!(matches!( |
| 483 |
OffsetDateTime::parse("x", &Rfc3339), |
| 484 |
invalid_component!("year") |
| 485 |
)); |
| 486 |
assert!(matches!( |
| 487 |
OffsetDateTime::parse("2021x", &Rfc3339), |
| 488 |
invalid_literal!() |
| 489 |
)); |
| 490 |
assert!(matches!( |
| 491 |
OffsetDateTime::parse("2021-x", &Rfc3339), |
| 492 |
invalid_component!("month") |
| 493 |
)); |
| 494 |
assert!(matches!( |
| 495 |
OffsetDateTime::parse("2021-0", &Rfc3339), |
| 496 |
invalid_component!("month") |
| 497 |
)); |
| 498 |
assert!(matches!( |
| 499 |
OffsetDateTime::parse("2021-01x", &Rfc3339), |
| 500 |
invalid_literal!() |
| 501 |
)); |
| 502 |
assert!(matches!( |
| 503 |
OffsetDateTime::parse("2021-01-0", &Rfc3339), |
| 504 |
invalid_component!("day") |
| 505 |
)); |
| 506 |
assert!(matches!( |
| 507 |
OffsetDateTime::parse("2021-01-01", &Rfc3339), |
| 508 |
invalid_component!("separator") |
| 509 |
)); |
| 510 |
assert!(matches!( |
| 511 |
OffsetDateTime::parse("2021-01-01T0", &Rfc3339), |
| 512 |
invalid_component!("hour") |
| 513 |
)); |
| 514 |
assert!(matches!( |
| 515 |
OffsetDateTime::parse("2021-01-01T00x", &Rfc3339), |
| 516 |
invalid_literal!() |
| 517 |
)); |
| 518 |
assert!(matches!( |
| 519 |
OffsetDateTime::parse("2021-01-01T00:0", &Rfc3339), |
| 520 |
invalid_component!("minute") |
| 521 |
)); |
| 522 |
assert!(matches!( |
| 523 |
OffsetDateTime::parse("2021-01-01T00:00x", &Rfc3339), |
| 524 |
invalid_literal!() |
| 525 |
)); |
| 526 |
assert!(matches!( |
| 527 |
OffsetDateTime::parse("2021-01-01T00:00:0", &Rfc3339), |
| 528 |
invalid_component!("second") |
| 529 |
)); |
| 530 |
assert!(matches!( |
| 531 |
OffsetDateTime::parse("2021-01-01T00:00:00.x", &Rfc3339), |
| 532 |
invalid_component!("subsecond") |
| 533 |
)); |
| 534 |
assert!(matches!( |
| 535 |
OffsetDateTime::parse("2021-01-01T00:00:00x", &Rfc3339), |
| 536 |
invalid_component!("offset hour") |
| 537 |
)); |
| 538 |
assert!(matches!( |
| 539 |
OffsetDateTime::parse("2021-01-01T00:00:00+0", &Rfc3339), |
| 540 |
invalid_component!("offset hour") |
| 541 |
)); |
| 542 |
assert!(matches!( |
| 543 |
OffsetDateTime::parse("2021-01-01T00:00:00+00x", &Rfc3339), |
| 544 |
invalid_literal!() |
| 545 |
)); |
| 546 |
assert!(matches!( |
| 547 |
OffsetDateTime::parse("2021-01-01T00:00:00+00:0", &Rfc3339), |
| 548 |
invalid_component!("offset minute") |
| 549 |
)); |
| 550 |
assert!(matches!( |
| 551 |
OffsetDateTime::parse("2021-01-01T00:00:00+24:00", &Rfc3339), |
| 552 |
invalid_component!("offset hour") |
| 553 |
)); |
| 554 |
assert!(matches!( |
| 555 |
OffsetDateTime::parse("2021-01-01T00:00:00+00:60", &Rfc3339), |
| 556 |
Err(error::Parse::TryFromParsed(error::TryFromParsed::ComponentRange(component))) if component.name() == "offset minute" |
| 557 |
)); |
| 558 |
assert!(matches!( |
| 559 |
OffsetDateTime::parse("2021-13-01T00:00:00Z", &Rfc3339), |
| 560 |
Err(error::Parse::TryFromParsed(error::TryFromParsed::ComponentRange(component))) if component.name() == "month" |
| 561 |
)); |
| 562 |
assert!(matches!( |
| 563 |
OffsetDateTime::parse("2021-12-31T23:59:61Z", &Rfc3339), |
| 564 |
Err(error::Parse::TryFromParsed(error::TryFromParsed::ComponentRange(component))) if component.name() == "second" && !component.is_conditional() |
| 565 |
)); |
| 566 |
assert!(matches!( |
| 567 |
OffsetDateTime::parse("2021-01-02T23:59:60Z", &Rfc3339), |
| 568 |
Err(error::Parse::TryFromParsed(error::TryFromParsed::ComponentRange(component))) if component.name() == "second" && component.is_conditional() |
| 569 |
)); |
| 570 |
assert!(matches!( |
| 571 |
OffsetDateTime::parse("2021-12-31T03:04:60Z", &Rfc3339), |
| 572 |
Err(error::Parse::TryFromParsed(error::TryFromParsed::ComponentRange(component))) if component.name() == "second" && component.is_conditional() |
| 573 |
)); |
| 574 |
assert!(matches!( |
| 575 |
OffsetDateTime::parse("2021-12-31T23:59:60+01:00", &Rfc3339), |
| 576 |
Err(error::Parse::TryFromParsed(error::TryFromParsed::ComponentRange(component))) if component.name() == "second" && component.is_conditional() |
| 577 |
)); |
| 578 |
} |
| 579 |
|
| 580 |
#[test] |
| 581 |
fn iso_8601() { |
| 582 |
assert_eq!( |
| 583 |
OffsetDateTime::parse("2021-01-02T03:04:05Z", &Iso8601::DEFAULT), |
| 584 |
Ok(datetime!(2021-01-02 03:04:05 UTC)) |
| 585 |
); |
| 586 |
assert_eq!( |
| 587 |
OffsetDateTime::parse("2021-002T03:04:05Z", &Iso8601::DEFAULT), |
| 588 |
Ok(datetime!(2021-002 03:04:05 UTC)) |
| 589 |
); |
| 590 |
assert_eq!( |
| 591 |
OffsetDateTime::parse("2021-W01-2T03:04:05Z", &Iso8601::DEFAULT), |
| 592 |
Ok(datetime!(2021-W01-2 03:04:05 UTC)) |
| 593 |
); |
| 594 |
assert_eq!( |
| 595 |
OffsetDateTime::parse("-002021-01-02T03:04:05+01:00", &Iso8601::DEFAULT), |
| 596 |
Ok(datetime!(-002021-01-02 03:04:05 +01:00)) |
| 597 |
); |
| 598 |
assert_eq!( |
| 599 |
OffsetDateTime::parse("20210102T03.1Z", &Iso8601::DEFAULT), |
| 600 |
Ok(datetime!(2021-01-02 03:06:00 UTC)) |
| 601 |
); |
| 602 |
assert_eq!( |
| 603 |
OffsetDateTime::parse("2021002T0304.1Z", &Iso8601::DEFAULT), |
| 604 |
Ok(datetime!(2021-002 03:04:06 UTC)) |
| 605 |
); |
| 606 |
assert_eq!( |
| 607 |
OffsetDateTime::parse("2021W012T030405.1-0100", &Iso8601::DEFAULT), |
| 608 |
Ok(datetime!(2021-W01-2 03:04:05.1 -01:00)) |
| 609 |
); |
| 610 |
assert_eq!( |
| 611 |
OffsetDateTime::parse("20210102T03Z", &Iso8601::DEFAULT), |
| 612 |
Ok(datetime!(2021-01-02 03:00:00 UTC)) |
| 613 |
); |
| 614 |
assert_eq!( |
| 615 |
OffsetDateTime::parse("20210102T0304Z", &Iso8601::DEFAULT), |
| 616 |
Ok(datetime!(2021-01-02 03:04:00 UTC)) |
| 617 |
); |
| 618 |
|
| 619 |
assert_eq!( |
| 620 |
UtcDateTime::parse("2021-01-02T03:04:05Z", &Iso8601::DEFAULT), |
| 621 |
Ok(utc_datetime!(2021-01-02 03:04:05)) |
| 622 |
); |
| 623 |
assert_eq!( |
| 624 |
UtcDateTime::parse("2021-002T03:04:05Z", &Iso8601::DEFAULT), |
| 625 |
Ok(utc_datetime!(2021-002 03:04:05)) |
| 626 |
); |
| 627 |
assert_eq!( |
| 628 |
UtcDateTime::parse("2021-W01-2T03:04:05Z", &Iso8601::DEFAULT), |
| 629 |
Ok(utc_datetime!(2021-W01-2 03:04:05)) |
| 630 |
); |
| 631 |
assert_eq!( |
| 632 |
UtcDateTime::parse("-002021-01-02T03:04:05+01:00", &Iso8601::DEFAULT), |
| 633 |
Ok(datetime!(-002021-01-02 03:04:05 +01:00).to_utc()) |
| 634 |
); |
| 635 |
assert_eq!( |
| 636 |
UtcDateTime::parse("20210102T03.1Z", &Iso8601::DEFAULT), |
| 637 |
Ok(utc_datetime!(2021-01-02 03:06:00)) |
| 638 |
); |
| 639 |
assert_eq!( |
| 640 |
UtcDateTime::parse("2021002T0304.1Z", &Iso8601::DEFAULT), |
| 641 |
Ok(utc_datetime!(2021-002 03:04:06)) |
| 642 |
); |
| 643 |
assert_eq!( |
| 644 |
UtcDateTime::parse("2021W012T030405.1-0100", &Iso8601::DEFAULT), |
| 645 |
Ok(datetime!(2021-W01-2 03:04:05.1 -01:00).to_utc()) |
| 646 |
); |
| 647 |
assert_eq!( |
| 648 |
UtcDateTime::parse("20210102T03Z", &Iso8601::DEFAULT), |
| 649 |
Ok(utc_datetime!(2021-01-02 03:00:00)) |
| 650 |
); |
| 651 |
assert_eq!( |
| 652 |
UtcDateTime::parse("20210102T0304Z", &Iso8601::DEFAULT), |
| 653 |
Ok(utc_datetime!(2021-01-02 03:04:00)) |
| 654 |
); |
| 655 |
assert_eq!(UtcOffset::parse("+07", &Iso8601::DEFAULT), Ok(offset!(+7))); |
| 656 |
assert_eq!( |
| 657 |
UtcOffset::parse("+0304", &Iso8601::DEFAULT), |
| 658 |
Ok(offset!(+03:04)) |
| 659 |
); |
| 660 |
assert_eq!( |
| 661 |
PrimitiveDateTime::parse("2022-07-22T12:52:50.349409", &Iso8601::DEFAULT), |
| 662 |
Ok(datetime!(2022-07-22 12:52:50.349409000)) |
| 663 |
); |
| 664 |
} |
| 665 |
|
| 666 |
#[expect(clippy::cognitive_complexity, reason = "all test the same thing")] |
| 667 |
#[test] |
| 668 |
fn iso_8601_error() { |
| 669 |
assert!(matches!( |
| 670 |
OffsetDateTime::parse("20210102T03:04Z", &Iso8601::DEFAULT), |
| 671 |
Err(error::Parse::ParseFromDescription( |
| 672 |
error::ParseFromDescription::UnexpectedTrailingCharacters { .. } |
| 673 |
)) |
| 674 |
)); |
| 675 |
assert!(matches!( |
| 676 |
OffsetDateTime::parse("20210102T03.", &Iso8601::DEFAULT), |
| 677 |
Err(error::Parse::ParseFromDescription( |
| 678 |
error::ParseFromDescription::UnexpectedTrailingCharacters { .. } |
| 679 |
)) |
| 680 |
)); |
| 681 |
assert!(matches!( |
| 682 |
OffsetDateTime::parse("2021-0102", &Iso8601::DEFAULT), |
| 683 |
Err(error::Parse::ParseFromDescription( |
| 684 |
error::ParseFromDescription::UnexpectedTrailingCharacters { .. } |
| 685 |
)) |
| 686 |
)); |
| 687 |
assert!(matches!( |
| 688 |
OffsetDateTime::parse("2021-01-x", &Iso8601::DEFAULT), |
| 689 |
Err(error::Parse::ParseFromDescription( |
| 690 |
error::ParseFromDescription::UnexpectedTrailingCharacters { .. } |
| 691 |
)) |
| 692 |
)); |
| 693 |
assert!(matches!( |
| 694 |
OffsetDateTime::parse("2021-Wx", &Iso8601::DEFAULT), |
| 695 |
Err(error::Parse::ParseFromDescription( |
| 696 |
error::ParseFromDescription::UnexpectedTrailingCharacters { .. } |
| 697 |
)) |
| 698 |
)); |
| 699 |
assert!(matches!( |
| 700 |
OffsetDateTime::parse("2021-W012", &Iso8601::DEFAULT), |
| 701 |
Err(error::Parse::ParseFromDescription( |
| 702 |
error::ParseFromDescription::UnexpectedTrailingCharacters { .. } |
| 703 |
)) |
| 704 |
)); |
| 705 |
assert!(matches!( |
| 706 |
OffsetDateTime::parse("2021-W01-x", &Iso8601::DEFAULT), |
| 707 |
Err(error::Parse::ParseFromDescription( |
| 708 |
error::ParseFromDescription::UnexpectedTrailingCharacters { .. } |
| 709 |
)) |
| 710 |
)); |
| 711 |
assert!(matches!( |
| 712 |
OffsetDateTime::parse("2021-01-02T03:x", &Iso8601::DEFAULT), |
| 713 |
Err(error::Parse::ParseFromDescription( |
| 714 |
error::ParseFromDescription::UnexpectedTrailingCharacters { .. } |
| 715 |
)) |
| 716 |
)); |
| 717 |
assert!(matches!( |
| 718 |
OffsetDateTime::parse("2021-01-02T03:04x", &Iso8601::DEFAULT), |
| 719 |
Err(error::Parse::ParseFromDescription( |
| 720 |
error::ParseFromDescription::UnexpectedTrailingCharacters { .. } |
| 721 |
)) |
| 722 |
)); |
| 723 |
assert!(matches!( |
| 724 |
OffsetDateTime::parse("2021-01-02T03:04:", &Iso8601::DEFAULT), |
| 725 |
Err(error::Parse::ParseFromDescription( |
| 726 |
error::ParseFromDescription::UnexpectedTrailingCharacters { .. } |
| 727 |
)) |
| 728 |
)); |
| 729 |
assert_eq!( |
| 730 |
OffsetDateTime::parse("01:02", &Iso8601::DEFAULT), |
| 731 |
Err(error::Parse::TryFromParsed( |
| 732 |
error::TryFromParsed::InsufficientInformation |
| 733 |
)) |
| 734 |
); |
| 735 |
|
| 736 |
assert!(matches!( |
| 737 |
UtcDateTime::parse("20210102T03:04Z", &Iso8601::DEFAULT), |
| 738 |
Err(error::Parse::ParseFromDescription( |
| 739 |
error::ParseFromDescription::UnexpectedTrailingCharacters { .. } |
| 740 |
)) |
| 741 |
)); |
| 742 |
assert!(matches!( |
| 743 |
UtcDateTime::parse("20210102T03.", &Iso8601::DEFAULT), |
| 744 |
Err(error::Parse::ParseFromDescription( |
| 745 |
error::ParseFromDescription::UnexpectedTrailingCharacters { .. } |
| 746 |
)) |
| 747 |
)); |
| 748 |
assert!(matches!( |
| 749 |
UtcDateTime::parse("2021-0102", &Iso8601::DEFAULT), |
| 750 |
Err(error::Parse::ParseFromDescription( |
| 751 |
error::ParseFromDescription::UnexpectedTrailingCharacters { .. } |
| 752 |
)) |
| 753 |
)); |
| 754 |
assert!(matches!( |
| 755 |
UtcDateTime::parse("2021-01-x", &Iso8601::DEFAULT), |
| 756 |
Err(error::Parse::ParseFromDescription( |
| 757 |
error::ParseFromDescription::UnexpectedTrailingCharacters { .. } |
| 758 |
)) |
| 759 |
)); |
| 760 |
assert!(matches!( |
| 761 |
UtcDateTime::parse("2021-Wx", &Iso8601::DEFAULT), |
| 762 |
Err(error::Parse::ParseFromDescription( |
| 763 |
error::ParseFromDescription::UnexpectedTrailingCharacters { .. } |
| 764 |
)) |
| 765 |
)); |
| 766 |
assert!(matches!( |
| 767 |
UtcDateTime::parse("2021-W012", &Iso8601::DEFAULT), |
| 768 |
Err(error::Parse::ParseFromDescription( |
| 769 |
error::ParseFromDescription::UnexpectedTrailingCharacters { .. } |
| 770 |
)) |
| 771 |
)); |
| 772 |
assert!(matches!( |
| 773 |
UtcDateTime::parse("2021-W01-x", &Iso8601::DEFAULT), |
| 774 |
Err(error::Parse::ParseFromDescription( |
| 775 |
error::ParseFromDescription::UnexpectedTrailingCharacters { .. } |
| 776 |
)) |
| 777 |
)); |
| 778 |
assert!(matches!( |
| 779 |
UtcDateTime::parse("2021-01-02T03:x", &Iso8601::DEFAULT), |
| 780 |
Err(error::Parse::ParseFromDescription( |
| 781 |
error::ParseFromDescription::UnexpectedTrailingCharacters { .. } |
| 782 |
)) |
| 783 |
)); |
| 784 |
assert!(matches!( |
| 785 |
UtcDateTime::parse("2021-01-02T03:04x", &Iso8601::DEFAULT), |
| 786 |
Err(error::Parse::ParseFromDescription( |
| 787 |
error::ParseFromDescription::UnexpectedTrailingCharacters { .. } |
| 788 |
)) |
| 789 |
)); |
| 790 |
assert!(matches!( |
| 791 |
UtcDateTime::parse("2021-01-02T03:04:", &Iso8601::DEFAULT), |
| 792 |
Err(error::Parse::ParseFromDescription( |
| 793 |
error::ParseFromDescription::UnexpectedTrailingCharacters { .. } |
| 794 |
)) |
| 795 |
)); |
| 796 |
assert_eq!( |
| 797 |
UtcDateTime::parse("01:02", &Iso8601::DEFAULT), |
| 798 |
Err(error::Parse::TryFromParsed( |
| 799 |
error::TryFromParsed::InsufficientInformation |
| 800 |
)) |
| 801 |
); |
| 802 |
} |
| 803 |
|
| 804 |
#[test] |
| 805 |
fn parse_time() -> time::Result<()> { |
| 806 |
let format_input_output = [ |
| 807 |
(fd::parse("[hour repr:12] [period]")?, "01 PM", time!(1 PM)), |
| 808 |
(fd::parse("[hour]")?, "12", time!(12:00)), |
| 809 |
( |
| 810 |
fd::parse("[hour]:[minute]:[second]")?, |
| 811 |
"13:02:03", |
| 812 |
time!(13:02:03), |
| 813 |
), |
| 814 |
( |
| 815 |
fd::parse("[hour repr:12]:[minute] [period]")?, |
| 816 |
"01:02 PM", |
| 817 |
time!(1:02 PM), |
| 818 |
), |
| 819 |
(fd::parse("[hour]:[minute]")?, "01:02", time!(1:02)), |
| 820 |
( |
| 821 |
fd::parse("[hour repr:12]:[minute] [period]")?, |
| 822 |
"01:02 AM", |
| 823 |
time!(1:02 AM), |
| 824 |
), |
| 825 |
(fd::parse("[hour]:[minute]")?, "01:02", time!(1:02)), |
| 826 |
(fd::parse("[hour repr:12] [period]")?, "12 AM", time!(12 AM)), |
| 827 |
(fd::parse("[hour repr:12] [period]")?, "12 PM", time!(12 PM)), |
| 828 |
]; |
| 829 |
|
| 830 |
for (format_description, input, output) in &format_input_output { |
| 831 |
assert_eq!(&Time::parse(input, format_description)?, output); |
| 832 |
assert_eq!( |
| 833 |
&Time::parse(input, &OwnedFormatItem::from(format_description))?, |
| 834 |
output |
| 835 |
); |
| 836 |
assert_eq!( |
| 837 |
&Time::parse( |
| 838 |
input, |
| 839 |
[OwnedFormatItem::from(format_description)].as_slice() |
| 840 |
)?, |
| 841 |
output |
| 842 |
); |
| 843 |
} |
| 844 |
|
| 845 |
Ok(()) |
| 846 |
} |
| 847 |
|
| 848 |
#[expect(clippy::cognitive_complexity, reason = "all test the same thing")] |
| 849 |
#[test] |
| 850 |
fn parse_time_err() -> time::Result<()> { |
| 851 |
assert_eq!( |
| 852 |
Time::try_from(Parsed::new()), |
| 853 |
Err(error::TryFromParsed::InsufficientInformation) |
| 854 |
); |
| 855 |
assert_eq!( |
| 856 |
Time::parse("", &fd::parse("")?), |
| 857 |
Err(error::Parse::TryFromParsed( |
| 858 |
error::TryFromParsed::InsufficientInformation |
| 859 |
)) |
| 860 |
); |
| 861 |
assert_eq!( |
| 862 |
Time::parse("12:34", &fd::parse("[hour]:[second]")?), |
| 863 |
Err(error::Parse::TryFromParsed( |
| 864 |
error::TryFromParsed::InsufficientInformation |
| 865 |
)) |
| 866 |
); |
| 867 |
assert_eq!( |
| 868 |
Time::parse("12:34", &fd::parse("[hour]:[subsecond]")?), |
| 869 |
Err(error::Parse::TryFromParsed( |
| 870 |
error::TryFromParsed::InsufficientInformation |
| 871 |
)) |
| 872 |
); |
| 873 |
assert!(matches!( |
| 874 |
Time::parse("13 PM", &fd::parse("[hour repr:12] [period]")?), |
| 875 |
Err(error::Parse::ParseFromDescription( |
| 876 |
error::ParseFromDescription::InvalidComponent("hour") |
| 877 |
)) |
| 878 |
)); |
| 879 |
assert!(matches!( |
| 880 |
Time::parse(" ", &fd::parse("")?), |
| 881 |
Err(error::Parse::ParseFromDescription( |
| 882 |
error::ParseFromDescription::UnexpectedTrailingCharacters { .. } |
| 883 |
)) |
| 884 |
)); |
| 885 |
assert!(matches!( |
| 886 |
Time::parse("a", &fd::parse("[subsecond digits:1]")?), |
| 887 |
Err(error::Parse::ParseFromDescription( |
| 888 |
error::ParseFromDescription::InvalidComponent("subsecond") |
| 889 |
)) |
| 890 |
)); |
| 891 |
assert!(matches!( |
| 892 |
Time::parse("1a", &fd::parse("[subsecond digits:2]")?), |
| 893 |
Err(error::Parse::ParseFromDescription( |
| 894 |
error::ParseFromDescription::InvalidComponent("subsecond") |
| 895 |
)) |
| 896 |
)); |
| 897 |
assert!(matches!( |
| 898 |
Time::parse("1a", &fd::parse_owned::<2>("[subsecond digits:2]")?), |
| 899 |
Err(error::Parse::ParseFromDescription( |
| 900 |
error::ParseFromDescription::InvalidComponent("subsecond") |
| 901 |
)) |
| 902 |
)); |
| 903 |
assert!(matches!( |
| 904 |
Time::parse( |
| 905 |
"1a", |
| 906 |
[fd::parse_owned::<2>("[subsecond digits:2]")?].as_slice() |
| 907 |
), |
| 908 |
Err(error::Parse::ParseFromDescription( |
| 909 |
error::ParseFromDescription::InvalidComponent("subsecond") |
| 910 |
)) |
| 911 |
)); |
| 912 |
assert!(matches!( |
| 913 |
Time::parse("12a", &fd::parse("[subsecond digits:3]")?), |
| 914 |
Err(error::Parse::ParseFromDescription( |
| 915 |
error::ParseFromDescription::InvalidComponent("subsecond") |
| 916 |
)) |
| 917 |
)); |
| 918 |
assert!(matches!( |
| 919 |
Time::parse("123a", &fd::parse("[subsecond digits:4]")?), |
| 920 |
Err(error::Parse::ParseFromDescription( |
| 921 |
error::ParseFromDescription::InvalidComponent("subsecond") |
| 922 |
)) |
| 923 |
)); |
| 924 |
assert!(matches!( |
| 925 |
Time::parse("1234a", &fd::parse("[subsecond digits:5]")?), |
| 926 |
Err(error::Parse::ParseFromDescription( |
| 927 |
error::ParseFromDescription::InvalidComponent("subsecond") |
| 928 |
)) |
| 929 |
)); |
| 930 |
assert!(matches!( |
| 931 |
Time::parse("12345a", &fd::parse("[subsecond digits:6]")?), |
| 932 |
Err(error::Parse::ParseFromDescription( |
| 933 |
error::ParseFromDescription::InvalidComponent("subsecond") |
| 934 |
)) |
| 935 |
)); |
| 936 |
assert!(matches!( |
| 937 |
Time::parse("123456a", &fd::parse("[subsecond digits:7]")?), |
| 938 |
Err(error::Parse::ParseFromDescription( |
| 939 |
error::ParseFromDescription::InvalidComponent("subsecond") |
| 940 |
)) |
| 941 |
)); |
| 942 |
assert!(matches!( |
| 943 |
Time::parse("1234567a", &fd::parse("[subsecond digits:8]")?), |
| 944 |
Err(error::Parse::ParseFromDescription( |
| 945 |
error::ParseFromDescription::InvalidComponent("subsecond") |
| 946 |
)) |
| 947 |
)); |
| 948 |
assert!(matches!( |
| 949 |
Time::parse("12345678a", &fd::parse("[subsecond digits:9]")?), |
| 950 |
Err(error::Parse::ParseFromDescription( |
| 951 |
error::ParseFromDescription::InvalidComponent("subsecond") |
| 952 |
)) |
| 953 |
)); |
| 954 |
|
| 955 |
Ok(()) |
| 956 |
} |
| 957 |
|
| 958 |
#[test] |
| 959 |
fn parse_date() -> time::Result<()> { |
| 960 |
let format_input_output = [ |
| 961 |
( |
| 962 |
fd::parse("[year]-[month]-[day]")?, |
| 963 |
"2021-01-02", |
| 964 |
date!(2021-01-02), |
| 965 |
), |
| 966 |
( |
| 967 |
fd::parse("[year repr:century range:standard][year repr:last_two]-[month]-[day]")?, |
| 968 |
"2021-01-02", |
| 969 |
date!(2021-01-02), |
| 970 |
), |
| 971 |
(fd::parse("[year]-[ordinal]")?, "2021-002", date!(2021-002)), |
| 972 |
( |
| 973 |
fd::parse("[year base:iso_week]-W[week_number]-[weekday repr:monday]")?, |
| 974 |
"2020-W53-6", |
| 975 |
date!(2021-01-02), |
| 976 |
), |
| 977 |
( |
| 978 |
fd::parse("[year]-W[week_number repr:monday]-[weekday repr:monday]")?, |
| 979 |
"2021-W00-6", |
| 980 |
date!(2021-01-02), |
| 981 |
), |
| 982 |
( |
| 983 |
fd::parse("[year]-W[week_number repr:sunday]-[weekday repr:sunday]")?, |
| 984 |
"2021-W00-6", |
| 985 |
date!(2021-01-02), |
| 986 |
), |
| 987 |
( |
| 988 |
fd::parse("[year]-W[week_number repr:sunday]-[weekday repr:sunday]")?, |
| 989 |
"2023-W01-1", |
| 990 |
date!(2023-01-02), |
| 991 |
), |
| 992 |
( |
| 993 |
fd::parse("[year]-W[week_number repr:sunday]-[weekday repr:sunday]")?, |
| 994 |
"2022-W00-7", |
| 995 |
date!(2022-01-02), |
| 996 |
), |
| 997 |
( |
| 998 |
fd::parse("[year]-W[week_number repr:sunday]-[weekday repr:sunday]")?, |
| 999 |
"2026-W00-5", |
| 1000 |
date!(2026-01-02), |
| 1001 |
), |
| 1002 |
( |
| 1003 |
fd::parse("[year]-W[week_number repr:sunday]-[weekday repr:sunday]")?, |
| 1004 |
"2025-W00-4", |
| 1005 |
date!(2025-01-02), |
| 1006 |
), |
| 1007 |
( |
| 1008 |
fd::parse("[year]-W[week_number repr:sunday]-[weekday repr:sunday]")?, |
| 1009 |
"2019-W00-3", |
| 1010 |
date!(2019-01-02), |
| 1011 |
), |
| 1012 |
( |
| 1013 |
fd::parse("[year]-W[week_number repr:sunday]-[weekday repr:sunday]")?, |
| 1014 |
"2018-W01-2", |
| 1015 |
date!(2018-01-02), |
| 1016 |
), |
| 1017 |
( |
| 1018 |
fd::parse( |
| 1019 |
"[year padding:space]-W[week_number repr:sunday padding:none]-[weekday \ |
| 1020 |
repr:sunday]", |
| 1021 |
)?, |
| 1022 |
" 201-W01-2", |
| 1023 |
date!(201-01-06), |
| 1024 |
), |
| 1025 |
]; |
| 1026 |
|
| 1027 |
for (format_description, input, output) in &format_input_output { |
| 1028 |
assert_eq!(&Date::parse(input, format_description)?, output); |
| 1029 |
assert_eq!( |
| 1030 |
&Date::parse(input, &OwnedFormatItem::from(format_description))?, |
| 1031 |
output |
| 1032 |
); |
| 1033 |
} |
| 1034 |
|
| 1035 |
Ok(()) |
| 1036 |
} |
| 1037 |
|
| 1038 |
#[expect(clippy::cognitive_complexity, reason = "all test the same thing")] |
| 1039 |
#[test] |
| 1040 |
fn parse_date_err() -> time::Result<()> { |
| 1041 |
assert_eq!( |
| 1042 |
Date::try_from(Parsed::new()), |
| 1043 |
Err(error::TryFromParsed::InsufficientInformation) |
| 1044 |
); |
| 1045 |
assert_eq!( |
| 1046 |
Date::parse("", &fd::parse("")?), |
| 1047 |
Err(error::Parse::TryFromParsed( |
| 1048 |
error::TryFromParsed::InsufficientInformation |
| 1049 |
)) |
| 1050 |
); |
| 1051 |
assert!(matches!( |
| 1052 |
Date::parse("a", &fd::parse("[year]")?), |
| 1053 |
Err(error::Parse::ParseFromDescription( |
| 1054 |
error::ParseFromDescription::InvalidComponent("year") |
| 1055 |
)) |
| 1056 |
)); |
| 1057 |
assert!(matches!( |
| 1058 |
Date::parse("0001", &fd::parse("[year sign:mandatory]")?), |
| 1059 |
Err(error::Parse::ParseFromDescription( |
| 1060 |
error::ParseFromDescription::InvalidComponent("year") |
| 1061 |
)) |
| 1062 |
)); |
| 1063 |
assert!(matches!( |
| 1064 |
Date::parse("0a", &fd::parse("[year repr:last_two]")?), |
| 1065 |
Err(error::Parse::ParseFromDescription( |
| 1066 |
error::ParseFromDescription::InvalidComponent("year") |
| 1067 |
)) |
| 1068 |
)); |
| 1069 |
assert!(matches!( |
| 1070 |
Date::parse("2021-366", &fd::parse("[year]-[ordinal]")?), |
| 1071 |
Err(error::Parse::TryFromParsed( |
| 1072 |
error::TryFromParsed::ComponentRange(component) |
| 1073 |
)) if component.name() == "ordinal" |
| 1074 |
)); |
| 1075 |
assert!(matches!( |
| 1076 |
Date::parse("2021-12-32", &fd::parse("[year]-[month]-[day]")?), |
| 1077 |
Err(error::Parse::ParseFromDescription( |
| 1078 |
error::ParseFromDescription::InvalidComponent("day") |
| 1079 |
)) |
| 1080 |
)); |
| 1081 |
assert!(matches!( |
| 1082 |
Date::parse("2021-02-30", &fd::parse("[year]-[month]-[day]")?), |
| 1083 |
Err(error::Parse::TryFromParsed( |
| 1084 |
error::TryFromParsed::ComponentRange(component) |
| 1085 |
)) if component.name() == "day" |
| 1086 |
)); |
| 1087 |
assert!(matches!( |
| 1088 |
Date::parse("2019-W53-1", &fd::parse("[year base:iso_week]-W[week_number]-[weekday repr:monday]")?), |
| 1089 |
Err(error::Parse::TryFromParsed( |
| 1090 |
error::TryFromParsed::ComponentRange(component) |
| 1091 |
)) if component.name() == "week" |
| 1092 |
)); |
| 1093 |
assert!(matches!( |
| 1094 |
Date::parse( |
| 1095 |
"2021-W54-1", |
| 1096 |
&fd::parse("[year base:iso_week]-W[week_number]-[weekday repr:monday]")? |
| 1097 |
), |
| 1098 |
Err(error::Parse::ParseFromDescription( |
| 1099 |
error::ParseFromDescription::InvalidComponent("week number") |
| 1100 |
)) |
| 1101 |
)); |
| 1102 |
assert!(matches!( |
| 1103 |
Date::parse("2019-W53-1", &fd::parse("[year]-W[week_number repr:sunday]-[weekday repr:monday]")?), |
| 1104 |
Err(error::Parse::TryFromParsed( |
| 1105 |
error::TryFromParsed::ComponentRange(component) |
| 1106 |
)) if component.name() == "ordinal" |
| 1107 |
)); |
| 1108 |
assert!(matches!( |
| 1109 |
Date::parse( |
| 1110 |
"2021-W54-1", |
| 1111 |
&fd::parse("[year]-W[week_number repr:sunday]-[weekday repr:monday]")? |
| 1112 |
), |
| 1113 |
Err(error::Parse::ParseFromDescription( |
| 1114 |
error::ParseFromDescription::InvalidComponent("week number") |
| 1115 |
)) |
| 1116 |
)); |
| 1117 |
assert!(matches!( |
| 1118 |
Date::parse("2019-W53-1", &fd::parse("[year]-W[week_number repr:monday]-[weekday repr:monday]")?), |
| 1119 |
Err(error::Parse::TryFromParsed( |
| 1120 |
error::TryFromParsed::ComponentRange(component) |
| 1121 |
)) if component.name() == "ordinal" |
| 1122 |
)); |
| 1123 |
assert!(matches!( |
| 1124 |
Date::parse( |
| 1125 |
"2021-W54-1", |
| 1126 |
&fd::parse("[year]-W[week_number repr:monday]-[weekday repr:monday]")? |
| 1127 |
), |
| 1128 |
Err(error::Parse::ParseFromDescription( |
| 1129 |
error::ParseFromDescription::InvalidComponent("week number") |
| 1130 |
)) |
| 1131 |
)); |
| 1132 |
assert!(matches!( |
| 1133 |
Date::parse("Ja", &fd::parse("[month repr:short]")?), |
| 1134 |
Err(error::Parse::ParseFromDescription( |
| 1135 |
error::ParseFromDescription::InvalidComponent("month") |
| 1136 |
)) |
| 1137 |
)); |
| 1138 |
assert!(matches!( |
| 1139 |
Date::parse(" 2a21", &fd::parse("[year padding:space]")?), |
| 1140 |
Err(error::Parse::ParseFromDescription( |
| 1141 |
error::ParseFromDescription::InvalidComponent("year") |
| 1142 |
)) |
| 1143 |
)); |
| 1144 |
|
| 1145 |
Ok(()) |
| 1146 |
} |
| 1147 |
|
| 1148 |
#[test] |
| 1149 |
fn parse_offset() -> time::Result<()> { |
| 1150 |
// Regression check for #522. |
| 1151 |
assert_eq!( |
| 1152 |
UtcOffset::parse( |
| 1153 |
"-00:01", |
| 1154 |
&fd::parse("[offset_hour sign:mandatory]:[offset_minute]")?, |
| 1155 |
), |
| 1156 |
Ok(offset!(-00:01)), |
| 1157 |
); |
| 1158 |
assert_eq!( |
| 1159 |
UtcOffset::parse( |
| 1160 |
"-00:00:01", |
| 1161 |
&fd::parse("[offset_hour sign:mandatory]:[offset_minute]:[offset_second]")?, |
| 1162 |
), |
| 1163 |
Ok(offset!(-00:00:01)), |
| 1164 |
); |
| 1165 |
|
| 1166 |
Ok(()) |
| 1167 |
} |
| 1168 |
|
| 1169 |
#[test] |
| 1170 |
fn parse_offset_err() -> time::Result<()> { |
| 1171 |
assert_eq!( |
| 1172 |
UtcOffset::parse("", &fd::parse("")?), |
| 1173 |
Err(error::Parse::TryFromParsed( |
| 1174 |
error::TryFromParsed::InsufficientInformation |
| 1175 |
)) |
| 1176 |
); |
| 1177 |
assert_eq!( |
| 1178 |
UtcOffset::parse("01", &fd::parse("[offset_hour sign:mandatory]")?), |
| 1179 |
Err(error::Parse::ParseFromDescription( |
| 1180 |
error::ParseFromDescription::InvalidComponent("offset hour") |
| 1181 |
)) |
| 1182 |
); |
| 1183 |
assert!(matches!( |
| 1184 |
UtcOffset::parse("24", &fd::parse("[offset_hour]")?), |
| 1185 |
Err(error::Parse::ParseFromDescription( |
| 1186 |
error::ParseFromDescription::InvalidComponent("offset hour") |
| 1187 |
)) |
| 1188 |
)); |
| 1189 |
assert!(matches!( |
| 1190 |
UtcOffset::parse("00:60", &fd::parse("[offset_hour]:[offset_minute]")?), |
| 1191 |
Err(error::Parse::ParseFromDescription( |
| 1192 |
error::ParseFromDescription::InvalidComponent("offset minute") |
| 1193 |
)) |
| 1194 |
)); |
| 1195 |
assert!(matches!( |
| 1196 |
UtcOffset::parse( |
| 1197 |
"00:00:60", |
| 1198 |
&fd::parse("[offset_hour]:[offset_minute]:[offset_second]")? |
| 1199 |
), |
| 1200 |
Err(error::Parse::ParseFromDescription( |
| 1201 |
error::ParseFromDescription::InvalidComponent("offset second") |
| 1202 |
)) |
| 1203 |
)); |
| 1204 |
|
| 1205 |
Ok(()) |
| 1206 |
} |
| 1207 |
|
| 1208 |
#[test] |
| 1209 |
fn parse_primitive_date_time() -> time::Result<()> { |
| 1210 |
assert_eq!( |
| 1211 |
PrimitiveDateTime::parse("2023-07-27 23", &fd::parse("[year]-[month]-[day] [hour]")?), |
| 1212 |
Ok(datetime!(2023-07-27 23:00)) |
| 1213 |
); |
| 1214 |
|
| 1215 |
Ok(()) |
| 1216 |
} |
| 1217 |
|
| 1218 |
#[test] |
| 1219 |
fn parse_primitive_date_time_err() -> time::Result<()> { |
| 1220 |
assert_eq!( |
| 1221 |
PrimitiveDateTime::parse("", &fd::parse("")?), |
| 1222 |
Err(error::Parse::TryFromParsed( |
| 1223 |
error::TryFromParsed::InsufficientInformation |
| 1224 |
)) |
| 1225 |
); |
| 1226 |
assert!(matches!( |
| 1227 |
PrimitiveDateTime::parse( |
| 1228 |
"2021-001 13 PM", |
| 1229 |
&fd::parse("[year]-[ordinal] [hour repr:12] [period]")? |
| 1230 |
), |
| 1231 |
Err(error::Parse::ParseFromDescription( |
| 1232 |
error::ParseFromDescription::InvalidComponent("hour") |
| 1233 |
)) |
| 1234 |
)); |
| 1235 |
assert!(matches!( |
| 1236 |
PrimitiveDateTime::parse( |
| 1237 |
"2023-07-27 23:30", |
| 1238 |
&fd::parse("[year]-[month]-[day] [hour]")? |
| 1239 |
), |
| 1240 |
Err(error::Parse::ParseFromDescription( |
| 1241 |
error::ParseFromDescription::UnexpectedTrailingCharacters { .. } |
| 1242 |
)) |
| 1243 |
)); |
| 1244 |
|
| 1245 |
Ok(()) |
| 1246 |
} |
| 1247 |
|
| 1248 |
#[test] |
| 1249 |
fn parse_offset_date_time_err() -> time::Result<()> { |
| 1250 |
assert_eq!( |
| 1251 |
OffsetDateTime::parse("", &fd::parse("")?), |
| 1252 |
Err(error::Parse::TryFromParsed( |
| 1253 |
error::TryFromParsed::InsufficientInformation |
| 1254 |
)) |
| 1255 |
); |
| 1256 |
assert!(matches!( |
| 1257 |
OffsetDateTime::parse("x", &fd::parse("[year]")?), |
| 1258 |
Err(error::Parse::ParseFromDescription( |
| 1259 |
error::ParseFromDescription::InvalidComponent("year") |
| 1260 |
)) |
| 1261 |
)); |
| 1262 |
assert!(matches!( |
| 1263 |
OffsetDateTime::parse( |
| 1264 |
"2021-001 12 PM +25", |
| 1265 |
&fd::parse("[year]-[ordinal] [hour repr:12] [period] [offset_hour sign:mandatory]")? |
| 1266 |
), |
| 1267 |
Err(error::Parse::ParseFromDescription( |
| 1268 |
error::ParseFromDescription::InvalidComponent("offset hour") |
| 1269 |
)) |
| 1270 |
)); |
| 1271 |
|
| 1272 |
Ok(()) |
| 1273 |
} |
| 1274 |
|
| 1275 |
#[test] |
| 1276 |
fn parse_utc_date_time() -> time::Result<()> { |
| 1277 |
assert_eq!( |
| 1278 |
UtcDateTime::parse("2023-07-27 23", &fd::parse("[year]-[month]-[day] [hour]")?), |
| 1279 |
Ok(utc_datetime!(2023-07-27 23:00)) |
| 1280 |
); |
| 1281 |
|
| 1282 |
Ok(()) |
| 1283 |
} |
| 1284 |
|
| 1285 |
#[test] |
| 1286 |
fn parse_utc_date_time_err() -> time::Result<()> { |
| 1287 |
assert_eq!( |
| 1288 |
UtcDateTime::parse("", &fd::parse("")?), |
| 1289 |
Err(error::Parse::TryFromParsed( |
| 1290 |
error::TryFromParsed::InsufficientInformation |
| 1291 |
)) |
| 1292 |
); |
| 1293 |
assert!(matches!( |
| 1294 |
UtcDateTime::parse( |
| 1295 |
"2021-001 13 PM", |
| 1296 |
&fd::parse("[year]-[ordinal] [hour repr:12] [period]")? |
| 1297 |
), |
| 1298 |
Err(error::Parse::ParseFromDescription( |
| 1299 |
error::ParseFromDescription::InvalidComponent("hour") |
| 1300 |
)) |
| 1301 |
)); |
| 1302 |
assert!(matches!( |
| 1303 |
UtcDateTime::parse( |
| 1304 |
"2023-07-27 23:30", |
| 1305 |
&fd::parse("[year]-[month]-[day] [hour]")? |
| 1306 |
), |
| 1307 |
Err(error::Parse::ParseFromDescription( |
| 1308 |
error::ParseFromDescription::UnexpectedTrailingCharacters { .. } |
| 1309 |
)) |
| 1310 |
)); |
| 1311 |
assert!(matches!( |
| 1312 |
UtcDateTime::parse("x", &fd::parse("[year]")?), |
| 1313 |
Err(error::Parse::ParseFromDescription( |
| 1314 |
error::ParseFromDescription::InvalidComponent("year") |
| 1315 |
)) |
| 1316 |
)); |
| 1317 |
assert!(matches!( |
| 1318 |
UtcDateTime::parse( |
| 1319 |
"2021-001 12 PM +25", |
| 1320 |
&fd::parse("[year]-[ordinal] [hour repr:12] [period] [offset_hour sign:mandatory]")? |
| 1321 |
), |
| 1322 |
Err(error::Parse::ParseFromDescription( |
| 1323 |
error::ParseFromDescription::InvalidComponent("offset hour") |
| 1324 |
)) |
| 1325 |
)); |
| 1326 |
|
| 1327 |
Ok(()) |
| 1328 |
} |
| 1329 |
|
| 1330 |
#[expect(clippy::cognitive_complexity, reason = "all test the same thing")] |
| 1331 |
#[test] |
| 1332 |
fn parse_components() -> time::Result<()> { |
| 1333 |
macro_rules! parse_component { |
| 1334 |
($component:expr, $input:expr, $(_. $property:ident() == $expected:expr);+ $(;)?) => { |
| 1335 |
let mut parsed = Parsed::new(); |
| 1336 |
parsed.parse_component($input, $component)?; |
| 1337 |
$(assert_eq!(parsed.$property(), $expected);)+ |
| 1338 |
}; |
| 1339 |
} |
| 1340 |
|
| 1341 |
parse_component!( |
| 1342 |
Component::Year( |
| 1343 |
modifier::Year::default() |
| 1344 |
.with_padding(modifier::Padding::Zero) |
| 1345 |
.with_repr(modifier::YearRepr::Full) |
| 1346 |
.with_range(modifier::YearRange::Extended) |
| 1347 |
.with_iso_week_based(false) |
| 1348 |
.with_sign_is_mandatory(false) |
| 1349 |
), |
| 1350 |
b"2021", |
| 1351 |
_.year() == Some(2021) |
| 1352 |
); |
| 1353 |
parse_component!( |
| 1354 |
Component::Year(modifier::Year::default() |
| 1355 |
.with_padding(modifier::Padding::Zero) |
| 1356 |
.with_repr(modifier::YearRepr::Century) |
| 1357 |
.with_range(modifier::YearRange::Extended) |
| 1358 |
.with_iso_week_based(false) |
| 1359 |
.with_sign_is_mandatory(false) |
| 1360 |
), |
| 1361 |
b"20", |
| 1362 |
_.year_century() == Some(20); |
| 1363 |
_.year_century_is_negative() == Some(false); |
| 1364 |
); |
| 1365 |
parse_component!( |
| 1366 |
Component::Year( |
| 1367 |
modifier::Year::default() |
| 1368 |
.with_padding(modifier::Padding::Zero) |
| 1369 |
.with_repr(modifier::YearRepr::LastTwo) |
| 1370 |
.with_range(modifier::YearRange::Extended) |
| 1371 |
.with_iso_week_based(false) |
| 1372 |
.with_sign_is_mandatory(false) |
| 1373 |
), |
| 1374 |
b"21", |
| 1375 |
_.year_last_two() == Some(21) |
| 1376 |
); |
| 1377 |
parse_component!( |
| 1378 |
Component::Year( |
| 1379 |
modifier::Year::default() |
| 1380 |
.with_padding(modifier::Padding::Zero) |
| 1381 |
.with_repr(modifier::YearRepr::Full) |
| 1382 |
.with_range(modifier::YearRange::Extended) |
| 1383 |
.with_iso_week_based(true) |
| 1384 |
.with_sign_is_mandatory(false) |
| 1385 |
), |
| 1386 |
b"2021", |
| 1387 |
_.iso_year() == Some(2021) |
| 1388 |
); |
| 1389 |
parse_component!( |
| 1390 |
Component::Year(modifier::Year::default() |
| 1391 |
.with_padding(modifier::Padding::Zero) |
| 1392 |
.with_repr(modifier::YearRepr::Century) |
| 1393 |
.with_range(modifier::YearRange::Extended) |
| 1394 |
.with_iso_week_based(true) |
| 1395 |
.with_sign_is_mandatory(false) |
| 1396 |
), |
| 1397 |
b"20", |
| 1398 |
_.iso_year_century() == Some(20); |
| 1399 |
_.iso_year_century_is_negative() == Some(false); |
| 1400 |
); |
| 1401 |
parse_component!( |
| 1402 |
Component::Year( |
| 1403 |
modifier::Year::default() |
| 1404 |
.with_padding(modifier::Padding::Zero) |
| 1405 |
.with_repr(modifier::YearRepr::LastTwo) |
| 1406 |
.with_range(modifier::YearRange::Extended) |
| 1407 |
.with_iso_week_based(true) |
| 1408 |
.with_sign_is_mandatory(false) |
| 1409 |
), |
| 1410 |
b"21", |
| 1411 |
_.iso_year_last_two() == Some(21) |
| 1412 |
); |
| 1413 |
parse_component!( |
| 1414 |
Component::Month( |
| 1415 |
modifier::Month::default() |
| 1416 |
.with_padding(modifier::Padding::Space) |
| 1417 |
.with_repr(modifier::MonthRepr::Numerical) |
| 1418 |
), |
| 1419 |
b" 1", |
| 1420 |
_.month() == Some(Month::January) |
| 1421 |
); |
| 1422 |
parse_component!( |
| 1423 |
Component::Month( |
| 1424 |
modifier::Month::default() |
| 1425 |
.with_padding(modifier::Padding::None) |
| 1426 |
.with_repr(modifier::MonthRepr::Short) |
| 1427 |
.with_case_sensitive(true) |
| 1428 |
), |
| 1429 |
b"Jan", |
| 1430 |
_.month() == Some(Month::January) |
| 1431 |
); |
| 1432 |
parse_component!( |
| 1433 |
Component::Month( |
| 1434 |
modifier::Month::default() |
| 1435 |
.with_padding(modifier::Padding::None) |
| 1436 |
.with_repr(modifier::MonthRepr::Short) |
| 1437 |
.with_case_sensitive(false) |
| 1438 |
), |
| 1439 |
b"jAn", |
| 1440 |
_.month() == Some(Month::January) |
| 1441 |
); |
| 1442 |
parse_component!( |
| 1443 |
Component::Month( |
| 1444 |
modifier::Month::default() |
| 1445 |
.with_padding(modifier::Padding::None) |
| 1446 |
.with_repr(modifier::MonthRepr::Long) |
| 1447 |
.with_case_sensitive(true) |
| 1448 |
), |
| 1449 |
b"January", |
| 1450 |
_.month() == Some(Month::January) |
| 1451 |
); |
| 1452 |
parse_component!( |
| 1453 |
Component::Month( |
| 1454 |
modifier::Month::default() |
| 1455 |
.with_padding(modifier::Padding::None) |
| 1456 |
.with_repr(modifier::MonthRepr::Long) |
| 1457 |
.with_case_sensitive(false) |
| 1458 |
), |
| 1459 |
b"jAnUaRy", |
| 1460 |
_.month() == Some(Month::January) |
| 1461 |
); |
| 1462 |
parse_component!( |
| 1463 |
Component::Ordinal(modifier::Ordinal::default().with_padding(modifier::Padding::Zero)), |
| 1464 |
b"012", |
| 1465 |
_.ordinal() == 12.try_into().ok() |
| 1466 |
); |
| 1467 |
parse_component!( |
| 1468 |
Component::Weekday( |
| 1469 |
modifier::Weekday::default() |
| 1470 |
.with_repr(modifier::WeekdayRepr::Short) |
| 1471 |
.with_one_indexed(false) |
| 1472 |
.with_case_sensitive(true) |
| 1473 |
), |
| 1474 |
b"Sun", |
| 1475 |
_.weekday() == Some(Weekday::Sunday) |
| 1476 |
); |
| 1477 |
parse_component!( |
| 1478 |
Component::Weekday( |
| 1479 |
modifier::Weekday::default() |
| 1480 |
.with_repr(modifier::WeekdayRepr::Short) |
| 1481 |
.with_one_indexed(false) |
| 1482 |
.with_case_sensitive(false) |
| 1483 |
), |
| 1484 |
b"sUn", |
| 1485 |
_.weekday() == Some(Weekday::Sunday) |
| 1486 |
); |
| 1487 |
parse_component!( |
| 1488 |
Component::Weekday( |
| 1489 |
modifier::Weekday::default() |
| 1490 |
.with_repr(modifier::WeekdayRepr::Long) |
| 1491 |
.with_one_indexed(false) |
| 1492 |
.with_case_sensitive(true) |
| 1493 |
), |
| 1494 |
b"Sunday", |
| 1495 |
_.weekday() == Some(Weekday::Sunday) |
| 1496 |
); |
| 1497 |
parse_component!( |
| 1498 |
Component::Weekday( |
| 1499 |
modifier::Weekday::default() |
| 1500 |
.with_repr(modifier::WeekdayRepr::Long) |
| 1501 |
.with_one_indexed(false) |
| 1502 |
.with_case_sensitive(false) |
| 1503 |
), |
| 1504 |
b"sUnDaY", |
| 1505 |
_.weekday() == Some(Weekday::Sunday) |
| 1506 |
); |
| 1507 |
parse_component!( |
| 1508 |
Component::Weekday( |
| 1509 |
modifier::Weekday::default() |
| 1510 |
.with_repr(modifier::WeekdayRepr::Sunday) |
| 1511 |
.with_one_indexed(false) |
| 1512 |
), |
| 1513 |
b"0", |
| 1514 |
_.weekday() == Some(Weekday::Sunday) |
| 1515 |
); |
| 1516 |
parse_component!( |
| 1517 |
Component::Weekday( |
| 1518 |
modifier::Weekday::default() |
| 1519 |
.with_repr(modifier::WeekdayRepr::Sunday) |
| 1520 |
.with_one_indexed(true) |
| 1521 |
), |
| 1522 |
b"1", |
| 1523 |
_.weekday() == Some(Weekday::Sunday) |
| 1524 |
); |
| 1525 |
parse_component!( |
| 1526 |
Component::Weekday( |
| 1527 |
modifier::Weekday::default() |
| 1528 |
.with_repr(modifier::WeekdayRepr::Monday) |
| 1529 |
.with_one_indexed(false) |
| 1530 |
), |
| 1531 |
b"6", |
| 1532 |
_.weekday() == Some(Weekday::Sunday) |
| 1533 |
); |
| 1534 |
parse_component!( |
| 1535 |
Component::Weekday( |
| 1536 |
modifier::Weekday::default() |
| 1537 |
.with_repr(modifier::WeekdayRepr::Monday) |
| 1538 |
.with_one_indexed(true) |
| 1539 |
), |
| 1540 |
b"7", |
| 1541 |
_.weekday() == Some(Weekday::Sunday) |
| 1542 |
); |
| 1543 |
parse_component!( |
| 1544 |
Component::WeekNumber( |
| 1545 |
modifier::WeekNumber::default() |
| 1546 |
.with_padding(modifier::Padding::None) |
| 1547 |
.with_repr(modifier::WeekNumberRepr::Sunday) |
| 1548 |
), |
| 1549 |
b"2", |
| 1550 |
_.sunday_week_number() == Some(2) |
| 1551 |
); |
| 1552 |
parse_component!( |
| 1553 |
Component::WeekNumber( |
| 1554 |
modifier::WeekNumber::default() |
| 1555 |
.with_padding(modifier::Padding::None) |
| 1556 |
.with_repr(modifier::WeekNumberRepr::Monday) |
| 1557 |
), |
| 1558 |
b"2", |
| 1559 |
_.monday_week_number() == Some(2) |
| 1560 |
); |
| 1561 |
parse_component!( |
| 1562 |
Component::WeekNumber( |
| 1563 |
modifier::WeekNumber::default() |
| 1564 |
.with_padding(modifier::Padding::None) |
| 1565 |
.with_repr(modifier::WeekNumberRepr::Iso) |
| 1566 |
), |
| 1567 |
b"2", |
| 1568 |
_.iso_week_number() == 2.try_into().ok() |
| 1569 |
); |
| 1570 |
parse_component!( |
| 1571 |
Component::Subsecond( |
| 1572 |
modifier::Subsecond::default().with_digits(modifier::SubsecondDigits::One) |
| 1573 |
), |
| 1574 |
b"1", |
| 1575 |
_.subsecond() == Some(100_000_000) |
| 1576 |
); |
| 1577 |
parse_component!( |
| 1578 |
Component::Subsecond( |
| 1579 |
modifier::Subsecond::default().with_digits(modifier::SubsecondDigits::Two) |
| 1580 |
), |
| 1581 |
b"12", |
| 1582 |
_.subsecond() == Some(120_000_000) |
| 1583 |
); |
| 1584 |
parse_component!( |
| 1585 |
Component::Subsecond( |
| 1586 |
modifier::Subsecond::default().with_digits(modifier::SubsecondDigits::Three) |
| 1587 |
), |
| 1588 |
b"123", |
| 1589 |
_.subsecond() == Some(123_000_000) |
| 1590 |
); |
| 1591 |
parse_component!( |
| 1592 |
Component::Subsecond( |
| 1593 |
modifier::Subsecond::default().with_digits(modifier::SubsecondDigits::Four) |
| 1594 |
), |
| 1595 |
b"1234", |
| 1596 |
_.subsecond() == Some(123_400_000) |
| 1597 |
); |
| 1598 |
parse_component!( |
| 1599 |
Component::Subsecond( |
| 1600 |
modifier::Subsecond::default().with_digits(modifier::SubsecondDigits::Five) |
| 1601 |
), |
| 1602 |
b"12345", |
| 1603 |
_.subsecond() == Some(123_450_000) |
| 1604 |
); |
| 1605 |
parse_component!( |
| 1606 |
Component::Subsecond( |
| 1607 |
modifier::Subsecond::default().with_digits(modifier::SubsecondDigits::Six) |
| 1608 |
), |
| 1609 |
b"123456", |
| 1610 |
_.subsecond() == Some(123_456_000) |
| 1611 |
); |
| 1612 |
parse_component!( |
| 1613 |
Component::Subsecond( |
| 1614 |
modifier::Subsecond::default().with_digits(modifier::SubsecondDigits::Seven) |
| 1615 |
), |
| 1616 |
b"1234567", |
| 1617 |
_.subsecond() == Some(123_456_700) |
| 1618 |
); |
| 1619 |
parse_component!( |
| 1620 |
Component::Subsecond( |
| 1621 |
modifier::Subsecond::default().with_digits(modifier::SubsecondDigits::Eight) |
| 1622 |
), |
| 1623 |
b"12345678", |
| 1624 |
_.subsecond() == Some(123_456_780) |
| 1625 |
); |
| 1626 |
parse_component!( |
| 1627 |
Component::Subsecond( |
| 1628 |
modifier::Subsecond::default().with_digits(modifier::SubsecondDigits::Nine) |
| 1629 |
), |
| 1630 |
b"123456789", |
| 1631 |
_.subsecond() == Some(123_456_789) |
| 1632 |
); |
| 1633 |
parse_component!( |
| 1634 |
Component::Subsecond( |
| 1635 |
modifier::Subsecond::default().with_digits(modifier::SubsecondDigits::OneOrMore) |
| 1636 |
), |
| 1637 |
b"123456789", |
| 1638 |
_.subsecond() == Some(123_456_789) |
| 1639 |
); |
| 1640 |
parse_component!( |
| 1641 |
Component::Period( |
| 1642 |
modifier::Period::default() |
| 1643 |
.with_is_uppercase(false) |
| 1644 |
.with_case_sensitive(true) |
| 1645 |
), |
| 1646 |
b"am", |
| 1647 |
_.hour_12_is_pm() == Some(false) |
| 1648 |
); |
| 1649 |
parse_component!( |
| 1650 |
Component::Period( |
| 1651 |
modifier::Period::default() |
| 1652 |
.with_is_uppercase(false) |
| 1653 |
.with_case_sensitive(false) |
| 1654 |
), |
| 1655 |
b"aM", |
| 1656 |
_.hour_12_is_pm() == Some(false) |
| 1657 |
); |
| 1658 |
let mut parsed = Parsed::new(); |
| 1659 |
let result = parsed.parse_component( |
| 1660 |
b"abcdef", |
| 1661 |
Component::Ignore(modifier::Ignore::count(const { NonZero::new(3).unwrap() })), |
| 1662 |
)?; |
| 1663 |
assert_eq!(result, b"def"); |
| 1664 |
let mut parsed = Parsed::new(); |
| 1665 |
let result = parsed.parse_component( |
| 1666 |
b"abcdef", |
| 1667 |
Component::Ignore(modifier::Ignore::count(const { NonZero::new(7).unwrap() })), |
| 1668 |
); |
| 1669 |
assert!(matches!( |
| 1670 |
result, |
| 1671 |
Err(error::ParseFromDescription::InvalidComponent("ignore")) |
| 1672 |
)); |
| 1673 |
parse_component!( |
| 1674 |
Component::UnixTimestamp( |
| 1675 |
modifier::UnixTimestamp::default() |
| 1676 |
.with_precision(modifier::UnixTimestampPrecision::Second) |
| 1677 |
.with_sign_is_mandatory(false) |
| 1678 |
), |
| 1679 |
b"1234567890", |
| 1680 |
_.unix_timestamp_nanos() == Some(1_234_567_890_000_000_000) |
| 1681 |
); |
| 1682 |
parse_component!( |
| 1683 |
Component::UnixTimestamp( |
| 1684 |
modifier::UnixTimestamp::default() |
| 1685 |
.with_precision(modifier::UnixTimestampPrecision::Millisecond) |
| 1686 |
.with_sign_is_mandatory(false) |
| 1687 |
), |
| 1688 |
b"1234567890123", |
| 1689 |
_.unix_timestamp_nanos() == Some(1_234_567_890_123_000_000) |
| 1690 |
); |
| 1691 |
parse_component!( |
| 1692 |
Component::UnixTimestamp( |
| 1693 |
modifier::UnixTimestamp::default() |
| 1694 |
.with_precision(modifier::UnixTimestampPrecision::Microsecond) |
| 1695 |
.with_sign_is_mandatory(false) |
| 1696 |
), |
| 1697 |
b"1234567890123456", |
| 1698 |
_.unix_timestamp_nanos() == Some(1_234_567_890_123_456_000) |
| 1699 |
); |
| 1700 |
parse_component!( |
| 1701 |
Component::UnixTimestamp( |
| 1702 |
modifier::UnixTimestamp::default() |
| 1703 |
.with_precision(modifier::UnixTimestampPrecision::Nanosecond) |
| 1704 |
.with_sign_is_mandatory(false) |
| 1705 |
), |
| 1706 |
b"1234567890123456789", |
| 1707 |
_.unix_timestamp_nanos() == Some(1_234_567_890_123_456_789) |
| 1708 |
); |
| 1709 |
parse_component!( |
| 1710 |
Component::UnixTimestamp( |
| 1711 |
modifier::UnixTimestamp::default() |
| 1712 |
.with_precision(modifier::UnixTimestampPrecision::Nanosecond) |
| 1713 |
.with_sign_is_mandatory(false) |
| 1714 |
), |
| 1715 |
b"-1234567890123456789", |
| 1716 |
_.unix_timestamp_nanos() == Some(-1_234_567_890_123_456_789) |
| 1717 |
); |
| 1718 |
|
| 1719 |
Ok(()) |
| 1720 |
} |
| 1721 |
|
| 1722 |
#[test] |
| 1723 |
fn parse_optional() -> time::Result<()> { |
| 1724 |
// Ensure full parsing works as expected. |
| 1725 |
let mut parsed = Parsed::new(); |
| 1726 |
let remaining_input = parsed.parse_item( |
| 1727 |
b"2021-01-02", |
| 1728 |
&BorrowedFormatItem::Optional(&BorrowedFormatItem::Compound(&fd::parse( |
| 1729 |
"[year]-[month]-[day]", |
| 1730 |
)?)), |
| 1731 |
)?; |
| 1732 |
assert!(remaining_input.is_empty()); |
| 1733 |
assert_eq!(parsed.year(), Some(2021)); |
| 1734 |
assert_eq!(parsed.month(), Some(Month::January)); |
| 1735 |
assert_eq!(parsed.day().map(NonZero::get), Some(2)); |
| 1736 |
|
| 1737 |
let mut parsed = Parsed::new(); |
| 1738 |
let remaining_input = parsed.parse_item( |
| 1739 |
b"2021-01-02", |
| 1740 |
&OwnedFormatItem::from(BorrowedFormatItem::Optional(&BorrowedFormatItem::Compound( |
| 1741 |
&fd::parse("[year]-[month]-[day]")?, |
| 1742 |
))), |
| 1743 |
)?; |
| 1744 |
assert!(remaining_input.is_empty()); |
| 1745 |
assert_eq!(parsed.year(), Some(2021)); |
| 1746 |
assert_eq!(parsed.month(), Some(Month::January)); |
| 1747 |
assert_eq!(parsed.day().map(NonZero::get), Some(2)); |
| 1748 |
|
| 1749 |
// Ensure a successful partial parse *does not* mutate `parsed`. |
| 1750 |
let mut parsed = Parsed::new(); |
| 1751 |
let remaining_input = parsed.parse_item( |
| 1752 |
b"2021-01", |
| 1753 |
&BorrowedFormatItem::Optional(&BorrowedFormatItem::Compound(&fd::parse( |
| 1754 |
"[year]-[month]-[day]", |
| 1755 |
)?)), |
| 1756 |
)?; |
| 1757 |
assert_eq!(remaining_input, b"2021-01"); |
| 1758 |
assert!(parsed.year().is_none()); |
| 1759 |
assert!(parsed.month().is_none()); |
| 1760 |
assert!(parsed.day().is_none()); |
| 1761 |
|
| 1762 |
let mut parsed = Parsed::new(); |
| 1763 |
let remaining_input = parsed.parse_item( |
| 1764 |
b"2021-01", |
| 1765 |
&OwnedFormatItem::from(BorrowedFormatItem::Optional(&BorrowedFormatItem::Compound( |
| 1766 |
&fd::parse("[year]-[month]-[day]")?, |
| 1767 |
))), |
| 1768 |
)?; |
| 1769 |
assert_eq!(remaining_input, b"2021-01"); |
| 1770 |
assert!(parsed.year().is_none()); |
| 1771 |
assert!(parsed.month().is_none()); |
| 1772 |
assert!(parsed.day().is_none()); |
| 1773 |
|
| 1774 |
Ok(()) |
| 1775 |
} |
| 1776 |
|
| 1777 |
#[expect(clippy::cognitive_complexity, reason = "all test the same thing")] |
| 1778 |
#[test] |
| 1779 |
fn parse_first() -> time::Result<()> { |
| 1780 |
// Ensure the first item is parsed correctly. |
| 1781 |
let mut parsed = Parsed::new(); |
| 1782 |
let remaining_input = parsed.parse_item( |
| 1783 |
b"2021-01-02", |
| 1784 |
&BorrowedFormatItem::First(&[BorrowedFormatItem::Compound(&fd::parse( |
| 1785 |
"[year]-[month]-[day]", |
| 1786 |
)?)]), |
| 1787 |
)?; |
| 1788 |
assert!(remaining_input.is_empty()); |
| 1789 |
assert_eq!(parsed.year(), Some(2021)); |
| 1790 |
assert_eq!(parsed.month(), Some(Month::January)); |
| 1791 |
assert_eq!(parsed.day().map(NonZero::get), Some(2)); |
| 1792 |
|
| 1793 |
let mut parsed = Parsed::new(); |
| 1794 |
let remaining_input = parsed.parse_item( |
| 1795 |
b"2021-01-02", |
| 1796 |
&OwnedFormatItem::from(BorrowedFormatItem::First(&[BorrowedFormatItem::Compound( |
| 1797 |
&fd::parse("[year]-[month]-[day]")?, |
| 1798 |
)])), |
| 1799 |
)?; |
| 1800 |
assert!(remaining_input.is_empty()); |
| 1801 |
assert_eq!(parsed.year(), Some(2021)); |
| 1802 |
assert_eq!(parsed.month(), Some(Month::January)); |
| 1803 |
assert_eq!(parsed.day().map(NonZero::get), Some(2)); |
| 1804 |
|
| 1805 |
// Ensure an empty slice is a no-op success. |
| 1806 |
let mut parsed = Parsed::new(); |
| 1807 |
let remaining_input = parsed.parse_item(b"2021-01-02", &BorrowedFormatItem::First(&[]))?; |
| 1808 |
assert_eq!(remaining_input, b"2021-01-02"); |
| 1809 |
assert!(parsed.year().is_none()); |
| 1810 |
assert!(parsed.month().is_none()); |
| 1811 |
assert!(parsed.day().is_none()); |
| 1812 |
|
| 1813 |
let mut parsed = Parsed::new(); |
| 1814 |
let remaining_input = |
| 1815 |
parsed.parse_item(b"2021-01-02", &OwnedFormatItem::First(Box::new([])))?; |
| 1816 |
assert_eq!(remaining_input, b"2021-01-02"); |
| 1817 |
assert!(parsed.year().is_none()); |
| 1818 |
assert!(parsed.month().is_none()); |
| 1819 |
assert!(parsed.day().is_none()); |
| 1820 |
|
| 1821 |
// Ensure success when the first item fails. |
| 1822 |
let mut parsed = Parsed::new(); |
| 1823 |
let remaining_input = parsed.parse_item( |
| 1824 |
b"2021-01-02", |
| 1825 |
&BorrowedFormatItem::First(&[ |
| 1826 |
BorrowedFormatItem::Compound(&fd::parse("[period]")?), |
| 1827 |
BorrowedFormatItem::Compound(&fd::parse("x")?), |
| 1828 |
BorrowedFormatItem::Compound(&fd::parse("[year]-[month]-[day]")?), |
| 1829 |
]), |
| 1830 |
)?; |
| 1831 |
assert!(remaining_input.is_empty()); |
| 1832 |
assert_eq!(parsed.year(), Some(2021)); |
| 1833 |
assert_eq!(parsed.month(), Some(Month::January)); |
| 1834 |
assert_eq!(parsed.day().map(NonZero::get), Some(2)); |
| 1835 |
|
| 1836 |
let mut parsed = Parsed::new(); |
| 1837 |
let remaining_input = parsed.parse_item( |
| 1838 |
b"2021-01-02", |
| 1839 |
&OwnedFormatItem::from(BorrowedFormatItem::First(&[ |
| 1840 |
BorrowedFormatItem::Compound(&fd::parse("[period]")?), |
| 1841 |
BorrowedFormatItem::Compound(&fd::parse("x")?), |
| 1842 |
BorrowedFormatItem::Compound(&fd::parse("[year]-[month]-[day]")?), |
| 1843 |
])), |
| 1844 |
)?; |
| 1845 |
assert!(remaining_input.is_empty()); |
| 1846 |
assert_eq!(parsed.year(), Some(2021)); |
| 1847 |
assert_eq!(parsed.month(), Some(Month::January)); |
| 1848 |
assert_eq!(parsed.day().map(NonZero::get), Some(2)); |
| 1849 |
|
| 1850 |
// Ensure the first error is returned. |
| 1851 |
let mut parsed = Parsed::new(); |
| 1852 |
let err = parsed |
| 1853 |
.parse_item( |
| 1854 |
b"2021-01-02", |
| 1855 |
&BorrowedFormatItem::First(&[ |
| 1856 |
BorrowedFormatItem::Compound(&fd::parse("[period]")?), |
| 1857 |
BorrowedFormatItem::Compound(&fd::parse("x")?), |
| 1858 |
]), |
| 1859 |
) |
| 1860 |
.expect_err("parsing should fail"); |
| 1861 |
assert_eq!(err, error::ParseFromDescription::InvalidComponent("period")); |
| 1862 |
|
| 1863 |
let mut parsed = Parsed::new(); |
| 1864 |
let err = parsed |
| 1865 |
.parse_item( |
| 1866 |
b"2021-01-02", |
| 1867 |
&OwnedFormatItem::from(BorrowedFormatItem::First(&[ |
| 1868 |
BorrowedFormatItem::Compound(&fd::parse("[period]")?), |
| 1869 |
BorrowedFormatItem::Compound(&fd::parse("x")?), |
| 1870 |
])), |
| 1871 |
) |
| 1872 |
.expect_err("parsing should fail"); |
| 1873 |
assert_eq!(err, error::ParseFromDescription::InvalidComponent("period")); |
| 1874 |
|
| 1875 |
Ok(()) |
| 1876 |
} |
| 1877 |
|
| 1878 |
#[test] |
| 1879 |
fn parse_unix_timestamp() -> time::Result<()> { |
| 1880 |
assert_eq!( |
| 1881 |
OffsetDateTime::parse("1234567890", &fd::parse("[unix_timestamp]")?)?, |
| 1882 |
datetime!(2009-02-13 23:31:30 UTC) |
| 1883 |
); |
| 1884 |
assert_eq!( |
| 1885 |
OffsetDateTime::parse( |
| 1886 |
"1234567890123", |
| 1887 |
&fd::parse("[unix_timestamp precision:millisecond]")? |
| 1888 |
)?, |
| 1889 |
datetime!(2009-02-13 23:31:30.123 UTC) |
| 1890 |
); |
| 1891 |
assert_eq!( |
| 1892 |
OffsetDateTime::parse( |
| 1893 |
"1234567890123456", |
| 1894 |
&fd::parse("[unix_timestamp precision:microsecond]")? |
| 1895 |
)?, |
| 1896 |
datetime!(2009-02-13 23:31:30.123456 UTC) |
| 1897 |
); |
| 1898 |
assert_eq!( |
| 1899 |
OffsetDateTime::parse( |
| 1900 |
"1234567890123456789", |
| 1901 |
&fd::parse("[unix_timestamp precision:nanosecond]")? |
| 1902 |
)?, |
| 1903 |
datetime!(2009-02-13 23:31:30.123456789 UTC) |
| 1904 |
); |
| 1905 |
|
| 1906 |
Ok(()) |
| 1907 |
} |
| 1908 |
|
| 1909 |
#[test] |
| 1910 |
fn parse_unix_timestamp_err() -> time::Result<()> { |
| 1911 |
assert_eq!( |
| 1912 |
OffsetDateTime::parse("1234567890", &fd::parse("[unix_timestamp sign:mandatory]")?), |
| 1913 |
Err(error::Parse::ParseFromDescription( |
| 1914 |
error::ParseFromDescription::InvalidComponent("unix_timestamp") |
| 1915 |
)) |
| 1916 |
); |
| 1917 |
assert_eq!( |
| 1918 |
OffsetDateTime::parse("a", &fd::parse("[unix_timestamp precision:second]")?), |
| 1919 |
Err(error::Parse::ParseFromDescription( |
| 1920 |
error::ParseFromDescription::InvalidComponent("unix_timestamp") |
| 1921 |
)) |
| 1922 |
); |
| 1923 |
assert_eq!( |
| 1924 |
OffsetDateTime::parse("a", &fd::parse("[unix_timestamp precision:millisecond]")?), |
| 1925 |
Err(error::Parse::ParseFromDescription( |
| 1926 |
error::ParseFromDescription::InvalidComponent("unix_timestamp") |
| 1927 |
)) |
| 1928 |
); |
| 1929 |
assert_eq!( |
| 1930 |
OffsetDateTime::parse("a", &fd::parse("[unix_timestamp precision:microsecond]")?), |
| 1931 |
Err(error::Parse::ParseFromDescription( |
| 1932 |
error::ParseFromDescription::InvalidComponent("unix_timestamp") |
| 1933 |
)) |
| 1934 |
); |
| 1935 |
assert_eq!( |
| 1936 |
OffsetDateTime::parse("a", &fd::parse("[unix_timestamp precision:nanosecond]")?), |
| 1937 |
Err(error::Parse::ParseFromDescription( |
| 1938 |
error::ParseFromDescription::InvalidComponent("unix_timestamp") |
| 1939 |
)) |
| 1940 |
); |
| 1941 |
|
| 1942 |
Ok(()) |
| 1943 |
} |
| 1944 |
|
| 1945 |
#[test] |
| 1946 |
fn issue_601() { |
| 1947 |
let date = OffsetDateTime::parse( |
| 1948 |
"1234567890.123", |
| 1949 |
&fd::parse("[unix_timestamp].[subsecond digits:3]").expect("format description is valid"), |
| 1950 |
); |
| 1951 |
|
| 1952 |
assert_eq!(date, Ok(datetime!(2009-02-13 23:31:30.123 +00:00:00))); |
| 1953 |
} |
| 1954 |
|
| 1955 |
#[test] |
| 1956 |
fn end() -> time::Result<()> { |
| 1957 |
let mut parsed = Parsed::new(); |
| 1958 |
let remaining_input = parsed.parse_item( |
| 1959 |
b"", |
| 1960 |
&BorrowedFormatItem::Component(Component::End(modifier::End::default())), |
| 1961 |
); |
| 1962 |
assert_eq!(remaining_input, Ok(b"".as_slice())); |
| 1963 |
|
| 1964 |
assert_eq!( |
| 1965 |
Time::parse("00:00", &fd::parse("[hour]:[minute][end]")?), |
| 1966 |
Ok(time!(0:00)) |
| 1967 |
); |
| 1968 |
assert_eq!( |
| 1969 |
Time::parse( |
| 1970 |
"00:00abcdef", |
| 1971 |
&fd::parse("[hour]:[minute][end trailing_input:discard]")? |
| 1972 |
), |
| 1973 |
Ok(time!(0:00)) |
| 1974 |
); |
| 1975 |
assert_eq!( |
| 1976 |
Time::parse( |
| 1977 |
"00:00:00", |
| 1978 |
&fd::parse_owned::<2>("[hour]:[minute][optional [[end]]]:[second]")? |
| 1979 |
), |
| 1980 |
Ok(time!(0:00)) |
| 1981 |
); |
| 1982 |
assert!(matches!( |
| 1983 |
Time::parse( |
| 1984 |
"00:00:00", |
| 1985 |
&fd::parse_owned::<2>("[hour]:[minute][end]:[second]")? |
| 1986 |
), |
| 1987 |
Err(error::Parse::ParseFromDescription( |
| 1988 |
error::ParseFromDescription::UnexpectedTrailingCharacters { .. } |
| 1989 |
)) |
| 1990 |
)); |
| 1991 |
assert!(matches!( |
| 1992 |
Time::parse( |
| 1993 |
"00:00:00", |
| 1994 |
&fd::parse_owned::<2>("[hour]:[minute][end trailing_input:discard]:[second]")? |
| 1995 |
), |
| 1996 |
Err(error::Parse::ParseFromDescription( |
| 1997 |
error::ParseFromDescription::InvalidLiteral { .. } |
| 1998 |
)) |
| 1999 |
)); |
| 2000 |
|
| 2001 |
Ok(()) |
| 2002 |
} |