forked from opensearch-project/OpenSearch-Dashboards
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: switch to momentjs to handle timezones (opensearch-project#436)
The current used time library, Luxon is using Intl.DateTimeFormatter as the main source for handling time zones. That library depends on browsers implementation. IE11 doesn't have any timezone and it will need a polyfill. This PR replace luxon with moment-timezone as peerDependency. I'm leaving luxon as a devDependency on the tests and on stories.
- Loading branch information
Showing
8 changed files
with
68 additions
and
31 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,11 @@ | ||
import moment from 'moment-timezone'; | ||
|
||
export function getMomentWithTz(date: number | Date, timeZone?: string) { | ||
if (timeZone === 'local' || !timeZone) { | ||
return moment(date); | ||
} | ||
if (timeZone.toLowerCase().startsWith('utc+') || timeZone.toLowerCase().startsWith('utc-')) { | ||
return moment(date).utcOffset(Number(timeZone.slice(3))); | ||
} | ||
return moment.tz(date, timeZone); | ||
} |
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,35 +1,30 @@ | ||
import { DateTime, Interval } from 'luxon'; | ||
import { TickFormatter, TickFormatterOptions } from '../../chart_types/xy_chart/utils/specs'; | ||
import { getMomentWithTz } from './date_time'; | ||
import moment from 'moment-timezone'; | ||
|
||
export function timeFormatter(format: string): TickFormatter { | ||
return (value: number, options?: TickFormatterOptions): string => { | ||
const dateTimeOptions = options && options.timeZone ? { zone: options.timeZone } : undefined; | ||
return DateTime.fromMillis(value, dateTimeOptions).toFormat(format); | ||
return getMomentWithTz(value, options && options.timeZone).format(format); | ||
}; | ||
} | ||
|
||
export function niceTimeFormatter(domain: [number, number]): TickFormatter { | ||
const minDate = DateTime.fromMillis(domain[0]); | ||
const maxDate = DateTime.fromMillis(domain[1]); | ||
const diff = Interval.fromDateTimes(minDate, maxDate); | ||
const format = niceTimeFormat(diff); | ||
const minDate = moment(domain[0]); | ||
const maxDate = moment(domain[1]); | ||
const diff = maxDate.diff(minDate, 'days'); | ||
const format = niceTimeFormatByDay(diff); | ||
return timeFormatter(format); | ||
} | ||
|
||
export function niceTimeFormat(interval: Interval) { | ||
const days = interval.length('days'); | ||
return niceTimeFormatByDay(days); | ||
} | ||
|
||
export function niceTimeFormatByDay(days: number) { | ||
if (days > 30) { | ||
return 'yyyy-MM-dd'; | ||
return 'YYYY-MM-DD'; | ||
} | ||
if (days > 7 && days <= 30) { | ||
return 'MMMM dd'; | ||
return 'MMMM DD'; | ||
} | ||
if (days > 1 && days <= 7) { | ||
return 'MM-dd HH:mm'; | ||
return 'MM-DD HH:mm'; | ||
} | ||
return 'HH:mm:ss'; | ||
} |
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