| 1 |
use std::io; |
| 2 |
use std::num::NonZero; |
| 3 |
|
| 4 |
use time::format_description::well_known::iso8601::{DateKind, OffsetPrecision, TimePrecision}; |
| 5 |
use time::format_description::well_known::{Iso8601, Rfc2822, Rfc3339, iso8601}; |
| 6 |
use time::format_description::{self, BorrowedFormatItem, OwnedFormatItem}; |
| 7 |
use time::macros::{date, datetime, format_description as fd, offset, time, utc_datetime}; |
| 8 |
use time::{OffsetDateTime, Time}; |
| 9 |
|
| 10 |
#[test] |
| 11 |
fn rfc_2822() -> time::Result<()> { |
| 12 |
assert_eq!( |
| 13 |
datetime!(2021-01-02 03:04:05 UTC).format(&Rfc2822)?, |
| 14 |
"Sat, 02 Jan 2021 03:04:05 +0000" |
| 15 |
); |
| 16 |
assert_eq!( |
| 17 |
utc_datetime!(2021-01-02 03:04:05).format(&Rfc2822)?, |
| 18 |
"Sat, 02 Jan 2021 03:04:05 +0000" |
| 19 |
); |
| 20 |
assert_eq!( |
| 21 |
datetime!(2021-01-02 03:04:05 +06:07).format(&Rfc2822)?, |
| 22 |
"Sat, 02 Jan 2021 03:04:05 +0607" |
| 23 |
); |
| 24 |
assert_eq!( |
| 25 |
datetime!(2021-01-02 03:04:05 -06:07).format(&Rfc2822)?, |
| 26 |
"Sat, 02 Jan 2021 03:04:05 -0607" |
| 27 |
); |
| 28 |
|
| 29 |
assert!(matches!( |
| 30 |
datetime!(1885-01-01 01:01:01 UTC).format(&Rfc2822), |
| 31 |
Err(time::error::Format::InvalidComponent("year")) |
| 32 |
)); |
| 33 |
assert!(matches!( |
| 34 |
utc_datetime!(1885-01-01 01:01:01).format(&Rfc2822), |
| 35 |
Err(time::error::Format::InvalidComponent("year")) |
| 36 |
)); |
| 37 |
assert!(matches!( |
| 38 |
datetime!(2000-01-01 00:00:00 +00:00:01).format(&Rfc2822), |
| 39 |
Err(time::error::Format::InvalidComponent("offset_second")) |
| 40 |
)); |
| 41 |
|
| 42 |
Ok(()) |
| 43 |
} |
| 44 |
|
| 45 |
#[test] |
| 46 |
fn rfc_3339() -> time::Result<()> { |
| 47 |
assert_eq!( |
| 48 |
datetime!(2021-01-02 03:04:05 UTC).format(&Rfc3339)?, |
| 49 |
"2021-01-02T03:04:05Z" |
| 50 |
); |
| 51 |
assert_eq!( |
| 52 |
datetime!(2021-01-02 03:04:05.1 UTC).format(&Rfc3339)?, |
| 53 |
"2021-01-02T03:04:05.1Z" |
| 54 |
); |
| 55 |
assert_eq!( |
| 56 |
datetime!(2021-01-02 03:04:05.12 UTC).format(&Rfc3339)?, |
| 57 |
"2021-01-02T03:04:05.12Z" |
| 58 |
); |
| 59 |
assert_eq!( |
| 60 |
datetime!(2021-01-02 03:04:05.123 UTC).format(&Rfc3339)?, |
| 61 |
"2021-01-02T03:04:05.123Z" |
| 62 |
); |
| 63 |
assert_eq!( |
| 64 |
datetime!(2021-01-02 03:04:05.123_4 UTC).format(&Rfc3339)?, |
| 65 |
"2021-01-02T03:04:05.1234Z" |
| 66 |
); |
| 67 |
assert_eq!( |
| 68 |
datetime!(2021-01-02 03:04:05.123_45 UTC).format(&Rfc3339)?, |
| 69 |
"2021-01-02T03:04:05.12345Z" |
| 70 |
); |
| 71 |
assert_eq!( |
| 72 |
datetime!(2021-01-02 03:04:05.123_456 UTC).format(&Rfc3339)?, |
| 73 |
"2021-01-02T03:04:05.123456Z" |
| 74 |
); |
| 75 |
assert_eq!( |
| 76 |
datetime!(2021-01-02 03:04:05.123_456_7 UTC).format(&Rfc3339)?, |
| 77 |
"2021-01-02T03:04:05.1234567Z" |
| 78 |
); |
| 79 |
assert_eq!( |
| 80 |
datetime!(2021-01-02 03:04:05.123_456_78 UTC).format(&Rfc3339)?, |
| 81 |
"2021-01-02T03:04:05.12345678Z" |
| 82 |
); |
| 83 |
assert_eq!( |
| 84 |
datetime!(2021-01-02 03:04:05.123_456_789 UTC).format(&Rfc3339)?, |
| 85 |
"2021-01-02T03:04:05.123456789Z" |
| 86 |
); |
| 87 |
assert_eq!( |
| 88 |
datetime!(2021-01-02 03:04:05.123_456_789 -01:02).format(&Rfc3339)?, |
| 89 |
"2021-01-02T03:04:05.123456789-01:02" |
| 90 |
); |
| 91 |
assert_eq!( |
| 92 |
datetime!(2021-01-02 03:04:05.123_456_789 +01:02).format(&Rfc3339)?, |
| 93 |
"2021-01-02T03:04:05.123456789+01:02" |
| 94 |
); |
| 95 |
|
| 96 |
assert!(matches!( |
| 97 |
datetime!(-0001-01-01 0:00 UTC).format(&Rfc3339), |
| 98 |
Err(time::error::Format::InvalidComponent("year")) |
| 99 |
)); |
| 100 |
assert!(matches!( |
| 101 |
datetime!(0000-01-01 0:00 +00:00:01).format(&Rfc3339), |
| 102 |
Err(time::error::Format::InvalidComponent("offset_second")) |
| 103 |
)); |
| 104 |
|
| 105 |
Ok(()) |
| 106 |
} |
| 107 |
|
| 108 |
#[test] |
| 109 |
fn iso_8601() -> time::Result<()> { |
| 110 |
macro_rules! assert_format_config { |
| 111 |
($formatted:literal $(, $($config:tt)+)?) => { |
| 112 |
assert_eq!( |
| 113 |
datetime!(2021-01-02 03:04:05 UTC).format( |
| 114 |
&Iso8601::<{ iso8601::Config::DEFAULT$($($config)+)?.encode() }> |
| 115 |
)?, |
| 116 |
$formatted |
| 117 |
); |
| 118 |
}; |
| 119 |
} |
| 120 |
|
| 121 |
assert_eq!( |
| 122 |
datetime!(-123_456-01-02 03:04:05 UTC).format( |
| 123 |
&Iso8601::< |
| 124 |
{ |
| 125 |
iso8601::Config::DEFAULT |
| 126 |
.set_year_is_six_digits(true) |
| 127 |
.encode() |
| 128 |
}, |
| 129 |
> |
| 130 |
)?, |
| 131 |
"-123456-01-02T03:04:05.000000000Z" |
| 132 |
); |
| 133 |
assert_eq!( |
| 134 |
datetime!(-123_456-01-02 03:04:05 UTC).format( |
| 135 |
&Iso8601::< |
| 136 |
{ |
| 137 |
iso8601::Config::DEFAULT |
| 138 |
.set_date_kind(DateKind::Ordinal) |
| 139 |
.set_year_is_six_digits(true) |
| 140 |
.encode() |
| 141 |
}, |
| 142 |
> |
| 143 |
)?, |
| 144 |
"-123456-002T03:04:05.000000000Z" |
| 145 |
); |
| 146 |
assert_eq!( |
| 147 |
datetime!(-123_456-01-02 03:04:05 UTC).format( |
| 148 |
&Iso8601::< |
| 149 |
{ |
| 150 |
iso8601::Config::DEFAULT |
| 151 |
.set_date_kind(DateKind::Week) |
| 152 |
.set_year_is_six_digits(true) |
| 153 |
.encode() |
| 154 |
}, |
| 155 |
> |
| 156 |
)?, |
| 157 |
"-123456-W01-4T03:04:05.000000000Z" |
| 158 |
); |
| 159 |
assert_eq!( |
| 160 |
datetime!(2021-01-02 03:04:05+1:00).format(&Iso8601::DEFAULT)?, |
| 161 |
"2021-01-02T03:04:05.000000000+01:00" |
| 162 |
); |
| 163 |
assert_eq!( |
| 164 |
datetime!(2021-01-02 03:04:05+1:00).format( |
| 165 |
&Iso8601::< |
| 166 |
{ |
| 167 |
iso8601::Config::DEFAULT |
| 168 |
.set_offset_precision(OffsetPrecision::Hour) |
| 169 |
.encode() |
| 170 |
}, |
| 171 |
> |
| 172 |
)?, |
| 173 |
"2021-01-02T03:04:05.000000000+01" |
| 174 |
); |
| 175 |
assert_format_config!("2021-01-02T03:04:05.000000000Z"); |
| 176 |
assert_format_config!("20210102T030405.000000000Z", .set_use_separators(false)); |
| 177 |
assert_format_config!("+002021-01-02T03:04:05.000000000Z", .set_year_is_six_digits(true)); |
| 178 |
assert_format_config!("2021-01-02T03Z", .set_time_precision(TimePrecision::Hour { decimal_digits: None })); |
| 179 |
assert_format_config!("2021-01-02T03:04Z", .set_time_precision(TimePrecision::Minute { decimal_digits: None })); |
| 180 |
assert_format_config!("2021-01-02T03:04:05Z", .set_time_precision(TimePrecision::Second { decimal_digits: None })); |
| 181 |
assert_format_config!("2021-002T03:04:05.000000000Z", .set_date_kind(DateKind::Ordinal)); |
| 182 |
assert_format_config!("2020-W53-6T03:04:05.000000000Z", .set_date_kind(DateKind::Week)); |
| 183 |
|
| 184 |
assert!(matches!( |
| 185 |
datetime!(+10_000-01-01 0:00 UTC).format(&Iso8601::DEFAULT), |
| 186 |
Err(time::error::Format::InvalidComponent("year")) |
| 187 |
)); |
| 188 |
assert!(matches!( |
| 189 |
datetime!(+10_000-W01-1 0:00 UTC).format( |
| 190 |
&Iso8601::< |
| 191 |
{ |
| 192 |
iso8601::Config::DEFAULT |
| 193 |
.set_date_kind(DateKind::Week) |
| 194 |
.encode() |
| 195 |
}, |
| 196 |
> |
| 197 |
), |
| 198 |
Err(time::error::Format::InvalidComponent("year")) |
| 199 |
)); |
| 200 |
assert!(matches!( |
| 201 |
datetime!(+10_000-001 0:00 UTC).format( |
| 202 |
&Iso8601::< |
| 203 |
{ |
| 204 |
iso8601::Config::DEFAULT |
| 205 |
.set_date_kind(DateKind::Ordinal) |
| 206 |
.encode() |
| 207 |
}, |
| 208 |
> |
| 209 |
), |
| 210 |
Err(time::error::Format::InvalidComponent("year")) |
| 211 |
)); |
| 212 |
assert!(matches!( |
| 213 |
datetime!(2021-01-02 03:04:05 +0:00:01).format(&Iso8601::DEFAULT), |
| 214 |
Err(time::error::Format::InvalidComponent("offset_second")) |
| 215 |
)); |
| 216 |
assert!(matches!( |
| 217 |
datetime!(2021-01-02 03:04:05 +0:01).format( |
| 218 |
&Iso8601::< |
| 219 |
{ |
| 220 |
iso8601::Config::DEFAULT |
| 221 |
.set_offset_precision(OffsetPrecision::Hour) |
| 222 |
.encode() |
| 223 |
}, |
| 224 |
> |
| 225 |
), |
| 226 |
Err(time::error::Format::InvalidComponent("offset_minute")) |
| 227 |
)); |
| 228 |
|
| 229 |
Ok(()) |
| 230 |
} |
| 231 |
|
| 232 |
#[test] |
| 233 |
fn iso_8601_issue_678() -> time::Result<()> { |
| 234 |
macro_rules! assert_format_config { |
| 235 |
($formatted:literal $(, $($config:tt)+)?) => { |
| 236 |
assert_eq!( |
| 237 |
datetime!(2021-01-02 03:04:05.999_999_999 UTC).format( |
| 238 |
&Iso8601::<{ iso8601::Config::DEFAULT$($($config)+)?.encode() }> |
| 239 |
)?, |
| 240 |
$formatted |
| 241 |
); |
| 242 |
}; |
| 243 |
} |
| 244 |
|
| 245 |
assert_format_config!("2021-01-02T03:04:05.999999999Z", .set_time_precision(TimePrecision::Second { decimal_digits: NonZero::new(9) })); |
| 246 |
assert_format_config!("2021-01-02T03:04:05.999999Z", .set_time_precision(TimePrecision::Second { decimal_digits: NonZero::new(6) })); |
| 247 |
assert_format_config!("2021-01-02T03:04:05.999Z", .set_time_precision(TimePrecision::Second { decimal_digits: NonZero::new(3) })); |
| 248 |
|
| 249 |
Ok(()) |
| 250 |
} |
| 251 |
|
| 252 |
#[test] |
| 253 |
fn format_time() -> time::Result<()> { |
| 254 |
let format_output = [ |
| 255 |
(fd!("[hour]"), "13"), |
| 256 |
(fd!("[hour repr:12]"), "01"), |
| 257 |
(fd!("[hour repr:12 padding:none]"), "1"), |
| 258 |
(fd!("[hour repr:12 padding:space]"), " 1"), |
| 259 |
(fd!("[hour repr:24]"), "13"), |
| 260 |
(fd!("[hour repr:24]"), "13"), |
| 261 |
(fd!("[hour repr:24 padding:none]"), "13"), |
| 262 |
(fd!("[hour repr:24 padding:space]"), "13"), |
| 263 |
(fd!("[minute]"), "02"), |
| 264 |
(fd!("[minute padding:none]"), "2"), |
| 265 |
(fd!("[minute padding:space]"), " 2"), |
| 266 |
(fd!("[minute padding:zero]"), "02"), |
| 267 |
(fd!("[period]"), "PM"), |
| 268 |
(fd!("[period case:upper]"), "PM"), |
| 269 |
(fd!("[period case:lower]"), "pm"), |
| 270 |
(fd!("[second]"), "03"), |
| 271 |
(fd!("[second padding:none]"), "3"), |
| 272 |
(fd!("[second padding:space]"), " 3"), |
| 273 |
(fd!("[second padding:zero]"), "03"), |
| 274 |
(fd!("[subsecond]"), "456789012"), |
| 275 |
(fd!("[subsecond digits:1]"), "4"), |
| 276 |
(fd!("[subsecond digits:2]"), "45"), |
| 277 |
(fd!("[subsecond digits:3]"), "456"), |
| 278 |
(fd!("[subsecond digits:4]"), "4567"), |
| 279 |
(fd!("[subsecond digits:5]"), "45678"), |
| 280 |
(fd!("[subsecond digits:6]"), "456789"), |
| 281 |
(fd!("[subsecond digits:7]"), "4567890"), |
| 282 |
(fd!("[subsecond digits:8]"), "45678901"), |
| 283 |
(fd!("[subsecond digits:9]"), "456789012"), |
| 284 |
(fd!("[subsecond digits:1+]"), "456789012"), |
| 285 |
]; |
| 286 |
|
| 287 |
for &(format_description, output) in &format_output { |
| 288 |
assert_eq!( |
| 289 |
time!(13:02:03.456_789_012).format(format_description)?, |
| 290 |
output |
| 291 |
); |
| 292 |
assert!( |
| 293 |
time!(13:02:03.456_789_012) |
| 294 |
.format_into(&mut io::sink(), format_description) |
| 295 |
.is_ok() |
| 296 |
); |
| 297 |
assert_eq!( |
| 298 |
time!(13:02:03.456_789_012).format(&OwnedFormatItem::from(format_description))?, |
| 299 |
output |
| 300 |
); |
| 301 |
assert!( |
| 302 |
time!(13:02:03.456_789_012) |
| 303 |
.format_into(&mut io::sink(), &OwnedFormatItem::from(format_description)) |
| 304 |
.is_ok() |
| 305 |
); |
| 306 |
} |
| 307 |
|
| 308 |
assert_eq!( |
| 309 |
time!(1:02:03).format(fd!("[hour repr:12][period]"))?, |
| 310 |
"01AM" |
| 311 |
); |
| 312 |
assert_eq!( |
| 313 |
Time::MIDNIGHT.format(fd!("[hour repr:12][period case:lower]"))?, |
| 314 |
"12am" |
| 315 |
); |
| 316 |
assert_eq!(Time::MIDNIGHT.format(fd!("[subsecond digits:1+]"))?, "0"); |
| 317 |
assert_eq!( |
| 318 |
time!(0:00:00.01).format(fd!("[subsecond digits:1+]"))?, |
| 319 |
"01" |
| 320 |
); |
| 321 |
assert_eq!( |
| 322 |
time!(0:00:00.001).format(fd!("[subsecond digits:1+]"))?, |
| 323 |
"001" |
| 324 |
); |
| 325 |
assert_eq!( |
| 326 |
time!(0:00:00.0001).format(fd!("[subsecond digits:1+]"))?, |
| 327 |
"0001" |
| 328 |
); |
| 329 |
assert_eq!( |
| 330 |
time!(0:00:00.00001).format(fd!("[subsecond digits:1+]"))?, |
| 331 |
"00001" |
| 332 |
); |
| 333 |
assert_eq!( |
| 334 |
time!(0:00:00.000001).format(fd!("[subsecond digits:1+]"))?, |
| 335 |
"000001" |
| 336 |
); |
| 337 |
assert_eq!( |
| 338 |
time!(0:00:00.0000001).format(fd!("[subsecond digits:1+]"))?, |
| 339 |
"0000001" |
| 340 |
); |
| 341 |
assert_eq!( |
| 342 |
time!(0:00:00.00000001).format(fd!("[subsecond digits:1+]"))?, |
| 343 |
"00000001" |
| 344 |
); |
| 345 |
assert_eq!( |
| 346 |
time!(0:00:00.000000001).format(fd!("[subsecond digits:1+]"))?, |
| 347 |
"000000001" |
| 348 |
); |
| 349 |
|
| 350 |
Ok(()) |
| 351 |
} |
| 352 |
|
| 353 |
#[test] |
| 354 |
fn display_time() { |
| 355 |
assert_eq!(time!(0:00).to_string(), "0:00:00.0"); |
| 356 |
assert_eq!(time!(23:59).to_string(), "23:59:00.0"); |
| 357 |
assert_eq!(time!(23:59:59).to_string(), "23:59:59.0"); |
| 358 |
assert_eq!(time!(0:00:01).to_string(), "0:00:01.0"); |
| 359 |
assert_eq!(time!(0:00:00.1).to_string(), "0:00:00.1"); |
| 360 |
assert_eq!(time!(0:00:00.01).to_string(), "0:00:00.01"); |
| 361 |
assert_eq!(time!(0:00:00.001).to_string(), "0:00:00.001"); |
| 362 |
assert_eq!(time!(0:00:00.000_1).to_string(), "0:00:00.0001"); |
| 363 |
assert_eq!(time!(0:00:00.000_01).to_string(), "0:00:00.00001"); |
| 364 |
assert_eq!(time!(0:00:00.000_001).to_string(), "0:00:00.000001"); |
| 365 |
assert_eq!(time!(0:00:00.000_000_1).to_string(), "0:00:00.0000001"); |
| 366 |
assert_eq!(time!(0:00:00.000_000_01).to_string(), "0:00:00.00000001"); |
| 367 |
assert_eq!(time!(0:00:00.000_000_001).to_string(), "0:00:00.000000001"); |
| 368 |
|
| 369 |
assert_eq!(format!("{:>12}", time!(0:00)), " 0:00:00.0"); |
| 370 |
assert_eq!(format!("{:x^14}", time!(0:00)), "xx0:00:00.0xxx"); |
| 371 |
} |
| 372 |
|
| 373 |
#[test] |
| 374 |
fn format_date() -> time::Result<()> { |
| 375 |
let format_output = [ |
| 376 |
(fd!("[day]"), "31"), |
| 377 |
(fd!("[month]"), "12"), |
| 378 |
(fd!("[month repr:short]"), "Dec"), |
| 379 |
(fd!("[month repr:long]"), "December"), |
| 380 |
(fd!("[ordinal]"), "365"), |
| 381 |
(fd!("[weekday]"), "Tuesday"), |
| 382 |
(fd!("[weekday repr:short]"), "Tue"), |
| 383 |
(fd!("[weekday repr:sunday]"), "3"), |
| 384 |
(fd!("[weekday repr:sunday one_indexed:false]"), "2"), |
| 385 |
(fd!("[weekday repr:monday]"), "2"), |
| 386 |
(fd!("[weekday repr:monday one_indexed:false]"), "1"), |
| 387 |
(fd!("[week_number]"), "01"), |
| 388 |
(fd!("[week_number padding:none]"), "1"), |
| 389 |
(fd!("[week_number padding:space]"), " 1"), |
| 390 |
(fd!("[week_number repr:sunday]"), "52"), |
| 391 |
(fd!("[week_number repr:monday]"), "52"), |
| 392 |
(fd!("[year]"), "2019"), |
| 393 |
(fd!("[year base:iso_week]"), "2020"), |
| 394 |
(fd!("[year sign:mandatory]"), "+2019"), |
| 395 |
(fd!("[year base:iso_week sign:mandatory]"), "+2020"), |
| 396 |
(fd!("[year repr:century]"), "20"), |
| 397 |
(fd!("[year repr:last_two]"), "19"), |
| 398 |
(fd!("[year base:iso_week repr:last_two]"), "20"), |
| 399 |
(fd!("[year range:standard]"), "2019"), |
| 400 |
(fd!("[year range:standard repr:century]"), "20"), |
| 401 |
(fd!("[year range:standard repr:last_two]"), "19"), |
| 402 |
]; |
| 403 |
|
| 404 |
for &(format_description, output) in &format_output { |
| 405 |
assert_eq!(date!(2019-12-31).format(format_description)?, output); |
| 406 |
assert!( |
| 407 |
date!(2019-12-31) |
| 408 |
.format_into(&mut io::sink(), format_description) |
| 409 |
.is_ok() |
| 410 |
); |
| 411 |
assert_eq!( |
| 412 |
date!(2019-12-31).format(&OwnedFormatItem::from(format_description))?, |
| 413 |
output |
| 414 |
); |
| 415 |
assert!( |
| 416 |
date!(2019-12-31) |
| 417 |
.format_into(&mut io::sink(), &OwnedFormatItem::from(format_description)) |
| 418 |
.is_ok() |
| 419 |
); |
| 420 |
} |
| 421 |
|
| 422 |
Ok(()) |
| 423 |
} |
| 424 |
|
| 425 |
#[test] |
| 426 |
fn format_date_err() { |
| 427 |
assert!(matches!( |
| 428 |
date!(+10_000-01-01).format(fd!("[year range:standard]")), |
| 429 |
Err(time::error::Format::ComponentRange(cr)) if cr.name() == "year" |
| 430 |
)); |
| 431 |
assert!(matches!( |
| 432 |
date!(+10_000-01-01).format(fd!("[year repr:century range:standard]")), |
| 433 |
Err(time::error::Format::ComponentRange(cr)) if cr.name() == "year" |
| 434 |
)); |
| 435 |
} |
| 436 |
|
| 437 |
#[test] |
| 438 |
fn display_date() { |
| 439 |
assert_eq!(date!(2019-01-01).to_string(), "2019-01-01"); |
| 440 |
assert_eq!(date!(2019-12-31).to_string(), "2019-12-31"); |
| 441 |
assert_eq!(date!(-4713-11-24).to_string(), "-4713-11-24"); |
| 442 |
assert_eq!(date!(-0001-01-01).to_string(), "-0001-01-01"); |
| 443 |
|
| 444 |
assert_eq!(date!(+10_000-01-01).to_string(), "+10000-01-01"); |
| 445 |
assert_eq!(date!(+100_000-01-01).to_string(), "+100000-01-01"); |
| 446 |
assert_eq!(date!(-10_000-01-01).to_string(), "-10000-01-01"); |
| 447 |
assert_eq!(date!(-100_000-01-01).to_string(), "-100000-01-01"); |
| 448 |
} |
| 449 |
|
| 450 |
#[test] |
| 451 |
fn format_offset() -> time::Result<()> { |
| 452 |
let value_format_output = [ |
| 453 |
( |
| 454 |
offset!(+01:02:03), |
| 455 |
fd!("[offset_hour sign:automatic]"), |
| 456 |
"01", |
| 457 |
), |
| 458 |
( |
| 459 |
offset!(+01:02:03), |
| 460 |
fd!("[offset_hour sign:mandatory]"), |
| 461 |
"+01", |
| 462 |
), |
| 463 |
( |
| 464 |
offset!(-01:02:03), |
| 465 |
fd!("[offset_hour sign:automatic]"), |
| 466 |
"-01", |
| 467 |
), |
| 468 |
( |
| 469 |
offset!(-01:02:03), |
| 470 |
fd!("[offset_hour sign:mandatory]"), |
| 471 |
"-01", |
| 472 |
), |
| 473 |
(offset!(+01:02:03), fd!("[offset_minute]"), "02"), |
| 474 |
(offset!(+01:02:03), fd!("[offset_second]"), "03"), |
| 475 |
]; |
| 476 |
|
| 477 |
for &(value, format_description, output) in &value_format_output { |
| 478 |
assert_eq!(value.format(format_description)?, output); |
| 479 |
assert!( |
| 480 |
value |
| 481 |
.format_into(&mut io::sink(), format_description) |
| 482 |
.is_ok() |
| 483 |
); |
| 484 |
assert_eq!( |
| 485 |
value.format(&OwnedFormatItem::from(format_description))?, |
| 486 |
output |
| 487 |
); |
| 488 |
assert!( |
| 489 |
value |
| 490 |
.format_into(&mut io::sink(), &OwnedFormatItem::from(format_description)) |
| 491 |
.is_ok() |
| 492 |
); |
| 493 |
} |
| 494 |
|
| 495 |
Ok(()) |
| 496 |
} |
| 497 |
|
| 498 |
#[test] |
| 499 |
fn display_offset() { |
| 500 |
assert_eq!(offset!(UTC).to_string(), "+00:00:00"); |
| 501 |
assert_eq!(offset!(+0:00:01).to_string(), "+00:00:01"); |
| 502 |
assert_eq!(offset!(-0:00:01).to_string(), "-00:00:01"); |
| 503 |
assert_eq!(offset!(+1).to_string(), "+01:00:00"); |
| 504 |
assert_eq!(offset!(-1).to_string(), "-01:00:00"); |
| 505 |
assert_eq!(offset!(+23:59).to_string(), "+23:59:00"); |
| 506 |
assert_eq!(offset!(-23:59).to_string(), "-23:59:00"); |
| 507 |
assert_eq!(offset!(+23:59:59).to_string(), "+23:59:59"); |
| 508 |
assert_eq!(offset!(-23:59:59).to_string(), "-23:59:59"); |
| 509 |
|
| 510 |
assert_eq!(format!("{:>10}", offset!(UTC)), " +00:00:00"); |
| 511 |
assert_eq!(format!("{:x^14}", offset!(UTC)), "xx+00:00:00xxx"); |
| 512 |
} |
| 513 |
|
| 514 |
#[test] |
| 515 |
fn format_pdt() -> time::Result<()> { |
| 516 |
let format_description = fd!("[year]-[month]-[day] [hour]:[minute]:[second].[subsecond]"); |
| 517 |
|
| 518 |
assert_eq!( |
| 519 |
datetime!(1970-01-01 0:00).format(format_description)?, |
| 520 |
"1970-01-01 00:00:00.0" |
| 521 |
); |
| 522 |
assert!( |
| 523 |
datetime!(1970-01-01 0:00) |
| 524 |
.format_into(&mut io::sink(), format_description) |
| 525 |
.is_ok() |
| 526 |
); |
| 527 |
assert_eq!( |
| 528 |
datetime!(1970-01-01 0:00).format(&OwnedFormatItem::from(format_description))?, |
| 529 |
"1970-01-01 00:00:00.0" |
| 530 |
); |
| 531 |
assert!( |
| 532 |
datetime!(1970-01-01 0:00) |
| 533 |
.format_into(&mut io::sink(), &OwnedFormatItem::from(format_description)) |
| 534 |
.is_ok() |
| 535 |
); |
| 536 |
|
| 537 |
Ok(()) |
| 538 |
} |
| 539 |
|
| 540 |
#[test] |
| 541 |
fn display_pdt() { |
| 542 |
assert_eq!( |
| 543 |
datetime!(1970-01-01 0:00).to_string(), |
| 544 |
String::from("1970-01-01 0:00:00.0") |
| 545 |
); |
| 546 |
assert_eq!( |
| 547 |
datetime!(1970-01-01 0:00:01).to_string(), |
| 548 |
String::from("1970-01-01 0:00:01.0") |
| 549 |
); |
| 550 |
} |
| 551 |
|
| 552 |
#[test] |
| 553 |
fn format_odt() -> time::Result<()> { |
| 554 |
let format_description = format_description::parse( |
| 555 |
"[year]-[month]-[day] [hour]:[minute]:[second].[subsecond digits:1+] [offset_hour \ |
| 556 |
sign:mandatory]:[offset_minute]:[offset_second]", |
| 557 |
)?; |
| 558 |
|
| 559 |
assert_eq!( |
| 560 |
datetime!(1970-01-01 0:00 UTC).format(&format_description)?, |
| 561 |
"1970-01-01 00:00:00.0 +00:00:00" |
| 562 |
); |
| 563 |
assert!( |
| 564 |
datetime!(1970-01-01 0:00 UTC) |
| 565 |
.format_into(&mut io::sink(), &format_description) |
| 566 |
.is_ok() |
| 567 |
); |
| 568 |
assert_eq!( |
| 569 |
datetime!(1970-01-01 0:00 UTC).format(&OwnedFormatItem::from(&format_description))?, |
| 570 |
"1970-01-01 00:00:00.0 +00:00:00" |
| 571 |
); |
| 572 |
assert!( |
| 573 |
datetime!(1970-01-01 0:00 UTC) |
| 574 |
.format_into(&mut io::sink(), &OwnedFormatItem::from(format_description)) |
| 575 |
.is_ok() |
| 576 |
); |
| 577 |
|
| 578 |
Ok(()) |
| 579 |
} |
| 580 |
|
| 581 |
#[test] |
| 582 |
fn display_odt() { |
| 583 |
assert_eq!( |
| 584 |
datetime!(1970-01-01 0:00 UTC).to_string(), |
| 585 |
"1970-01-01 0:00:00.0 +00:00:00" |
| 586 |
); |
| 587 |
} |
| 588 |
|
| 589 |
#[test] |
| 590 |
fn format_udt() -> time::Result<()> { |
| 591 |
let format_description = fd!("[year]-[month]-[day] [hour]:[minute]:[second].[subsecond]"); |
| 592 |
|
| 593 |
assert_eq!( |
| 594 |
utc_datetime!(1970-01-01 0:00).format(format_description)?, |
| 595 |
"1970-01-01 00:00:00.0" |
| 596 |
); |
| 597 |
assert!( |
| 598 |
utc_datetime!(1970-01-01 0:00) |
| 599 |
.format_into(&mut io::sink(), format_description) |
| 600 |
.is_ok() |
| 601 |
); |
| 602 |
assert_eq!( |
| 603 |
utc_datetime!(1970-01-01 0:00).format(&OwnedFormatItem::from(format_description))?, |
| 604 |
"1970-01-01 00:00:00.0" |
| 605 |
); |
| 606 |
assert!( |
| 607 |
utc_datetime!(1970-01-01 0:00) |
| 608 |
.format_into(&mut io::sink(), &OwnedFormatItem::from(format_description)) |
| 609 |
.is_ok() |
| 610 |
); |
| 611 |
|
| 612 |
Ok(()) |
| 613 |
} |
| 614 |
|
| 615 |
#[test] |
| 616 |
fn display_udt() { |
| 617 |
assert_eq!( |
| 618 |
utc_datetime!(1970-01-01 0:00).to_string(), |
| 619 |
"1970-01-01 0:00:00.0 +00" |
| 620 |
); |
| 621 |
} |
| 622 |
|
| 623 |
#[test] |
| 624 |
fn insufficient_type_information() { |
| 625 |
let assert_insufficient_type_information = |res| { |
| 626 |
assert!(matches!( |
| 627 |
res, |
| 628 |
Err(time::error::Format::InsufficientTypeInformation { .. }) |
| 629 |
)); |
| 630 |
}; |
| 631 |
assert_insufficient_type_information(Time::MIDNIGHT.format(fd!("[year]"))); |
| 632 |
assert_insufficient_type_information(Time::MIDNIGHT.format(&BorrowedFormatItem::First(&[ |
| 633 |
BorrowedFormatItem::Compound(fd!("[year]")), |
| 634 |
]))); |
| 635 |
} |
| 636 |
|
| 637 |
#[expect(clippy::cognitive_complexity, reason = "all test the same thing")] |
| 638 |
#[test] |
| 639 |
fn failed_write() -> time::Result<()> { |
| 640 |
macro_rules! assert_err { |
| 641 |
($val:expr, $format:expr) => {{ |
| 642 |
let val = $val; |
| 643 |
let format = $format; |
| 644 |
let success_len = val.format(&format)?.len(); |
| 645 |
for len in 0..success_len { |
| 646 |
let mut buf = &mut vec![0; len][..]; |
| 647 |
let res = val.format_into(&mut buf, &format); |
| 648 |
assert!(matches!( |
| 649 |
res, |
| 650 |
Err(time::error::Format::StdIo(e)) if e.kind() == io::ErrorKind::WriteZero) |
| 651 |
); |
| 652 |
} |
| 653 |
}}; |
| 654 |
} |
| 655 |
|
| 656 |
assert_err!(Time::MIDNIGHT, fd!("foo")); |
| 657 |
assert_err!(Time::MIDNIGHT, OwnedFormatItem::from(fd!("foo"))); |
| 658 |
assert_err!(Time::MIDNIGHT, BorrowedFormatItem::Compound(fd!("foo"))); |
| 659 |
assert_err!( |
| 660 |
Time::MIDNIGHT, |
| 661 |
BorrowedFormatItem::Optional(&BorrowedFormatItem::Compound(fd!("foo"))) |
| 662 |
); |
| 663 |
assert_err!( |
| 664 |
Time::MIDNIGHT, |
| 665 |
OwnedFormatItem::from(BorrowedFormatItem::Optional(&BorrowedFormatItem::Compound( |
| 666 |
fd!("foo") |
| 667 |
))) |
| 668 |
); |
| 669 |
assert_err!(OffsetDateTime::UNIX_EPOCH, Rfc3339); |
| 670 |
assert_err!(datetime!(2021-001 0:00:00.1 UTC), Rfc3339); |
| 671 |
assert_err!(datetime!(2021-001 0:00 +0:01), Rfc3339); |
| 672 |
assert_err!(OffsetDateTime::UNIX_EPOCH, Rfc2822); |
| 673 |
assert_err!(OffsetDateTime::UNIX_EPOCH, Iso8601::DEFAULT); |
| 674 |
assert_err!(datetime!(2021-001 0:00 +0:01), Iso8601::DEFAULT); |
| 675 |
assert_err!( |
| 676 |
OffsetDateTime::UNIX_EPOCH, |
| 677 |
Iso8601::< |
| 678 |
{ |
| 679 |
iso8601::Config::DEFAULT |
| 680 |
.set_year_is_six_digits(true) |
| 681 |
.encode() |
| 682 |
}, |
| 683 |
> |
| 684 |
); |
| 685 |
assert_err!( |
| 686 |
OffsetDateTime::UNIX_EPOCH, |
| 687 |
Iso8601::< |
| 688 |
{ |
| 689 |
iso8601::Config::DEFAULT |
| 690 |
.set_date_kind(DateKind::Ordinal) |
| 691 |
.encode() |
| 692 |
}, |
| 693 |
> |
| 694 |
); |
| 695 |
assert_err!( |
| 696 |
OffsetDateTime::UNIX_EPOCH, |
| 697 |
Iso8601::< |
| 698 |
{ |
| 699 |
iso8601::Config::DEFAULT |
| 700 |
.set_year_is_six_digits(true) |
| 701 |
.set_date_kind(DateKind::Ordinal) |
| 702 |
.encode() |
| 703 |
}, |
| 704 |
> |
| 705 |
); |
| 706 |
assert_err!( |
| 707 |
OffsetDateTime::UNIX_EPOCH, |
| 708 |
Iso8601::< |
| 709 |
{ |
| 710 |
iso8601::Config::DEFAULT |
| 711 |
.set_year_is_six_digits(true) |
| 712 |
.set_date_kind(DateKind::Week) |
| 713 |
.encode() |
| 714 |
}, |
| 715 |
> |
| 716 |
); |
| 717 |
assert_err!( |
| 718 |
OffsetDateTime::UNIX_EPOCH, |
| 719 |
Iso8601::< |
| 720 |
{ |
| 721 |
iso8601::Config::DEFAULT |
| 722 |
.set_date_kind(DateKind::Week) |
| 723 |
.encode() |
| 724 |
}, |
| 725 |
> |
| 726 |
); |
| 727 |
assert_err!( |
| 728 |
OffsetDateTime::UNIX_EPOCH, |
| 729 |
Iso8601::< |
| 730 |
{ |
| 731 |
iso8601::Config::DEFAULT |
| 732 |
.set_time_precision(TimePrecision::Minute { |
| 733 |
decimal_digits: None, |
| 734 |
}) |
| 735 |
.encode() |
| 736 |
}, |
| 737 |
> |
| 738 |
); |
| 739 |
assert_err!( |
| 740 |
OffsetDateTime::UNIX_EPOCH, |
| 741 |
Iso8601::< |
| 742 |
{ |
| 743 |
iso8601::Config::DEFAULT |
| 744 |
.set_time_precision(TimePrecision::Hour { |
| 745 |
decimal_digits: None, |
| 746 |
}) |
| 747 |
.encode() |
| 748 |
}, |
| 749 |
> |
| 750 |
); |
| 751 |
|
| 752 |
assert_err!(Time::MIDNIGHT, fd!("[hour padding:space]")); |
| 753 |
assert_err!(offset!(+1), fd!("[offset_hour sign:mandatory]")); |
| 754 |
assert_err!(offset!(-1), fd!("[offset_hour]")); |
| 755 |
assert_err!(date!(-1-001), fd!("[year]")); |
| 756 |
assert_err!(date!(2021-001), fd!("[year sign:mandatory]")); |
| 757 |
assert_err!(date!(+999_999-001), fd!("[year]")); |
| 758 |
assert_err!(date!(+99_999-001), fd!("[year]")); |
| 759 |
|
| 760 |
let component_names = [ |
| 761 |
"day", |
| 762 |
"month", |
| 763 |
"ordinal", |
| 764 |
"weekday", |
| 765 |
"week_number", |
| 766 |
"year", |
| 767 |
"hour", |
| 768 |
"minute", |
| 769 |
"period", |
| 770 |
"second", |
| 771 |
"subsecond", |
| 772 |
"offset_hour", |
| 773 |
"offset_minute", |
| 774 |
"offset_second", |
| 775 |
]; |
| 776 |
for component in &component_names { |
| 777 |
let component = format!("[{component}]"); |
| 778 |
assert_err!( |
| 779 |
OffsetDateTime::UNIX_EPOCH, |
| 780 |
format_description::parse(&component)? |
| 781 |
); |
| 782 |
} |
| 783 |
|
| 784 |
Ok(()) |
| 785 |
} |
| 786 |
|
| 787 |
#[test] |
| 788 |
fn first() -> time::Result<()> { |
| 789 |
assert_eq!(Time::MIDNIGHT.format(&BorrowedFormatItem::First(&[]))?, ""); |
| 790 |
assert_eq!( |
| 791 |
Time::MIDNIGHT.format(&BorrowedFormatItem::First(&[BorrowedFormatItem::Compound( |
| 792 |
fd!("[hour]") |
| 793 |
)]))?, |
| 794 |
"00" |
| 795 |
); |
| 796 |
assert_eq!( |
| 797 |
Time::MIDNIGHT.format(&OwnedFormatItem::First(Box::new([])))?, |
| 798 |
"" |
| 799 |
); |
| 800 |
assert_eq!( |
| 801 |
Time::MIDNIGHT.format(&OwnedFormatItem::from(BorrowedFormatItem::First(&[ |
| 802 |
BorrowedFormatItem::Compound(fd!("[hour]")) |
| 803 |
])))?, |
| 804 |
"00" |
| 805 |
); |
| 806 |
|
| 807 |
Ok(()) |
| 808 |
} |
| 809 |
|
| 810 |
#[test] |
| 811 |
fn ignore() -> time::Result<()> { |
| 812 |
assert_eq!(Time::MIDNIGHT.format(fd!("[ignore count:2]"))?, ""); |
| 813 |
|
| 814 |
Ok(()) |
| 815 |
} |
| 816 |
|
| 817 |
#[test] |
| 818 |
fn end() -> time::Result<()> { |
| 819 |
assert_eq!(Time::MIDNIGHT.format(fd!("[end]"))?, ""); |
| 820 |
|
| 821 |
Ok(()) |
| 822 |
} |
| 823 |
|
| 824 |
#[test] |
| 825 |
fn unix_timestamp() -> time::Result<()> { |
| 826 |
let dt = datetime!(2009-02-13 23:31:30.123456789 UTC); |
| 827 |
|
| 828 |
assert_eq!(dt.format(&fd!("[unix_timestamp]"))?, "1234567890"); |
| 829 |
assert_eq!( |
| 830 |
dt.format(&fd!("[unix_timestamp sign:mandatory]"))?, |
| 831 |
"+1234567890" |
| 832 |
); |
| 833 |
assert_eq!( |
| 834 |
dt.format(&fd!("[unix_timestamp precision:millisecond]"))?, |
| 835 |
"1234567890123" |
| 836 |
); |
| 837 |
assert_eq!( |
| 838 |
dt.format(&fd!("[unix_timestamp precision:microsecond]"))?, |
| 839 |
"1234567890123456" |
| 840 |
); |
| 841 |
assert_eq!( |
| 842 |
dt.format(&fd!("[unix_timestamp precision:nanosecond]"))?, |
| 843 |
"1234567890123456789" |
| 844 |
); |
| 845 |
assert_eq!( |
| 846 |
datetime!(1969-12-31 23:59:59 UTC).format(&fd!("[unix_timestamp]"))?, |
| 847 |
"-1" |
| 848 |
); |
| 849 |
|
| 850 |
Ok(()) |
| 851 |
} |