Skip to content

Commit

Permalink
Replace time with jiff
Browse files Browse the repository at this point in the history
This wasn't planned, but `time` (despite being pulled as our own dependency)
stopped working. Maybe it wasn't new enough anymore?
  • Loading branch information
Byron committed Aug 23, 2024
1 parent 056d306 commit 96735f5
Show file tree
Hide file tree
Showing 10 changed files with 113 additions and 303 deletions.
358 changes: 88 additions & 270 deletions Cargo.lock

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ cargo_metadata = "0.18.0"
log = "0.4.14"
toml_edit = "0.21.0"
semver = "1.0.4"
crates-index = { version = "2.2.0", default-features = false, features = ["git-performance", "git-https"] }
crates-index = { version = "3.2.0", default-features = false, features = ["git-performance", "git-https"] }
cargo_toml = "0.17.2"
winnow = "0.5.12"
git-conventional = "0.12.0"
time = "0.3.23"
jiff = "0.1.8"
pulldown-cmark = "0.9.0"
bitflags = "2"

Expand Down
2 changes: 1 addition & 1 deletion src/changelog/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ pub enum Section {
/// A segment describing a particular release
Release {
name: Version,
date: Option<time::OffsetDateTime>,
date: Option<jiff::Zoned>,
/// the amount of # in front of the heading denoting the release name
heading_level: usize,
/// What came right before the version
Expand Down
10 changes: 4 additions & 6 deletions src/changelog/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,7 @@ struct Headline {
level: usize,
version_prefix: String,
version: Option<semver::Version>,
date: Option<time::OffsetDateTime>,
date: Option<jiff::Zoned>,
}

impl<'a> TryFrom<&'a str> for Headline {
Expand Down Expand Up @@ -496,11 +496,9 @@ fn headline<'a, E: ParserError<&'a str> + FromExternalError<&'a str, ()>>(i: &mu
"(",
(take_n_digits(4), "-", take_n_digits(2), "-", take_n_digits(2)).try_map(
|(year, _, month, _, day)| {
time::Month::try_from(month as u8).map_err(|_| ()).and_then(|month| {
time::Date::from_calendar_date(year as i32, month, day as u8)
.map_err(|_| ())
.map(|d| d.midnight().assume_utc())
})
jiff::civil::Date::new(year as i16, month as i8, day as i8)
.map_err(|_| ())
.and_then(|d| d.to_zoned(jiff::tz::TimeZone::UTC).map_err(|_| ()))
},
),
")",
Expand Down
11 changes: 5 additions & 6 deletions src/changelog/section/from_history.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use std::{collections::BTreeMap, ops::Sub};

use cargo_metadata::Package;
use gix::prelude::ObjectIdExt;
use time::OffsetDateTime;

use crate::{
changelog,
Expand All @@ -12,7 +11,7 @@ use crate::{
Section,
},
commit, utils,
utils::{is_top_level_package, time_to_offset_date_time},
utils::{is_top_level_package, time_to_zoned_time},
};

impl Section {
Expand Down Expand Up @@ -91,12 +90,12 @@ impl Section {
{
let duration = history
.last()
.map(|last| date_time.sub(time_to_offset_date_time(last.commit_time)));
.map(|last| date_time.sub(&time_to_zoned_time(last.commit_time).expect("valid time")));
segments.push(Segment::Statistics(section::Data::Generated(
section::segment::CommitStatistics {
count: history.len(),
duration,
time_passed_since_last_release: prev_date_time.map(|prev_time| date_time.sub(prev_time)),
time_passed_since_last_release: prev_date_time.map(|prev_time| date_time.sub(&prev_time)),
conventional_count: history.iter().filter(|item| item.message.kind.is_some()).count(),
unique_issues: {
let mut v = commits_by_category
Expand Down Expand Up @@ -158,7 +157,7 @@ impl Section {
}
}

fn segment_head_time(segment: &commit::history::Segment<'_>, repo: &gix::Repository) -> OffsetDateTime {
fn segment_head_time(segment: &commit::history::Segment<'_>, repo: &gix::Repository) -> jiff::Zoned {
let time = segment
.head
.peeled
Expand All @@ -170,5 +169,5 @@ fn segment_head_time(segment: &commit::history::Segment<'_>, repo: &gix::Reposit
.committer
.time;

time_to_offset_date_time(time)
time_to_zoned_time(time).expect("always valid time")
}
4 changes: 2 additions & 2 deletions src/changelog/section/segment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,13 +110,13 @@ pub struct CommitStatistics {
/// Amount of commits that contributed to the release
pub count: usize,
/// The time span from first to last commit, if there is more than one.
pub duration: Option<time::Duration>,
pub duration: Option<jiff::Span>,
/// Amount of commits that could be parsed as git-conventional
pub conventional_count: usize,
/// The issue numbers that were referenced in commit messages
pub unique_issues: Vec<details::Category>,
/// The duration from the release before this one, if this isn't the first release.
pub time_passed_since_last_release: Option<time::Duration>,
pub time_passed_since_last_release: Option<jiff::Span>,
}

impl CommitStatistics {
Expand Down
12 changes: 6 additions & 6 deletions src/changelog/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -320,20 +320,20 @@ impl section::Segment {
count,
if *count == 1 { "commit" } else { "commits" },
match duration {
Some(duration) if duration.whole_days() > 0 => format!(
Some(duration) if duration.get_days() > 0 => format!(
" over the course of {} calendar {}.",
duration.whole_days(),
if duration.whole_days() == 1 { "day" } else { "days" }
duration.get_days(),
if duration.get_days() == 1 { "day" } else { "days" }
),
_ => ".".into(),
}
)?;
if let Some(time_between_releases) = time_passed_since_last_release.filter(|d| d.whole_days() > 0) {
if let Some(time_between_releases) = time_passed_since_last_release.filter(|d| d.get_days() > 0) {
writeln!(
out,
" - {} {} passed between releases.",
time_between_releases.whole_days(),
if time_between_releases.whole_days() == 1 {
time_between_releases.get_days(),
if time_between_releases.get_days() == 1 {
"day"
} else {
"days"
Expand Down
2 changes: 0 additions & 2 deletions src/cli/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ fn main() -> anyhow::Result<()> {
unsafe {
// SAFETY: We do nothing that could block.
gix::interrupt::init_handler(2, || {})?;
// SAFETY: we don't manipulate the environment from any thread
time::util::local_offset::set_soundness(time::util::local_offset::Soundness::Unsound);
}
let args: Args = Args::parse();
match args.subcommands {
Expand Down
6 changes: 3 additions & 3 deletions src/command/release/manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ fn gather_changelog_data<'meta>(
release_section_by_publishee,
made_change,
} = &mut out;
let next_commit_date = crate::utils::time_to_offset_date_time(crate::git::author()?.time);
let next_commit_date = crate::utils::time_to_zoned_time(crate::git::author()?.time).expect("valid time");
for (publishee, new_version) in crates_and_versions_to_be_published {
let lock = gix::lock::File::acquire_to_update_resource(
&publishee.manifest_path,
Expand Down Expand Up @@ -431,7 +431,7 @@ fn gather_changelog_data<'meta>(
);
}
*name = changelog::Version::Semantic((*new_version).to_owned());
*date = Some(next_commit_date);
*date = Some(next_commit_date.clone());
let recent_section = log.sections.remove(recent_idx);
match log
.sections
Expand All @@ -457,7 +457,7 @@ fn gather_changelog_data<'meta>(
recent_version
);
}
*date = Some(next_commit_date);
*date = Some(next_commit_date.clone());
}
changelog::Section::Verbatim { .. } => unreachable!("BUG: checked in prior function"),
};
Expand Down
7 changes: 2 additions & 5 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ use cargo_metadata::{
};
use gix::bstr::{BStr, ByteSlice};
use semver::{Version, VersionReq};
use time::OffsetDateTime;

pub struct Program {
pub found: bool,
Expand Down Expand Up @@ -157,10 +156,8 @@ pub fn component_to_bytes(c: Utf8Component<'_>) -> &[u8] {
}
}

pub fn time_to_offset_date_time(time: gix::date::Time) -> OffsetDateTime {
time::OffsetDateTime::from_unix_timestamp(time.seconds)
.expect("always valid unix time")
.replace_offset(time::UtcOffset::from_whole_seconds(time.offset).expect("valid offset"))
pub fn time_to_zoned_time(time: gix::date::Time) -> anyhow::Result<jiff::Zoned> {
Ok(jiff::Timestamp::new(time.seconds, 0)?.to_zoned(jiff::tz::Offset::from_seconds(time.offset)?.to_time_zone()))
}

#[cfg(test)]
Expand Down

0 comments on commit 96735f5

Please sign in to comment.