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]]
name = "chrono"
version = "0.4.34"
version = "0.4.35"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5bc015644b92d5890fab7489e49d21f879d5c990186827d42ec511919404f38b"
checksum = "8eaf5903dcbc0a39312feb77df2ff4c76387d591b9fc7b04a238dcf8bb62639a"
dependencies = [
"android-tzdata",
"iana-time-zone",

View File

@ -280,7 +280,9 @@ pub fn run_seq_dates(
}
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,
None => {
return Err(ShellError::GenericError {
@ -303,6 +305,16 @@ pub fn run_seq_dates(
let is_out_of_range =
|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;
if is_out_of_range(next) {
return Err(ShellError::GenericError {
@ -330,7 +342,7 @@ pub fn run_seq_dates(
}
}
ret.push(Value::string(date_string, call_span));
next += Duration::days(step_size);
next += step_size;
if is_out_of_range(next) {
break;

View File

@ -1,6 +1,6 @@
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_test_support::{
fs::{line_ending, Stub},
@ -280,9 +280,10 @@ impl Distribution<TestRow> for Standard {
where
R: rand::Rng + ?Sized,
{
let naive_dt =
NaiveDateTime::from_timestamp_millis(rng.gen_range(0..2324252554000)).unwrap();
let dt = DateTime::from_naive_utc_and_offset(naive_dt, chrono::Utc.fix());
let dt = DateTime::from_timestamp_millis(rng.gen_range(0..2324252554000))
.unwrap()
.fixed_offset();
let rand_string = Alphanumeric.sample_string(rng, 10);
// 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
fn split_weeks(duration: Duration) -> (Option<i64>, Duration) {
let weeks = duration.num_weeks();
let remainder = duration - Duration::weeks(weeks);
normalize_split(weeks, remainder)
normalize_split(weeks, Duration::try_weeks(weeks), duration)
}
/// Split this a duration into number of whole days and the remainder
fn split_days(duration: Duration) -> (Option<i64>, Duration) {
let days = duration.num_days();
let remainder = duration - Duration::days(days);
normalize_split(days, remainder)
normalize_split(days, Duration::try_days(days), duration)
}
/// Split this a duration into number of whole hours and the remainder
fn split_hours(duration: Duration) -> (Option<i64>, Duration) {
let hours = duration.num_hours();
let remainder = duration - Duration::hours(hours);
normalize_split(hours, remainder)
normalize_split(hours, Duration::try_hours(hours), duration)
}
/// Split this a duration into number of whole minutes and the remainder
fn split_minutes(duration: Duration) -> (Option<i64>, Duration) {
let minutes = duration.num_minutes();
let remainder = duration - Duration::minutes(minutes);
normalize_split(minutes, remainder)
normalize_split(minutes, Duration::try_minutes(minutes), duration)
}
/// Split this a duration into number of whole seconds and the remainder
fn split_seconds(duration: Duration) -> (Option<i64>, Duration) {
let seconds = duration.num_seconds();
let remainder = duration - Duration::seconds(seconds);
normalize_split(seconds, remainder)
normalize_split(seconds, Duration::try_seconds(seconds), duration)
}
/// Split this a duration into number of whole milliseconds and the remainder
fn split_milliseconds(duration: Duration) -> (Option<i64>, Duration) {
let millis = duration.num_milliseconds();
let remainder = duration - Duration::milliseconds(millis);
normalize_split(millis, remainder)
normalize_split(millis, Duration::try_milliseconds(millis), duration)
}
/// Split this a duration into number of whole seconds and the remainder
fn split_microseconds(duration: Duration) -> (Option<i64>, Duration) {
let micros = duration.num_microseconds().unwrap_or_default();
let remainder = duration - Duration::microseconds(micros);
normalize_split(micros, remainder)
normalize_split(micros, Duration::microseconds(micros), duration)
}
/// Split this a duration into number of whole seconds and the remainder
fn split_nanoseconds(duration: Duration) -> (Option<i64>, Duration) {
let nanos = duration.num_nanoseconds().unwrap_or_default();
let remainder = duration - Duration::nanoseconds(nanos);
normalize_split(nanos, remainder)
normalize_split(nanos, Duration::nanoseconds(nanos), duration)
}
fn normalize_split(
wholes: impl Into<Option<i64>>,
remainder: Duration,
wholes: i64,
wholes_duration: impl Into<Option<Duration>>,
total_duration: Duration,
) -> (Option<i64>, Duration) {
let wholes = wholes.into().map(i64::abs).filter(|x| *x > 0);
(wholes, remainder)
match wholes_duration.into() {
Some(wholes_duration) if wholes != 0 => {
(Some(wholes), total_duration - wholes_duration)
}
_ => (None, total_duration),
}
}
let mut periods = vec![];
@ -4057,7 +4054,7 @@ mod tests {
}
mod into_string {
use chrono::{DateTime, FixedOffset, NaiveDateTime};
use chrono::{DateTime, FixedOffset};
use rstest::rstest;
use super::*;
@ -4065,11 +4062,11 @@ mod tests {
#[test]
fn test_datetime() {
let string = Value::test_date(DateTime::from_naive_utc_and_offset(
NaiveDateTime::from_timestamp_millis(-123456789).unwrap(),
FixedOffset::east_opt(0).unwrap(),
))
.to_expanded_string("", &Default::default());
let date = DateTime::from_timestamp_millis(-123456789)
.unwrap()
.with_timezone(&FixedOffset::east_opt(0).unwrap());
let string = Value::test_date(date).to_expanded_string("", &Default::default());
// We need to cut the humanized part off for tests to work, because
// it is relative to current time.
@ -4079,11 +4076,11 @@ mod tests {
#[test]
fn test_negative_year_datetime() {
let string = Value::test_date(DateTime::from_naive_utc_and_offset(
NaiveDateTime::from_timestamp_millis(-72135596800000).unwrap(),
FixedOffset::east_opt(0).unwrap(),
))
.to_expanded_string("", &Default::default());
let date = DateTime::from_timestamp_millis(-72135596800000)
.unwrap()
.with_timezone(&FixedOffset::east_opt(0).unwrap());
let string = Value::test_date(date).to_expanded_string("", &Default::default());
// We need to cut the humanized part off for tests to work, because
// 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 {
// 11_644_473_600 is the number of seconds between the Windows epoch (1601-01-01) and
// 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 =
NaiveDate::from_ymd_opt(1601, 1, 1).and_then(|nd| nd.and_hms_opt(0, 0, 0));
if let Some(base) = base {