-
-
Notifications
You must be signed in to change notification settings - Fork 338
feat: Support ISO 8601 date strings with full precision for all formatting functions where dates can be passed #758
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 2 commits
ed74ea6
656e701
275a403
8bbaaa7
fa44e74
d9d31c5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,10 @@ | ||
| import DateTimeFormatOptions from './DateTimeFormatOptions'; | ||
| import Formats from './Formats'; | ||
| import IntlError, {IntlErrorCode} from './IntlError'; | ||
| import IntlError, { IntlErrorCode } from './IntlError'; | ||
martinmunillas marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| import NumberFormatOptions from './NumberFormatOptions'; | ||
| import RelativeTimeFormatOptions from './RelativeTimeFormatOptions'; | ||
| import TimeZone from './TimeZone'; | ||
| import {defaultOnError} from './defaults'; | ||
| import { defaultOnError } from './defaults'; | ||
|
|
||
| const SECOND = 1; | ||
| const MINUTE = SECOND * 60; | ||
|
|
@@ -31,7 +31,7 @@ const UNIT_SECONDS: Record<Intl.RelativeTimeFormatUnit, number> = { | |
| quarter: QUARTER, | ||
| quarters: QUARTER, | ||
| year: YEAR, | ||
| years: YEAR | ||
| years: YEAR, | ||
| } as const; | ||
|
|
||
| function resolveRelativeTimeUnit(seconds: number) { | ||
|
|
@@ -75,7 +75,7 @@ export default function createFormatter({ | |
| locale, | ||
| now: globalNow, | ||
| onError = defaultOnError, | ||
| timeZone: globalTimeZone | ||
| timeZone: globalTimeZone, | ||
| }: Props) { | ||
| function resolveFormatOrOptions<Options>( | ||
| typeFormats: Record<string, Options> | undefined, | ||
|
|
@@ -128,7 +128,7 @@ export default function createFormatter({ | |
|
|
||
| function dateTime( | ||
| /** If a number is supplied, this is interpreted as a UTC timestamp. */ | ||
martinmunillas marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| value: Date | number, | ||
| value: Date | number | string, | ||
| /** If a time zone is supplied, the `value` is converted to that time zone. | ||
| * Otherwise the user time zone will be used. */ | ||
| formatOrOptions?: string | DateTimeFormatOptions | ||
|
|
@@ -140,7 +140,7 @@ export default function createFormatter({ | |
| (options) => { | ||
| if (!options?.timeZone) { | ||
| if (globalTimeZone) { | ||
| options = {...options, timeZone: globalTimeZone}; | ||
| options = { ...options, timeZone: globalTimeZone }; | ||
| } else { | ||
| onError( | ||
| new IntlError( | ||
|
|
@@ -153,6 +153,22 @@ export default function createFormatter({ | |
| } | ||
| } | ||
|
|
||
| if (typeof value === 'string') { | ||
| const str = value; | ||
| value = new Date(value); | ||
|
||
| if (isNaN(value.getTime())) { | ||
| onError( | ||
| new IntlError( | ||
| IntlErrorCode.INVALID_FORMAT, | ||
| process.env.NODE_ENV !== 'production' | ||
| ? `The \`value\` string parameter does not follow a valid ISO 8601 format. For more information check: https://tc39.es/ecma262/multipage/numbers-and-dates.html#sec-date-time-string-format` | ||
| : undefined | ||
| ) | ||
| ); | ||
| return str; | ||
| } | ||
| } | ||
|
|
||
| return new Intl.DateTimeFormat(locale, options).format(value); | ||
| } | ||
| ); | ||
|
|
@@ -219,7 +235,7 @@ export default function createFormatter({ | |
| const value = calculateRelativeTimeValue(seconds, unit); | ||
|
|
||
| return new Intl.RelativeTimeFormat(locale, { | ||
| numeric: 'auto' | ||
| numeric: 'auto', | ||
| }).format(value, unit); | ||
| } catch (error) { | ||
| onError( | ||
|
|
@@ -238,5 +254,5 @@ export default function createFormatter({ | |
| ); | ||
| } | ||
|
|
||
| return {dateTime, number, relativeTime, list}; | ||
| return { dateTime, number, relativeTime, list }; | ||
| } | ||


There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the existing
FORMATTING_ERRORwould do for this and avoids increasing the bundle size more than necessary.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought so at first but then I thought FORMATTING_ERROR was a formatting error but not an error because of the format while parsing. but if it still makes sense to you I'll revert it