jiff 0.2.23

A date-time library that encourages you to jump into the pit of success. This library is heavily inspired by the Temporal project.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
use crate::{civil::Weekday, error, tz::Offset, util::escape};

#[derive(Clone, Debug)]
pub(crate) enum Error {
    ColonCount {
        directive: u8,
    },
    DirectiveFailure {
        directive: u8,
        colons: u8,
    },
    DirectiveFailureDot {
        directive: u8,
    },
    ExpectedDirectiveAfterColons,
    ExpectedDirectiveAfterFlag {
        flag: u8,
    },
    ExpectedDirectiveAfterWidth,
    FailedStrftime,
    FailedStrptime,
    FailedWidth,
    InvalidDate,
    InvalidISOWeekDate,
    InvalidWeekdayMonday {
        got: Weekday,
    },
    InvalidWeekdaySunday {
        got: Weekday,
    },
    MismatchOffset {
        parsed: Offset,
        got: Offset,
    },
    MismatchWeekday {
        parsed: Weekday,
        got: Weekday,
    },
    MissingTimeHourForFractional,
    MissingTimeHourForMinute,
    MissingTimeHourForSecond,
    MissingTimeMinuteForFractional,
    MissingTimeMinuteForSecond,
    MissingTimeSecondForFractional,
    RangeTimestamp,
    RangeWidth,
    RequiredDateForDateTime,
    RequiredDateTimeForTimestamp,
    RequiredDateTimeForZoned,
    RequiredOffsetForTimestamp,
    RequiredSomeDayForDate,
    RequiredTimeForDateTime,
    RequiredYearForDate,
    UnconsumedStrptime {
        #[cfg(feature = "alloc")]
        remaining: alloc::boxed::Box<[u8]>,
    },
    UnexpectedEndAfterDot,
    UnexpectedEndAfterPercent,
    UnknownDirectiveAfterDot {
        directive: u8,
    },
    UnknownDirective {
        directive: u8,
    },
    ZonedOffsetOrTz,
}

impl Error {
    pub(crate) fn unconsumed(_remaining: &[u8]) -> Error {
        Error::UnconsumedStrptime {
            #[cfg(feature = "alloc")]
            remaining: _remaining.into(),
        }
    }
}

impl error::IntoError for Error {
    fn into_error(self) -> error::Error {
        self.into()
    }
}

impl From<Error> for error::Error {
    #[cold]
    #[inline(never)]
    fn from(err: Error) -> error::Error {
        error::ErrorKind::FmtStrtime(err).into()
    }
}

impl core::fmt::Display for Error {
    fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
        use self::Error::*;

