• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

lpenz / ogle / 15745024169

18 Jun 2025 10:34PM UTC coverage: 73.105%. Remained the same
15745024169

push

github

lpenz
InputStream refactored and mostly working

More tests pending.

24 of 72 new or added lines in 3 files covered. (33.33%)

20 existing lines in 4 files now uncovered.

405 of 554 relevant lines covered (73.1%)

1.88 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

95.59
/src/time_wrapper.rs
1
// Copyright (C) 2025 Leandro Lisboa Penz <lpenz@lpenz.org>
2
// This file is subject to the terms and conditions defined in
3
// file 'LICENSE', which is part of this source code package.
4

5
use std::fmt;
6

7
type Inner = chrono::DateTime<chrono::Utc>;
8

9
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
10
pub struct Instant(Inner);
11

12
impl Instant {
13
    pub fn epoch() -> Self {
8✔
14
        Instant(chrono::DateTime::UNIX_EPOCH)
8✔
15
    }
8✔
16

17
    #[cfg(test)]
18
    pub fn incr(&mut self) -> Self {
6✔
19
        let me = *self;
6✔
20
        *self = &me + &Duration::seconds(1);
6✔
21
        me
6✔
22
    }
6✔
23
}
24

25
impl Default for Instant {
26
    fn default() -> Self {
7✔
27
        Self::epoch()
7✔
28
    }
7✔
29
}
30

31
impl fmt::Display for Instant {
32
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1✔
33
        // Use UTC in tests, locatime in prod
1✔
34
        #[cfg(not(test))]
1✔
35
        let dt = chrono::DateTime::<chrono::Local>::from(self.0);
1✔
36
        #[cfg(test)]
1✔
37
        let dt = self.0;
1✔
38
        write!(f, "{}", dt.format("%Y-%m-%d %H:%M:%S"))
1✔
39
    }
1✔
40
}
41

42
impl From<Inner> for Instant {
43
    fn from(dt: Inner) -> Self {
4✔
44
        Self(dt)
4✔
45
    }
4✔
46
}
47

48
impl std::ops::Add<&Duration> for &Instant {
49
    type Output = Instant;
50
    fn add(self, other: &Duration) -> Instant {
18✔
51
        Instant(self.0 + other.0)
18✔
52
    }
18✔
53
}
54

55
impl std::ops::Sub for &Instant {
56
    type Output = Duration;
57
    fn sub(self, other: Self) -> Self::Output {
1✔
58
        Duration(self.0 - other.0)
1✔
59
    }
1✔
60
}
61

62
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
63
pub struct Duration(chrono::Duration);
64

65
impl Duration {
66
    pub const MAX: Self = Self(chrono::Duration::MAX);
67

68
    /// An absurd duration (a millenia) that is safer to add/subtract without overflowing.
69
    pub const INFINITE: Self = Self::seconds(3600 * 24 * 365 * 1000);
70

71
    pub const fn seconds(value: i64) -> Self {
19✔
72
        Self(chrono::Duration::seconds(value))
19✔
73
    }
19✔
74

75
    pub const fn milliseconds(value: i64) -> Self {
1✔
76
        Self(chrono::Duration::milliseconds(value))
1✔
77
    }
1✔
78

79
    pub const fn num_seconds(&self) -> i64 {
1✔
80
        self.0.num_seconds()
1✔
81
    }
1✔
82

83
    pub const fn num_milliseconds(&self) -> i64 {
1✔
84
        self.0.num_milliseconds()
1✔
85
    }
1✔
86
}
87

88
impl Default for Duration {
UNCOV
89
    fn default() -> Self {
×
UNCOV
90
        Self::milliseconds(1)
×
UNCOV
91
    }
×
92
}
93

94
impl From<Duration> for std::time::Duration {
95
    fn from(duration: Duration) -> Self {
2✔
96
        duration.0.to_std().unwrap()
2✔
97
    }
2✔
98
}
99

100
impl From<Duration> for tokio::time::Interval {
101
    fn from(duration: Duration) -> Self {
2✔
102
        tokio::time::interval(duration.into())
2✔
103
    }
2✔
104
}
105

106
#[cfg(test)]
107
mod test {
108
    use super::*;
109

110
    use crate::sys_input::SysInputApi;
111
    use crate::sys_input::SysInputReal;
112

113
    #[test]
114
    fn basic_instant() {
1✔
115
        let ten = Duration::seconds(10);
1✔
116
        let sys = SysInputReal::default();
1✔
117
        let now = sys.now();
1✔
118
        let now2 = sys.now();
1✔
119
        assert!(&now2 - &now < ten);
1✔
120
        let now3 = &now2 + &ten;
1✔
121
        assert!(&now3 > &now2);
1✔
122
    }
1✔
123

124
    #[test]
125
    fn print_instant() {
1✔
126
        let epoch = Instant::epoch();
1✔
127
        let string = format!("{}", epoch);
1✔
128
        // Let's just test the start, as the time can vary with the local timezone.
1✔
129
        assert_eq!(string, "1970-01-01 00:00:00");
1✔
130
    }
1✔
131

132
    #[test]
133
    fn basic_duration() {
1✔
134
        assert_eq!(Duration::seconds(10).num_seconds(), 10);
1✔
135
        assert_eq!(Duration::milliseconds(10).num_milliseconds(), 10);
1✔
136
    }
1✔
137
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc