-
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.
update timestamp calculation on dsnviewr
- Loading branch information
Showing
4 changed files
with
58 additions
and
34 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
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
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,32 +1,50 @@ | ||
export function getRelativeTime(timestamp: string): string { | ||
const date = new Date(timestamp); | ||
const now = new Date(); | ||
const diffInSeconds = Math.floor((now.getTime() - date.getTime()) / 1000); | ||
|
||
if (diffInSeconds < 60) { | ||
return `${diffInSeconds}s ago`; | ||
} | ||
|
||
const diffInMinutes = Math.floor(diffInSeconds / 60); | ||
if (diffInMinutes < 60) { | ||
return `${diffInMinutes}m ago`; | ||
} | ||
|
||
const diffInHours = Math.floor(diffInMinutes / 60); | ||
if (diffInHours < 24) { | ||
return `${diffInHours}h ago`; | ||
} | ||
|
||
const diffInDays = Math.floor(diffInHours / 24); | ||
if (diffInDays < 30) { | ||
return `${diffInDays}d ago`; | ||
} | ||
|
||
const diffInMonths = Math.floor(diffInDays / 30); | ||
if (diffInMonths < 12) { | ||
return `${diffInMonths}mo ago`; | ||
} | ||
|
||
const diffInYears = Math.floor(diffInDays / 365); | ||
return `${diffInYears}y ago`; | ||
} | ||
import dayjs from 'dayjs' | ||
import relativeTime from 'dayjs/plugin/relativeTime' | ||
import utc from 'dayjs/plugin/utc' | ||
|
||
dayjs.extend(relativeTime) | ||
dayjs.extend(utc) | ||
|
||
export const formatSeconds = (seconds: number | bigint): string => { | ||
if (typeof seconds === 'number' && seconds < 0) { | ||
throw new Error('Seconds cannot be negative') | ||
} else if (typeof seconds === 'bigint' && seconds < 0n) { | ||
throw new Error('Seconds cannot be negative') | ||
} | ||
|
||
const timeUnits = [ | ||
{ unit: 'd', value: 86400n }, | ||
{ unit: 'h', value: 3600n }, | ||
{ unit: 'm', value: 60n }, | ||
{ unit: 's', value: 1n }, | ||
] | ||
|
||
let secondsBigInt = BigInt(seconds) | ||
|
||
return ( | ||
timeUnits | ||
.reduce((result, { unit, value }) => { | ||
if (secondsBigInt >= value) { | ||
const time = secondsBigInt / value | ||
secondsBigInt %= value | ||
result += `${time}${unit} ` | ||
} | ||
return result | ||
}, '') | ||
.trim() || '0s' | ||
) | ||
} | ||
|
||
export const utcToLocalRelativeTime = (timestamp: string): string => { | ||
const now = dayjs() | ||
const time = dayjs.utc(timestamp).local() | ||
const diffInSeconds = now.diff(time, 'second') | ||
|
||
if (diffInSeconds < 60) return `${diffInSeconds} seconds ago` | ||
return time.fromNow(true) + ' ago' | ||
} | ||
|
||
export const utcToLocalTime = (timestamp: string): string => | ||
dayjs.utc(timestamp).local().format('DD MMM YYYY | HH:mm:ss(Z)') | ||
|
||
export const currentYear = (): number => dayjs().year() |
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