timespan/naive/
naive_time_span.rs

1// timespan - A simple timespan for chrono times.
2//
3// Copyright (C) 2017
4//     Fin Christensen <fin.christensen@posteo.de>
5//
6// This program is free software: you can redistribute it and/or modify
7// it under the terms of the GNU General Public License as published by
8// the Free Software Foundation, either version 3 of the License, or
9// (at your option) any later version.
10//
11// This program is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14// GNU General Public License for more details.
15//
16// You should have received a copy of the GNU General Public License
17// along with this program.  If not, see <http://www.gnu.org/licenses/>.
18
19use crate::Error;
20use crate::Formatable;
21use crate::Parsable;
22use crate::Span;
23use crate::Spanable;
24use chrono::format::{DelayedFormat, StrftimeItems};
25use chrono::{Duration, NaiveTime};
26
27impl Spanable for NaiveTime {
28    #[inline]
29    fn signed_duration_since(self, other: Self) -> Duration {
30        NaiveTime::signed_duration_since(self, other)
31    }
32}
33
34impl Formatable for NaiveTime {
35    #[inline]
36    fn format<'a>(&self, fmt: &'a str) -> DelayedFormat<StrftimeItems<'a>> {
37        NaiveTime::format(self, fmt)
38    }
39}
40
41impl Parsable for NaiveTime {
42    #[inline]
43    fn parse_from_str(s: &str, fmt: &str) -> Result<Self, Error> {
44        NaiveTime::parse_from_str(s, fmt).map_err(|e| Error::Parsing(e))
45    }
46}
47
48/// The `NaiveTimeSpan` alias is a span consisting of `chrono::NaiveTime`s.
49///
50/// It can be used to represent time spans that do not depend on a specific time zone.
51///
52/// The `NaiveTimeSpan` can be formatted and parsed from a string. It has full
53/// support for `serde` serialization and deserialization.
54///
55/// # Example
56///
57/// ~~~~
58/// # extern crate timespan; fn main() {
59/// use timespan::NaiveTimeSpan;
60///
61/// let a: NaiveTimeSpan = "17:30:00 - 19:15:00".parse().unwrap();
62/// let b = NaiveTimeSpan::parse_from_str(
63///     "05.30 PM - 07.15 PM",
64///     "{start} - {end}",
65///     "%I.%M %P", "%I.%M %P"
66/// ).unwrap();
67///
68/// let f = a.format("from {start} to {end}", "%R", "%R");
69/// assert!(format!("{}", f) == "from 17:30 to 19:15");
70/// assert!(a == b);
71/// # }
72/// ~~~~
73pub type NaiveTimeSpan = Span<NaiveTime>;