Skip to content
This repository has been archived by the owner on Jul 30, 2018. It is now read-only.

Commit

Permalink
Fix date parsing for DST dates (#67)
Browse files Browse the repository at this point in the history
* Implement a workaround for a Globalize.js bug
(globalizejs/globalize#689) that incorrectly
parses date strings when the resulting date follows a different rule
from today's date (e.g., the parsed date is in standard time while
today's date is in daylight savings time).

* Temporarily pin Globalize.js version to prevent the `fixParsedDate`
method from breaking on the next release of Globalize.js.
  • Loading branch information
Matt Wistrand authored Mar 15, 2017
1 parent 441787e commit 42370c5
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"@dojo/shim": "2.0.0-beta.10"
},
"dependencies": {
"globalize": "~1.2.2"
"globalize": "1.2.2"
},
"devDependencies": {
"@dojo/interfaces": "2.0.0-alpha.11",
Expand Down
31 changes: 29 additions & 2 deletions src/date.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,27 @@ export type RelativeTimeFormatterOptions = {
form?: RelativeTimeLength;
}

// Globalize.js incorrectly handles timezone offsets when parsing date strings.
// This is resolved with https://github.com/globalizejs/globalize/pull/693, and
// will be included with the next release. Until then, the following workaround
// is needed.
const TODAY_OFFSET = new Date().getTimezoneOffset();
function fixParsedDate(date: Date, optionsOrLocale?: DateFormatterOptions | string): Date {
if (!date || !optionsOrLocale || typeof optionsOrLocale === 'string') {
return date;
}

const offset = date.getTimezoneOffset();
const { datetime } = optionsOrLocale;

if (offset === TODAY_OFFSET || (datetime !== 'long' && datetime !== 'full')) {
return date;
}

date.setMinutes(date.getMinutes() - (offset - TODAY_OFFSET));
return date;
}

/**
* Format a date according to the specified options for the specified or current locale.
*
Expand Down Expand Up @@ -142,10 +163,14 @@ export function getDateFormatter(optionsOrLocale?: DateFormatterOptions | string
export function getDateParser(options?: DateFormatterOptions, locale?: string): DateParser;
export function getDateParser(locale?: string): DateParser;
export function getDateParser(optionsOrLocale?: DateFormatterOptions | string, locale?: string): DateParser {
return globalizeDelegator<DateFormatterOptions, DateParser>('dateParser', {
const parser = globalizeDelegator<DateFormatterOptions, DateParser>('dateParser', {
locale,
optionsOrLocale
});

return function (dateString: string): Date {
return fixParsedDate(parser(dateString), optionsOrLocale);
};
}

/**
Expand Down Expand Up @@ -193,9 +218,11 @@ export function getRelativeTimeFormatter(unit: string, optionsOrLocale?: Relativ
export function parseDate(value: string, options?: DateFormatterOptions, locale?: string): Date;
export function parseDate(value: string, locale?: string): Date;
export function parseDate(value: string, optionsOrLocale?: DateFormatterOptions | string, locale?: string): Date {
return globalizeDelegator<string, DateFormatterOptions, Date>('parseDate', {
const date = globalizeDelegator<string, DateFormatterOptions, Date>('parseDate', {
locale,
optionsOrLocale,
value
});

return fixParsedDate(date, optionsOrLocale);
}

0 comments on commit 42370c5

Please sign in to comment.