Skip to content

Commit

Permalink
HXCS-3381 fix date widget
Browse files Browse the repository at this point in the history
  • Loading branch information
Adriano Costa authored and Adriano Costa committed Mar 29, 2024
1 parent b9f047f commit bede4ab
Showing 1 changed file with 29 additions and 25 deletions.
54 changes: 29 additions & 25 deletions lib/core/src/lib/common/utils/date-fns-utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ describe('DateFnsUtils', () => {
});
});


it('should format a date correctly', () => {
const date = new Date('2023-09-22T12:00:00Z');
const dateFormat = 'yyyy-MM-dd';
Expand All @@ -53,27 +52,18 @@ describe('DateFnsUtils', () => {
});

it('should parse datetime', () => {
const parsed = DateFnsUtils.parseDate(
'09-02-9999 09:10 AM',
'dd-MM-yyyy hh:mm aa'
);
const parsed = DateFnsUtils.parseDate('09-02-9999 09:10 AM', 'dd-MM-yyyy hh:mm aa');
expect(isValid(parsed));
expect(parsed.toISOString()).toBe('9999-02-09T09:10:00.000Z');
});

it('should format datetime with custom moment format', () => {
const parsed = DateFnsUtils.formatDate(
new Date('9999-12-30T10:30:00.000Z'),
'MM-DD-YYYY HH:mm A'
);
const parsed = DateFnsUtils.formatDate(new Date('9999-12-30T10:30:00.000Z'), 'MM-DD-YYYY HH:mm A');
expect(parsed).toBe('12-30-9999 10:30 AM');
});

it('should parse moment datetime ISO', () => {
const parsed = DateFnsUtils.parseDate(
'1982-03-13T10:00:00Z',
'YYYY-MM-DDTHH:mm:ssZ'
);
const parsed = DateFnsUtils.parseDate('1982-03-13T10:00:00Z', 'YYYY-MM-DDTHH:mm:ssZ');
expect(parsed.toISOString()).toBe('1982-03-13T10:00:00.000Z');
});

Expand All @@ -87,27 +77,18 @@ describe('DateFnsUtils', () => {
});

it('should parse alternative ISO datetime', () => {
const result = DateFnsUtils.parseDate(
'1982-03-13T10:00:000Z',
`yyyy-MM-dd'T'HH:mm:sssXXX`
);
const result = DateFnsUtils.parseDate('1982-03-13T10:00:000Z', `yyyy-MM-dd'T'HH:mm:sssXXX`);

expect(result.toISOString()).toBe('1982-03-13T10:00:00.000Z');
});

it('should parse the datetime with zone', () => {
const result = DateFnsUtils.parseDate(
'1982-03-13T10:00:000+01:00',
`yyyy-MM-dd'T'HH:mm:sssXXX`
);
const result = DateFnsUtils.parseDate('1982-03-13T10:00:000+01:00', `yyyy-MM-dd'T'HH:mm:sssXXX`);
expect(result.toISOString()).toBe('1982-03-13T09:00:00.000Z');
});

it('should parse datetime with zone in moment format', () => {
const result = DateFnsUtils.parseDate(
'1982-03-13T10:00:00+0100',
`YYYY-MM-DDTHH:mm:ssZZ`
);
const result = DateFnsUtils.parseDate('1982-03-13T10:00:00+0100', `YYYY-MM-DDTHH:mm:ssZZ`);
expect(result.toISOString()).toBe('1982-03-13T09:00:00.000Z');
});

Expand All @@ -132,4 +113,27 @@ describe('DateFnsUtils', () => {
expect(resultLocal).toBeInstanceOf(Date);
expect(resultUtc).toBeInstanceOf(Date);
});

it('should convert UTC dates to have the same day, month, year displayed in different timezone', () => {
const dateUTC = new Date('Wed Jan 01 2020 00:00:00 GMT+0000');
const dateUSA = new Date('Tue Dec 31 2019 16:00:00 GMT-0800');
const dateJapan = new Date('Wed Jan 01 2020 09:00:00 GMT+0900');
const forceUtcDateUTC = DateFnsUtils.forceUtc(dateUTC);
const forceUtcDateUSA = DateFnsUtils.forceUtc(dateUSA);
const forceUtcDateJapan = DateFnsUtils.forceUtc(dateJapan);
const forceLocalDateUTC = DateFnsUtils.forceUtc(forceUtcDateUTC);
const forceLocalDateUSA = DateFnsUtils.forceUtc(forceUtcDateUSA);
const forceLocalDateJapan = DateFnsUtils.forceUtc(forceUtcDateJapan);

expect(forceUtcDateUTC).toEqual(forceUtcDateUSA);
expect(forceUtcDateUSA).toEqual(forceUtcDateJapan);
expect(forceLocalDateUTC).toEqual(forceLocalDateUSA);
expect(forceLocalDateUSA).toEqual(forceLocalDateJapan);
expect(forceUtcDateUSA.getDate()).toBe(1);
expect(forceUtcDateUSA.getMonth()).toBe(0);
expect(forceUtcDateUSA.getFullYear()).toBe(2020);
expect(forceLocalDateJapan.getDate()).toBe(1);
expect(forceLocalDateJapan.getMonth()).toBe(0);
expect(forceLocalDateJapan.getFullYear()).toBe(2020);
});
});

0 comments on commit bede4ab

Please sign in to comment.