From 762353945a744ae4b89970b9e08c2c22a52ddbff Mon Sep 17 00:00:00 2001 From: Ian2020 Date: Wed, 29 Nov 2023 13:12:24 +0000 Subject: Add new --align-intervals option. --- src/times.cc | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) (limited to 'src/times.cc') diff --git a/src/times.cc b/src/times.cc index 2c45cfcf..b66a6e30 100644 --- a/src/times.cc +++ b/src/times.cc @@ -1112,6 +1112,7 @@ date_interval_t date_parser_t::parse() range.end_inclusive = end_inclusive; period.range = date_specifier_or_range_t(range); + period.since_specified = static_cast(since_specifier); } else if (inclusion_specifier) { period.range = date_specifier_or_range_t(*inclusion_specifier); @@ -1180,7 +1181,7 @@ date_t date_duration_t::find_nearest(const date_t& date, skip_quantum_t skip) return result; } -void date_interval_t::stabilize(const optional& date) +void date_interval_t::stabilize(const optional& date, bool align_intervals) { #if DEBUG_ON if (date) @@ -1220,7 +1221,11 @@ void date_interval_t::stabilize(const optional& date) // These start on most recent period start quantum before when DEBUG("times.interval", "stabilize: monthly, quarterly or yearly duration"); - start = date_duration_t::find_nearest(when, duration->quantum); + if (align_intervals && since_specified) { + start = when; + } else { + start = date_duration_t::find_nearest(when, duration->quantum); + } break; case date_duration_t::WEEKS: // Weeks start on the beginning of week prior to 400 remainder period length @@ -1228,9 +1233,13 @@ void date_interval_t::stabilize(const optional& date) // implies period is never less than 400 days not too unreasonable DEBUG("times.interval", "stabilize: weekly duration"); { - int period = duration->length * 7; - start = date_duration_t::find_nearest( - when - gregorian::days(period + 400 % period), duration->quantum); + if (align_intervals && since_specified) { + start = when; + } else { + int period = duration->length * 7; + start = date_duration_t::find_nearest( + when - gregorian::days(period + 400 % period), duration->quantum); + } } break; default: @@ -1298,9 +1307,10 @@ void date_interval_t::stabilize(const optional& date) } bool date_interval_t::find_period(const date_t& date, + const bool align_intervals, const bool allow_shift) { - stabilize(date); + stabilize(date, align_intervals); if (finish && date > *finish) { DEBUG("times.interval", -- cgit v1.2.3