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

lennart-k / ical-rs / #63

08 Jan 2026 10:13PM UTC coverage: 80.862% (+3.1%) from 77.802%
#63

Pull #2

lennart-k
methods for calendar import
Pull Request #2: Major overhaul

818 of 1029 new or added lines in 27 files covered. (79.49%)

21 existing lines in 9 files now uncovered.

1145 of 1416 relevant lines covered (80.86%)

2.83 hits per line

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

77.97
/src/types/datetime.rs
1
use crate::types::{Timezone, Value};
2
use crate::{property::ContentLine, types::CalDateTimeError};
3
use chrono::{DateTime, Datelike, Duration, Local, NaiveDate, NaiveDateTime, Utc};
4
use chrono_tz::Tz;
5
use std::{collections::HashMap, ops::Add};
6

7
const LOCAL_DATE_TIME: &str = "%Y%m%dT%H%M%S";
8
const UTC_DATE_TIME: &str = "%Y%m%dT%H%M%SZ";
9

10
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
11
// Form 1, example: 19980118T230000 -> Local
12
// Form 2, example: 19980119T070000Z -> UTC
13
// Form 3, example: TZID=America/New_York:19980119T020000 -> Olson
14
// https://en.wikipedia.org/wiki/Tz_database
15
pub struct CalDateTime(pub(crate) DateTime<Timezone>);
16

17
impl From<CalDateTime> for DateTime<rrule::Tz> {
NEW
18
    fn from(value: CalDateTime) -> Self {
×
NEW
19
        value.0.with_timezone(&value.timezone().into())
×
20
    }
21
}
22

23
impl From<DateTime<rrule::Tz>> for CalDateTime {
24
    fn from(value: DateTime<rrule::Tz>) -> Self {
1✔
25
        Self(value.with_timezone(&value.timezone().into()))
1✔
26
    }
27
}
28

29
impl From<DateTime<Timezone>> for CalDateTime {
30
    fn from(value: DateTime<Timezone>) -> Self {
1✔
31
        Self(value)
1✔
32
    }
33
}
34

35
impl From<DateTime<Local>> for CalDateTime {
NEW
36
    fn from(value: DateTime<Local>) -> Self {
×
NEW
37
        Self(value.with_timezone(&Timezone::Local))
×
38
    }
39
}
40

41
impl From<DateTime<Utc>> for CalDateTime {
42
    fn from(value: DateTime<Utc>) -> Self {
3✔
43
        Self(value.with_timezone(&Timezone::Olson(chrono_tz::UTC)))
4✔
44
    }
45
}
46

47
impl Add<Duration> for CalDateTime {
48
    type Output = Self;
49

50
    fn add(self, duration: Duration) -> Self::Output {
1✔
51
        Self(self.0 + duration)
1✔
52
    }
53
}
54

