From c916c39a8b22111afcdfdc053925f47f68805ad2 Mon Sep 17 00:00:00 2001 From: user1823 <92206575+user1823@users.noreply.github.com> Date: Wed, 6 Aug 2025 15:00:44 +0530 Subject: [PATCH] Limit time studied today to minutes (#4242) * Limit time studied today to minutes * Update timespan.rs * Update today.rs * Update timespan.rs * Update today.rs * Update today.rs * Update time.ts * Update time.ts * Update timespan.rs * Update timespan.rs * Update timespan.rs * Update today.rs --- rslib/src/scheduler/timespan.rs | 11 +++++++++-- rslib/src/stats/today.rs | 11 ++++++++--- ts/lib/tslib/time.ts | 2 +- 3 files changed, 18 insertions(+), 6 deletions(-) diff --git a/rslib/src/scheduler/timespan.rs b/rslib/src/scheduler/timespan.rs index b015e3e1e..9ae53d78c 100644 --- a/rslib/src/scheduler/timespan.rs +++ b/rslib/src/scheduler/timespan.rs @@ -57,10 +57,10 @@ const SECOND: f32 = 1.0; const MINUTE: f32 = 60.0 * SECOND; const HOUR: f32 = 60.0 * MINUTE; const DAY: f32 = 24.0 * HOUR; -const MONTH: f32 = 30.417 * DAY; // 365/12 ≈ 30.417 const YEAR: f32 = 365.0 * DAY; +const MONTH: f32 = YEAR / 12.0; -#[derive(Clone, Copy)] +#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord)] pub(crate) enum TimespanUnit { Seconds, Minutes, @@ -111,6 +111,13 @@ impl Timespan { } } + pub fn to_unit(self, unit: TimespanUnit) -> Timespan { + Timespan { + seconds: self.seconds, + unit, + } + } + /// Round seconds and days to integers, otherwise /// truncates to one decimal place. pub fn as_rounded_unit(self) -> f32 { diff --git a/rslib/src/stats/today.rs b/rslib/src/stats/today.rs index f856ce271..d9680c282 100644 --- a/rslib/src/stats/today.rs +++ b/rslib/src/stats/today.rs @@ -5,17 +5,18 @@ use anki_i18n::I18n; use crate::prelude::*; use crate::scheduler::timespan::Timespan; +use crate::scheduler::timespan::TimespanUnit; pub fn studied_today(cards: u32, secs: f32, tr: &I18n) -> String { let span = Timespan::from_secs(secs).natural_span(); - let amount = span.as_unit(); - let unit = span.unit().as_str(); + let unit = std::cmp::min(span.unit(), TimespanUnit::Minutes); + let amount = span.to_unit(unit).as_unit(); let secs_per_card = if cards > 0 { secs / (cards as f32) } else { 0.0 }; - tr.statistics_studied_today(unit, secs_per_card, amount, cards) + tr.statistics_studied_today(unit.as_str(), secs_per_card, amount, cards) .into() } @@ -41,5 +42,9 @@ mod test { &studied_today(3, 13.0, &tr).replace('\n', " "), "Studied 3 cards in 13 seconds today (4.33s/card)" ); + assert_eq!( + &studied_today(300, 5400.0, &tr).replace('\n', " "), + "Studied 300 cards in 90 minutes today (18s/card)" + ); } } diff --git a/ts/lib/tslib/time.ts b/ts/lib/tslib/time.ts index f40758d8d..25d70eef3 100644 --- a/ts/lib/tslib/time.ts +++ b/ts/lib/tslib/time.ts @@ -89,7 +89,7 @@ export function naturalWholeUnit(secs: number): TimespanUnit { } export function studiedToday(cards: number, secs: number): string { - const unit = naturalUnit(secs); + const unit = Math.min(naturalUnit(secs), TimespanUnit.Minutes); const amount = unitAmount(unit, secs); const name = unitName(unit);