speedate 0.17.0

Fast and simple datetime, date, time and duration parsing
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
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
use std::cmp::Ordering;
use std::default::Default;
use std::fmt;
use std::str::FromStr;

use crate::config::TimeConfigBuilder;
use crate::{get_digit, get_digit_unchecked, ConfigError, ParseError, TimeConfig};

/// A Time
///
/// Allowed formats:
/// * `HH:MM:SS`
/// * `HH:MM:SS.FFFFFF` 1 to 6 digits are allowed
/// * `HH:MM`
/// * `HH:MM:SSZ`
/// * `HH:MM:SS.FFFFFFZ`
///
/// Fractions of a second are to microsecond precision, if the value contains greater
/// precision, an error is raised.
///
/// # Comparison
///
/// `Time` supports equality (`==`) and inequality (`>`, `<`, `>=`, `<=`) comparisons.
///
/// See [Time::partial_cmp] for how this works.
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub struct Time {
    /// Hour: 0 to 23
    pub hour: u8,
    /// Minute: 0 to 59
    pub minute: u8,
    /// Second: 0 to 59
    pub second: u8,
    /// microseconds: 0 to 999999
    pub microsecond: u32,
    /// timezone offset in seconds if provided, must be >-24h and <24h
    // This range is to match python,
    // Note: [Stack Overflow suggests](https://stackoverflow.com/a/8131056/949890) larger offsets can happen
    pub tz_offset: Option<i32>,
}

impl fmt::Display for Time {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        if self.microsecond != 0 {
            let mut buf: [u8; 15] = *b"00:00:00.000000";
            crate::display_num_buf(2, 0, self.hour as u32, &mut buf);
            crate::display_num_buf(2, 3, self.minute as u32, &mut buf);
            crate::display_num_buf(2, 6, self.second as u32, &mut buf);
            crate::display_num_buf(6, 9, self.microsecond, &mut buf);
            f.write_str(std::str::from_utf8(&buf[..]).unwrap())?
        } else {
            let mut buf: [u8; 8] = *b"00:00:00";
            crate::display_num_buf(2, 0, self.hour as u32, &mut buf);
            crate::display_num_buf(2, 3, self.minute as u32, &mut buf);
            crate::display_num_buf(2, 6, self.second as u32, &mut buf);
            f.write_str(std::str::from_utf8(&buf[..]).unwrap())?
        }
        if let Some(tz_offset) = self.tz_offset {
            if tz_offset == 0 {
                write!(f, "Z")?;
            } else {
                // tz offset is given in seconds, so we do convertions from seconds -> mins -> hours
                let total_minutes = tz_offset / 60;
                let hours = total_minutes / 60;
                let minutes = total_minutes % 60;
                let mut buf: [u8; 6] = *b"+00:00";
                if tz_offset < 0 {
                    buf[0] = b'-';
                }
                crate::display_num_buf(2, 1, hours.unsigned_abs(), &mut buf);
                crate::display_num_buf(2, 4, minutes.unsigned_abs(), &mut buf);
                f.write_str(std::str::from_utf8(&buf[..]).unwrap())?;
            }
        }
        Ok(())
    }
}

impl FromStr for Time {
    type Err = ParseError;

    #[inline]
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        Self::parse_str(s)
    }
}

