aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHampusM <hampus@hampusmat.com>2021-05-12 12:03:56 +0200
committerHampusM <hampus@hampusmat.com>2021-05-12 12:03:56 +0200
commit3ea0d6e4c790f41c83d142e024d04db1c9834dec (patch)
tree935cf60cb6b5f89832238b2e0e4897e45f4f51dd
parent35750791cdf588f74ac6cf84fab4af911df7a0cb (diff)
Switched to date-fns's implementation of time difference formatting
-rw-r--r--api/git.js23
1 files changed, 2 insertions, 21 deletions
diff --git a/api/git.js b/api/git.js
index 8574dfa..47b47bc 100644
--- a/api/git.js
+++ b/api/git.js
@@ -1,5 +1,5 @@
const { spawn } = require("child_process");
-const { differenceInMilliseconds } = require('date-fns');
+const { formatDistance } = require('date-fns');
const fs = require('fs');
function execGit(path, action, args = [""], error)
@@ -56,22 +56,6 @@ function getLog(base_dir, path)
})
}
-function getOptimalDateDifference(difference)
-{
- const time_values = { "second": 1000, "minute": 60000, "hour": 3600000, "day": 86400000, "week": 604800000, "month": 2629800000, "year": 31557600000 };
-
- let last;
- for(const [key, value] of Object.entries(time_values)) {
- if(difference > value) {
- last = key;
- continue;
- }
- break;
- }
-
- return `${Math.round(difference / time_values[last])} ${last}s`;
-}
-
function getTimeSinceLatestCommit(path)
{
return new Promise((resolve) =>
@@ -84,10 +68,7 @@ function getTimeSinceLatestCommit(path)
git.on("close", (code) =>
{
- const commit_timestamp = new Date(Number(Buffer.concat(commit).toString()) * 1000);
- const now_date = new Date();
-
- resolve(getOptimalDateDifference(differenceInMilliseconds(now_date, commit_timestamp)));
+ resolve(formatDistance(new Date(), new Date(Number(Buffer.concat(commit).toString()) * 1000)));
});
});
}