Skip to content

Commit ad20abc

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

File tree

2 files changed

+38
-16
lines changed

2 files changed

+38
-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

+37-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,22 @@ 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 =
205+
TimeTrigger::get_next_time(current, config.interval, config.modulate).unwrap();
193206
let next_roll_time = if config.max_random_delay > 0 {
194207
let random_delay = rand::thread_rng().gen_range(0..config.max_random_delay);
195-
next_time + Duration::seconds(random_delay as i64)
208+
next_time
209+
+ Duration::try_seconds(random_delay as i64)
210+
.unwrap_or(Duration::try_milliseconds(i64::MAX).unwrap())
196211
} else {
197212
next_time
198213
};
@@ -207,13 +222,13 @@ impl TimeTrigger {
207222
current: DateTime<Local>,
208223
interval: TimeTriggerInterval,
209224
modulate: bool,
210-
) -> DateTime<Local> {
225+
) -> Result<DateTime<Local>, TimeTrigerError> {
211226
let year = current.year();
212227
if let TimeTriggerInterval::Year(n) = interval {
213228
let n = n as i32;
214229
let increment = if modulate { n - year % n } else { n };
215230
let year_new = year + increment;
216-
return Local.with_ymd_and_hms(year_new, 1, 1, 0, 0, 0).unwrap();
231+
return Ok(Local.with_ymd_and_hms(year_new, 1, 1, 0, 0, 0).unwrap());
217232
}
218233

219234
if let TimeTriggerInterval::Month(n) = interval {
@@ -224,9 +239,9 @@ impl TimeTrigger {
224239
let num_months_new = num_months + increment;
225240
let year_new = (num_months_new / 12) as i32;
226241
let month_new = (num_months_new) % 12 + 1;
227-
return Local
242+
return Ok(Local
228243
.with_ymd_and_hms(year_new, month_new, 1, 0, 0, 0)
229-
.unwrap();
244+
.unwrap());
230245
}
231246

232247
let month = current.month();
@@ -236,14 +251,17 @@ impl TimeTrigger {
236251
let weekday = current.weekday().num_days_from_monday() as i64; // Monday is the first day of the week
237252
let time = Local.with_ymd_and_hms(year, month, day, 0, 0, 0).unwrap();
238253
let increment = if modulate { n - week0 % n } else { n };
239-
return time + Duration::weeks(increment) - Duration::days(weekday);
254+
let dur =
255+
try_from!(try_weeks, increment, interval) - try_from!(try_days, weekday, interval);
256+
return Ok(time + dur);
240257
}
241258

242259
if let TimeTriggerInterval::Day(n) = interval {
243260
let ordinal0 = current.ordinal0() as i64;
244261
let time = Local.with_ymd_and_hms(year, month, day, 0, 0, 0).unwrap();
245262
let increment = if modulate { n - ordinal0 % n } else { n };
246-
return time + Duration::days(increment);
263+
let dur = try_from!(try_days, increment, interval);
264+
return Ok(time + dur);
247265
}
248266

249267
let hour = current.hour();
@@ -252,7 +270,8 @@ impl TimeTrigger {
252270
.with_ymd_and_hms(year, month, day, hour, 0, 0)
253271
.unwrap();
254272
let increment = if modulate { n - (hour as i64) % n } else { n };
255-
return time + Duration::hours(increment);
273+
let dur = try_from!(try_hours, increment, interval);
274+
return Ok(time + dur);
256275
}
257276

258277
let min = current.minute();
@@ -261,7 +280,8 @@ impl TimeTrigger {
261280
.with_ymd_and_hms(year, month, day, hour, min, 0)
262281
.unwrap();
263282
let increment = if modulate { n - (min as i64) % n } else { n };
264-
return time + Duration::minutes(increment);
283+
let dur = try_from!(try_minutes, increment, interval);
284+
return Ok(time + dur);
265285
}
266286

267287
let sec = current.second();
@@ -270,7 +290,8 @@ impl TimeTrigger {
270290
.with_ymd_and_hms(year, month, day, hour, min, sec)
271291
.unwrap();
272292
let increment = if modulate { n - (sec as i64) % n } else { n };
273-
return time + Duration::seconds(increment);
293+
let dur = try_from!(try_seconds, increment, interval);
294+
return Ok(time + dur);
274295
}
275296
panic!("Should not reach here!");
276297
}
@@ -283,8 +304,9 @@ impl Trigger for TimeTrigger {
283304
let now = SystemTime::now()
284305
.duration_since(UNIX_EPOCH)
285306
.expect("system time before Unix epoch");
286-
NaiveDateTime::from_timestamp_opt(now.as_secs() as i64, now.subsec_nanos())
307+
DateTime::from_timestamp(now.as_secs() as i64, now.subsec_nanos())
287308
.unwrap()
309+
.naive_local()
288310
.and_local_timezone(Local)
289311
.unwrap()
290312
};

0 commit comments

Comments
 (0)