        match *self {
            ColonCount { directive } => write!(
                f,
                "invalid number of `:` in `%{directive}` directive",
                directive = escape::Byte(directive),
            ),
            DirectiveFailure { directive, colons } => write!(
                f,
                "%{colons}{directive} failed",
                colons = escape::RepeatByte { byte: b':', count: colons },
                directive = escape::Byte(directive),
            ),
            DirectiveFailureDot { directive } => write!(
                f,
                "%.{directive} failed",
                directive = escape::Byte(directive),
            ),
            ExpectedDirectiveAfterColons => f.write_str(
                "expected to find specifier directive after colons, \
                 but found end of format string",
            ),
            ExpectedDirectiveAfterFlag { flag } => write!(
                f,
                "expected to find specifier directive after flag \
                 `{flag}`, but found end of format string",
                flag = escape::Byte(flag),
            ),
            ExpectedDirectiveAfterWidth => f.write_str(
                "expected to find specifier directive after parsed width, \
                 but found end of format string",
            ),
            FailedStrftime => f.write_str("strftime formatting failed"),
            FailedStrptime => f.write_str("strptime parsing failed"),
            FailedWidth => {
                f.write_str("failed to parse conversion specifier width")
            }
            InvalidDate => f.write_str("invalid date"),
            InvalidISOWeekDate => f.write_str("invalid ISO 8601 week date"),
            InvalidWeekdayMonday { got } => write!(
                f,
                "weekday `{got:?}` is not valid for \
                 Monday based week number",
            ),
            InvalidWeekdaySunday { got } => write!(
                f,
                "weekday `{got:?}` is not valid for \
                 Sunday based week number",
            ),
            MissingTimeHourForFractional => f.write_str(
                "parsing format did not include hour directive, \
                 but did include fractional second directive (cannot have \
                 smaller time units with bigger time units missing)",
            ),
            MissingTimeHourForMinute => f.write_str(
                "parsing format did not include hour directive, \
                 but did include minute directive (cannot have \
                 smaller time units with bigger time units missing)",
            ),
            MissingTimeHourForSecond => f.write_str(
                "parsing format did not include hour directive, \
                 but did include second directive (cannot have \
                 smaller time units with bigger time units missing)",
            ),
            MissingTimeMinuteForFractional => f.write_str(
                "parsing format did not include minute directive, \
                 but did include fractional second directive (cannot have \
                 smaller time units with bigger time units missing)",
            ),
            MissingTimeMinuteForSecond => f.write_str(
                "parsing format did not include minute directive, \
                 but did include second directive (cannot have \
                 smaller time units with bigger time units missing)",
            ),
            MissingTimeSecondForFractional => f.write_str(
                "parsing format did not include second directive, \
                 but did include fractional second directive (cannot have \
                 smaller time units with bigger time units missing)",
            ),
            MismatchOffset { parsed, got } => write!(
                f,
                "parsed time zone offset `{parsed}`, but \
                 offset from timestamp and time zone is `{got}`",
            ),
            MismatchWeekday { parsed, got } => write!(
                f,
                "parsed weekday `{parsed:?}` does not match \
                 weekday `{got:?}` from parsed date",
            ),
            RangeTimestamp => f.write_str(
                "parsed datetime and offset, \
                 but combining them into a zoned datetime \
                 is outside Jiff's supported timestamp range",
            ),
            RangeWidth => write!(
                f,
                "parsed width is too big, max is {max}",
                max = u8::MAX
            ),
            RequiredDateForDateTime => {
                f.write_str("date required to parse datetime")
            }
            RequiredDateTimeForTimestamp => {
                f.write_str("datetime required to parse timestamp")
            }
            RequiredDateTimeForZoned => {
                f.write_str("datetime required to parse zoned datetime")
            }
            RequiredOffsetForTimestamp => {
                f.write_str("offset required to parse timestamp")
            }
            RequiredSomeDayForDate => f.write_str(
                "a month/day, day-of-year or week date must be \
                 present to create a date, but none were found",
            ),
            RequiredTimeForDateTime => {
                f.write_str("time required to parse datetime")
            }
            RequiredYearForDate => f.write_str("year required to parse date"),
            #[cfg(feature = "alloc")]
            UnconsumedStrptime { ref remaining } => write!(
                f,
                "strptime expects to consume the entire input, but \
                 `{remaining}` remains unparsed",
                remaining = escape::Bytes(remaining),
            ),
            #[cfg(not(feature = "alloc"))]
            UnconsumedStrptime {} => f.write_str(
                "strptime expects to consume the entire input, but \
                 there is unparsed input remaining",
            ),
            UnexpectedEndAfterDot => f.write_str(
                "invalid format string, expected directive after `%.`",
            ),
            UnexpectedEndAfterPercent => f.write_str(
                "invalid format string, expected byte after `%`, \
                 but found end of format string",
            ),
            UnknownDirective { directive } => write!(
                f,
                "found unrecognized specifier directive `{directive}`",
                directive = escape::Byte(directive),
            ),
            UnknownDirectiveAfterDot { directive } => write!(
                f,
                "found unrecognized specifier directive `{directive}` \
                 following `%.`",
                directive = escape::Byte(directive),
            ),
            ZonedOffsetOrTz => f.write_str(
                "either offset (from `%z`) or IANA time zone identifier \
                 (from `%Q`) is required for parsing zoned datetime",
            ),
        }
    }
}

