From 9168a22dc7e8a62de29e73f188dee53a97886cd6 Mon Sep 17 00:00:00 2001 From: Eric Huss Date: Fri, 31 Mar 2023 07:20:22 -0700 Subject: [PATCH] Update chrono --- Cargo.lock | 4 +-- src/git.rs | 5 ++- src/github.rs | 6 ++-- src/main.rs | 56 +++++++++++++++++--------------- src/repo_access.rs | 3 +- src/toolchains.rs | 16 +++++---- tests/cmd/start-in-future.stderr | 2 +- 7 files changed, 51 insertions(+), 41 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 81c9ad3..603a115 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -118,9 +118,9 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" [[package]] name = "chrono" -version = "0.4.22" +version = "0.4.24" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bfd4d1b31faaa3a89d7934dbded3111da0d2ef28e3ebccdb4f0179f5929d1ef1" +checksum = "4e3c5919066adf22df73762e50cffcde3a758f2a848b113b586d1f86728b673b" dependencies = [ "iana-time-zone", "js-sys", diff --git a/src/git.rs b/src/git.rs index 43876a6..ab56551 100644 --- a/src/git.rs +++ b/src/git.rs @@ -20,7 +20,10 @@ impl Commit { fn from_git2_commit(commit: &mut Git2Commit<'_>) -> Self { Commit { sha: commit.id().to_string(), - date: Utc.timestamp(commit.time().seconds(), 0).date(), + date: Utc + .timestamp_opt(commit.time().seconds(), 0) + .unwrap() + .date_naive(), summary: String::from_utf8_lossy(commit.summary_bytes().unwrap()).to_string(), } } diff --git a/src/github.rs b/src/github.rs index 1c07185..5f11080 100644 --- a/src/github.rs +++ b/src/github.rs @@ -3,7 +3,7 @@ use reqwest::header::{HeaderMap, HeaderValue, InvalidHeaderValue, AUTHORIZATION, use reqwest::{self, blocking::Client, blocking::Response}; use serde::{Deserialize, Serialize}; -use crate::{parse_to_utc_date, Commit, GitDate}; +use crate::{parse_to_naive_date, Commit, GitDate}; #[derive(Serialize, Deserialize, Debug)] struct GithubCommitComparison { @@ -42,7 +42,7 @@ impl GithubCommitElem { self.commit.committer.date.split_once('T').context( "commit date should folllow the ISO 8061 format, eg: 2022-05-04T09:55:51Z", )?; - Ok(parse_to_utc_date(date_str)?) + Ok(parse_to_naive_date(date_str)?) } fn git_commit(self) -> anyhow::Result { @@ -255,7 +255,7 @@ mod tests { fn test_github() { let c = get_commit("25674202bb7415e0c0ecd07856749cfb7f591be6").unwrap(); let expected_c = Commit { sha: "25674202bb7415e0c0ecd07856749cfb7f591be6".to_string(), - date: parse_to_utc_date("2022-05-04").unwrap(), + date: parse_to_naive_date("2022-05-04").unwrap(), summary: "Auto merge of #96695 - JohnTitor:rollup-oo4fc1h, r=JohnTitor\n\nRollup of 6 pull requests\n\nSuccessful merges:\n\n - #96597 (openbsd: unbreak build on native platform)\n - #96662 (Fix typo in lint levels doc)\n - #96668 (Fix flaky rustdoc-ui test because it did not replace time result)\n - #96679 (Quick fix for #96223.)\n - #96684 (Update `ProjectionElem::Downcast` documentation)\n - #96686 (Add some TAIT-related tests)\n\nFailed merges:\n\nr? `@ghost`\n`@rustbot` modify labels: rollup".to_string() }; assert_eq!(c, expected_c) diff --git a/src/main.rs b/src/main.rs index ab60405..95bcdd1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -14,7 +14,7 @@ use std::process; use std::str::FromStr; use anyhow::{bail, Context}; -use chrono::{Date, Duration, NaiveDate, Utc}; +use chrono::{Duration, NaiveDate, Utc}; use clap::{ArgAction, Parser, ValueEnum}; use colored::Colorize; use github::get_pr_comments; @@ -31,7 +31,7 @@ use crate::github::get_commit; use crate::least_satisfying::{least_satisfying, Satisfies}; use crate::repo_access::{AccessViaGithub, AccessViaLocalGit, RustRepositoryAccessor}; use crate::toolchains::{ - download_progress, parse_to_utc_date, DownloadParams, InstallError, TestOutcome, Toolchain, + download_progress, parse_to_naive_date, DownloadParams, InstallError, TestOutcome, Toolchain, ToolchainSpec, NIGHTLY_SERVER, YYYY_MM_DD, }; @@ -181,7 +181,11 @@ a date (YYYY-MM-DD), git tag name (e.g. 1.58.0) or git commit SHA." without_cargo: bool, } -pub type GitDate = Date; +pub type GitDate = NaiveDate; + +pub fn today() -> NaiveDate { + Utc::now().date_naive() +} fn validate_dir(s: &str) -> anyhow::Result { let path: PathBuf = s.parse()?; @@ -204,7 +208,7 @@ enum Bound { impl FromStr for Bound { type Err = std::convert::Infallible; fn from_str(s: &str) -> Result { - parse_to_utc_date(s) + parse_to_naive_date(s) .map(Self::Date) .or_else(|_| Ok(Self::Commit(s.to_string()))) } @@ -473,7 +477,7 @@ fn fixup_bounds( fn check_bounds(start: &Option, end: &Option) -> anyhow::Result<()> { // current UTC date - let current = Utc::today(); + let current = today(); match (start, end) { // start date is after end date (Some(Bound::Date(start)), Some(Bound::Date(end))) if end < start => { @@ -592,7 +596,7 @@ impl Config { &nightly_bisection_result.searched[nightly_bisection_result.found]; if let ToolchainSpec::Nightly { date } = nightly_regression.spec { - let previous_date = date.pred(); + let previous_date = date.pred_opt().unwrap(); let working_commit = Bound::Date(previous_date).sha()?; let bad_commit = Bound::Date(date).sha()?; @@ -871,7 +875,7 @@ impl Config { } } -fn get_start_date(cfg: &Config) -> Date { +fn get_start_date(cfg: &Config) -> NaiveDate { if let Some(Bound::Date(date)) = cfg.args.start { date } else { @@ -879,7 +883,7 @@ fn get_start_date(cfg: &Config) -> Date { } } -fn get_end_date(cfg: &Config) -> Date { +fn get_end_date(cfg: &Config) -> NaiveDate { if let Some(Bound::Date(date)) = cfg.args.end { date } else { @@ -888,13 +892,13 @@ fn get_end_date(cfg: &Config) -> Date { // nightly (if available). (Some(date), None) => date, // --start only, assume --end=today - _ => Utc::today(), + _ => today(), } } } -fn date_is_future(test_date: Date) -> bool { - test_date > Utc::today() +fn date_is_future(test_date: NaiveDate) -> bool { + test_date > today() } impl Config { @@ -907,7 +911,7 @@ impl Config { let dl_spec = DownloadParams::for_nightly(self); // before this date we didn't have -std packages - let end_at = Date::from_utc(NaiveDate::from_ymd(2015, 10, 20), Utc); + let end_at = NaiveDate::from_ymd_opt(2015, 10, 20).unwrap(); let mut first_success = None; let mut nightly_date = get_start_date(self); @@ -977,7 +981,7 @@ impl Config { } Err(InstallError::NotFound { .. }) => { // go back just one day, presumably missing a nightly - nightly_date = nightly_date.pred(); + nightly_date = nightly_date.pred_opt().unwrap(); eprintln!( "*** unable to install {}. roll back one day and try again...", t @@ -1044,7 +1048,7 @@ fn toolchains_between(cfg: &Config, a: ToolchainSpec, b: ToolchainSpec) -> Vec, ) -> anyhow::Result { let dl_spec = DownloadParams::for_ci(self); - commits.retain(|c| Utc::today() - c.date < Duration::days(167)); + commits.retain(|c| today() - c.date < Duration::days(167)); if commits.is_empty() { bail!( @@ -1275,47 +1279,47 @@ mod tests { // Start and end date validations #[test] fn test_check_bounds_valid_bounds() { - let date1 = chrono::Utc::today().pred(); - let date2 = chrono::Utc::today().pred(); + let date1 = today().pred_opt().unwrap(); + let date2 = today().pred_opt().unwrap(); assert!(check_bounds(&Some(Bound::Date(date1)), &Some(Bound::Date(date2))).is_ok()); } #[test] fn test_check_bounds_invalid_start_after_end() { - let start = chrono::Utc::today(); - let end = chrono::Utc::today().pred(); + let start = today(); + let end = today().pred_opt().unwrap(); assert!(check_bounds(&Some(Bound::Date(start)), &Some(Bound::Date(end))).is_err()); } #[test] fn test_check_bounds_invalid_start_after_current() { - let start = chrono::Utc::today().succ(); - let end = chrono::Utc::today(); + let start = today().succ_opt().unwrap(); + let end = today(); assert!(check_bounds(&Some(Bound::Date(start)), &Some(Bound::Date(end))).is_err()); } #[test] fn test_check_bounds_invalid_start_after_current_without_end() { - let start = chrono::Utc::today().succ(); + let start = today().succ_opt().unwrap(); assert!(check_bounds(&Some(Bound::Date(start)), &None).is_err()); } #[test] fn test_check_bounds_invalid_end_after_current() { - let start = chrono::Utc::today(); - let end = chrono::Utc::today().succ(); + let start = today(); + let end = today().succ_opt().unwrap(); assert!(check_bounds(&Some(Bound::Date(start)), &Some(Bound::Date(end))).is_err()); } #[test] fn test_check_bounds_invalid_end_after_current_without_start() { - let end = chrono::Utc::today().succ(); + let end = today().succ_opt().unwrap(); assert!(check_bounds(&None, &Some(Bound::Date(end))).is_err()); } #[test] fn test_nightly_finder_iterator() { - let start_date = Date::from_utc(NaiveDate::from_ymd(2019, 01, 01), Utc); + let start_date = NaiveDate::from_ymd_opt(2019, 01, 01).unwrap(); let iter = NightlyFinderIter::new(start_date); diff --git a/src/repo_access.rs b/src/repo_access.rs index 7b3d9d1..ea1b384 100644 --- a/src/repo_access.rs +++ b/src/repo_access.rs @@ -59,7 +59,8 @@ impl RustRepositoryAccessor for AccessViaGithub { // this bound on the github search. let since_date = self .bound_to_date(Bound::Commit(start_sha.to_string()))? - .pred(); + .pred_opt() + .unwrap(); eprintln!( "fetching (via remote github) commits from max({}, {}) to {}", diff --git a/src/toolchains.rs b/src/toolchains.rs index af639c9..16e68a0 100644 --- a/src/toolchains.rs +++ b/src/toolchains.rs @@ -4,7 +4,7 @@ use std::io::{self, Read, Write}; use std::path::{Path, PathBuf}; use std::process::{self, Command, Stdio}; -use chrono::{Date, NaiveDate, Utc}; +use chrono::NaiveDate; use colored::Colorize; use dialoguer::Select; use flate2::read::GzDecoder; @@ -17,9 +17,7 @@ use tar::Archive; use tee::TeeReader; use xz2::read::XzDecoder; -use crate::Config; - -pub type GitDate = Date; +use crate::{Config, GitDate}; pub const YYYY_MM_DD: &str = "%Y-%m-%d"; @@ -88,7 +86,11 @@ impl Toolchain { .ok() .filter(|v| v.channel == Channel::Nightly) // rustc commit date is off-by-one, see #112 - .and_then(|v| parse_to_utc_date(&v.commit_date?).ok().map(|d| d.succ())) + .and_then(|v| { + parse_to_naive_date(&v.commit_date?) + .ok() + .map(|d| d.succ_opt().unwrap()) + }) } pub(crate) fn is_current_nightly(&self) -> bool { @@ -362,8 +364,8 @@ impl Toolchain { } } -pub fn parse_to_utc_date(s: &str) -> chrono::ParseResult { - NaiveDate::parse_from_str(s, YYYY_MM_DD).map(|date| Date::from_utc(date, Utc)) +pub fn parse_to_naive_date(s: &str) -> chrono::ParseResult { + NaiveDate::parse_from_str(s, YYYY_MM_DD) } #[derive(Clone, PartialEq, Eq, Debug)] diff --git a/tests/cmd/start-in-future.stderr b/tests/cmd/start-in-future.stderr index eac8985..5754ba1 100644 --- a/tests/cmd/start-in-future.stderr +++ b/tests/cmd/start-in-future.stderr @@ -1 +1 @@ -ERROR: start date should be on or before current date, got start date request: 9999-01-01UTC and current date is [..]UTC +ERROR: start date should be on or before current date, got start date request: 9999-01-01 and current date is [..]