impl PartialOrd for Time {
    /// Compare two times by inequality.
    ///
    /// `Time` supports equality (`==`, `!=`) and inequality comparisons (`>`, `<`, `>=` & `<=`).
    ///
    /// # Examples
    ///
    /// ```
    /// use speedate::Time;
    ///
    /// let t1 = Time::parse_str("04:05:06.07").unwrap();
    /// let t2 = Time::parse_str("04:05:06.08").unwrap();
    ///
    /// assert!(t1 < t2);
    /// ```
    ///
    ///  # Comparison with Timezones
    ///
    /// When comparing two times, we want "less than" or "greater than" refer to "earlier" or "later"
    /// in the absolute course of time. We therefore need to be careful when comparing times with different
    /// timezones. (If it wasn't for timezones, we could omit all this extra logic and thinking and just compare
    /// struct members directly as we do with [crate::Date] and [crate::Duration]).
    ///
    /// See [crate::DateTime::partial_cmp] for more information about comparisons with timezones.
    ///
    /// ## Timezone Examples
    ///
    /// ```
    /// use speedate::Time;
    ///
    /// let t1 = Time::parse_str("15:00:00Z").unwrap();
    /// let t2 = Time::parse_str("15:00:00+01:00").unwrap();
    ///
    /// assert!(t1 > t2);
    ///
    /// let t3 = Time::parse_str("15:00:00-01:00").unwrap();
    /// let t4 = Time::parse_str("15:00:00+01:00").unwrap();
    ///
    /// assert!(t3 > t4);
    /// ```
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        match (self.tz_offset, other.tz_offset) {
            (Some(tz_offset), Some(other_tz_offset)) => match (self.total_seconds() as i64 - tz_offset as i64)
                .partial_cmp(&(other.total_seconds() as i64 - other_tz_offset as i64))
            {
                Some(Ordering::Equal) => self.microsecond.partial_cmp(&other.microsecond),
                otherwise => otherwise,
            },
            _ => match self.total_seconds().partial_cmp(&other.total_seconds()) {
                Some(Ordering::Equal) => self.microsecond.partial_cmp(&other.microsecond),
                otherwise => otherwise,
            },
        }
    }
}

impl Time {
    /// Parse a time from a string
    ///
    /// # Arguments
    ///
    /// * `str` - The string to parse
    ///
    /// # Examples
    ///
    /// ```
    /// use speedate::Time;
    ///
    /// let d = Time::parse_str("12:13:14.123456").unwrap();
    /// assert_eq!(
    ///     d,
    ///     Time {
    ///         hour: 12,
    ///         minute: 13,
    ///         second: 14,
    ///         microsecond: 123456,
    ///         tz_offset: None,
    ///     }
    /// );
    /// assert_eq!(d.to_string(), "12:13:14.123456");
    /// ```
    #[inline]
    pub fn parse_str(str: &str) -> Result<Self, ParseError> {
        Self::parse_bytes(str.as_bytes())
    }

    /// Parse a time from bytes
    ///
    /// # Arguments
    ///
    /// * `bytes` - The bytes to parse
    ///
    /// # Examples
    ///
    /// ```
    /// use speedate::Time;
    ///
    /// let d = Time::parse_bytes(b"12:13:14.123456").unwrap();
    /// assert_eq!(
    ///     d,
    ///     Time {
    ///         hour: 12,
    ///         minute: 13,
    ///         second: 14,
    ///         microsecond: 123456,
    ///         tz_offset: None,
    ///     }
    /// );
    /// assert_eq!(d.to_string(), "12:13:14.123456");
    /// ```
    #[inline]
    pub fn parse_bytes(bytes: &[u8]) -> Result<Self, ParseError> {
        Self::parse_bytes_offset(bytes, 0, &TimeConfigBuilder::new().build())
    }

    /// Same as `Time::parse_bytes` but with a `TimeConfig`.
    ///
    /// # Arguments
    ///
    /// * `bytes` - The bytes to parse
    /// * `config` - The `TimeConfig` to use
    ///
    /// # Examples
    ///
    /// ```
    /// use speedate::{Time, TimeConfigBuilder};
    ///
    /// let d = Time::parse_bytes_with_config(b"12:13:14.123456", &TimeConfigBuilder::new().build()).unwrap();
    /// assert_eq!(
    ///     d,
    ///     Time {
    ///         hour: 12,
    ///         minute: 13,
    ///         second: 14,
    ///         microsecond: 123456,
    ///         tz_offset: None,
    ///     }
    /// );
    /// assert_eq!(d.to_string(), "12:13:14.123456");
    /// ```
    #[inline]
    pub fn parse_bytes_with_config(bytes: &[u8], config: &TimeConfig) -> Result<Self, ParseError> {
        Self::parse_bytes_offset(bytes, 0, config)
    }

