Fix chrono deprecation warnings (#12091)

# Description
Bumps `chrono` to 0.4.35 and fixes any deprecation warnings.
This commit is contained in:
Ian Manske 2024-03-07 12:01:30 +00:00 committed by GitHub
parent 87fa86c60e
commit dfe072fd30
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 52 additions and 40 deletions

4
Cargo.lock generated
View file

@ -656,9 +656,9 @@ dependencies = [
[[package]] [[package]]
name = "chrono" name = "chrono"
version = "0.4.34" version = "0.4.35"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5bc015644b92d5890fab7489e49d21f879d5c990186827d42ec511919404f38b" checksum = "8eaf5903dcbc0a39312feb77df2ff4c76387d591b9fc7b04a238dcf8bb62639a"
dependencies = [ dependencies = [
"android-tzdata", "android-tzdata",
"iana-time-zone", "iana-time-zone",

View file

@ -280,7 +280,9 @@ pub fn run_seq_dates(
} }
if days_to_output != 0 { if days_to_output != 0 {
end_date = match start_date.checked_add_signed(Duration::days(days_to_output)) { end_date = match Duration::try_days(days_to_output)
.and_then(|days| start_date.checked_add_signed(days))
{
Some(date) => date, Some(date) => date,
None => { None => {
return Err(ShellError::GenericError { return Err(ShellError::GenericError {
@ -303,6 +305,16 @@ pub fn run_seq_dates(
let is_out_of_range = let is_out_of_range =
|next| (step_size > 0 && next > end_date) || (step_size < 0 && next < end_date); |next| (step_size > 0 && next > end_date) || (step_size < 0 && next < end_date);
let Some(step_size) = Duration::try_days(step_size) else {
return Err(ShellError::GenericError {
error: "increment magnitude is too large".into(),
msg: "increment magnitude is too large".into(),
span: Some(call_span),
help: None,
inner: vec![],
});
};
let mut next = start_date; let mut next = start_date;
if is_out_of_range(next) { if is_out_of_range(next) {
return Err(ShellError::GenericError { return Err(ShellError::GenericError {
@ -330,7 +342,7 @@ pub fn run_seq_dates(
} }
} }
ret.push(Value::string(date_string, call_span)); ret.push(Value::string(date_string, call_span));
next += Duration::days(step_size); next += step_size;
if is_out_of_range(next) { if is_out_of_range(next) {
break; break;

View file

@ -1,6 +1,6 @@
use std::{io::Write, path::PathBuf}; use std::{io::Write, path::PathBuf};
use chrono::{DateTime, FixedOffset, NaiveDateTime, Offset}; use chrono::{DateTime, FixedOffset};
use nu_protocol::{ast::PathMember, record, Span, Value}; use nu_protocol::{ast::PathMember, record, Span, Value};
use nu_test_support::{ use nu_test_support::{
fs::{line_ending, Stub}, fs::{line_ending, Stub},
@ -280,9 +280,10 @@ impl Distribution<TestRow> for Standard {
where where
R: rand::Rng + ?Sized, R: rand::Rng + ?Sized,
{ {
let naive_dt = let dt = DateTime::from_timestamp_millis(rng.gen_range(0..2324252554000))
NaiveDateTime::from_timestamp_millis(rng.gen_range(0..2324252554000)).unwrap(); .unwrap()
let dt = DateTime::from_naive_utc_and_offset(naive_dt, chrono::Utc.fix()); .fixed_offset();
let rand_string = Alphanumeric.sample_string(rng, 10); let rand_string = Alphanumeric.sample_string(rng, 10);
// limit the size of the numbers to work around // limit the size of the numbers to work around

View file

@ -3779,65 +3779,62 @@ pub fn format_duration_as_timeperiod(duration: i64) -> (i32, Vec<TimePeriod>) {
/// Split this a duration into number of whole weeks and the remainder /// Split this a duration into number of whole weeks and the remainder
fn split_weeks(duration: Duration) -> (Option<i64>, Duration) { fn split_weeks(duration: Duration) -> (Option<i64>, Duration) {
let weeks = duration.num_weeks(); let weeks = duration.num_weeks();
let remainder = duration - Duration::weeks(weeks); normalize_split(weeks, Duration::try_weeks(weeks), duration)
normalize_split(weeks, remainder)
} }
/// Split this a duration into number of whole days and the remainder /// Split this a duration into number of whole days and the remainder
fn split_days(duration: Duration) -> (Option<i64>, Duration) { fn split_days(duration: Duration) -> (Option<i64>, Duration) {
let days = duration.num_days(); let days = duration.num_days();
let remainder = duration - Duration::days(days); normalize_split(days, Duration::try_days(days), duration)
normalize_split(days, remainder)
} }
/// Split this a duration into number of whole hours and the remainder /// Split this a duration into number of whole hours and the remainder
fn split_hours(duration: Duration) -> (Option<i64>, Duration) { fn split_hours(duration: Duration) -> (Option<i64>, Duration) {
let hours = duration.num_hours(); let hours = duration.num_hours();
let remainder = duration - Duration::hours(hours); normalize_split(hours, Duration::try_hours(hours), duration)
normalize_split(hours, remainder)
} }
/// Split this a duration into number of whole minutes and the remainder /// Split this a duration into number of whole minutes and the remainder
fn split_minutes(duration: Duration) -> (Option<i64>, Duration) { fn split_minutes(duration: Duration) -> (Option<i64>, Duration) {
let minutes = duration.num_minutes(); let minutes = duration.num_minutes();
let remainder = duration - Duration::minutes(minutes); normalize_split(minutes, Duration::try_minutes(minutes), duration)
normalize_split(minutes, remainder)
} }
/// Split this a duration into number of whole seconds and the remainder /// Split this a duration into number of whole seconds and the remainder
fn split_seconds(duration: Duration) -> (Option<i64>, Duration) { fn split_seconds(duration: Duration) -> (Option<i64>, Duration) {
let seconds = duration.num_seconds(); let seconds = duration.num_seconds();
let remainder = duration - Duration::seconds(seconds); normalize_split(seconds, Duration::try_seconds(seconds), duration)
normalize_split(seconds, remainder)
} }
/// Split this a duration into number of whole milliseconds and the remainder /// Split this a duration into number of whole milliseconds and the remainder
fn split_milliseconds(duration: Duration) -> (Option<i64>, Duration) { fn split_milliseconds(duration: Duration) -> (Option<i64>, Duration) {
let millis = duration.num_milliseconds(); let millis = duration.num_milliseconds();
let remainder = duration - Duration::milliseconds(millis); normalize_split(millis, Duration::try_milliseconds(millis), duration)
normalize_split(millis, remainder)
} }
/// Split this a duration into number of whole seconds and the remainder /// Split this a duration into number of whole seconds and the remainder
fn split_microseconds(duration: Duration) -> (Option<i64>, Duration) { fn split_microseconds(duration: Duration) -> (Option<i64>, Duration) {
let micros = duration.num_microseconds().unwrap_or_default(); let micros = duration.num_microseconds().unwrap_or_default();
let remainder = duration - Duration::microseconds(micros); normalize_split(micros, Duration::microseconds(micros), duration)
normalize_split(micros, remainder)
} }
/// Split this a duration into number of whole seconds and the remainder /// Split this a duration into number of whole seconds and the remainder
fn split_nanoseconds(duration: Duration) -> (Option<i64>, Duration) { fn split_nanoseconds(duration: Duration) -> (Option<i64>, Duration) {
let nanos = duration.num_nanoseconds().unwrap_or_default(); let nanos = duration.num_nanoseconds().unwrap_or_default();
let remainder = duration - Duration::nanoseconds(nanos); normalize_split(nanos, Duration::nanoseconds(nanos), duration)
normalize_split(nanos, remainder)
} }
fn normalize_split( fn normalize_split(
wholes: impl Into<Option<i64>>, wholes: i64,
remainder: Duration, wholes_duration: impl Into<Option<Duration>>,
total_duration: Duration,
) -> (Option<i64>, Duration) { ) -> (Option<i64>, Duration) {
let wholes = wholes.into().map(i64::abs).filter(|x| *x > 0); match wholes_duration.into() {
(wholes, remainder) Some(wholes_duration) if wholes != 0 => {
(Some(wholes), total_duration - wholes_duration)
}
_ => (None, total_duration),
}
} }
let mut periods = vec![]; let mut periods = vec![];
@ -4057,7 +4054,7 @@ mod tests {
} }
mod into_string { mod into_string {
use chrono::{DateTime, FixedOffset, NaiveDateTime}; use chrono::{DateTime, FixedOffset};
use rstest::rstest; use rstest::rstest;
use super::*; use super::*;
@ -4065,11 +4062,11 @@ mod tests {
#[test] #[test]
fn test_datetime() { fn test_datetime() {
let string = Value::test_date(DateTime::from_naive_utc_and_offset( let date = DateTime::from_timestamp_millis(-123456789)
NaiveDateTime::from_timestamp_millis(-123456789).unwrap(), .unwrap()
FixedOffset::east_opt(0).unwrap(), .with_timezone(&FixedOffset::east_opt(0).unwrap());
))
.to_expanded_string("", &Default::default()); let string = Value::test_date(date).to_expanded_string("", &Default::default());
// We need to cut the humanized part off for tests to work, because // We need to cut the humanized part off for tests to work, because
// it is relative to current time. // it is relative to current time.
@ -4079,11 +4076,11 @@ mod tests {
#[test] #[test]
fn test_negative_year_datetime() { fn test_negative_year_datetime() {
let string = Value::test_date(DateTime::from_naive_utc_and_offset( let date = DateTime::from_timestamp_millis(-72135596800000)
NaiveDateTime::from_timestamp_millis(-72135596800000).unwrap(), .unwrap()
FixedOffset::east_opt(0).unwrap(), .with_timezone(&FixedOffset::east_opt(0).unwrap());
))
.to_expanded_string("", &Default::default()); let string = Value::test_date(date).to_expanded_string("", &Default::default());
// We need to cut the humanized part off for tests to work, because // We need to cut the humanized part off for tests to work, because
// it is relative to current time. // it is relative to current time.

View file

@ -149,7 +149,9 @@ pub fn collect_proc(interval: Duration, _with_thread: bool) -> Vec<ProcessInfo>
let start_time = if let Some((start, _, _, _)) = times { let start_time = if let Some((start, _, _, _)) = times {
// 11_644_473_600 is the number of seconds between the Windows epoch (1601-01-01) and // 11_644_473_600 is the number of seconds between the Windows epoch (1601-01-01) and
// the Linux epoch (1970-01-01). // the Linux epoch (1970-01-01).
let time = chrono::Duration::seconds(start as i64 / 10_000_000); let Some(time) = chrono::Duration::try_seconds(start as i64 / 10_000_000) else {
continue;
};
let base = let base =
NaiveDate::from_ymd_opt(1601, 1, 1).and_then(|nd| nd.and_hms_opt(0, 0, 0)); NaiveDate::from_ymd_opt(1601, 1, 1).and_then(|nd| nd.and_hms_opt(0, 0, 0));
if let Some(base) = base { if let Some(base) = base {