import {HOUR} from '@/units.js'; import {ZonedDateTime} from '@/'; import {describe, it, expect} from 'vitest'; import {TimeZone} from '@clarify/zone-data'; import importer from '@clarify/zone-data/importer'; let saoPauloFormatter = new Intl.DateTimeFormat('nb-no', { timeStyle: 'long', dateStyle: 'short', timeZone: 'America/Sao_Paulo', }); describe('ZonedDateTime#startOfDay', () => { it('adds day - 1', async () => { let timeZone = new TimeZone('Test/StartOfDay', [1679958000000, null], [-60, -120]); let date = new ZonedDateTime(1679958000000 + HOUR * 3, timeZone); expect(date.hour).toEqual(4); expect(date.day).toEqual(28); date = date.startOfDay(); expect(date.hour).toEqual(1); expect(date.day).toEqual(28); }); it('adds day - 12', async () => { let timeZone = new TimeZone('Test/StartOfDay', [1679965200000, null], [-60, -120]); let date = new ZonedDateTime(1679965200000 + HOUR * 3, timeZone); expect(date.hour).toEqual(6); expect(date.day).toEqual(28); date = date.startOfDay(); expect(date.hour).toEqual(0); expect(date.day).toEqual(28); date = date.startOfDay(); expect(date.hour).toEqual(0); expect(date.day).toEqual(28); }); it('adds day + 12', async () => { let timeZone = new TimeZone('Test/StartOfDay', [1679965200000, null], [60, 120]); let date = new ZonedDateTime(1679965200000 + HOUR * 3, timeZone); expect(date.hour).toEqual(2); expect(date.day).toEqual(28); date = date.startOfDay(); expect(date.hour).toEqual(0); expect(date.day).toEqual(28); date = date.startOfDay(); expect(date.hour).toEqual(0); expect(date.day).toEqual(28); }); it('works', async () => { let timeZone = await importer['America/Sao_Paulo'](); let date = new ZonedDateTime(Date.UTC(2018, 10, 4, 10, 0, 0), timeZone); expect(saoPauloFormatter.format(new Date(date.epochMilliseconds))).equal('04.11.2018, 08:00:00 GMT-2'); date = date.startOfDay(); expect(date.hour, 'starts date at 01:00').toEqual(1); expect(saoPauloFormatter.format(new Date(date.epochMilliseconds))).equal('04.11.2018, 01:00:00 GMT-2'); }); });