    /// Create a time from seconds and microseconds.
    ///
    /// # Arguments
    ///
    /// * `timestamp_second` - timestamp in seconds
    /// * `timestamp_microsecond` - microseconds fraction of a second timestamp
    ///
    /// If `seconds + timestamp_microsecond` exceeds 86400, an error is returned.
    ///
    /// # Examples
    ///
    /// ```
    /// use speedate::Time;
    ///
    /// let d = Time::from_timestamp(3740, 123).unwrap();
    /// assert_eq!(d.to_string(), "01:02:20.000123");
    /// ```
    pub fn from_timestamp(timestamp_second: u32, timestamp_microsecond: u32) -> Result<Self, ParseError> {
        Time::from_timestamp_with_config(
            timestamp_second,
            timestamp_microsecond,
            &TimeConfigBuilder::new().build(),
        )
    }

    /// Like `from_timestamp` but with a `TimeConfig`
    ///
    /// # Arguments
    ///
    /// * `timestamp_second` - timestamp in seconds
    /// * `timestamp_microsecond` - microseconds fraction of a second timestamp
    /// * `config` - the `TimeConfig` to use
    ///
    /// If `seconds + timestamp_microsecond` exceeds 86400, an error is returned.
    ///
    /// # Examples
    ///
    /// ```
    /// use speedate::{Time, TimeConfigBuilder};
    ///
    /// let d = Time::from_timestamp_with_config(3740, 123, &TimeConfigBuilder::new().build()).unwrap();
    /// assert_eq!(d.to_string(), "01:02:20.000123");
    /// ```
    pub fn from_timestamp_with_config(
        timestamp_second: u32,
        timestamp_microsecond: u32,
        config: &TimeConfig,
    ) -> Result<Self, ParseError> {
        let mut second = timestamp_second;
        let mut microsecond = timestamp_microsecond;
        if microsecond >= 1_000_000 {
            second = second
                .checked_add(microsecond / 1_000_000)
                .ok_or(ParseError::TimeTooLarge)?;
            microsecond %= 1_000_000;
        }
        if second >= 86_400 {
            return Err(ParseError::TimeTooLarge);
        }
        Ok(Self {
            hour: (second / 3600) as u8,
            minute: ((second % 3600) / 60) as u8,
            second: (second % 60) as u8,
            microsecond,
            tz_offset: config.unix_timestamp_offset,
        })
    }

    /// Parse a time from bytes with a starting index, extra characters at the end of the string result in an error
    pub(crate) fn parse_bytes_offset(bytes: &[u8], offset: usize, config: &TimeConfig) -> Result<Self, ParseError> {
        let pure_time = PureTime::parse(bytes, offset, config)?;

        // Parse the timezone offset
        let mut tz_offset: Option<i32> = None;
        let mut position = pure_time.position;

        if let Some(next_char) = bytes.get(position).copied() {
            position += 1;
            if next_char == b'Z' || next_char == b'z' {
                tz_offset = Some(0);
            } else {
                let sign = match next_char {
                    b'+' => 1,
                    b'-' => -1,
                    226 => {
                        // U+2212 MINUS "−" is allowed under ISO 8601 for negative timezones
                        // > python -c 'print([c for c in "−".encode()])'
                        // its raw byte values are [226, 136, 146]
                        if bytes.get(position).copied() != Some(136) {
                            return Err(ParseError::InvalidCharTzSign);
                        }
                        if bytes.get(position + 1).copied() != Some(146) {
                            return Err(ParseError::InvalidCharTzSign);
                        }
                        position += 2;
                        -1
                    }
                    _ => return Err(ParseError::InvalidCharTzSign),
                };

                let h1 = get_digit!(bytes, position, InvalidCharTzHour) as i32;
                let h2 = get_digit!(bytes, position + 1, InvalidCharTzHour) as i32;

                let m1 = match bytes.get(position + 2) {
                    Some(b':') => {
                        position += 3;
                        get_digit!(bytes, position, InvalidCharTzMinute) as i32
                    }
                    Some(c) if c.is_ascii_digit() => {
                        position += 2;
                        (c - b'0') as i32
                    }
                    _ => return Err(ParseError::InvalidCharTzMinute),
                };
                let m2 = get_digit!(bytes, position + 1, InvalidCharTzMinute) as i32;

                let minute_seconds = m1 * 600 + m2 * 60;
                if minute_seconds >= 3600 {
                    return Err(ParseError::OutOfRangeTzMinute);
                }

                let offset_val = sign * (h1 * 36000 + h2 * 3600 + minute_seconds);
                // TZ must be less than 24 hours to match python
                if offset_val.abs() >= 24 * 3600 {
                    return Err(ParseError::OutOfRangeTz);
                }
                tz_offset = Some(offset_val);
                position += 2;
            }
        }

        if bytes.len() > position {
            return Err(ParseError::ExtraCharacters);
        }

        Ok(Self {
            hour: pure_time.hour,
            minute: pure_time.minute,
            second: pure_time.second,
            microsecond: pure_time.microsecond,
            tz_offset,
        })
    }

