Skip to content

Commit

Permalink
Merge pull request #121 from lsndr/fix/properly-serialize-timezone
Browse files Browse the repository at this point in the history
fix(rruleset): respect timezone when serializing `EXDATE` and `RDATE`
  • Loading branch information
fmeringdal authored Jul 9, 2024
2 parents 12a1c0a + 9a303d4 commit f0416ed
Showing 1 changed file with 27 additions and 2 deletions.
29 changes: 27 additions & 2 deletions rrule/src/core/rruleset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,11 @@ impl Display for RRuleSet {
let mut rdates = self
.rdate
.iter()
.map(|dt| dt.format("%Y%m%dT%H%M%SZ").to_string())
.map(|dt| {
let maybe_zulu = if dt.timezone().is_local() { "" } else { "Z" };

format!("{}{}", dt.format("%Y%m%dT%H%M%S"), maybe_zulu)
})
.collect::<Vec<_>>()
.join(",");
if !rdates.is_empty() {
Expand All @@ -320,7 +324,11 @@ impl Display for RRuleSet {
let mut exdates = self
.exdate
.iter()
.map(|dt| dt.format("%Y%m%dT%H%M%SZ").to_string())
.map(|dt| {
let maybe_zulu = if dt.timezone().is_local() { "" } else { "Z" };

format!("{}{}", dt.format("%Y%m%dT%H%M%S"), maybe_zulu)
})
.collect::<Vec<_>>()
.join(",");
if !exdates.is_empty() {
Expand Down Expand Up @@ -387,4 +395,21 @@ mod tests {
// Serialize to string again
assert_eq!(rruleset.to_string(), rruleset_str);
}

#[test]
fn respect_local_timezone_in_exdates_rdates() {
let rruleset_str = "DTSTART:20120201T093000Z\nRRULE:FREQ=DAILY;COUNT=3;BYHOUR=9;BYMINUTE=30;BYSECOND=0\nRDATE;VALUE=DATE-TIME:19970101T000000,19970120T000000\nEXRULE:FREQ=YEARLY;COUNT=8;BYMONTH=6,7;BYMONTHDAY=1;BYHOUR=9;BYMINUTE=30;BYSECOND=0\nEXDATE;VALUE=DATE-TIME:19970121T000000";
let rruleset = RRuleSet::from_str(rruleset_str).unwrap();

// Serialize to string again
assert_eq!(rruleset.to_string(), rruleset_str);
}

fn respect_utc_timezone_in_exdates_rdates() {
let rruleset_str = "DTSTART:20120201T093000Z\nRRULE:FREQ=DAILY;COUNT=3;BYHOUR=9;BYMINUTE=30;BYSECOND=0\nRDATE;VALUE=DATE-TIME:19970101T000000Z,19970120T000000Z\nEXRULE:FREQ=YEARLY;COUNT=8;BYMONTH=6,7;BYMONTHDAY=1;BYHOUR=9;BYMINUTE=30;BYSECOND=0\nEXDATE;VALUE=DATE-TIME:19970121T000000Z";
let rruleset = RRuleSet::from_str(rruleset_str).unwrap();

// Serialize to string again
assert_eq!(rruleset.to_string(), rruleset_str);
}
}

0 comments on commit f0416ed

Please sign in to comment.