Debian Codegraph @

debian/rust-vendor/time/tests/integration/formatting.rs

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