    /// Get the total seconds of the time
    ///
    /// E.g. hours + minutes + seconds
    ///
    /// # Examples
    ///
    /// ```
    /// use speedate::Time;
    ///
    /// let d = Time::parse_str("12:13:14.123456").unwrap();
    /// assert_eq!(d.total_seconds(), 12 * 3600 + 13 * 60 + 14);
    /// ```
    pub fn total_seconds(&self) -> u32 {
        let mut total_seconds = self.hour as u32 * 3600;
        total_seconds += self.minute as u32 * 60;
        total_seconds += self.second as u32;
        total_seconds
    }

    /// Get the total milliseconds of the time.
    ///
    /// # Examples
    ///
    /// ```
    /// use speedate::Time;
    ///
    /// let d = Time::parse_str("12:13:14.123456").unwrap();
    /// assert_eq!(d.total_ms(), (12 * 3600 + 13 * 60 + 14) * 1000 + 123);
    /// ```
    pub fn total_ms(&self) -> u32 {
        self.total_seconds() * 1000 + self.microsecond / 1000
    }

    /// Clone the time and set a new timezone offset.
    ///
    /// The returned time will represent a different point in time since the timezone offset is changed without
    /// modifying the time. See [Time::in_timezone] for alternative behaviour.
    ///
    /// # Arguments
    ///
    /// * `tz_offset` - optional timezone offset in seconds.
    ///
    /// This method will return `Err(ParseError::OutOfRangeTz)` if `abs(tz_offset)` is not less than
    /// 24 hours - `86_400` seconds.
    ///
    /// # Examples
    ///
    /// ```
    /// use speedate::Time;
    ///
    /// let t1 = Time::parse_str("12:13:14Z").unwrap();
    ///
    /// let t2 = t1.with_timezone_offset(Some(-8 * 3600)).unwrap();
    /// assert_eq!(t2.to_string(), "12:13:14-08:00");
    /// ```
    pub fn with_timezone_offset(&self, tz_offset: Option<i32>) -> Result<Self, ParseError> {
        if let Some(offset_val) = tz_offset {
            if offset_val.abs() >= 24 * 3600 {
                return Err(ParseError::OutOfRangeTz);
            }
        }
        let mut time = *self;
        time.tz_offset = tz_offset;
        Ok(time)
    }

    /// Create a new time in a different timezone.
    /// See [Time::with_timezone_offset] for alternative behaviour.
    ///
    /// The time must have an offset, otherwise a `ParseError::TzRequired` error is returned.
    ///
    /// # Arguments
    ///
    /// * `tz_offset` - new timezone offset in seconds.
    ///
    /// # Examples
    ///
    /// ```
    /// use speedate::Time;
    ///
    /// let t1 = Time::parse_str("15:00:00Z").unwrap();
    ///
    /// let t2 = t1.in_timezone(7200).unwrap();
    // / assert_eq!(t2.to_string(), "17:00:00+02:00");
    /// ```
    pub fn in_timezone(&self, tz_offset: i32) -> Result<Self, ParseError> {
        if tz_offset.abs() >= 24 * 3600 {
            Err(ParseError::OutOfRangeTz)
        } else if let Some(current_offset) = self.tz_offset {
            let offset = tz_offset - current_offset;
            let seconds = self.total_seconds().saturating_add_signed(offset);
            let mut time = Self::from_timestamp(seconds, self.microsecond)?;
            time.tz_offset = Some(offset);
            Ok(time)
        } else {
            Err(ParseError::TzRequired)
        }
    }
}

