import {describe, it, expect} from 'vitest'; import {ZonedDateTime} from '@/'; let lordHoweFormatter = new Intl.DateTimeFormat('nb-no', { timeStyle: 'long', dateStyle: 'short', timeZone: 'Australia/Lord_Howe', }); let osloFormatter = new Intl.DateTimeFormat('nb-no', { timeStyle: 'long', dateStyle: 'short', timeZone: 'Europe/Oslo', }); describe('ZonedDateTime', () => { describe('#round', () => { it('with string as option', async () => { let date = new ZonedDateTime(Date.parse('2024-03-08T15:03:57.265Z'), 'Europe/Oslo'); expect(date.round('second').toJSON()).toEqual('2024-03-08T16:03:57.000+01:00[Europe/Oslo]'); expect(date.round('minute').toJSON()).toEqual('2024-03-08T16:04:00.000+01:00[Europe/Oslo]'); expect(date.round('hour').toJSON()).toEqual('2024-03-08T16:00:00.000+01:00[Europe/Oslo]'); expect(date.round('day').toJSON()).toEqual('2024-03-09T00:00:00.000+01:00[Europe/Oslo]'); }); it('with metod and rounding increment', async () => { let date = new ZonedDateTime(Date.parse('2024-03-08T15:03:57.265Z'), 'Europe/Oslo'); expect(date.round({smallestUnit: 'second', roundingIncrement: 2, roundingMode: 'ceil'}).toJSON()).toEqual( '2024-03-08T16:03:58.000+01:00[Europe/Oslo]', ); expect(date.round({smallestUnit: 'minute', roundingIncrement: 10, roundingMode: 'ceil'}).toJSON()).toEqual( '2024-03-08T16:10:00.000+01:00[Europe/Oslo]', ); expect(date.round({smallestUnit: 'hour', roundingIncrement: 2, roundingMode: 'ceil'}).toJSON()).toEqual( '2024-03-08T18:00:00.000+01:00[Europe/Oslo]', ); expect(date.round({smallestUnit: 'day', roundingIncrement: 1, roundingMode: 'ceil'}).toJSON()).toEqual( '2024-03-09T00:00:00.000+01:00[Europe/Oslo]', ); }); it('with string as option', async () => { let date = new ZonedDateTime(Date.parse('2024-03-08T10:03:57.265Z'), 'Europe/Oslo'); expect(date.round('second').toJSON()).toEqual('2024-03-08T11:03:57.000+01:00[Europe/Oslo]'); expect(date.round('minute').toJSON()).toEqual('2024-03-08T11:04:00.000+01:00[Europe/Oslo]'); expect(date.round('hour').toJSON()).toEqual('2024-03-08T11:00:00.000+01:00[Europe/Oslo]'); expect(date.round('day').toJSON()).toEqual('2024-03-08T00:00:00.000+01:00[Europe/Oslo]'); }); it('with string as option (DTS)', async () => { let date = new ZonedDateTime(Date.UTC(2024, 9, 27, 2, 3, 4, 345), 'Europe/Oslo'); expect(date.round('second').toJSON()).toEqual('2024-10-27T03:03:04.000+01:00[Europe/Oslo]'); expect(date.round('minute').toJSON()).toEqual('2024-10-27T03:03:00.000+01:00[Europe/Oslo]'); expect(date.round('hour').toJSON()).toEqual('2024-10-27T03:00:00.000+01:00[Europe/Oslo]'); expect(date.round('day').toJSON()).toEqual('2024-10-27T00:00:00.000+02:00[Europe/Oslo]'); }); it('with string as option (DTS) 2', async () => { let date = new ZonedDateTime(Date.UTC(2024, 2, 31, 0, 3, 4, 345), 'Europe/Oslo'); expect(osloFormatter.format(date.epochMilliseconds)).toEqual('31.03.2024, 01:03:04 CET'); expect(date.round({smallestUnit: 'second', roundingMode: 'ceil', roundingIncrement: 2}).toJSON()).toEqual( '2024-03-31T01:03:06.000+01:00[Europe/Oslo]', ); expect(date.round({smallestUnit: 'minute', roundingMode: 'ceil', roundingIncrement: 2}).toJSON()).toEqual( '2024-03-31T01:04:00.000+01:00[Europe/Oslo]', ); expect(date.round({smallestUnit: 'hour', roundingMode: 'ceil', roundingIncrement: 2}).toJSON()).toEqual( '2024-03-31T03:00:00.000+02:00[Europe/Oslo]', ); expect(date.round({smallestUnit: 'day', roundingMode: 'ceil', roundingIncrement: 1}).toJSON()).toEqual( '2024-04-01T00:00:00.000+02:00[Europe/Oslo]', ); let first = date.round({smallestUnit: 'hour', roundingMode: 'ceil', roundingIncrement: 2}); let second = first.round({smallestUnit: 'hour', roundingMode: 'ceil', roundingIncrement: 2}); expect(osloFormatter.format(first.epochMilliseconds)).toEqual('31.03.2024, 03:00:00 CEST'); expect(osloFormatter.format(second.epochMilliseconds)).toEqual('31.03.2024, 04:00:00 CEST'); expect(second.toJSON()).toEqual('2024-03-31T04:00:00.000+02:00[Europe/Oslo]'); }); it('with string as option (DTS end)', async () => { let date = new ZonedDateTime(Date.UTC(2024, 9, 27, 0, 3, 5), 'Europe/Oslo'); expect(osloFormatter.format(date.epochMilliseconds)).toEqual('27.10.2024, 02:03:05 CEST'); expect( osloFormatter.format( date.round({smallestUnit: 'second', roundingMode: 'ceil', roundingIncrement: 2}).epochMilliseconds, ), ).toEqual('27.10.2024, 02:03:06 CEST'); expect( osloFormatter.format( date.round({smallestUnit: 'minute', roundingMode: 'ceil', roundingIncrement: 2}).epochMilliseconds, ), ).toEqual('27.10.2024, 02:04:00 CEST'); expect( osloFormatter.format( date.round({smallestUnit: 'hour', roundingMode: 'ceil', roundingIncrement: 2}).epochMilliseconds, ), ).toEqual('27.10.2024, 04:00:00 CET'); expect( osloFormatter.format( date.round({smallestUnit: 'hour', roundingMode: 'ceil', roundingIncrement: 3}).epochMilliseconds, ), ).toEqual('27.10.2024, 03:00:00 CET'); expect( osloFormatter.format( date.round({smallestUnit: 'day', roundingMode: 'ceil', roundingIncrement: 1}).epochMilliseconds, ), ).toEqual('28.10.2024, 00:00:00 CET'); }); describe('Australia/Lord_Howe', () => { it('with string as option (DTS end)', async () => { let date = new ZonedDateTime(Date.UTC(2023, 8, 30, 15, 28, 4, 345), 'Australia/Lord_Howe'); expect(lordHoweFormatter.format(new Date(date.epochMilliseconds))).toEqual('01.10.2023, 01:58:04 GMT+10:30'); expect( lordHoweFormatter.format( date.round({smallestUnit: 'hour', roundingMode: 'ceil', roundingIncrement: 1}).epochMilliseconds, ), ).toEqual('01.10.2023, 02:30:00 GMT+11'); expect( lordHoweFormatter.format( date.round({smallestUnit: 'hour', roundingMode: 'ceil', roundingIncrement: 2}).epochMilliseconds, ), ).toEqual('01.10.2023, 02:30:00 GMT+11'); expect( lordHoweFormatter.format( date.round({smallestUnit: 'hour', roundingMode: 'ceil', roundingIncrement: 3}).epochMilliseconds, ), ).toEqual('01.10.2023, 03:00:00 GMT+11'); expect( lordHoweFormatter.format( date.round({smallestUnit: 'hour', roundingMode: 'ceil', roundingIncrement: 4}).epochMilliseconds, ), ).toEqual('01.10.2023, 04:00:00 GMT+11'); expect( lordHoweFormatter.format( date.round({smallestUnit: 'day', roundingMode: 'ceil', roundingIncrement: 1}).epochMilliseconds, ), ).toEqual('02.10.2023, 00:00:00 GMT+11'); }); it('with string as option (DTS start)', async () => { let date = new ZonedDateTime(Date.UTC(2023, 3, 1, 14, 28, 4, 345), 'Australia/Lord_Howe'); expect(lordHoweFormatter.format(new Date(date.epochMilliseconds))).toEqual('02.04.2023, 01:28:04 GMT+11'); expect( lordHoweFormatter.format( date.round({smallestUnit: 'hour', roundingMode: 'ceil', roundingIncrement: 1}).epochMilliseconds, ), ).toEqual('02.04.2023, 02:00:00 GMT+10:30'); expect( lordHoweFormatter.format( date.round({smallestUnit: 'hour', roundingMode: 'ceil', roundingIncrement: 3}).epochMilliseconds, ), ).toEqual('02.04.2023, 03:00:00 GMT+10:30'); }); }); }); });