From e76f6a3fe3cb77fa651954f3f96b245e9c97955a Mon Sep 17 00:00:00 2001 From: "plebeius.eth" Date: Thu, 17 Aug 2023 13:29:46 +0200 Subject: [PATCH] add precise date time function --- package.json | 1 - src/utils/getFormattedTime.js | 39 ++++++++++++++++++++++++++++------- yarn.lock | 12 ----------- 3 files changed, 32 insertions(+), 20 deletions(-) diff --git a/package.json b/package.json index bcaf0e0a..990d4f51 100644 --- a/package.json +++ b/package.json @@ -21,7 +21,6 @@ "eslint": "8.36.0", "eslint-config-react-app": "7.0.1", "ext-name": "5.0.0", - "javascript-time-ago": "^2.5.9", "lodash": "4.17.21", "mock-require": "3.0.3", "postcss": "8.4.21", diff --git a/src/utils/getFormattedTime.js b/src/utils/getFormattedTime.js index 3f301819..82545135 100644 --- a/src/utils/getFormattedTime.js +++ b/src/utils/getFormattedTime.js @@ -1,14 +1,39 @@ -import en from 'javascript-time-ago/locale/en' -import TimeAgo from 'javascript-time-ago' - -TimeAgo.addDefaultLocale(en); -const timeAgo = new TimeAgo('en-US'); +const pluralize = (unit, value) => { + return `${value} ${unit}${value > 1 ? 's' : ''}`; +}; const getFormattedTime = (timestamp) => { try { - return timeAgo.format(timestamp * 1000) + const currentTime = new Date().getTime(); + const differenceInMilliseconds = currentTime - (timestamp * 1000); + + const years = Math.floor(differenceInMilliseconds / (1000 * 60 * 60 * 24 * 365.25)); + const months = Math.floor((differenceInMilliseconds % (1000 * 60 * 60 * 24 * 365.25)) / (1000 * 60 * 60 * 24 * 30)); + const days = Math.floor((differenceInMilliseconds % (1000 * 60 * 60 * 24 * 30)) / (1000 * 60 * 60 * 24)); + const hours = Math.floor((differenceInMilliseconds % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60)); + const minutes = Math.floor((differenceInMilliseconds % (1000 * 60 * 60)) / (1000 * 60)); + const seconds = Math.floor((differenceInMilliseconds % (1000 * 60)) / 1000); + + if (years > 0) { + return months > 0 ? `${pluralize('year', years)} and ${pluralize('month', months)} ago` : `${pluralize('year', years)} ago`; + } else if (months > 0) { + return `${pluralize('month', months)} and ${pluralize('day', days)} ago`; + } else if (days > 0) { + return `${pluralize('day', days)} and ${pluralize('hour', hours)} ago`; + } else if (hours > 0) { + return `${pluralize('hour', hours)} and ${pluralize('minute', minutes)} ago`; + } else if (minutes > 0) { + return `${pluralize('minute', minutes)} and ${pluralize('second', seconds)} ago`; + } else if (seconds > 30) { + return `${pluralize('second', seconds)} ago`; + } else { + return "just now"; + } + } + catch (e) { + console.error("Error in getFormattedTime:", e); + return "[error]"; } - catch (e) {} }; export default getFormattedTime; \ No newline at end of file diff --git a/yarn.lock b/yarn.lock index 12a1c00f..62e757e4 100644 --- a/yarn.lock +++ b/yarn.lock @@ -8997,13 +8997,6 @@ jake@^10.8.5: filelist "^1.0.4" minimatch "^3.1.2" -javascript-time-ago@^2.5.9: - version "2.5.9" - resolved "https://registry.yarnpkg.com/javascript-time-ago/-/javascript-time-ago-2.5.9.tgz#3c5d8012cd493d764c6b26a0ffe6e8b20afcf1fe" - integrity sha512-pQ8mNco/9g9TqWXWWjP0EWl6i/lAQScOyEeXy5AB+f7MfLSdgyV9BJhiOD1zrIac/lrxPYOWNbyl/IW8CW5n0A== - dependencies: - relative-time-format "^1.1.6" - jest-changed-files@^27.5.1: version "27.5.1" resolved "https://registry.yarnpkg.com/jest-changed-files/-/jest-changed-files-27.5.1.tgz#a348aed00ec9bf671cc58a66fcbe7c3dfd6a68f5" @@ -12707,11 +12700,6 @@ relateurl@^0.2.7: resolved "https://registry.yarnpkg.com/relateurl/-/relateurl-0.2.7.tgz#54dbf377e51440aca90a4cd274600d3ff2d888a9" integrity sha512-G08Dxvm4iDN3MLM0EsP62EDV9IuhXPR6blNz6Utcp7zyV3tr4HVNINt6MpaRWbxoOHT3Q7YN2P+jaHX8vUbgog== -relative-time-format@^1.1.6: - version "1.1.6" - resolved "https://registry.yarnpkg.com/relative-time-format/-/relative-time-format-1.1.6.tgz#724a5fbc3794b8e0471b6b61419af2ce699eb9f1" - integrity sha512-aCv3juQw4hT1/P/OrVltKWLlp15eW1GRcwP1XdxHrPdZE9MtgqFpegjnTjLhi2m2WI9MT/hQQtE+tjEWG1hgkQ== - remark-breaks@3.0.2: version "3.0.2" resolved "https://registry.yarnpkg.com/remark-breaks/-/remark-breaks-3.0.2.tgz#f466b9d3474d7323146c0149fc1496dabadd908e"