Debian Codegraph @

.cargo/vendor/delharc/src/header/timestamp.rs

raw
1 use core::fmt;
2 use chrono::{LocalResult, prelude::*};
3
4 /// The type returned when parsing last modified timestamp.
5 #[derive(Debug, Clone, PartialEq, Eq)]
6 pub enum TimestampResult {
7 /// The timestamp could not be parsed.
8 None,
9 /// The timestamp value lacks the information about the time zone.
10 Naive(NaiveDateTime),
11 /// The timestamp value in the UTC time zone.
12 Utc(DateTime<Utc>)
13 }
14
15 impl TimestampResult {
16 pub fn is_none(&self) -> bool {
17 if let TimestampResult::None = self {
18 return true
19 }
20 false
21 }
22
23 pub fn is_naive(&self) -> bool {
24 if let TimestampResult::Naive(..) = self {
25 return true
26 }
27 false
28 }
29
30 pub fn is_utc(&self) -> bool {
31 if let TimestampResult::Utc(..) = self {
32 return true
33 }
34 false
35 }
36
37 /// Return a `Naive` date and time variant as is or `Utc` variant as naive date time in the UTC time zone.
38 pub fn to_naive_utc(&self) -> Option<NaiveDateTime> {
39 match self {
40 TimestampResult::Naive(dt) => Some(*dt),
41 TimestampResult::Utc(dt) => Some(dt.naive_utc()),
42 _ => None
43 }
44 }
45
46 /// Return a `Naive` date and time variant as is or `Utc` variant as naive date time in the `Local` time zone.
47 pub fn to_naive_local(&self) -> Option<NaiveDateTime> {
48 match self {
49 TimestampResult::Naive(dt) => Some(*dt),
50 TimestampResult::Utc(dt) => Some(dt.naive_local()),
51 _ => None
52 }
53 }
54
55 /// Return a date time in the UTC time zone.
56 ///
57 /// In this instance the `Naive` date and time variant is assumed to be in the UTC time zone.
58 pub fn to_utc(&self) -> Option<DateTime<Utc>> {
59 match self {
60 TimestampResult::Naive(dt) => Some(DateTime::from_naive_utc_and_offset(*dt, Utc)),
61 TimestampResult::Utc(dt) => Some(*dt),
62 _ => None
63 }
64 }
65
66 #[cfg(feature = "std")]
67 /// Return a date time in the `Local` time zone.
68 ///
69 /// In this instance the `Naive` date and time variant is assumed to be in the `Local` time zone.
70 ///
71 /// # `no_std`
72 ///
73 /// This method is only available with `std` feature enabled.
74 pub fn to_local(&self) -> Option<DateTime<Local>> {
75 match self {
76 TimestampResult::Naive(dt) => Local.from_local_datetime(dt).single(),
77 TimestampResult::Utc(dt) => Some(dt.with_timezone(&Local)),
78 _ => None
79 }
80 }
81 }
82
83 impl fmt::Display for TimestampResult {
84 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
85 match self {
86 TimestampResult::None => "-".fmt(f),
87 TimestampResult::Naive(dt) => dt.fmt(f),
88 TimestampResult::Utc(dt) => dt.fmt(f)
89 }
90 }
91 }
92
93 impl From<NaiveDateTime> for TimestampResult {
94 fn from(dt: NaiveDateTime) -> Self {
95 TimestampResult::Naive(dt)
96 }
97 }
98
99 impl<T: TimeZone> From<DateTime<T>> for TimestampResult {
100 fn from(dt: DateTime<T>) -> Self {
101 TimestampResult::Utc(dt.with_timezone(&Utc))
102 }
103 }
104
105 impl From<Option<NaiveDateTime>> for TimestampResult {
106 fn from(dt: Option<NaiveDateTime>) -> Self {
107 match dt {
108 Some(dt) => TimestampResult::Naive(dt),
109 None => TimestampResult::None
110 }
111 }
112 }
113
114 impl<T: TimeZone> From<LocalResult<DateTime<T>>> for TimestampResult {
115 fn from(dt: LocalResult<DateTime<T>>) -> Self {
116 match dt {
117 LocalResult::None|LocalResult::Ambiguous(..) => TimestampResult::None,
118 LocalResult::Single(dt) => dt.into(),
119 }
120 }
121 }