-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
frontend i18n/dateTime: Add default locale+TZ under test
This is because different systems have different default locales and different timezones - which makes testing more difficult. Moved date/time formatting functions out of helpers into src/i18n/dateTime. The cross-env package is for cross platform setting of env vars.
- Loading branch information
Showing
10 changed files
with
70 additions
and
36 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
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,51 @@ | ||
type DateParam = string | number | Date; | ||
|
||
export function toLocaleString( | ||
date: DateParam, | ||
locales?: string | string[], | ||
options?: Intl.DateTimeFormatOptions | ||
) { | ||
// Locales and timezones can be different, so we set a default when under test. | ||
if (process.env.TEST_TZ) { | ||
const newOptions = options ? options : {}; | ||
return new Date(date).toLocaleString('en-US', { ...newOptions, timeZone: 'UTC' }); | ||
} else { | ||
return new Date(date).toLocaleString(locales, options); | ||
} | ||
} | ||
|
||
export function toLocaleDateString( | ||
date: DateParam, | ||
locales?: string | string[], | ||
options?: Intl.DateTimeFormatOptions | ||
) { | ||
if (process.env.TEST_TZ) { | ||
// Locales and timezones can be different, so we set a default when under test. | ||
const newOptions = options ? options : {}; | ||
return new Date(date).toLocaleDateString('en-US', { ...newOptions, timeZone: 'UTC' }); | ||
} else { | ||
return new Date(date).toLocaleDateString(locales, options); | ||
} | ||
} | ||
|
||
export function getMinuteDifference(date1: number, date2: number) { | ||
return (date1 - date2) / 1000 / 60; | ||
} | ||
|
||
export function makeLocaleTime( | ||
timestamp: string | Date | number, | ||
opts: { useDate?: boolean; showTime?: boolean; dateFormat?: Intl.DateTimeFormatOptions } = {} | ||
) { | ||
const { useDate = true, showTime = true, dateFormat = {} } = opts; | ||
const date = new Date(timestamp); | ||
const formattedDate = toLocaleDateString(date, undefined, dateFormat); | ||
const timeFormat = toLocaleString(date, undefined, { hour: '2-digit', minute: '2-digit' }); | ||
|
||
if (useDate && showTime) { | ||
return `${formattedDate} ${timeFormat}`; | ||
} | ||
if (useDate) { | ||
return formattedDate; | ||
} | ||
return timeFormat; | ||
} |
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