-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract date operations to utility functions
Drop converting migrationStarted and migrationCompleted to UTC in the data transformation layer. Signed-off-by: Radoslaw Szwajkowski <[email protected]>
- Loading branch information
Showing
7 changed files
with
163 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import { | ||
areSameDayInUTCZero, | ||
changeFormatToISODate, | ||
changeTimeZoneToUTCZero, | ||
isValidDate, | ||
parseISOtoJSDate, | ||
toISODate, | ||
} from '../dates'; | ||
|
||
describe('changeTimeZoneToUTCZero', () => { | ||
test('from UTC+02:00', () => { | ||
expect(changeTimeZoneToUTCZero('2023-10-31T01:30:00.000+02:00')).toBe( | ||
'2023-10-30T23:30:00.000Z', | ||
); | ||
}); | ||
test('invalid input', () => { | ||
expect(changeTimeZoneToUTCZero('2023-broken-10-31T01:30:00.000+02:00')).toBe(undefined); | ||
}); | ||
}); | ||
|
||
describe('changeFormatToISODate', () => { | ||
test('from ISO date time with zone', () => { | ||
expect(changeFormatToISODate('2023-10-31T01:30:00.000+02:00')).toBe('2023-10-31'); | ||
}); | ||
test('invalid input', () => { | ||
expect(changeFormatToISODate('2023-broken-10-31T01:30:00.000+02:00')).toBe(undefined); | ||
}); | ||
}); | ||
|
||
describe('toISODate', () => { | ||
test('unix epoch', () => { | ||
expect(toISODate(new Date(0))).toBe('1970-01-01'); | ||
}); | ||
test('missing date', () => { | ||
expect(toISODate(undefined)).toBe(undefined); | ||
}); | ||
test('invalid date', () => { | ||
expect(toISODate(new Date('foo'))).toBe(undefined); | ||
}); | ||
}); | ||
|
||
describe('isValidDate', () => { | ||
test('2023-10-31T01:30:00.000+02:00', () => { | ||
expect(isValidDate('2023-10-31T01:30:00.000+02:00')).toBeTruthy(); | ||
}); | ||
test('invalid string', () => { | ||
expect(isValidDate('2023-broken-10-31')).toBeFalsy(); | ||
}); | ||
test('invalid number of days', () => { | ||
expect(isValidDate('2023-10-60')).toBeFalsy(); | ||
}); | ||
}); | ||
|
||
describe('parseISOtoJSDate', () => { | ||
test('2023-10-31T01:30:00.000+02:00', () => { | ||
const date = parseISOtoJSDate('2023-10-31T01:30:00.000+02:00'); | ||
expect(date.toUTCString()).toBe('Mon, 30 Oct 2023 23:30:00 GMT'); | ||
}); | ||
test('invalid input', () => { | ||
expect(parseISOtoJSDate('2023-broken-10-31T01:30:00.000+02:00')).toBe(undefined); | ||
}); | ||
}); | ||
|
||
describe('areSameDayInUTCZero', () => { | ||
test('the same date', () => { | ||
expect( | ||
areSameDayInUTCZero('2023-10-31T01:30:00.000+02:00', '2023-10-29T23:30:00.000-02:00'), | ||
).toBeTruthy(); | ||
}); | ||
test('the different dates', () => { | ||
expect( | ||
areSameDayInUTCZero('2023-10-31T10:00:00.000+02:00', '2023-10-29T14:00:00.000-02:00'), | ||
).toBeFalsy(); | ||
}); | ||
test('one date invalid', () => { | ||
expect(areSameDayInUTCZero('2023-10-31T10:00:00.000+02:00', '2023-foo')).toBeFalsy(); | ||
}); | ||
test('one date missing, one invalid', () => { | ||
expect(areSameDayInUTCZero(undefined, '2023-foo')).toBeFalsy(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,55 @@ | ||
import { DateTime } from 'luxon'; | ||
|
||
/** | ||
* Converts a given ISO date string in a known format and timezone to a UTC ISO string. | ||
* Converts a given ISO date time string to UTC+00:00 time zone. | ||
* | ||
* @param {string} isoDateString - The ISO date string in a known format and timezone. | ||
* @returns {string} The equivalent UTC ISO string if date is valid or undefined otherwise. | ||
* @param {string} isoDateString - The ISO date time string | ||
* @returns {string} The equivalent UTC+00:00 date time ISO string if input is valid or undefined otherwise. | ||
*/ | ||
export function convertToUTC(isoDateString: string): string | undefined { | ||
export function changeTimeZoneToUTCZero(isoDateString: string): string | undefined { | ||
const date = DateTime.fromISO(isoDateString); | ||
return date.isValid ? date.toUTC().toISO() : undefined; | ||
} | ||
|
||
/** | ||
* Converts a given ISO date time string to ISO date string(no time). | ||
* | ||
* @param {string} isoDateString - The ISO date time string | ||
* @returns {string} The equivalent ISO date string if input is valid or undefined otherwise. | ||
*/ | ||
export const changeFormatToISODate = (isoDateString: string): string | undefined => { | ||
// preserve the original zone | ||
const date = DateTime.fromISO(isoDateString, { setZone: true, zone: 'utc' }); | ||
return date.isValid ? date.toISODate() : undefined; | ||
}; | ||
|
||
/** | ||
* Prints JS Date instance as ISO date format (no time) | ||
* @param date | ||
* @returns ISO date string if input is valid or undefined otherwise. | ||
*/ | ||
export const toISODate = (date: Date): string => { | ||
const dt = DateTime.fromJSDate(date); | ||
return dt.isValid ? dt.toISODate() : undefined; | ||
}; | ||
|
||
export const isValidDate = (isoDateString: string) => DateTime.fromISO(isoDateString).isValid; | ||
|
||
/** | ||
* | ||
* @param isoDateString | ||
* @returns JS Date instance if input is valid or undefined otherwise. | ||
*/ | ||
export const parseISOtoJSDate = (isoDateString: string) => { | ||
const date = DateTime.fromISO(isoDateString); | ||
return date.isValid ? date.toJSDate() : undefined; | ||
}; | ||
|
||
/** | ||
* | ||
* @param a ISO date(time) formatted string | ||
* @param b ISO date(time) formatted string | ||
* @returns true if both dates are on the same day in UTC+00:00 | ||
*/ | ||
export const areSameDayInUTCZero = (a: string, b: string): boolean => | ||
DateTime.fromISO(a).toUTC().hasSame(DateTime.fromISO(b).toUTC(), 'day'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.