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
#![allow(non_snake_case)]
use crate::{
error,
format::{
parse::{
try_consume_exact_digits, try_consume_first_match,
AmPm::{AM, PM},
},
Padding, ParseResult, ParsedItems,
},
Time,
};
use core::{
fmt::{self, Formatter},
num::NonZeroU8,
};
#[allow(unused_imports)]
use standback::prelude::*;
pub(crate) fn fmt_H(f: &mut Formatter<'_>, time: Time, padding: Padding) -> fmt::Result {
pad!(f, padding, 2, time.hour())
}
pub(crate) fn parse_H(items: &mut ParsedItems, s: &mut &str, padding: Padding) -> ParseResult<()> {
items.hour_24 = Some(try_consume_exact_digits(s, 2, padding).ok_or(error::Parse::InvalidHour)?);
Ok(())
}
pub(crate) fn fmt_I(f: &mut Formatter<'_>, time: Time, padding: Padding) -> fmt::Result {
pad!(f, padding, 2, (time.hour() as i8 - 1).rem_euclid(12) + 1)
}
pub(crate) fn parse_I(items: &mut ParsedItems, s: &mut &str, padding: Padding) -> ParseResult<()> {
items.hour_12 = Some(
try_consume_exact_digits(s, 2, padding)
.and_then(NonZeroU8::new)
.ok_or(error::Parse::InvalidHour)?,
);
Ok(())
}
pub(crate) fn fmt_M(f: &mut Formatter<'_>, time: Time, padding: Padding) -> fmt::Result {
pad!(f, padding, 2, time.minute())
}
pub(crate) fn parse_M(items: &mut ParsedItems, s: &mut &str, padding: Padding) -> ParseResult<()> {
items.minute =
Some(try_consume_exact_digits(s, 2, padding).ok_or(error::Parse::InvalidMinute)?);
Ok(())
}
pub(crate) fn fmt_N(f: &mut Formatter<'_>, time: Time) -> fmt::Result {
write!(f, "{:09}", time.nanosecond)
}
pub(crate) fn parse_N(items: &mut ParsedItems, s: &mut &str) -> ParseResult<()> {
items.nanosecond =
Some(try_consume_exact_digits(s, 9, Padding::None).ok_or(error::Parse::InvalidNanosecond)?);
Ok(())
}
pub(crate) fn fmt_p(f: &mut Formatter<'_>, time: Time) -> fmt::Result {
if time.hour() < 12 {
f.write_str("am")
} else {
f.write_str("pm")
}
}
pub(crate) fn parse_p(items: &mut ParsedItems, s: &mut &str) -> ParseResult<()> {
items.am_pm = Some(
try_consume_first_match(s, [("am", AM), ("pm", PM)].iter().cloned())
.ok_or(error::Parse::InvalidAmPm)?,
);
Ok(())
}
pub(crate) fn fmt_P(f: &mut Formatter<'_>, time: Time) -> fmt::Result {
if time.hour() < 12 {
f.write_str("AM")
} else {
f.write_str("PM")
}
}
pub(crate) fn parse_P(items: &mut ParsedItems, s: &mut &str) -> ParseResult<()> {
items.am_pm = Some(
try_consume_first_match(s, [("AM", AM), ("PM", PM)].iter().cloned())
.ok_or(error::Parse::InvalidAmPm)?,
);
Ok(())
}
pub(crate) fn fmt_S(f: &mut Formatter<'_>, time: Time, padding: Padding) -> fmt::Result {
pad!(f, padding, 2, time.second())
}
pub(crate) fn parse_S(items: &mut ParsedItems, s: &mut &str, padding: Padding) -> ParseResult<()> {
items.second =
Some(try_consume_exact_digits(s, 2, padding).ok_or(error::Parse::InvalidSecond)?);
Ok(())
}