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
// time.rs
//
// Copyright 2021-2023 nee <nee-git@patchouli.garden>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
// SPDX-License-Identifier: GPL-3.0-or-later
use chrono::prelude::*;
// a decimal second is 0.864 of a standard second
// 100 seconds = 1 minute
// 100 minutes = 1 hour
// 10 hours = 1 day
#[derive(PartialEq, Debug)]
pub struct DecimalTime {
pub secs: i32,
pub frac: i32, // Still in std nanoseconds
}
impl DecimalTime {
pub fn hours(&self) -> i32 {
self.secs / (100 * 100)
}
pub fn minutes(&self) -> i32 {
(self.secs / 100) % 100
}
pub fn seconds(&self) -> i32 {
self.secs % 100
}
pub fn num_seconds_from_midnight(&self) -> i32 {
self.secs
}
pub fn standard_seconds(&self) -> i32 {
(self.secs as f32 / SECOND_TO_DEC_FACTOR) as i32
}
pub fn standard_nanoseconds(&self) -> i32 {
self.frac
}
pub fn fraction(&self) -> f32 {
(self.secs as f32) / (10.0 * 100.0 * 100.0)
}
}
pub const STD_SECONDS_IN_STD_DAY: u32 = 86400;
const DEC_SECONDS_IN_DECIMAL_DAY: u32 = 10 * 100 * 100;
const SECOND_TO_DEC_FACTOR: f32 = DEC_SECONDS_IN_DECIMAL_DAY as f32 / STD_SECONDS_IN_STD_DAY as f32;
const STD_NANOSECONDS_IN_STD_SECOND: u32 = 1000000000;
const STD_NANOSECONDS_IN_DEC_SECOND: u32 =
(STD_NANOSECONDS_IN_STD_SECOND as f32 / SECOND_TO_DEC_FACTOR) as u32;
impl From<NaiveTime> for DecimalTime {
fn from(v: NaiveTime) -> DecimalTime {
let nano = v.nanosecond();
if nano >= STD_NANOSECONDS_IN_DEC_SECOND {
DecimalTime {
secs: (v.num_seconds_from_midnight() as f32 * SECOND_TO_DEC_FACTOR) as i32 + 1,
frac: (nano - STD_NANOSECONDS_IN_DEC_SECOND) as i32,
}
} else {
DecimalTime {
secs: (v.num_seconds_from_midnight() as f32 * SECOND_TO_DEC_FACTOR) as i32,
frac: nano as i32,
}
}
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn from() {
(|| {
let date = NaiveTime::from_hms_opt(0, 0, 0)?;
assert_eq!(DecimalTime { secs: 0, frac: 0 }, DecimalTime::from(date));
let date = NaiveTime::from_hms_opt(12, 0, 0)?;
assert_eq!(
DecimalTime {
secs: 50000,
frac: 0
},
DecimalTime::from(date)
);
let date = NaiveTime::from_hms_opt(6, 0, 0)?;
assert_eq!(
DecimalTime {
secs: 25000,
frac: 0
},
DecimalTime::from(date)
);
let date = NaiveTime::from_hms_opt(18, 0, 0)?;
assert_eq!(
DecimalTime {
secs: 75000,
frac: 0
},
DecimalTime::from(date)
);
let date = NaiveTime::from_hms_opt(23, 59, 59)?;
assert_eq!(
DecimalTime {
secs: 99998,
frac: 0
},
DecimalTime::from(date)
);
let date = NaiveTime::from_hms_nano_opt(23, 59, 59, STD_NANOSECONDS_IN_DEC_SECOND - 1)?;
assert_eq!(
DecimalTime {
secs: 99998,
frac: 863999999
},
DecimalTime::from(date)
);
let date = NaiveTime::from_hms_nano_opt(23, 59, 59, STD_NANOSECONDS_IN_DEC_SECOND)?;
assert_eq!(
DecimalTime {
secs: 99999,
frac: 0
},
DecimalTime::from(date)
);
let date = NaiveTime::from_hms_nano_opt(23, 59, 59, STD_NANOSECONDS_IN_DEC_SECOND + 1)?;
assert_eq!(
DecimalTime {
secs: 99999,
frac: 1
},
DecimalTime::from(date)
);
let date = NaiveTime::from_hms_nano_opt(23, 59, 59, STD_NANOSECONDS_IN_STD_SECOND - 1)?;
assert_eq!(
DecimalTime {
secs: 99999,
frac: 135999999
},
DecimalTime::from(date)
);
Some(())
})();
}
}