-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix relative time format cutoff times
- Loading branch information
1 parent
05be8ea
commit 74b215a
Showing
1 changed file
with
17 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
<?php | ||
|
||
define('VERSION', '2.0.1'); | ||
define('VERSION', '2.0.2'); | ||
|
||
define('PUBLIC_FOLDER', __DIR__ . '/public'); | ||
|
||
|
@@ -343,32 +343,30 @@ public function __toString(): string | |
<script data-turbolinks-eval="false" src="https://cdn.jsdelivr.net/npm/[email protected]/darkreader.min.js"></script> | ||
<script> | ||
$[if `process.env.DATE_FORMAT === "relative"`]$ | ||
function getRelativeTimeString( | ||
date, | ||
lang = navigator.language | ||
) { | ||
// Allow dates or times to be passed | ||
function getRelativeTimeString(date, lang = navigator.language) { | ||
const timeMs = typeof date === "number" ? date : date.getTime(); | ||
|
||
// Get the amount of seconds between the given date and now | ||
const deltaSeconds = Math.round((timeMs - Date.now()) / 1000); | ||
|
||
// Array reprsenting one minute, hour, day, week, month, etc in seconds | ||
const cutoffs = [60, 3600, 86400, 86400 * 7, 86400 * 30, 86400 * 365, Infinity]; | ||
|
||
// Array equivalent to the above but in the string representation of the units | ||
const units = ["second", "minute", "hour", "day", "week", "month", "year"]; | ||
|
||
// Grab the ideal cutoff unit | ||
const unitIndex = cutoffs.findIndex(cutoff => cutoff > Math.abs(deltaSeconds)); | ||
// Find the ideal cutoff unit by iterating manually | ||
let unitIndex = 0; | ||
while (unitIndex < cutoffs.length && Math.abs(deltaSeconds) >= cutoffs[unitIndex]) { | ||
unitIndex++; | ||
} | ||
|
||
// Calculate the time difference in the current unit | ||
const timeInCurrentUnit = Math.abs(deltaSeconds) / (unitIndex ? cutoffs[unitIndex - 1] : 1); | ||
|
||
// Adjust the displayed time based on the 50% threshold | ||
const adjustedTime = Math.floor(timeInCurrentUnit); | ||
|
||
// Get the divisor to divide from the seconds. E.g. if our unit is "day" our divisor | ||
// is one day in seconds, so we can divide our seconds by this to get the # of days | ||
const divisor = unitIndex ? cutoffs[unitIndex - 1] : 1; | ||
// Include the negative sign for time that has passed | ||
const sign = deltaSeconds < 0 ? "-" : ""; | ||
|
||
// Intl.RelativeTimeFormat do its magic | ||
const rtf = new Intl.RelativeTimeFormat(lang, { /* style:"short", */ numeric: "auto" }); | ||
return rtf.format(Math.floor(deltaSeconds / divisor), units[unitIndex]); | ||
const rtf = new Intl.RelativeTimeFormat(lang, { numeric: "auto" }); | ||
return rtf.format(sign + adjustedTime, units[unitIndex]); | ||
} | ||
$[end]$ | ||
|
||
|