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