Repository Public
1/**2 * =============================================================================3 * SP BOUCHER | INFRASTRUCTURE & AUTOMATION4 * ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * Project : MacLustr Social Engine & Remote Navigator8 * File : apps/social-web/src/time.test.ts9 * Module : social-web10 * Repository : https://git.spboucher.ai/maclustr-social11 * Project Website : https://social.spboucher.ai12 * Created : 2026-12-0113 * Last Modified : 2026-12-0114 *15 * Author : Simon-Pierre Boucher16 * Position : Professor of Finance17 * Institution : Université du Québec en Outaouais (UQO)18 * Organization : SP Boucher19 * Website : https://spboucher.ai20 * Git : https://git.spboucher.ai21 *22 * Preferred Contact: admin@spboucher.ai23 * Technical Contact: dev@spboucher.ai24 * Security/Admin : admin@spboucher.ai25 *26 * Copyright (c) 202627 * Simon-Pierre Boucher28 * All rights reserved where applicable. This file is governed by the29 * applicable project license, copyright notices and relevant law.30 * Existing third-party rights and notices remain unaffected.31 *32 * License : None specified (no license file in this repository)33 *34 * Do not embed credentials, private keys, access tokens,35 * certificates or confidential personal information.36 *37 * =============================================================================38 * END OF SP BOUCHER SOURCE HEADER39 * =============================================================================40 */4142// Run with: npx vitest run --root apps/social-web43import { describe, expect, it } from 'vitest';44import { addDays, addMonths, civilInZone, dayKeyInZone, monthGrid, weekOf, weekdayMon, zonedToUtc } from './time.ts';4546const TOR = 'America/Toronto';47describe('zonedToUtc', () => {48 it('converts an ordinary winter and summer time in Toronto', () => {49 const w = zonedToUtc({ y: 2026, m: 1, d: 15, h: 9, mi: 30 }, TOR);50 expect(w).toEqual({ kind: 'ok', utc: new Date('2026-01-15T14:30:00.000Z') });51 const s = zonedToUtc({ y: 2026, m: 7, d: 15, h: 9, mi: 30 }, TOR);52 expect(s).toEqual({ kind: 'ok', utc: new Date('2026-07-15T13:30:00.000Z') });53 });54 it('reports the spring-forward gap (2026-03-08 02:30 does not exist in Toronto)', () => {55 const r = zonedToUtc({ y: 2026, m: 3, d: 8, h: 2, mi: 30 }, TOR);56 expect(r.kind).toBe('gap');57 if (r.kind === 'gap') { expect(r.before.toISOString()).toBe('2026-03-08T06:30:00.000Z'); expect(r.after.toISOString()).toBe('2026-03-08T07:30:00.000Z'); }58 });59 it('reports the fall-back overlap (2026-11-01 01:30 happens twice in Toronto)', () => {60 const r = zonedToUtc({ y: 2026, m: 11, d: 1, h: 1, mi: 30 }, TOR);61 expect(r).toEqual({ kind: 'overlap', first: new Date('2026-11-01T05:30:00.000Z'), second: new Date('2026-11-01T06:30:00.000Z') });62 });63 it('is exact at the edges of the transitions', () => {64 expect(zonedToUtc({ y: 2026, m: 3, d: 8, h: 1, mi: 59 }, TOR)).toEqual({ kind: 'ok', utc: new Date('2026-03-08T06:59:00.000Z') });65 expect(zonedToUtc({ y: 2026, m: 3, d: 8, h: 3, mi: 0 }, TOR)).toEqual({ kind: 'ok', utc: new Date('2026-03-08T07:00:00.000Z') });66 expect(zonedToUtc({ y: 2026, m: 11, d: 1, h: 2, mi: 0 }, TOR)).toEqual({ kind: 'ok', utc: new Date('2026-11-01T07:00:00.000Z') });67 expect(zonedToUtc({ y: 2026, m: 11, d: 1, h: 0, mi: 59 }, TOR)).toEqual({ kind: 'ok', utc: new Date('2026-11-01T04:59:00.000Z') });68 });69 it('handles other zones: UTC, Paris, a half-hour zone, and a zone east of the date line', () => {70 expect(zonedToUtc({ y: 2026, m: 6, d: 1, h: 12, mi: 0 }, 'UTC')).toEqual({ kind: 'ok', utc: new Date('2026-06-01T12:00:00.000Z') });71 expect(zonedToUtc({ y: 2026, m: 6, d: 1, h: 12, mi: 0 }, 'Europe/Paris')).toEqual({ kind: 'ok', utc: new Date('2026-06-01T10:00:00.000Z') });72 expect(zonedToUtc({ y: 2026, m: 3, d: 29, h: 2, mi: 30 }, 'Europe/Paris').kind).toBe('gap');73 expect(zonedToUtc({ y: 2026, m: 6, d: 1, h: 12, mi: 0 }, 'Asia/Kolkata')).toEqual({ kind: 'ok', utc: new Date('2026-06-01T06:30:00.000Z') });74 expect(zonedToUtc({ y: 2026, m: 6, d: 1, h: 0, mi: 15 }, 'Pacific/Auckland')).toEqual({ kind: 'ok', utc: new Date('2026-05-31T12:15:00.000Z') });75 });76 it('round-trips every quarter hour of the two Toronto transition days that exists exactly once', () => {77 for (const [m, d] of [[3, 8], [11, 1]] as const) for (let h = 0; h < 24; h++) for (const mi of [0, 15, 30, 45]) {78 const c = { y: 2026, m, d, h, mi }; const r = zonedToUtc(c, TOR);79 if (r.kind === 'ok') expect(civilInZone(r.utc, TOR)).toEqual(c);80 else if (r.kind === 'overlap') { expect(civilInZone(r.first, TOR)).toEqual(c); expect(civilInZone(r.second, TOR)).toEqual(c); expect(r.second.getTime() - r.first.getTime()).toBe(3_600_000); }81 else expect(m === 3 && h === 2).toBe(true);82 }83 });84});85describe('calendar keys', () => {86 it('buckets an instant by the day it falls on in the zone', () => {87 expect(dayKeyInZone('2026-03-01T03:30:00Z', TOR)).toBe('2026-02-28');88 expect(dayKeyInZone('2026-03-01T05:00:00Z', TOR)).toBe('2026-03-01');89 });90 it('does civil arithmetic across months, years and DST days', () => {91 expect(addDays('2026-03-07', 2)).toBe('2026-03-09');92 expect(addDays('2026-12-31', 1)).toBe('2027-01-01');93 expect(addMonths('2026-12-15', 1)).toBe('2027-01-01');94 expect(weekdayMon('2026-12-01')).toBe(1);95 expect(weekOf('2026-12-01')).toEqual(['2026-11-30', '2026-12-01', '2026-12-02', '2026-12-03', '2026-12-04', '2026-12-05', '2026-12-06']);96 const g = monthGrid('2026-03-15');97 expect(g[0]).toBe('2026-02-23'); expect(g[g.length - 1]).toBe('2026-04-05'); expect(g.length % 7).toBe(0);98 });99});100