Something went wrong. Try again.
message board on atproto atchan.net
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899import { test } from 'node:test'import assert from 'node:assert/strict'import { coarse_duration, full_date, relative_time } from '#lib/datetime.js'
const iso = '2026-08-17T09:30:00.000Z'
// full_date and relative_time memoise their Intl formatters (a page of posts// asks for the same ones over and over), so the cache key has to carry// everything that varies between them.test('cached formatters still vary by locale and timezone', () => { const ny = full_date(iso, 'America/New_York', 'en-US') const tokyo = full_date(iso, 'Asia/Tokyo', 'en-US') const berlin = full_date(iso, 'Europe/Berlin', 'de-DE') assert.notEqual(ny, tokyo) assert.notEqual(ny, berlin) // repeat calls hit the cache and must agree with the first assert.equal(full_date(iso, 'America/New_York', 'en-US'), ny) assert.equal(full_date(iso, 'Asia/Tokyo', 'en-US'), tokyo) assert.match(ny, /05:30:00 AM EDT/) assert.match(tokyo, /06:30:00 PM GMT\+9/)})
test('relative_time still switches units and honours locale', () => { const now = Date.parse(iso) assert.equal(relative_time(iso, now, 'UTC', 'en-US'), 'now') assert.equal(relative_time(iso, now + 45_000, 'UTC', 'en-US'), '45s') assert.equal(relative_time(iso, now + 5 * 60_000, 'UTC', 'en-US'), '5m') assert.equal(relative_time(iso, now + 3 * 3_600_000, 'UTC', 'en-US'), '3h') // past a day it counts in days, then falls through to a plain date // (in the viewer's zone) once it's a week old assert.equal(relative_time(iso, now + 48 * 3_600_000, 'UTC', 'en-US'), '2d') assert.equal( relative_time(iso, now + 8 * 24 * 3_600_000, 'UTC', 'en-US'), '08/17/2026' ) assert.equal( relative_time(iso, now + 8 * 24 * 3_600_000, 'UTC', 'de-DE'), '17.08.2026' ) // interleaved locales must not serve each other's cached formatter assert.equal(relative_time(iso, now + 45_000, 'UTC', 'de-DE'), '45 Sek.') assert.equal(relative_time(iso, now + 45_000, 'UTC', 'en-US'), '45s')})
test('coarse_duration keeps to the two largest units that apply', () => { assert.equal(coarse_duration(0), '0s') assert.equal(coarse_duration(45), '45s') assert.equal(coarse_duration(90), '1m') assert.equal(coarse_duration(3600), '1h 0m') assert.equal(coarse_duration(3 * 3600 + 25 * 60), '3h 25m') // past a day the minutes drop off rather than reading 9d 4h 37m assert.equal(coarse_duration(9 * 86400 + 4 * 3600 + 37 * 60), '9d 4h') // a negative clock reading is a bug upstream, not something to render as one assert.equal(coarse_duration(-5), '0s')})
test('coarse_duration groups the day count for the viewer', () => { const days = 1234 * 86400 assert.equal(coarse_duration(days, 'en-US'), '1,234d 0h') assert.equal(coarse_duration(days, 'de-DE'), '1.234d 0h')})
test('relative_time exact never says "now"', () => { const now = Date.parse(iso) // the conversational form buckets anything under 10s as "now" assert.equal(relative_time(iso, now, 'UTC', 'en-US'), 'now') assert.equal(relative_time(iso, now + 400, 'UTC', 'en-US'), 'now') assert.equal(relative_time(iso, now + 3_000, 'UTC', 'en-US'), 'now')
// exact reports what it measured: ms under a second, then seconds assert.equal(relative_time(iso, now, 'UTC', 'en-US', true), '0ms') assert.equal(relative_time(iso, now + 400, 'UTC', 'en-US', true), '400ms') assert.equal(relative_time(iso, now + 999, 'UTC', 'en-US', true), '999ms') assert.equal(relative_time(iso, now + 1_000, 'UTC', 'en-US', true), '1s') assert.equal(relative_time(iso, now + 3_000, 'UTC', 'en-US', true), '3s') assert.equal(relative_time(iso, now + 45_000, 'UTC', 'en-US', true), '45s')
// a clock that ran backwards shouldn't render a negative reading assert.equal(relative_time(iso, now - 500, 'UTC', 'en-US', true), '0ms')})
test('relative_time exact leaves the larger units alone', () => { const now = Date.parse(iso) for (const exact of [false, true]) { assert.equal( relative_time(iso, now + 5 * 60_000, 'UTC', 'en-US', exact), '5m' ) assert.equal( relative_time(iso, now + 3 * 3_600_000, 'UTC', 'en-US', exact), '3h' ) assert.equal( relative_time(iso, now + 48 * 3_600_000, 'UTC', 'en-US', exact), '2d' ) }})