Debian Codegraph @

examples/timezone_support.rs

raw symbols SCIP
1 //! # Timezone Support
2 //!
3 //! This examples uses `RRuleSet` with one `RRule` that yields recurrences
4 //! in the Europe/Berlin timezone, and one EXDATE that is specified
5 //! in UTC and collides with one of those recurrences.
6
7 use chrono::{DateTime, TimeZone};
8 use rrule::{Frequency, RRule, Tz};
9
10 fn main() {
11 let tz = Tz::Europe__Berlin;
12 let start_date = tz.with_ymd_and_hms(2020, 1, 1, 9, 0, 0).unwrap();
13 let exdate = Tz::UTC.with_ymd_and_hms(2020, 1, 2, 8, 0, 0).unwrap();
14
15 // Build an rrule set that occurs daily at 9:00 for 4 times
16 let rrule_set = RRule::default()
17 .count(4)
18 .freq(Frequency::Daily)
19 .build(start_date)
20 .expect("RRule invalid")
21 // Exdate in the UTC at 8:00 which is 9:00 in Berlin and therefore
22 // collides with the second rrule occurrence.
23 .exdate(exdate);
24
25 let recurrences = rrule_set.all_unchecked();
26 // RRule contained 4 recurrences but 1 was filtered away by the exdate
27 assert_eq!(recurrences.len(), 3);
28
29 // If you want to get back the DateTimes in another timezone, you can just iterate over the result
30 // and convert them to another timezone by using the with_timezone method provided by the DateTime type.
31 // Refer to the chrono and chrono-tz crates for more documentation on working with the DateTime type.
32
33 // Convert to `chrono_tz::Tz`
34 let _recurrences_in_moscow_tz: Vec<DateTime<chrono_tz::Tz>> = recurrences
35 .iter()
36 .map(|d| d.with_timezone(&chrono_tz::Europe::Moscow))
37 .collect();
38
39 println!("Done, everything worked.");
40 }