55
impl CalDateTime {
56
    pub fn parse_prop(
5✔
57
        prop: &ContentLine,
58
        timezones: &HashMap<String, Option<chrono_tz::Tz>>,
59
    ) -> Result<Self, CalDateTimeError> {
60
        let prop_value = prop
12✔
61
            .value
62
            .as_ref()
63
            .ok_or_else(|| CalDateTimeError::InvalidDatetimeFormat("empty property".into()))?;
6✔
64

65
        let timezone = if let Some(tzid) = prop.params.get_tzid() {
10✔
66
            if let Some(timezone) = timezones.get(tzid) {
7✔
67
                timezone.to_owned()
4✔
68
            } else {
69
                // TZID refers to timezone that does not exist
NEW
70
                return Err(CalDateTimeError::InvalidTZID(tzid.to_string()));
×
71
            }
72
        } else {
73
            // No explicit timezone specified.
74
            // This is valid and will be localtime or UTC depending on the value
75
            // We will stick to this default as documented in https://github.com/lennart-k/rustical/issues/102
76
            None
4✔
77
        };
78

79
        Self::parse(prop_value, timezone)
4✔
80
    }
81

82
    #[must_use]
83
    pub fn format(&self) -> String {
5✔
84
        match self.timezone() {
5✔
85
            Timezone::Olson(chrono_tz::UTC) => self.0.format(UTC_DATE_TIME).to_string(),
2✔
86
            _ => self.0.format(LOCAL_DATE_TIME).to_string(),
1✔
87
        }
88
    }
89

90
    pub fn parse(value: &str, timezone: Option<Tz>) -> Result<Self, CalDateTimeError> {
4✔
91
        if let Ok(datetime) = NaiveDateTime::parse_from_str(value, LOCAL_DATE_TIME) {
6✔
92
            if let Some(timezone) = timezone {
3✔
93
                return Ok(Self(
1✔
94
                    datetime
2✔
95
                        .and_local_timezone(timezone.into())
1✔
96
                        .earliest()
2✔
97
                        .ok_or(CalDateTimeError::LocalTimeGap)?,
1✔
98
                ));
99
            }
100
            return Ok(Self(
1✔
101
                datetime
1✔
102
                    .and_local_timezone(Timezone::Local)
1✔
103
                    .earliest()
1✔
104
                    .ok_or(CalDateTimeError::LocalTimeGap)?,
1✔
105
            ));
106
        }
107

108
        if let Ok(datetime) = NaiveDateTime::parse_from_str(value, UTC_DATE_TIME) {
7✔
109
            return Ok(datetime.and_utc().into());
4✔
110
        }
111

112
        Err(CalDateTimeError::InvalidDatetimeFormat(value.to_string()))
1✔
113
    }
114

115
    #[must_use]
116
    pub fn utc(&self) -> DateTime<Utc> {
1✔
117
        self.0.to_utc()
1✔
118
    }
119

120
    #[must_use]
121
    pub fn timezone(&self) -> Timezone {
5✔
122
        self.0.timezone()
5✔
123
    }
124

125
    #[must_use]
NEW
126
    pub fn date_floor(&self) -> NaiveDate {
×
NEW
127
        self.0.date_naive()
×
128
    }
129
    #[must_use]
NEW
130
    pub fn date_ceil(&self) -> NaiveDate {
×
NEW
131
        let date = self.0.date_naive();
×
NEW
132
        date.succ_opt().unwrap_or(date)
×
133
    }
134
}
135

136
impl From<CalDateTime> for DateTime<Utc> {
NEW
137
    fn from(value: CalDateTime) -> Self {
×
NEW
138
        value.utc()
×
139
    }
140
}
141

142
#[cfg(not(tarpaulin_include))]
143
impl Datelike for CalDateTime {
144
    fn year(&self) -> i32 {
145
        self.0.year()
146
    }
147
    fn month(&self) -> u32 {
148
        self.0.month()
149
    }
150

151
    fn month0(&self) -> u32 {
152
        self.0.month0()
153
    }
154
    fn day(&self) -> u32 {
155
        self.0.day()
156
    }
157
    fn day0(&self) -> u32 {
158
        self.0.day0()
159
    }
160
    fn ordinal(&self) -> u32 {
161
        self.0.ordinal()
162
    }
163
    fn ordinal0(&self) -> u32 {
164
        self.0.ordinal0()
165
    }
166
    fn weekday(&self) -> chrono::Weekday {
167
        self.0.weekday()
168
    }
169
    fn iso_week(&self) -> chrono::IsoWeek {
170
        self.0.iso_week()
171
    }
172
    fn with_year(&self, year: i32) -> Option<Self> {
173
        Some(Self(self.0.with_year(year)?))
174
    }
175
    fn with_month(&self, month: u32) -> Option<Self> {
176
        Some(Self(self.0.with_month(month)?))
177
    }
178
    fn with_month0(&self, month0: u32) -> Option<Self> {
179
        Some(Self(self.0.with_month0(month0)?))
180
    }
181
    fn with_day(&self, day: u32) -> Option<Self> {
182
        Some(Self(self.0.with_day(day)?))
183
    }
184
    fn with_day0(&self, day0: u32) -> Option<Self> {
185
        Some(Self(self.0.with_day0(day0)?))
186
    }
187
    fn with_ordinal(&self, ordinal: u32) -> Option<Self> {
188
        Some(Self(self.0.with_ordinal(ordinal)?))
189
    }
190
    fn with_ordinal0(&self, ordinal0: u32) -> Option<Self> {
191
        Some(Self(self.0.with_ordinal0(ordinal0)?))
192
    }
193
}
194

195
impl Value for CalDateTime {
196
    fn value_type(&self) -> Option<&'static str> {
2✔
197
        Some("DATE-TIME")
198
    }
199
    fn value(&self) -> String {
5✔
200
        self.format()
5✔
201
    }
202

203
    fn utc_or_local(self) -> Self {
1✔
204
        match self.timezone() {
1✔
NEW
205
            Timezone::Local => self.clone(),
×
206
            Timezone::Olson(_) => Self(self.0.with_timezone(&Timezone::utc())),
1✔
207
        }
208
    }
209
}
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