#[derive(Clone, Debug)]
pub(crate) enum ParseError {
    ExpectedAmPm,
    ExpectedAmPmTooShort,
    ExpectedIanaTz,
    ExpectedIanaTzEndOfInput,
    ExpectedMonthAbbreviation,
    ExpectedMonthAbbreviationTooShort,
    ExpectedWeekdayAbbreviation,
    ExpectedWeekdayAbbreviationTooShort,
    ExpectedChoice {
        available: &'static [&'static [u8]],
    },
    ExpectedFractionalDigit,
    ExpectedMatchLiteralByte {
        expected: u8,
        got: u8,
    },
    ExpectedMatchLiteralEndOfInput {
        expected: u8,
    },
    ExpectedNonEmpty {
        directive: u8,
    },
    #[cfg(not(feature = "alloc"))]
    NotAllowedAlloc {
        directive: u8,
        colons: u8,
    },
    NotAllowedLocaleClockTime,
    NotAllowedLocaleDate,
    NotAllowedLocaleDateAndTime,
    NotAllowedLocaleTwelveHourClockTime,
    NotAllowedTimeZoneAbbreviation,
    ParseDay,
    ParseDayOfYear,
    ParseCentury,
    ParseFractionalSeconds,
    ParseHour,
    ParseIsoWeekNumber,
    ParseIsoWeekYear,
    ParseIsoWeekYearTwoDigit,
    ParseMinute,
    ParseMondayWeekNumber,
    ParseMonth,
    ParseSecond,
    ParseSundayWeekNumber,
    ParseTimestamp,
    ParseWeekdayNumber,
    ParseYear,
    ParseYearTwoDigit,
    UnknownMonthName,
    UnknownWeekdayAbbreviation,
}

impl error::IntoError for ParseError {
    fn into_error(self) -> error::Error {
        self.into()
    }
}

impl From<ParseError> for error::Error {
    #[cold]
    #[inline(never)]
    fn from(err: ParseError) -> error::Error {
        error::ErrorKind::FmtStrtimeParse(err).into()
    }
}

impl core::fmt::Display for ParseError {
    fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
        use self::ParseError::*;

