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
//! Parse parts of an ISO 8601-formatted value.
use crate::error;
use crate::error::ParseFromDescription::{InvalidComponent, InvalidLiteral};
use crate::format_description::well_known::iso8601::EncodedConfig;
use crate::format_description::well_known::Iso8601;
use crate::parsing::combinator::rfc::iso8601::{
day, dayk, dayo, float, hour, min, month, week, year, ExtendedKind,
};
use crate::parsing::combinator::{ascii_char, sign};
use crate::parsing::{Parsed, ParsedItem};
impl<const CONFIG: EncodedConfig> Iso8601<CONFIG> {
// Basic: [year][month][day]
// Extended: [year]["-"][month]["-"][day]
// Basic: [year][dayo]
// Extended: [year]["-"][dayo]
// Basic: [year]["W"][week][dayk]
// Extended: [year]["-"]["W"][week]["-"][dayk]
/// Parse a date in the basic or extended format. Reduced precision is permitted.
pub(crate) fn parse_date<'a>(
parsed: &'a mut Parsed,
extended_kind: &'a mut ExtendedKind,
) -> impl FnMut(&[u8]) -> Result<&[u8], error::Parse> + 'a {
move |input| {
// Same for any acceptable format.
let ParsedItem(mut input, year) = year(input).ok_or(InvalidComponent("year"))?;
*extended_kind = match ascii_char::<b'-'>(input) {
Some(ParsedItem(new_input, ())) => {
input = new_input;
ExtendedKind::Extended
}
None => ExtendedKind::Basic, // no separator before mandatory month/ordinal/week
};
let mut ret_error = match (|| {
let ParsedItem(mut input, month) = month(input).ok_or(InvalidComponent("month"))?;
if extended_kind.is_extended() {
input = ascii_char::<b'-'>(input)
.ok_or(InvalidLiteral)?
.into_inner();
}
let ParsedItem(input, day) = day(input).ok_or(InvalidComponent("day"))?;
Ok(ParsedItem(input, (month, day)))
})() {
Ok(ParsedItem(input, (month, day))) => {
*parsed = parsed
.with_year(year)
.ok_or(InvalidComponent("year"))?
.with_month(month)
.ok_or(InvalidComponent("month"))?
.with_day(day)
.ok_or(InvalidComponent("day"))?;
return Ok(input);
}
Err(err) => err,
};
// Don't check for `None`, as the error from year-month-day will always take priority.
if let Some(ParsedItem(input, ordinal)) = dayo(input) {
*parsed = parsed
.with_year(year)
.ok_or(InvalidComponent("year"))?
.with_ordinal(ordinal)
.ok_or(InvalidComponent("ordinal"))?;
return Ok(input);
}
match (|| {
let input = ascii_char::<b'W'>(input)
.ok_or((false, InvalidLiteral))?
.into_inner();
let ParsedItem(mut input, week) =
week(input).ok_or((true, InvalidComponent("week")))?;
if extended_kind.is_extended() {
input = ascii_char::<b'-'>(input)
.ok_or((true, InvalidLiteral))?
.into_inner();
}
let ParsedItem(input, weekday) =
dayk(input).ok_or((true, InvalidComponent("weekday")))?;
Ok(ParsedItem(input, (week, weekday)))
})() {
Ok(ParsedItem(input, (week, weekday))) => {
*parsed = parsed
.with_iso_year(year)
.ok_or(InvalidComponent("year"))?
.with_iso_week_number(week)
.ok_or(InvalidComponent("week"))?
.with_weekday(weekday)
.ok_or(InvalidComponent("weekday"))?;
return Ok(input);
}
Err((false, _err)) => {}
// This error is more accurate than the one from year-month-day.
Err((true, err)) => ret_error = err,
}
Err(ret_error.into())
}
}
// Basic: ["T"][hour][min][sec]
// Extended: ["T"][hour][":"][min][":"][sec]
// Reduced precision: components after [hour] (including their preceding separator) can be
// omitted. ["T"] can be omitted if there is no date present.
/// Parse a time in the basic or extended format. Reduced precision is permitted.
pub(crate) fn parse_time<'a>(
parsed: &'a mut Parsed,
extended_kind: &'a mut ExtendedKind,
date_is_present: bool,
) -> impl FnMut(&[u8]) -> Result<&[u8], error::Parse> + 'a {
move |mut input| {
if date_is_present {
input = ascii_char::<b'T'>(input)
.ok_or(InvalidLiteral)?
.into_inner();
}
let ParsedItem(mut input, hour) = float(input).ok_or(InvalidComponent("hour"))?;
match hour {
(hour, None) => parsed.set_hour_24(hour).ok_or(InvalidComponent("hour"))?,
(hour, Some(fractional_part)) => {
*parsed = parsed
.with_hour_24(hour)
.ok_or(InvalidComponent("hour"))?
.with_minute((fractional_part * 60.0) as _)
.ok_or(InvalidComponent("minute"))?
.with_second((fractional_part * 3600.0 % 60.) as _)
.ok_or(InvalidComponent("second"))?
.with_subsecond(
(fractional_part * 3_600. * 1_000_000_000. % 1_000_000_000.) as _,
)
.ok_or(InvalidComponent("subsecond"))?;
return Ok(input);
}
};
if let Some(ParsedItem(new_input, ())) = ascii_char::<b':'>(input) {
extended_kind
.coerce_extended()
.ok_or(InvalidComponent("minute"))?;
input = new_input;
};
let mut input = match float(input) {
Some(ParsedItem(input, (minute, None))) => {
extended_kind.coerce_basic();
parsed
.set_minute(minute)
.ok_or(InvalidComponent("minute"))?;
input
}
Some(ParsedItem(input, (minute, Some(fractional_part)))) => {
// `None` is valid behavior, so don't error if this fails.
extended_kind.coerce_basic();
*parsed = parsed
.with_minute(minute)
.ok_or(InvalidComponent("minute"))?
.with_second((fractional_part * 60.) as _)
.ok_or(InvalidComponent("second"))?
.with_subsecond(
(fractional_part * 60. * 1_000_000_000. % 1_000_000_000.) as _,
)
.ok_or(InvalidComponent("subsecond"))?;
return Ok(input);
}
// colon was present, so minutes are required
None if extended_kind.is_extended() => {
return Err(error::Parse::ParseFromDescription(InvalidComponent(
"minute",
)));
}
None => {
// Missing components are assumed to be zero.
*parsed = parsed
.with_minute(0)
.ok_or(InvalidComponent("minute"))?
.with_second(0)
.ok_or(InvalidComponent("second"))?
.with_subsecond(0)
.ok_or(InvalidComponent("subsecond"))?;
return Ok(input);
}
};
if extended_kind.is_extended() {
match ascii_char::<b':'>(input) {
Some(ParsedItem(new_input, ())) => input = new_input,
None => {
*parsed = parsed
.with_second(0)
.ok_or(InvalidComponent("second"))?
.with_subsecond(0)
.ok_or(InvalidComponent("subsecond"))?;
return Ok(input);
}
}
}
let (input, second, subsecond) = match float(input) {
Some(ParsedItem(input, (second, None))) => (input, second, 0),
Some(ParsedItem(input, (second, Some(fractional_part)))) => {
(input, second, round(fractional_part * 1_000_000_000.) as _)
}
None if extended_kind.is_extended() => {
return Err(error::Parse::ParseFromDescription(InvalidComponent(
"second",
)));
}
// Missing components are assumed to be zero.
None => (input, 0, 0),
};
*parsed = parsed
.with_second(second)
.ok_or(InvalidComponent("second"))?
.with_subsecond(subsecond)
.ok_or(InvalidComponent("subsecond"))?;
Ok(input)
}
}
// Basic: [±][hour][min] or ["Z"]
// Extended: [±][hour][":"][min] or ["Z"]
// Reduced precision: [±][hour] or ["Z"]
/// Parse a UTC offset in the basic or extended format. Reduced precision is supported.
pub(crate) fn parse_offset<'a>(
parsed: &'a mut Parsed,
extended_kind: &'a mut ExtendedKind,
) -> impl FnMut(&[u8]) -> Result<&[u8], error::Parse> + 'a {
move |input| {
if let Some(ParsedItem(input, ())) = ascii_char::<b'Z'>(input) {
*parsed = parsed
.with_offset_hour(0)
.ok_or(InvalidComponent("offset hour"))?
.with_offset_minute_signed(0)
.ok_or(InvalidComponent("offset minute"))?
.with_offset_second_signed(0)
.ok_or(InvalidComponent("offset second"))?;
return Ok(input);
}
let ParsedItem(input, sign) = sign(input).ok_or(InvalidComponent("offset hour"))?;
let mut input = hour(input)
.and_then(|parsed_item| {
parsed_item.consume_value(|hour| {
parsed.set_offset_hour(if sign == b'-' {
-(hour as i8)
} else {
hour as _
})
})
})
.ok_or(InvalidComponent("offset hour"))?;
if extended_kind.maybe_extended() {
if let Some(ParsedItem(new_input, ())) = ascii_char::<b':'>(input) {
extended_kind
.coerce_extended()
.ok_or(InvalidComponent("offset minute"))?;
input = new_input;
};
}
let input = min(input)
.and_then(|parsed_item| {
parsed_item.consume_value(|min| {
parsed.set_offset_minute_signed(if sign == b'-' {
-(min as i8)
} else {
min as _
})
})
})
.ok_or(InvalidComponent("offset minute"))?;
// If `:` was present, the format has already been set to extended. As such, this call
// will do nothing in that case. If there wasn't `:` but minutes were
// present, we know it's the basic format. Do not use `?` on the call, as
// returning `None` is valid behavior.
extended_kind.coerce_basic();
Ok(input)
}
}
}
/// Round wrapper that uses hardware implementation if `std` is available, falling back to manual
/// implementation for `no_std`
fn round(value: f64) -> f64 {
#[cfg(feature = "std")]
{
value.round()
}
#[cfg(not(feature = "std"))]
{
round_impl(value)
}
}
#[cfg(not(feature = "std"))]
#[allow(clippy::missing_docs_in_private_items)]
fn round_impl(value: f64) -> f64 {
debug_assert!(value.is_sign_positive() && !value.is_nan());
let f = value % 1.;
if f < 0.5 { value - f } else { value - f + 1. }
}