Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update chrono #268

Merged
merged 1 commit into from
Mar 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion src/git.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/github.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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<Commit> {
Expand Down Expand Up @@ -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)
Expand Down
56 changes: 30 additions & 26 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
};

Expand Down Expand Up @@ -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<Utc>;
pub type GitDate = NaiveDate;

pub fn today() -> NaiveDate {
Utc::now().date_naive()
}

fn validate_dir(s: &str) -> anyhow::Result<PathBuf> {
let path: PathBuf = s.parse()?;
Expand All @@ -204,7 +208,7 @@ enum Bound {
impl FromStr for Bound {
type Err = std::convert::Infallible;
fn from_str(s: &str) -> Result<Self, Self::Err> {
parse_to_utc_date(s)
parse_to_naive_date(s)
.map(Self::Date)
.or_else(|_| Ok(Self::Commit(s.to_string())))
}
Expand Down Expand Up @@ -473,7 +477,7 @@ fn fixup_bounds(

fn check_bounds(start: &Option<Bound>, end: &Option<Bound>) -> 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 => {
Expand Down Expand Up @@ -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()?;
Expand Down Expand Up @@ -871,15 +875,15 @@ impl Config {
}
}

fn get_start_date(cfg: &Config) -> Date<Utc> {
fn get_start_date(cfg: &Config) -> NaiveDate {
if let Some(Bound::Date(date)) = cfg.args.start {
date
} else {
get_end_date(cfg)
}
}

fn get_end_date(cfg: &Config) -> Date<Utc> {
fn get_end_date(cfg: &Config) -> NaiveDate {
if let Some(Bound::Date(date)) = cfg.args.end {
date
} else {
Expand All @@ -888,13 +892,13 @@ fn get_end_date(cfg: &Config) -> Date<Utc> {
// nightly (if available).
(Some(date), None) => date,
// --start only, assume --end=today
_ => Utc::today(),
_ => today(),
}
}
}

fn date_is_future(test_date: Date<Utc>) -> bool {
test_date > Utc::today()
fn date_is_future(test_date: NaiveDate) -> bool {
test_date > today()
}

impl Config {
Expand All @@ -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);
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -1044,7 +1048,7 @@ fn toolchains_between(cfg: &Config, a: ToolchainSpec, b: ToolchainSpec) -> Vec<T
std_targets: std_targets.clone(),
};
toolchains.push(t);
date = date.succ();
date = date.succ_opt().unwrap();
}
toolchains
}
Expand Down Expand Up @@ -1110,7 +1114,7 @@ impl Config {
mut commits: Vec<Commit>,
) -> anyhow::Result<BisectionResult> {
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!(
Expand Down Expand Up @@ -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);

Expand Down
3 changes: 2 additions & 1 deletion src/repo_access.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {}",
Expand Down
16 changes: 9 additions & 7 deletions src/toolchains.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -17,9 +17,7 @@ use tar::Archive;
use tee::TeeReader;
use xz2::read::XzDecoder;

use crate::Config;

pub type GitDate = Date<Utc>;
use crate::{Config, GitDate};

pub const YYYY_MM_DD: &str = "%Y-%m-%d";

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -362,8 +364,8 @@ impl Toolchain {
}
}

pub fn parse_to_utc_date(s: &str) -> chrono::ParseResult<GitDate> {
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<GitDate> {
NaiveDate::parse_from_str(s, YYYY_MM_DD)
}

#[derive(Clone, PartialEq, Eq, Debug)]
Expand Down
2 changes: 1 addition & 1 deletion tests/cmd/start-in-future.stderr
Original file line number Diff line number Diff line change
@@ -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 [..]