/// Used internally for parsing both times and durations from time format
pub(crate) struct PureTime {
    /// Hour: 0 to 23
    hour: u8,
    /// Minute: 0 to 59
    minute: u8,
    /// Second: 0 to 59
    second: u8,
    /// microseconds: 0 to 999999
    pub microsecond: u32,
    /// position of the cursor after parsing
    pub position: usize,
}

impl PureTime {
    pub fn parse(bytes: &[u8], offset: usize, config: &TimeConfig) -> Result<Self, ParseError> {
        if bytes.len() - offset < 5 {
            return Err(ParseError::TooShort);
        }
        let hour: u8;
        let minute: u8;
        unsafe {
            let h1 = get_digit_unchecked!(bytes, offset, InvalidCharHour);
            let h2 = get_digit_unchecked!(bytes, offset + 1, InvalidCharHour);
            hour = h1 * 10 + h2;

            match bytes.get_unchecked(offset + 2) {
                b':' => (),
                _ => return Err(ParseError::InvalidCharTimeSep),
            }
            let m1 = get_digit_unchecked!(bytes, offset + 3, InvalidCharMinute);
            let m2 = get_digit_unchecked!(bytes, offset + 4, InvalidCharMinute);
            minute = m1 * 10 + m2;
        }

        if hour > 23 {
            return Err(ParseError::OutOfRangeHour);
        }

        if minute > 59 {
            return Err(ParseError::OutOfRangeMinute);
        }

        let mut length: usize = 5;
        let (second, microsecond) = match bytes.get(offset + 5) {
            Some(b':') => {
                let s1 = get_digit!(bytes, offset + 6, InvalidCharSecond);
                let s2 = get_digit!(bytes, offset + 7, InvalidCharSecond);
                let second = s1 * 10 + s2;
                if second > 59 {
                    return Err(ParseError::OutOfRangeSecond);
                }
                length = 8;

                let mut microsecond = 0;
                let frac_sep = bytes.get(offset + 8).copied();
                if frac_sep == Some(b'.') || frac_sep == Some(b',') {
                    length = 9;
                    let mut i: usize = 0;
                    loop {
                        match bytes.get(offset + length + i) {
                            Some(c) if c.is_ascii_digit() => {
                                // If we've passed `i=6` then we are "truncating" the extra precision
                                // The easiest way to do this is to simply no-op and continue the loop
                                if i < 6 {
                                    microsecond *= 10;
                                    microsecond += (c - b'0') as u32;
                                }
                            }
                            _ => {
                                break;
                            }
                        }
                        i += 1;
                        if i > 6 {
                            match config.microseconds_precision_overflow_behavior {
                                MicrosecondsPrecisionOverflowBehavior::Truncate => continue,
                                MicrosecondsPrecisionOverflowBehavior::Error => {
                                    return Err(ParseError::SecondFractionTooLong)
                                }
                            }
                        }
                    }
                    if i == 0 {
                        return Err(ParseError::SecondFractionMissing);
                    }
                    if i < 6 {
                        microsecond *= 10_u32.pow(6 - i as u32);
                    }
                    length += i;
                }
                (second, microsecond)
            }
            _ => (0, 0),
        };

        Ok(Self {
            hour,
            minute,
            second,
            microsecond,
            position: offset + length,
        })
    }

    pub fn total_seconds(&self) -> u32 {
        self.hour as u32 * 3_600 + self.minute as u32 * 60 + self.second as u32
    }
}

#[derive(Debug, Clone, Default, Copy, PartialEq)]
pub enum MicrosecondsPrecisionOverflowBehavior {
    Truncate,
    #[default]
    Error,
}

impl FromStr for MicrosecondsPrecisionOverflowBehavior {
    type Err = ConfigError;
    fn from_str(value: &str) -> Result<Self, ConfigError> {
        match value.to_lowercase().as_str() {
            "truncate" => Ok(Self::Truncate),
            "error" => Ok(Self::Error),
            _ => Err(ConfigError::UnknownMicrosecondsPrecisionOverflowBehaviorString),
        }
    }
}