Skip to content

Commit abd38ff

Browse files
committed
fix: replace deprecated API in chrono 0.4.35 for converting to time
1 parent f688e38 commit abd38ff

File tree

2 files changed

+35
-16
lines changed

2 files changed

+35
-16
lines changed

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ harness = false
5858

5959
[dependencies]
6060
arc-swap = "1.6"
61-
chrono = { version = "0.4.23", optional = true, features = ["clock"], default-features = false }
61+
chrono = { version = "0.4.35", optional = true, features = ["clock"], default-features = false }
6262
flate2 = { version = "1.0", optional = true }
6363
fnv = "1.0"
6464
humantime = { version = "2.1", optional = true }

src/append/rolling_file/policy/compound/trigger/time.rs

+34-15
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
//!
33
//! Requires the `time_trigger` feature.
44
5-
#[cfg(test)]
6-
use chrono::NaiveDateTime;
75
use chrono::{DateTime, Datelike, Duration, Local, TimeZone, Timelike};
86
#[cfg(test)]
97
use mock_instant::{SystemTime, UNIX_EPOCH};
@@ -13,11 +11,18 @@ use serde::de;
1311
#[cfg(feature = "config_parsing")]
1412
use std::fmt;
1513
use std::sync::RwLock;
14+
use thiserror::Error;
1615

1716
use crate::append::rolling_file::{policy::compound::trigger::Trigger, LogFile};
1817
#[cfg(feature = "config_parsing")]
1918
use crate::config::{Deserialize, Deserializers};
2019

20+
macro_rules! try_from {
21+
($func: ident, $para: expr, $interval: expr) => {
22+
Duration::$func($para).ok_or(TimeTrigerError::TooLargeInterval($interval))?
23+
};
24+
}
25+
2126
#[cfg(feature = "config_parsing")]
2227
/// Configuration for the time trigger.
2328
#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug, Default, serde::Deserialize)]
@@ -73,6 +78,12 @@ impl Default for TimeTriggerInterval {
7378
}
7479
}
7580

