diff --git a/src/changelog/section/from_history.rs b/src/changelog/section/from_history.rs index acda64f..eb75606 100644 --- a/src/changelog/section/from_history.rs +++ b/src/changelog/section/from_history.rs @@ -1,4 +1,4 @@ -use std::{collections::BTreeMap, ops::Sub}; +use std::collections::BTreeMap; use cargo_metadata::Package; use gix::prelude::ObjectIdExt; @@ -88,14 +88,32 @@ impl Section { .as_ref() .filter(|_| selection.contains(Selection::COMMIT_STATISTICS)) { - let duration = history - .last() - .map(|last| date_time.sub(&time_to_zoned_time(last.commit_time).expect("valid time"))); + let duration = history.last().and_then(|last| { + let first_commit_time = time_to_zoned_time(last.commit_time).expect("valid time"); + let span = date_time + .since( + jiff::ZonedDifference::new(&first_commit_time) + .smallest(jiff::Unit::Day) + .largest(jiff::Unit::Day), + ) + .ok()?; + Some(span.get_days()) + }); + let time_passed_since_last_release = prev_date_time.and_then(|prev_time| { + let span = date_time + .since( + jiff::ZonedDifference::new(&prev_time) + .smallest(jiff::Unit::Day) + .largest(jiff::Unit::Day), + ) + .ok()?; + Some(span.get_days()) + }); 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, conventional_count: history.iter().filter(|item| item.message.kind.is_some()).count(), unique_issues: { let mut v = commits_by_category diff --git a/src/changelog/section/segment.rs b/src/changelog/section/segment.rs index caeb990..f92ac0f 100644 --- a/src/changelog/section/segment.rs +++ b/src/changelog/section/segment.rs @@ -109,14 +109,14 @@ impl Details { 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, + /// The time span, in days, from first to last commit, if there is more than one. + pub duration: Option, /// 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, - /// The duration from the release before this one, if this isn't the first release. - pub time_passed_since_last_release: Option, + /// The duration, in days, from the release before this one, if this isn't the first release. + pub time_passed_since_last_release: Option, } impl CommitStatistics { diff --git a/src/changelog/write.rs b/src/changelog/write.rs index 8fb7615..4806bcc 100644 --- a/src/changelog/write.rs +++ b/src/changelog/write.rs @@ -320,19 +320,15 @@ impl section::Segment { count, if *count == 1 { "commit" } else { "commits" }, match duration { - Some(duration) if duration.get_days() > 0 => format!( + &Some(duration) if duration > 0 => format!( " over the course of {} calendar {}.", - duration.get_days(), - if duration.get_days() == 1 { "day" } else { "days" } + duration, + if duration == 1 { "day" } else { "days" } ), _ => ".".into(), } )?; - if let Some(days_between_releases) = time_passed_since_last_release - .and_then(|d| d.total(jiff::Unit::Day).ok()) - .map(|d| d.floor() as u64) - .filter(|d| *d > 0) - { + if let Some(days_between_releases) = time_passed_since_last_release.filter(|d| *d > 0) { writeln!( out, " - {} {} passed between releases.",