Initial commit
This commit is contained in:
22
src/utils/time.ts
Normal file
22
src/utils/time.ts
Normal file
@@ -0,0 +1,22 @@
|
||||
import { Temporal } from "@js-temporal/polyfill";
|
||||
|
||||
function lt(one: Temporal.Duration, other: Temporal.DurationLike): boolean {
|
||||
return Temporal.Duration.compare(one, other) < 0
|
||||
}
|
||||
|
||||
export function printTime(time: Temporal.ZonedDateTime): string {
|
||||
const delta = time.until(Temporal.Now.zonedDateTimeISO())
|
||||
if (lt(delta, { minutes: 1 })) return 'now'
|
||||
if (lt(delta, { hours: 1 })) return `${delta.minutes} minutes ago`
|
||||
if (lt(delta, { days: 1 })) return `${delta.hours} hours ago`
|
||||
if (lt(delta, { days: 7 })) return `${delta.round({ smallestUnit: 'days' }).days} days ago`
|
||||
return `at ${time.toPlainDate().toString()} ${time.toPlainTime().toString({ smallestUnit: 'minutes' })}`
|
||||
}
|
||||
|
||||
export function parseTime(string: string): Temporal.ZonedDateTime {
|
||||
return Temporal.ZonedDateTime.from(string)
|
||||
}
|
||||
|
||||
export function isValidTime(string: string): boolean {
|
||||
try { parseTime(string); return true } catch { return false }
|
||||
}
|
||||
Reference in New Issue
Block a user