about summary refs log tree commit diff
diff options
context:
space:
mode:
authorHailey <me@haileyok.com>2024-05-13 08:39:34 -0700
committerGitHub <noreply@github.com>2024-05-13 08:39:34 -0700
commit5cd4ac3a34f629945ccb86e451fbf20dd06e6863 (patch)
treeaf281b0a6b439c6b8bf666b7586ed349353f0ce9
parent73d094c67e53506fd3c4ab2c29b37ab481cd9331 (diff)
downloadvoidsky-5cd4ac3a34f629945ccb86e451fbf20dd06e6863.tar.zst
get a little more accurate with month length (#3981)
* get a little more accurate with month length

* create some wiggle room, create some specific tests

* update more tests
-rw-r--r--__tests__/lib/string.test.ts42
-rw-r--r--src/lib/strings/time.ts21
2 files changed, 57 insertions, 6 deletions
diff --git a/__tests__/lib/string.test.ts b/__tests__/lib/string.test.ts
index c8a209dfa..75cbaeea5 100644
--- a/__tests__/lib/string.test.ts
+++ b/__tests__/lib/string.test.ts
@@ -143,6 +143,10 @@ describe('makeRecordUri', () => {
 })
 
 describe('ago', () => {
+  const oneYearDate = new Date(
+    new Date().setMonth(new Date().getMonth() - 11),
+  ).setDate(new Date().getDate() - 28)
+
   const inputs = [
     1671461038,
     '04 Dec 1995 00:12:00 GMT',
@@ -151,7 +155,32 @@ describe('ago', () => {
     new Date().setMinutes(new Date().getMinutes() - 10),
     new Date().setHours(new Date().getHours() - 1),
     new Date().setDate(new Date().getDate() - 1),
+    new Date().setDate(new Date().getDate() - 20),
+    new Date().setDate(new Date().getDate() - 25),
+    new Date().setDate(new Date().getDate() - 28),
+    new Date().setDate(new Date().getDate() - 29),
+    new Date().setDate(new Date().getDate() - 30),
     new Date().setMonth(new Date().getMonth() - 1),
+    new Date(new Date().setMonth(new Date().getMonth() - 1)).setDate(
+      new Date().getDate() - 20,
+    ),
+    new Date(new Date().setMonth(new Date().getMonth() - 1)).setDate(
+      new Date().getDate() - 25,
+    ),
+    new Date(new Date().setMonth(new Date().getMonth() - 1)).setDate(
+      new Date().getDate() - 28,
+    ),
+    new Date(new Date().setMonth(new Date().getMonth() - 1)).setDate(
+      new Date().getDate() - 29,
+    ),
+    new Date().setMonth(new Date().getMonth() - 11),
+    new Date(new Date().setMonth(new Date().getMonth() - 11)).setDate(
+      new Date().getDate() - 20,
+    ),
+    new Date(new Date().setMonth(new Date().getMonth() - 11)).setDate(
+      new Date().getDate() - 25,
+    ),
+    oneYearDate,
   ]
   const outputs = [
     new Date(1671461038).toLocaleDateString(),
@@ -161,7 +190,20 @@ describe('ago', () => {
     '10m',
     '1h',
     '1d',
+    '20d',
+    '25d',
+    '28d',
+    '29d',
+    '1mo',
+    '1mo',
+    '1mo',
     '1mo',
+    '2mo',
+    '2mo',
+    '11mo',
+    '11mo',
+    '11mo',
+    new Date(oneYearDate).toLocaleDateString(),
   ]
 
   it('correctly calculates how much time passed, in a string', () => {
diff --git a/src/lib/strings/time.ts b/src/lib/strings/time.ts
index 3e162af1a..8de4b52ae 100644
--- a/src/lib/strings/time.ts
+++ b/src/lib/strings/time.ts
@@ -2,8 +2,8 @@ const NOW = 5
 const MINUTE = 60
 const HOUR = MINUTE * 60
 const DAY = HOUR * 24
-const MONTH = DAY * 28
-const YEAR = DAY * 365
+const MONTH_30 = DAY * 30
+const MONTH = DAY * 30.41675 // This results in 365.001 days in a year, which is close enough for nearly all cases
 export function ago(date: number | string | Date): string {
   let ts: number
   if (typeof date === 'string') {
@@ -22,12 +22,21 @@ export function ago(date: number | string | Date): string {
     return `${Math.floor(diffSeconds / MINUTE)}m`
   } else if (diffSeconds < DAY) {
     return `${Math.floor(diffSeconds / HOUR)}h`
-  } else if (diffSeconds < MONTH) {
+  } else if (diffSeconds < MONTH_30) {
     return `${Math.round(diffSeconds / DAY)}d`
-  } else if (diffSeconds < YEAR) {
-    return `${Math.floor(diffSeconds / MONTH)}mo`
   } else {
-    return new Date(ts).toLocaleDateString()
+    let months = diffSeconds / MONTH
+    if (months % 1 >= 0.9) {
+      months = Math.ceil(months)
+    } else {
+      months = Math.floor(months)
+    }
+
+    if (months < 12) {
+      return `${months}mo`
+    } else {
+      return new Date(ts).toLocaleDateString()
+    }
   }
 }