        match *self {
            ExpectedAmPm => f.write_str("expected to find `AM` or `PM`"),
            ExpectedAmPmTooShort => f.write_str(
                "expected to find `AM` or `PM`, \
                 but the remaining input is too short \
                 to contain one",
            ),
            ExpectedIanaTz => f.write_str(
                "expected to find the start of an IANA time zone \
                 identifier name or component",
            ),
            ExpectedIanaTzEndOfInput => f.write_str(
                "expected to find the start of an IANA time zone \
                 identifier name or component, \
                 but found end of input instead",
            ),
            ExpectedMonthAbbreviation => {
                f.write_str("expected to find month name abbreviation")
            }
            ExpectedMonthAbbreviationTooShort => f.write_str(
                "expected to find month name abbreviation, \
                 but the remaining input is too short \
                 to contain one",
            ),
            ExpectedWeekdayAbbreviation => {
                f.write_str("expected to find weekday abbreviation")
            }
            ExpectedWeekdayAbbreviationTooShort => f.write_str(
                "expected to find weekday abbreviation, \
                 but the remaining input is too short \
                 to contain one",
            ),
            ExpectedChoice { available } => {
                f.write_str(
                    "failed to find expected value, available choices are: ",
                )?;
                for (i, choice) in available.iter().enumerate() {
                    if i > 0 {
                        f.write_str(", ")?;
                    }
                    write!(f, "{}", escape::Bytes(choice))?;
                }
                Ok(())
            }
            ExpectedFractionalDigit => f.write_str(
                "expected at least one fractional decimal digit, \
                 but did not find any",
            ),
            ExpectedMatchLiteralByte { expected, got } => write!(
                f,
                "expected to match literal byte `{expected}` from \
                 format string, but found byte `{got}` in input",
                expected = escape::Byte(expected),
                got = escape::Byte(got),
            ),
            ExpectedMatchLiteralEndOfInput { expected } => write!(
                f,
                "expected to match literal byte `{expected}` from \
                 format string, but found end of input",
                expected = escape::Byte(expected),
            ),
            ExpectedNonEmpty { directive } => write!(
                f,
                "expected non-empty input for directive `%{directive}`, \
                 but found end of input",
                directive = escape::Byte(directive),
            ),
            #[cfg(not(feature = "alloc"))]
            NotAllowedAlloc { directive, colons } => write!(
                f,
                "cannot parse `%{colons}{directive}` \
                 without Jiff's `alloc` feature enabled",
                colons = escape::RepeatByte { byte: b':', count: colons },
                directive = escape::Byte(directive),
            ),
            NotAllowedLocaleClockTime => {
                f.write_str("parsing locale clock time is not allowed")
            }
            NotAllowedLocaleDate => {
                f.write_str("parsing locale date is not allowed")
            }
            NotAllowedLocaleDateAndTime => {
                f.write_str("parsing locale date and time is not allowed")
            }
            NotAllowedLocaleTwelveHourClockTime => {
                f.write_str("parsing locale 12-hour clock time is not allowed")
            }
            NotAllowedTimeZoneAbbreviation => {
                f.write_str("parsing time zone abbreviation is not allowed")
            }
            ParseCentury => {
                f.write_str("failed to parse year number for century")
            }
            ParseDay => f.write_str("failed to parse day number"),
            ParseDayOfYear => {
                f.write_str("failed to parse day of year number")
            }
            ParseFractionalSeconds => f.write_str(
                "failed to parse fractional second component \
                 (up to 9 digits, nanosecond precision)",
            ),
            ParseHour => f.write_str("failed to parse hour number"),
            ParseMinute => f.write_str("failed to parse minute number"),
            ParseWeekdayNumber => {
                f.write_str("failed to parse weekday number")
            }
            ParseIsoWeekNumber => {
                f.write_str("failed to parse ISO 8601 week number")
            }
            ParseIsoWeekYear => {
                f.write_str("failed to parse ISO 8601 week year")
            }
            ParseIsoWeekYearTwoDigit => {
                f.write_str("failed to parse 2-digit ISO 8601 week year")
            }
            ParseMondayWeekNumber => {
                f.write_str("failed to parse Monday-based week number")
            }
            ParseMonth => f.write_str("failed to parse month number"),
            ParseSecond => f.write_str("failed to parse second number"),
            ParseSundayWeekNumber => {
                f.write_str("failed to parse Sunday-based week number")
            }
            ParseTimestamp => {
                f.write_str("failed to parse Unix timestamp (in seconds)")
            }
            ParseYear => f.write_str("failed to parse year"),
            ParseYearTwoDigit => f.write_str("failed to parse 2-digit year"),
            UnknownMonthName => f.write_str("unrecognized month name"),
            UnknownWeekdayAbbreviation => {
                f.write_str("unrecognized weekday abbreviation")
            }
        }
    }
}

#[derive(Clone, Debug)]
pub(crate) enum FormatError {
    RequiresDate,
    RequiresInstant,
    RequiresOffset,
    RequiresTime,
    RequiresTimeZone,
    RequiresTimeZoneOrOffset,
    InvalidUtf8,
    ZeroPrecisionFloat,
    ZeroPrecisionNano,
}

impl error::IntoError for FormatError {
    fn into_error(self) -> error::Error {
        self.into()
    }
}

impl From<FormatError> for error::Error {
    #[cold]
    #[inline(never)]
    fn from(err: FormatError) -> error::Error {
        error::ErrorKind::FmtStrtimeFormat(err).into()
    }
}

impl core::fmt::Display for FormatError {
    fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
        use self::FormatError::*;

        match *self {
            RequiresDate => f.write_str("requires date to format"),
            RequiresInstant => f.write_str(
                "requires instant (a timestamp or a date, time and offset)",
            ),
            RequiresTime => f.write_str("requires time to format"),
            RequiresOffset => f.write_str("requires time zone offset"),
            RequiresTimeZone => {
                f.write_str("requires IANA time zone identifier")
            }
            RequiresTimeZoneOrOffset => f.write_str(
                "requires IANA time zone identifier or \
                 time zone offset, but neither were present",
            ),
            InvalidUtf8 => {
                f.write_str("invalid format string, it must be valid UTF-8")
            }
            ZeroPrecisionFloat => {
                f.write_str("zero precision with %f is not allowed")
            }
            ZeroPrecisionNano => {
                f.write_str("zero precision with %N is not allowed")
            }
        }
    }
}