Update time.ts to handle very small or negative time differences (#1390)
* Update time.ts to handle very small or negative time differences Right now, posts can appear to be from the future with a negative time difference (i.e. -3s appears). This change defines 'NOW' as less than 5 seconds old, and returns 'now' in that case. It's not clear how localisation is handled - this may need translation. * Add test for 'now' in time/ago(...) Add tests for ago() for right now (i.e. 'now') and 10s ago to ensure the seconds case is still tested
This commit is contained in:
parent
6d73ed96e1
commit
775aa87540
2 changed files with 7 additions and 2 deletions
|
@ -1,3 +1,4 @@
|
|||
const NOW = 5
|
||||
const MINUTE = 60
|
||||
const HOUR = MINUTE * 60
|
||||
const DAY = HOUR * 24
|
||||
|
@ -13,7 +14,9 @@ export function ago(date: number | string | Date): string {
|
|||
ts = date
|
||||
}
|
||||
const diffSeconds = Math.floor((Date.now() - ts) / 1e3)
|
||||
if (diffSeconds < MINUTE) {
|
||||
if (diffSeconds < NOW) {
|
||||
return `now`
|
||||
} else if (diffSeconds < MINUTE) {
|
||||
return `${diffSeconds}s`
|
||||
} else if (diffSeconds < HOUR) {
|
||||
return `${Math.floor(diffSeconds / MINUTE)}m`
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue