| 1 |
// For the full copyright and license information, please view the LICENSE |
| 2 |
// file that was distributed with this source code. |
| 3 |
|
| 4 |
// spell-checker:ignore shhmm colonless |
| 5 |
|
| 6 |
//! Parse a time item (without a date). |
| 7 |
//! |
| 8 |
//! The GNU docs state: |
| 9 |
//! |
| 10 |
//! > More generally, the time of day may be given as ‘hour:minute:second’, |
| 11 |
//! > where hour is a number between 0 and 23, minute is a number between 0 and |
| 12 |
//! > 59, and second is a number between 0 and 59 possibly followed by ‘.’ or |
| 13 |
//! > ‘,’ and a fraction containing one or more digits. Alternatively, |
| 14 |
//! > ‘:second’ can be omitted, in which case it is taken to be zero. On the |
| 15 |
//! > rare hosts that support leap seconds, second may be 60. |
| 16 |
//! > |
| 17 |
//! > If the time is followed by ‘am’ or ‘pm’ (or ‘a.m.’ or ‘p.m.’), hour is |
| 18 |
//! > restricted to run from 1 to 12, and ‘:minute’ may be omitted (taken to be |
| 19 |
//! > zero). ‘am’ indicates the first half of the day, ‘pm’ indicates the |
| 20 |
//! > second half of the day. In this notation, 12 is the predecessor of 1: |
| 21 |
//! > midnight is ‘12am’ while noon is ‘12pm’. (This is the zero-oriented |
| 22 |
//! > interpretation of ‘12am’ and ‘12pm’, as opposed to the old tradition |
| 23 |
//! > derived from Latin which uses ‘12m’ for noon and ‘12pm’ for midnight.) |
| 24 |
//! > |
| 25 |
//! > The time may alternatively be followed by a time zone correction, |
| 26 |
//! > expressed as ‘shhmm’, where s is ‘+’ or ‘-’, hh is a number of zone hours |
| 27 |
//! > and mm is a number of zone minutes. The zone minutes term, mm, may be |
| 28 |
//! > omitted, in which case the one- or two-digit correction is interpreted as |
| 29 |
//! > a number of hours. You can also separate hh from mm with a colon. When a |
| 30 |
//! > time zone correction is given this way, it forces interpretation of the |
| 31 |
//! > time relative to Coordinated Universal Time (UTC), overriding any |
| 32 |
//! > previous specification for the time zone or the local time zone. For |
| 33 |
//! > example, ‘+0530’ and ‘+05:30’ both stand for the time zone 5.5 hours |
| 34 |
//! > ahead of UTC (e.g., India). This is the best way to specify a time zone |
| 35 |
//! > correction by fractional parts of an hour. The maximum zone correction is |
| 36 |
//! > 24 hours. |
| 37 |
//! > |
| 38 |
//! > Either ‘am’/‘pm’ or a time zone correction may be specified, but not both. |
| 39 |
|
| 40 |
use winnow::{ |
| 41 |
combinator::{alt, opt, preceded}, |
| 42 |
error::ErrMode, |
| 43 |
ModalResult, Parser, |
| 44 |
}; |
| 45 |
|
| 46 |
use super::{ |
| 47 |
epoch::sec_and_nsec, |
| 48 |
offset::{timezone_offset, Offset}, |
| 49 |
primitive::{colon, ctx_err, dec_uint, s}, |
| 50 |
}; |
| 51 |
|
| 52 |
#[derive(PartialEq, Clone, Debug, Default)] |
| 53 |
pub(crate) struct Time { |
| 54 |
pub(crate) hour: u8, |
| 55 |
pub(crate) minute: u8, |
| 56 |
pub(crate) second: u8, |
| 57 |
pub(crate) nanosecond: u32, |
| 58 |
pub(super) offset: Option<Offset>, |
| 59 |
} |
| 60 |
|
| 61 |
impl TryFrom<Time> for jiff::civil::Time { |
| 62 |
type Error = &'static str; |
| 63 |
|
| 64 |
fn try_from(time: Time) -> Result<Self, Self::Error> { |
| 65 |
jiff::civil::Time::new( |
| 66 |
time.hour as i8, |
| 67 |
time.minute as i8, |
| 68 |
time.second as i8, |
| 69 |
time.nanosecond as i32, |
| 70 |
) |
| 71 |
.map_err(|_| "time is not valid") |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
#[derive(Clone)] |
| 76 |
enum Meridiem { |
| 77 |
Am, |
| 78 |
Pm, |
| 79 |
} |
| 80 |
|
| 81 |
pub(crate) fn parse(input: &mut &str) -> ModalResult<Time> { |
| 82 |
alt((am_pm_time, iso)).parse_next(input) |
| 83 |
} |
| 84 |
|
| 85 |
/// Parse an ISO 8601 time string |
| 86 |
/// |
| 87 |
/// Also used by the [`combined`](super::combined) module |
| 88 |
pub(super) fn iso(input: &mut &str) -> ModalResult<Time> { |
| 89 |
alt(( |
| 90 |
(hour24, timezone_offset).map(|(hour, offset)| Time { |
| 91 |
hour, |
| 92 |
minute: 0, |
| 93 |
second: 0, |
| 94 |
nanosecond: 0, |
| 95 |
offset: Some(offset), |
| 96 |
}), |
| 97 |
( |
| 98 |
hour24, |
| 99 |
colon, |
| 100 |
minute, |
| 101 |
opt(preceded(colon, second)), |
| 102 |
opt(timezone_offset), |
| 103 |
) |
| 104 |
.map(|(hour, _, minute, sec_nsec, offset)| Time { |
| 105 |
hour, |
| 106 |
minute, |
| 107 |
second: sec_nsec.map_or(0, |(s, _)| s), |
| 108 |
nanosecond: sec_nsec.map_or(0, |(_, ns)| ns), |
| 109 |
offset, |
| 110 |
}), |
| 111 |
)) |
| 112 |
.parse_next(input) |
| 113 |
} |
| 114 |
|
| 115 |
/// Parse a time ending with AM or PM |
| 116 |
/// |
| 117 |
/// The hours are restricted to 12 or lower in this format |
| 118 |
fn am_pm_time(input: &mut &str) -> ModalResult<Time> { |
| 119 |
let (h, m, sec_nsec, meridiem) = ( |
| 120 |
hour12, |
| 121 |
opt(preceded(colon, minute)), |
| 122 |
opt(preceded(colon, second)), |
| 123 |
alt(( |
| 124 |
s("am").value(Meridiem::Am), |
| 125 |
s("a.m.").value(Meridiem::Am), |
| 126 |
s("pm").value(Meridiem::Pm), |
| 127 |
s("p.m.").value(Meridiem::Pm), |
| 128 |
)), |
| 129 |
) |
| 130 |
.parse_next(input)?; |
| 131 |
|
| 132 |
if h == 0 { |
| 133 |
return Err(ErrMode::Cut(ctx_err( |
| 134 |
"hour must be greater than 0 when meridiem is specified", |
| 135 |
))); |
| 136 |
} |
| 137 |
|
| 138 |
let mut h = h % 12; |
| 139 |
if let Meridiem::Pm = meridiem { |
| 140 |
h += 12; |
| 141 |
} |
| 142 |
Ok(Time { |
| 143 |
hour: h, |
| 144 |
minute: m.unwrap_or(0), |
| 145 |
second: sec_nsec.map_or(0, |(s, _)| s), |
| 146 |
nanosecond: sec_nsec.map_or(0, |(_, ns)| ns), |
| 147 |
offset: None, |
| 148 |
}) |
| 149 |
} |
| 150 |
|
| 151 |
/// Parse a number of hours in `0..24`. |
| 152 |
pub(super) fn hour24(input: &mut &str) -> ModalResult<u8> { |
| 153 |
s(dec_uint).verify(|x| *x < 24).parse_next(input) |
| 154 |
} |
| 155 |
|
| 156 |
/// Parse a number of hours in `0..=12`. |
| 157 |
fn hour12(input: &mut &str) -> ModalResult<u8> { |
| 158 |
s(dec_uint).verify(|x| *x <= 12).parse_next(input) |
| 159 |
} |
| 160 |
|
| 161 |
/// Parse a number of minutes in `0..60`. |
| 162 |
pub(super) fn minute(input: &mut &str) -> ModalResult<u8> { |
| 163 |
s(dec_uint).verify(|x| *x < 60).parse_next(input) |
| 164 |
} |
| 165 |
|
| 166 |
/// Parse a number of seconds in `0..60` and an optional number of nanoseconds |
| 167 |
/// (default to 0 if not set). |
| 168 |
fn second(input: &mut &str) -> ModalResult<(u8, u32)> { |
| 169 |
s(sec_and_nsec) |
| 170 |
.verify_map(|(s, ns)| if s < 60 { Some((s as u8, ns)) } else { None }) |
| 171 |
.parse_next(input) |
| 172 |
} |
| 173 |
|
| 174 |
#[cfg(test)] |
| 175 |
mod tests { |
| 176 |
use super::*; |
| 177 |
|
| 178 |
#[test] |
| 179 |
fn simple() { |
| 180 |
let reference = Time { |
| 181 |
hour: 20, |
| 182 |
minute: 2, |
| 183 |
second: 0, |
| 184 |
nanosecond: 0, |
| 185 |
offset: None, |
| 186 |
}; |
| 187 |
|
| 188 |
for mut s in [ |
| 189 |
"20:02:00.000000", |
| 190 |
"20:02:00", |
| 191 |
"20:02+:00", |
| 192 |
"20:02-:00", |
| 193 |
"20----:02--(these hyphens are ignored)--:00", |
| 194 |
"20++++:02++(these plusses are ignored)++:00", |
| 195 |
"20: (A comment!) 02 (Another comment!) :00", |
| 196 |
"20:02 (A nested (comment!)) :00", |
| 197 |
"20:02 (So (many (nested) comments!!!!)) :00", |
| 198 |
"20 : 02 : 00.000000", |
| 199 |
"20:02", |
| 200 |
"20 : 02", |
| 201 |
"8:02pm", |
| 202 |
"8: 02 pm", |
| 203 |
"8:02p.m.", |
| 204 |
"8: 02 p.m.", |
| 205 |
] { |
| 206 |
let old_s = s.to_owned(); |
| 207 |
assert_eq!( |
| 208 |
parse(&mut s).ok(), |
| 209 |
Some(reference.clone()), |
| 210 |
"Format string: {old_s}" |
| 211 |
); |
| 212 |
} |
| 213 |
} |
| 214 |
|
| 215 |
#[test] |
| 216 |
fn invalid() { |
| 217 |
assert!(parse(&mut "00:00am").is_err()); |
| 218 |
assert!(parse(&mut "00:00:00am").is_err()); |
| 219 |
} |
| 220 |
|
| 221 |
#[test] |
| 222 |
fn hours_only() { |
| 223 |
let reference = Time { |
| 224 |
hour: 11, |
| 225 |
minute: 0, |
| 226 |
second: 0, |
| 227 |
nanosecond: 0, |
| 228 |
offset: None, |
| 229 |
}; |
| 230 |
|
| 231 |
for mut s in [ |
| 232 |
"11am", |
| 233 |
"11 am", |
| 234 |
"11 - am", |
| 235 |
"11 + am", |
| 236 |
"11 a.m.", |
| 237 |
"11 : 00", |
| 238 |
"11:00:00", |
| 239 |
] { |
| 240 |
let old_s = s.to_owned(); |
| 241 |
assert_eq!( |
| 242 |
parse(&mut s).ok(), |
| 243 |
Some(reference.clone()), |
| 244 |
"Format string: {old_s}" |
| 245 |
); |
| 246 |
} |
| 247 |
} |
| 248 |
|
| 249 |
#[test] |
| 250 |
fn nanoseconds() { |
| 251 |
let reference = Time { |
| 252 |
hour: 11, |
| 253 |
minute: 0, |
| 254 |
second: 0, |
| 255 |
nanosecond: 123450000, |
| 256 |
offset: None, |
| 257 |
}; |
| 258 |
|
| 259 |
for mut s in ["11:00:00.12345", "11:00:00.12345am"] { |
| 260 |
let old_s = s.to_owned(); |
| 261 |
assert_eq!( |
| 262 |
parse(&mut s).ok(), |
| 263 |
Some(reference.clone()), |
| 264 |
"Format string: {old_s}" |
| 265 |
); |
| 266 |
} |
| 267 |
|
| 268 |
let reference = Time { |
| 269 |
hour: 11, |
| 270 |
minute: 0, |
| 271 |
second: 0, |
| 272 |
nanosecond: 123456789, |
| 273 |
offset: None, |
| 274 |
}; |
| 275 |
|
| 276 |
for mut s in ["11:00:00.123456789", "11:00:00.1234567890123"] { |
| 277 |
let old_s = s.to_owned(); |
| 278 |
assert_eq!( |
| 279 |
parse(&mut s).ok(), |
| 280 |
Some(reference.clone()), |
| 281 |
"Format string: {old_s}" |
| 282 |
); |
| 283 |
} |
| 284 |
} |
| 285 |
|
| 286 |
#[test] |
| 287 |
fn noon() { |
| 288 |
let reference = Time { |
| 289 |
hour: 12, |
| 290 |
minute: 0, |
| 291 |
second: 0, |
| 292 |
nanosecond: 0, |
| 293 |
offset: None, |
| 294 |
}; |
| 295 |
|
| 296 |
for mut s in [ |
| 297 |
"12:00", |
| 298 |
"12pm", |
| 299 |
"12 pm", |
| 300 |
"12 (A comment!) pm", |
| 301 |
"12 pm", |
| 302 |
"12 p.m.", |
| 303 |
] { |
| 304 |
let old_s = s.to_owned(); |
| 305 |
assert_eq!( |
| 306 |
parse(&mut s).ok(), |
| 307 |
Some(reference.clone()), |
| 308 |
"Format string: {old_s}" |
| 309 |
); |
| 310 |
} |
| 311 |
} |
| 312 |
|
| 313 |
#[test] |
| 314 |
fn midnight() { |
| 315 |
let reference = Time { |
| 316 |
hour: 0, |
| 317 |
minute: 0, |
| 318 |
second: 0, |
| 319 |
nanosecond: 0, |
| 320 |
offset: None, |
| 321 |
}; |
| 322 |
|
| 323 |
for mut s in ["00:00", "12am"] { |
| 324 |
let old_s = s.to_owned(); |
| 325 |
assert_eq!( |
| 326 |
parse(&mut s).ok(), |
| 327 |
Some(reference.clone()), |
| 328 |
"Format string: {old_s}" |
| 329 |
); |
| 330 |
} |
| 331 |
} |
| 332 |
|
| 333 |
#[test] |
| 334 |
fn offset_hours() { |
| 335 |
let reference = Time { |
| 336 |
hour: 1, |
| 337 |
minute: 23, |
| 338 |
second: 0, |
| 339 |
nanosecond: 0, |
| 340 |
offset: Some((false, 5, 0).try_into().unwrap()), |
| 341 |
}; |
| 342 |
|
| 343 |
for mut s in [ |
| 344 |
"1:23+5", |
| 345 |
"1:23 + 5", |
| 346 |
"1:23+05", |
| 347 |
"1:23 + 5 : 00", |
| 348 |
"1:23+05:00", |
| 349 |
"1:23+05:0", |
| 350 |
] { |
| 351 |
let old_s = s.to_owned(); |
| 352 |
assert_eq!( |
| 353 |
parse(&mut s).ok(), |
| 354 |
Some(reference.clone()), |
| 355 |
"Format string: {old_s}" |
| 356 |
); |
| 357 |
} |
| 358 |
} |
| 359 |
|
| 360 |
#[test] |
| 361 |
fn offset_hours_and_minutes() { |
| 362 |
let reference = Time { |
| 363 |
hour: 3, |
| 364 |
minute: 45, |
| 365 |
second: 0, |
| 366 |
nanosecond: 0, |
| 367 |
offset: Some((false, 5, 35).try_into().unwrap()), |
| 368 |
}; |
| 369 |
|
| 370 |
for mut s in [ |
| 371 |
"3:45+535", |
| 372 |
"3:45-+535", |
| 373 |
"03:45+535", |
| 374 |
"3 : 45 + 535", |
| 375 |
"3:45+0535", |
| 376 |
"3:45+5:35", |
| 377 |
"3:45+05:35", |
| 378 |
"3:45 + 05 : 35", |
| 379 |
] { |
| 380 |
let old_s = s.to_owned(); |
| 381 |
assert_eq!( |
| 382 |
parse(&mut s).ok(), |
| 383 |
Some(reference.clone()), |
| 384 |
"Format string: {old_s}" |
| 385 |
); |
| 386 |
} |
| 387 |
} |
| 388 |
|
| 389 |
#[test] |
| 390 |
fn offset_minutes() { |
| 391 |
let reference = Time { |
| 392 |
hour: 3, |
| 393 |
minute: 45, |
| 394 |
second: 0, |
| 395 |
nanosecond: 0, |
| 396 |
offset: Some((false, 0, 35).try_into().unwrap()), |
| 397 |
}; |
| 398 |
|
| 399 |
for mut s in [ |
| 400 |
"3:45+035", |
| 401 |
"03:45+035", |
| 402 |
"3 : 45 + 035", |
| 403 |
"3:45+0035", |
| 404 |
"3:45+0:35", |
| 405 |
"3:45+00:35", |
| 406 |
"3:45 + 00 : 35", |
| 407 |
] { |
| 408 |
let old_s = s.to_owned(); |
| 409 |
assert_eq!( |
| 410 |
parse(&mut s).ok(), |
| 411 |
Some(reference.clone()), |
| 412 |
"Format string: {old_s}" |
| 413 |
); |
| 414 |
} |
| 415 |
} |
| 416 |
|
| 417 |
#[test] |
| 418 |
fn offset_negative() { |
| 419 |
let reference = Time { |
| 420 |
hour: 3, |
| 421 |
minute: 45, |
| 422 |
second: 0, |
| 423 |
nanosecond: 0, |
| 424 |
offset: Some((true, 5, 35).try_into().unwrap()), |
| 425 |
}; |
| 426 |
|
| 427 |
for mut s in [ |
| 428 |
"3:45-535", |
| 429 |
"03:45-535", |
| 430 |
"3 : 45 - 535", |
| 431 |
"3:45-0535", |
| 432 |
"3:45-5:35", |
| 433 |
"3:45-05:35", |
| 434 |
"3:45 - 05 : 35", |
| 435 |
] { |
| 436 |
let old_s = s.to_owned(); |
| 437 |
assert_eq!( |
| 438 |
parse(&mut s).ok(), |
| 439 |
Some(reference.clone()), |
| 440 |
"Format string: {old_s}" |
| 441 |
); |
| 442 |
} |
| 443 |
} |
| 444 |
} |