81+
#[derive(Debug, Error)]
82+
enum TimeTrigerError {
83+
#[error("The integer value {0:?} for the specified time trigger interval is too large, it must be less than 9,223,372,036,854,775,807 seconds.")]
84+
TooLargeInterval(TimeTriggerInterval),
85+
}
86+
7687
#[cfg(feature = "config_parsing")]
7788
impl<'de> serde::Deserialize<'de> for TimeTriggerInterval {
7889
fn deserialize<D>(d: D) -> Result<Self, D::Error>
@@ -181,18 +192,19 @@ impl TimeTrigger {
181192
let now: std::time::Duration = SystemTime::now()
182193
.duration_since(UNIX_EPOCH)
183194
.expect("system time before Unix epoch");
184-
NaiveDateTime::from_timestamp_opt(now.as_secs() as i64, now.subsec_nanos())
195+
DateTime::from_timestamp(now.as_secs() as i64, now.subsec_nanos())
185196
.unwrap()
197+
.naive_local()
186198
.and_local_timezone(Local)
187199
.unwrap()
188200
};
189201

190202
#[cfg(not(test))]
191203
let current = Local::now();
192-
let next_time = TimeTrigger::get_next_time(current, config.interval, config.modulate);
204+
let next_time = TimeTrigger::get_next_time(current, config.interval, config.modulate).unwrap();
193205
let next_roll_time = if config.max_random_delay > 0 {
194206
let random_delay = rand::thread_rng().gen_range(0..config.max_random_delay);
195-
next_time + Duration::seconds(random_delay as i64)
207+
next_time + Duration::try_seconds(random_delay as i64).unwrap_or(Duration::try_milliseconds(i64::MAX).unwrap())
196208
} else {
197209
next_time
198210
};
@@ -207,13 +219,13 @@ impl TimeTrigger {
207219
current: DateTime<Local>,
208220
interval: TimeTriggerInterval,
209221
modulate: bool,
210-
) -> DateTime<Local> {
222+
) -> Result<DateTime<Local>, TimeTrigerError> {
211223
let year = current.year();
212224
if let TimeTriggerInterval::Year(n) = interval {
213225
let n = n as i32;
214226
let increment = if modulate { n - year % n } else { n };
215227
let year_new = year + increment;
216-
return Local.with_ymd_and_hms(year_new, 1, 1, 0, 0, 0).unwrap();
228+
return Ok(Local.with_ymd_and_hms(year_new, 1, 1, 0, 0, 0).unwrap());
217229
}
218230

219231
if let TimeTriggerInterval::Month(n) = interval {
@@ -224,9 +236,9 @@ impl TimeTrigger {
224236
let num_months_new = num_months + increment;
225237
let year_new = (num_months_new / 12) as i32;
226238
let month_new = (num_months_new) % 12 + 1;
227-
return Local
239+
return Ok(Local
228240
.with_ymd_and_hms(year_new, month_new, 1, 0, 0, 0)
229-
.unwrap();
241+
.unwrap());
230242
}
231243

232244
let month = current.month();
@@ -236,14 +248,17 @@ impl TimeTrigger {
236248
let weekday = current.weekday().num_days_from_monday() as i64; // Monday is the first day of the week
237249
let time = Local.with_ymd_and_hms(year, month, day, 0, 0, 0).unwrap();
238250
let increment = if modulate { n - week0 % n } else { n };
239-
return time + Duration::weeks(increment) - Duration::days(weekday);
251+
let dur =
252+
try_from!(try_weeks, increment, interval) - try_from!(try_days, weekday, interval);
253+
return Ok(time + dur);
240254
}
241255

242256
if let TimeTriggerInterval::Day(n) = interval {
243257
let ordinal0 = current.ordinal0() as i64;
244258
let time = Local.with_ymd_and_hms(year, month, day, 0, 0, 0).unwrap();
245259
let increment = if modulate { n - ordinal0 % n } else { n };
246-
return time + Duration::days(increment);
260+
let dur = try_from!(try_days, increment, interval);
261+
return Ok(time + dur);
247262
}
248263

249264
let hour = current.hour();
@@ -252,7 +267,8 @@ impl TimeTrigger {
252267
.with_ymd_and_hms(year, month, day, hour, 0, 0)
253268
.unwrap();
254269
let increment = if modulate { n - (hour as i64) % n } else { n };
255-
return time + Duration::hours(increment);
270+
let dur = try_from!(try_hours, increment, interval);
271+
return Ok(time + dur);
256272
}
257273

258274
let min = current.minute();
@@ -261,7 +277,8 @@ impl TimeTrigger {
261277
.with_ymd_and_hms(year, month, day, hour, min, 0)
262278
.unwrap();
263279
let increment = if modulate { n - (min as i64) % n } else { n };
264-
return time + Duration::minutes(increment);
280+
let dur = try_from!(try_minutes, increment, interval);
281+
return Ok(time + dur);
265282
}
266283

267284
let sec = current.second();
@@ -270,7 +287,8 @@ impl TimeTrigger {
270287
.with_ymd_and_hms(year, month, day, hour, min, sec)
271288
.unwrap();
272289
let increment = if modulate { n - (sec as i64) % n } else { n };
273-
return time + Duration::seconds(increment);
290+
let dur = try_from!(try_seconds, increment, interval);
291+
return Ok(time + dur);
274292
}
275293
panic!("Should not reach here!");
276294
}
@@ -283,8 +301,9 @@ impl Trigger for TimeTrigger {
283301
let now = SystemTime::now()
284302
.duration_since(UNIX_EPOCH)
285303
.expect("system time before Unix epoch");
286-
NaiveDateTime::from_timestamp_opt(now.as_secs() as i64, now.subsec_nanos())
304+
DateTime::from_timestamp(now.as_secs() as i64, now.subsec_nanos())
287305
.unwrap()
306+
.naive_local()
288307
.and_local_timezone(Local)
289308
.unwrap()
290309
};

0 commit comments

Comments
 (0)