Improve natural unit conversion for a time b/w 360 to 365 days (#3901)

* Improve natural unit conversion for a time b/w 360 to 365 days

Previously, 363 days would be converted to 12.1 months, which is quite confusing because
- a user would think that if the value is more than 12 months, why it isn't displayed in years
- the value is actually less than a year, which is counterintuitive as 12.1 m suggests a value more than a year.

* precise

* Update time.ts to match timespan.rs

* Add another test

* Use average duration of a month instead

* Update time.ts

* Update test_schedv3.py

* Update time.test.ts
This commit is contained in:
user1823 2025-04-13 09:56:34 +05:30 committed by GitHub
parent 332830e5d7
commit e546c6d11f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 9 additions and 5 deletions

View file

@ -537,7 +537,7 @@ def test_nextIvl():
assert ni(c, 3) == 21600000 assert ni(c, 3) == 21600000
# (* 100 2.5 1.3 86400)28080000.0 # (* 100 2.5 1.3 86400)28080000.0
assert ni(c, 4) == 28080000 assert ni(c, 4) == 28080000
assert without_unicode_isolation(col.sched.nextIvlStr(c, 4)) == "10.8mo" assert without_unicode_isolation(col.sched.nextIvlStr(c, 4)) == "10.7mo"
def test_bury(): def test_bury():

View file

@ -57,7 +57,7 @@ const SECOND: f32 = 1.0;
const MINUTE: f32 = 60.0 * SECOND; const MINUTE: f32 = 60.0 * SECOND;
const HOUR: f32 = 60.0 * MINUTE; const HOUR: f32 = 60.0 * MINUTE;
const DAY: f32 = 24.0 * HOUR; const DAY: f32 = 24.0 * HOUR;
const MONTH: f32 = 30.0 * DAY; const MONTH: f32 = 30.417 * DAY; // 365/12 ≈ 30.417
const YEAR: f32 = 365.0 * DAY; const YEAR: f32 = 365.0 * DAY;
#[derive(Clone, Copy)] #[derive(Clone, Copy)]
@ -188,6 +188,10 @@ mod test {
assert_eq!(time_span(30.3, &tr, true), "30.3 seconds"); assert_eq!(time_span(30.3, &tr, true), "30.3 seconds");
assert_eq!(time_span(90.0, &tr, false), "1.5 minutes"); assert_eq!(time_span(90.0, &tr, false), "1.5 minutes");
assert_eq!(time_span(45.0 * 86_400.0, &tr, false), "1.5 months"); assert_eq!(time_span(45.0 * 86_400.0, &tr, false), "1.5 months");
assert_eq!(time_span(364.0 * 86_400.0, &tr, false), "12 months");
assert_eq!(time_span(364.0 * 86_400.0, &tr, true), "11.97 months");
assert_eq!(time_span(365.0 * 86_400.0, &tr, false), "1 year");
assert_eq!(time_span(365.0 * 86_400.0, &tr, true), "1 year");
assert_eq!(time_span(365.0 * 86_400.0 * 1.5, &tr, false), "1.5 years"); assert_eq!(time_span(365.0 * 86_400.0 * 1.5, &tr, false), "1.5 years");
} }
} }

View file

@ -12,7 +12,7 @@ test("natural unit", () => {
expect(naturalUnit(60 * 60 - 1)).toBe(TimespanUnit.Minutes); expect(naturalUnit(60 * 60 - 1)).toBe(TimespanUnit.Minutes);
expect(naturalUnit(60 * 60)).toBe(TimespanUnit.Hours); expect(naturalUnit(60 * 60)).toBe(TimespanUnit.Hours);
expect(naturalUnit(60 * 60 * 24)).toBe(TimespanUnit.Days); expect(naturalUnit(60 * 60 * 24)).toBe(TimespanUnit.Days);
expect(naturalUnit(60 * 60 * 24 * 30)).toBe(TimespanUnit.Months); expect(naturalUnit(60 * 60 * 24 * 31)).toBe(TimespanUnit.Months);
}); });
test("natural whole unit", () => { test("natural whole unit", () => {

View file

@ -7,8 +7,8 @@ export const SECOND = 1.0;
export const MINUTE = 60.0 * SECOND; export const MINUTE = 60.0 * SECOND;
export const HOUR = 60.0 * MINUTE; export const HOUR = 60.0 * MINUTE;
export const DAY = 24.0 * HOUR; export const DAY = 24.0 * HOUR;
export const MONTH = 30.0 * DAY; export const MONTH = 30.417 * DAY; // 365/12 ≈ 30.417
export const YEAR = 12.0 * MONTH; export const YEAR = 365.0 * DAY;
export enum TimespanUnit { export enum TimespanUnit {
Seconds, Seconds,