Skip to content

Commit 28d30c0

Browse files
committed
Use simpler constructor to cast from chrono_tz::Tz
Instead of generating one const variant of Tz for each chrono_tz::tz variant, provide a const function to convert chrono_tz::Tz into Tz in a static context. The main change here is the introduction of Tz::from_static(). Other changes merely adapt existing code to use this instead of chrono_tz directly.
1 parent ed127d0 commit 28d30c0

21 files changed

Lines changed: 88 additions & 1864 deletions

rrule-afl-fuzz/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ rust-version.workspace = true
1212
[dependencies]
1313
afl = "*"
1414
chrono = "0.4.39"
15+
chrono-tz = "0.10.1"
1516
num-traits = "0.2.19"
1617

1718
[dependencies.rrule]

rrule-afl-fuzz/src/take_data.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,11 @@ pub fn take_datetime(input: &mut &[u8]) -> DateTime<Tz> {
6767
LocalResult::None => {
6868
// Will always succeed
6969
let nanos: i64 = take_data_i64(input);
70-
Utc.timestamp_nanos(nanos).with_timezone(&Tz::UTC)
70+
Utc.timestamp_nanos(nanos)
71+
.with_timezone(&Tz::from_static(chrono_tz::UTC))
7172
}
7273
LocalResult::Single(datetime) | LocalResult::Ambiguous(datetime, _) => {
73-
datetime.with_timezone(&Tz::UTC)
74+
datetime.with_timezone(&Tz::from_static(chrono_tz::UTC))
7475
}
7576
}
7677
}

rrule-debugger/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ rust-version.workspace = true
1212
[dependencies]
1313
rrule = { path = "../rrule" }
1414
chrono = "0.4.39"
15+
chrono-tz = "0.10.1"
1516
clap = { version = "4.5.26", features = ["derive"] }
1617
rrule-afl-fuzz = { version = "0.1.0", path = "../rrule-afl-fuzz" }
1718
log = "0.4.25"

rrule-debugger/src/debug.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ fn test_parsed_rrule() {
3636
}
3737

3838
fn ymd_hms(year: i32, month: u32, day: u32, hour: u32, minute: u32, second: u32) -> DateTime<Tz> {
39-
Tz::UTC
39+
Tz::from(chrono_tz::UTC)
4040
.with_ymd_and_hms(year, month, day, hour, minute, second)
4141
.unwrap()
4242
}

rrule/examples/manual_rrule.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ use rrule::{Frequency, RRule, Tz};
77

88
fn main() {
99
// Build an RRuleSet that starts the first day in 2020 at 9:00AM and occurs daily 5 times
10-
let start_date = Tz::UTC.with_ymd_and_hms(2020, 1, 1, 9, 0, 0).unwrap();
10+
let start_date = Tz::from(chrono_tz::UTC)
11+
.with_ymd_and_hms(2020, 1, 1, 9, 0, 0)
12+
.unwrap();
1113
let rrule_set = RRule::default()
1214
.count(5)
1315
.freq(Frequency::Daily)

rrule/examples/manual_rrule_set.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,23 @@ fn main() {
1919
NWeekday::Every(Weekday::Tue),
2020
NWeekday::Every(Weekday::Wed),
2121
])
22-
.build(Tz::UTC.with_ymd_and_hms(2020, 1, 1, 9, 0, 0).unwrap())
22+
.build(
23+
Tz::from(chrono_tz::UTC)
24+
.with_ymd_and_hms(2020, 1, 1, 9, 0, 0)
25+
.unwrap(),
26+
)
2327
.expect("RRule invalid");
2428

2529
// Build exrule that occurs weekly on Wednesday
2630
let exrule = RRule::default()
2731
.count(4)
2832
.freq(Frequency::Weekly)
2933
.by_weekday(vec![NWeekday::Every(Weekday::Wed)])
30-
.validate(Tz::UTC.with_ymd_and_hms(2020, 1, 1, 9, 0, 0).unwrap())
34+
.validate(
35+
Tz::from(chrono_tz::UTC)
36+
.with_ymd_and_hms(2020, 1, 1, 9, 0, 0)
37+
.unwrap(),
38+
)
3139
.expect("RRule invalid");
3240

3341
let recurrences = rrule_set.exrule(exrule).all(10).dates;

rrule/examples/timezone_support.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@ use chrono::{DateTime, TimeZone};
88
use rrule::{Frequency, RRule, Tz};
99

1010
fn main() {
11-
let tz = Tz::Europe__Berlin;
11+
let tz = Tz::from(chrono_tz::Europe::Berlin);
1212
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();
13+
let exdate = Tz::from(chrono_tz::UTC)
14+
.with_ymd_and_hms(2020, 1, 2, 8, 0, 0)
15+
.unwrap();
1416

1517
// Build an rrule set that occurs daily at 9:00 for 4 times
1618
let rrule_set = RRule::default()

rrule/src/core/rruleset.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,9 @@ mod tests {
355355
let rruleset = RRuleSet::from_str(rruleset_str).unwrap();
356356

357357
// Check start date
358-
let dt_start = Tz::UTC.with_ymd_and_hms(2012, 2, 1, 9, 30, 0).unwrap();
358+
let dt_start = Tz::from(chrono_tz::UTC)
359+
.with_ymd_and_hms(2012, 2, 1, 9, 30, 0)
360+
.unwrap();
359361
assert_eq!(rruleset.dt_start, dt_start);
360362

361363
// Check rrule
@@ -371,8 +373,12 @@ mod tests {
371373
assert_eq!(
372374
rruleset.rdate,
373375
vec![
374-
Tz::UTC.with_ymd_and_hms(1997, 1, 1, 0, 0, 0).unwrap(),
375-
Tz::UTC.with_ymd_and_hms(1997, 1, 20, 0, 0, 0).unwrap()
376+
Tz::from(chrono_tz::UTC)
377+
.with_ymd_and_hms(1997, 1, 1, 0, 0, 0)
378+
.unwrap(),
379+
Tz::from(chrono_tz::UTC)
380+
.with_ymd_and_hms(1997, 1, 20, 0, 0, 0)
381+
.unwrap()
376382
]
377383
);
378384

@@ -389,7 +395,9 @@ mod tests {
389395
// Check exdate
390396
assert_eq!(
391397
rruleset.exdate,
392-
vec![Tz::UTC.with_ymd_and_hms(1997, 1, 21, 0, 0, 0).unwrap()]
398+
vec![Tz::from(chrono_tz::UTC)
399+
.with_ymd_and_hms(1997, 1, 21, 0, 0, 0)
400+
.unwrap()]
393401
);
394402

395403
// Serialize to string again

0 commit comments

Comments
 (0)