|
15 | 15 | //! > ‘@1483228800’ represents 2017-01-01 00:00:00 UTC, and there is no way to |
16 | 16 | //! > represent the intervening leap second 2016-12-31 23:59:60 UTC. |
17 | 17 |
|
18 | | -use winnow::{combinator::preceded, ModalResult, Parser}; |
| 18 | +use winnow::{ |
| 19 | + ascii::digit1, |
| 20 | + combinator::{opt, preceded}, |
| 21 | + token::one_of, |
| 22 | + ModalResult, Parser, |
| 23 | +}; |
19 | 24 |
|
20 | | -use super::primitive::{float, s}; |
| 25 | +use super::primitive::{dec_uint, s}; |
21 | 26 |
|
22 | | -/// Parse a timestamp in the form of `@1234567890`. |
23 | | -pub fn parse(input: &mut &str) -> ModalResult<f64> { |
24 | | - s(preceded("@", float)).parse_next(input) |
| 27 | +/// Represents a timestamp with nanosecond accuracy. |
| 28 | +/// |
| 29 | +/// # Invariants |
| 30 | +/// |
| 31 | +/// - `nanosecond` is always in the range of `0..1_000_000_000`. |
| 32 | +/// - Negative timestamps are represented by a negative `second` value and a |
| 33 | +/// positive `nanosecond` value. |
| 34 | +#[derive(Debug, PartialEq)] |
| 35 | +pub(crate) struct Timestamp { |
| 36 | + pub(crate) second: i64, |
| 37 | + pub(crate) nanosecond: u32, |
| 38 | +} |
| 39 | + |
| 40 | +/// Parse a timestamp in the form of `1234567890` or `-1234567890.12345` or |
| 41 | +/// `1234567890,12345`. |
| 42 | +pub(crate) fn parse(input: &mut &str) -> ModalResult<Timestamp> { |
| 43 | + (s("@"), opt(s(one_of(['-', '+']))), sec_and_nsec) |
| 44 | + .verify_map(|(_, sign, (sec, nsec))| { |
| 45 | + let sec = i64::try_from(sec).ok()?; |
| 46 | + let (second, nanosecond) = match (sign, nsec) { |
| 47 | + (Some('-'), 0) => (-sec, 0), |
| 48 | + // Truncate towards minus infinity. |
| 49 | + (Some('-'), _) => ((-sec).checked_sub(1)?, 1_000_000_000 - nsec), |
| 50 | + _ => (sec, nsec), |
| 51 | + }; |
| 52 | + Some(Timestamp { second, nanosecond }) |
| 53 | + }) |
| 54 | + .parse_next(input) |
| 55 | +} |
| 56 | + |
| 57 | +/// Parse a second value in the form of `1234567890` or `1234567890.12345` or |
| 58 | +/// `1234567890,12345`. |
| 59 | +/// |
| 60 | +/// The first part represents whole seconds. The optional second part represents |
| 61 | +/// fractional seconds, parsed as a nanosecond value from up to 9 digits |
| 62 | +/// (padded with zeros on the right if fewer digits are present). If the second |
| 63 | +/// part is omitted, it defaults to 0 nanoseconds. |
| 64 | +pub(super) fn sec_and_nsec(input: &mut &str) -> ModalResult<(u64, u32)> { |
| 65 | + (s(dec_uint), opt(preceded(one_of(['.', ',']), digit1))) |
| 66 | + .verify_map(|(sec, opt_nsec_str)| match opt_nsec_str { |
| 67 | + Some(nsec_str) if nsec_str.len() >= 9 => Some((sec, nsec_str[..9].parse().ok()?)), |
| 68 | + Some(nsec_str) => { |
| 69 | + let multiplier = 10_u32.pow(9 - nsec_str.len() as u32); |
| 70 | + Some((sec, nsec_str.parse::<u32>().ok()?.checked_mul(multiplier)?)) |
| 71 | + } |
| 72 | + None => Some((sec, 0)), |
| 73 | + }) |
| 74 | + .parse_next(input) |
25 | 75 | } |
26 | 76 |
|
27 | 77 | #[cfg(test)] |
28 | 78 | mod tests { |
29 | | - use super::parse; |
| 79 | + use super::*; |
| 80 | + |
| 81 | + #[test] |
| 82 | + fn sec_and_nsec_test() { |
| 83 | + let mut input = "1234567890"; |
| 84 | + assert_eq!(sec_and_nsec(&mut input).unwrap(), (1234567890, 0)); |
30 | 85 |
|
31 | | - fn float_eq(a: f64, b: f64) -> bool { |
32 | | - (a - b).abs() < f64::EPSILON |
| 86 | + let mut input = "1234567890.12345"; |
| 87 | + assert_eq!(sec_and_nsec(&mut input).unwrap(), (1234567890, 123450000)); |
| 88 | + |
| 89 | + let mut input = "1234567890,12345"; |
| 90 | + assert_eq!(sec_and_nsec(&mut input).unwrap(), (1234567890, 123450000)); |
| 91 | + |
| 92 | + let mut input = "1234567890.1234567890123"; |
| 93 | + assert_eq!(sec_and_nsec(&mut input).unwrap(), (1234567890, 123456789)); |
33 | 94 | } |
34 | 95 |
|
35 | 96 | #[test] |
36 | | - fn float() { |
| 97 | + fn timestamp() { |
37 | 98 | let mut input = "@1234567890"; |
38 | | - assert!(float_eq(parse(&mut input).unwrap(), 1234567890.0)); |
| 99 | + assert_eq!( |
| 100 | + parse(&mut input).unwrap(), |
| 101 | + Timestamp { |
| 102 | + second: 1234567890, |
| 103 | + nanosecond: 0, |
| 104 | + } |
| 105 | + ); |
| 106 | + |
| 107 | + let mut input = "@ 1234567890"; |
| 108 | + assert_eq!( |
| 109 | + parse(&mut input).unwrap(), |
| 110 | + Timestamp { |
| 111 | + second: 1234567890, |
| 112 | + nanosecond: 0, |
| 113 | + } |
| 114 | + ); |
| 115 | + |
| 116 | + let mut input = "@ -1234567890"; |
| 117 | + assert_eq!( |
| 118 | + parse(&mut input).unwrap(), |
| 119 | + Timestamp { |
| 120 | + second: -1234567890, |
| 121 | + nanosecond: 0, |
| 122 | + } |
| 123 | + ); |
| 124 | + |
| 125 | + let mut input = "@ - 1234567890"; |
| 126 | + assert_eq!( |
| 127 | + parse(&mut input).unwrap(), |
| 128 | + Timestamp { |
| 129 | + second: -1234567890, |
| 130 | + nanosecond: 0, |
| 131 | + } |
| 132 | + ); |
39 | 133 |
|
40 | 134 | let mut input = "@1234567890.12345"; |
41 | | - assert!(float_eq(parse(&mut input).unwrap(), 1234567890.12345)); |
| 135 | + assert_eq!( |
| 136 | + parse(&mut input).unwrap(), |
| 137 | + Timestamp { |
| 138 | + second: 1234567890, |
| 139 | + nanosecond: 123450000, |
| 140 | + } |
| 141 | + ); |
42 | 142 |
|
43 | 143 | let mut input = "@1234567890,12345"; |
44 | | - assert!(float_eq(parse(&mut input).unwrap(), 1234567890.12345)); |
| 144 | + assert_eq!( |
| 145 | + parse(&mut input).unwrap(), |
| 146 | + Timestamp { |
| 147 | + second: 1234567890, |
| 148 | + nanosecond: 123450000, |
| 149 | + } |
| 150 | + ); |
45 | 151 |
|
46 | 152 | let mut input = "@-1234567890.12345"; |
47 | | - assert_eq!(parse(&mut input).unwrap(), -1234567890.12345); |
| 153 | + assert_eq!( |
| 154 | + parse(&mut input).unwrap(), |
| 155 | + Timestamp { |
| 156 | + second: -1234567891, |
| 157 | + nanosecond: 876550000, |
| 158 | + } |
| 159 | + ); |
48 | 160 | } |
49 | 161 | } |
0 commit comments