@ -1,18 +1,21 @@
import { parseUTCDate , formatMonthYYYY , formatDay , formatDayShort } from '../../util/date'
import { parseUTCDate , formatMonthYYYY , formatDay , formatDayShort } from '../../util/date'
const browserDateFormat = Intl . DateTimeFormat ( navigator . language , { hour : 'numeric' } )
const is12HourClock = function ( ) {
const is12HourClock = function ( ) {
return browserDateFormat . resolvedOptions ( ) . hour12
}
const parseISODate = function ( isoDate ) {
const parseISODate = function ( isoDate ) {
const date = parseUTCDate ( isoDate )
const minutes = date . getMinutes ( ) ;
return { date , minutes }
const year = date . getFullYear ( )
return { date , minutes , year }
}
const formatHours = function ( isoDate ) {
const getYearString = ( options , year ) => options . shouldShowYear ? ` ${ year } ` : ''
const formatHours = function ( isoDate ) {
const monthIndex = 1
const dateParts = isoDate . split ( /[^0-9]/ ) ;
dateParts [ monthIndex ] = dateParts [ monthIndex ] - 1
@ -37,9 +40,9 @@ const weekIntervalFormatter = {
const formatted = this . short ( isoDate , options )
return options . isBucketPartial ? ` Partial week of ${ formatted } ` : ` Week of ${ formatted } `
} ,
short ( isoDate , _ options) {
const { date } = parseISODate ( isoDate )
return formatDayShort ( date )
short ( isoDate , options) {
const { date , year } = parseISODate ( isoDate )
return ` ${ formatDayShort ( date ) } ${ getYearString ( options , year ) } `
}
}
@ -48,9 +51,9 @@ const dateIntervalFormatter = {
const { date } = parseISODate ( isoDate )
return formatDay ( date )
} ,
short ( isoDate , _ options) {
const { date } = parseISODate ( isoDate )
return formatDayShort ( date )
short ( isoDate , options) {
const { date , year } = parseISODate ( isoDate )
return ` ${ formatDayShort ( date ) } ${ getYearString ( options , year ) } `
}
}
@ -60,7 +63,7 @@ const hourIntervalFormatter = {
} ,
short ( isoDate , _options ) {
const formatted = formatHours ( isoDate )
if ( is12HourClock ( ) ) {
return formatted . replace ( ' ' , '' ) . toLowerCase ( )
} else {
@ -109,19 +112,22 @@ const factory = {
* The preferred date and time format in the dashboard depends on the selected
* interval and period . For example , in real - time view only the time is necessary ,
* while other intervals require dates to be displayed .
* @ param { Object } config - Configuration object for determining formatter .
*
* @ param { string } interval - The interval of the query , e . g . ` minute ` , ` hour `
* @ param { boolean } longForm - Whether the formatted result should be in long or
* @ param { string } config. interval - The interval of the query , e . g . ` minute ` , ` hour `
* @ param { boolean } config. longForm - Whether the formatted result should be in long or
* short form .
* @ param { string } period - The period of the query , e . g . ` 12mo ` , ` day `
* @ param { boolean } isPeriodFull - Indicates whether the interval has been cut
* @ param { string } config. period - The period of the query , e . g . ` 12mo ` , ` day `
* @ param { boolean } config. isPeriodFull - Indicates whether the interval has been cut
* off by the requested date range or not . If false , the returned formatted date
* indicates this cut off , e . g . ` Partial week of November 8 ` .
* @ param { boolean } config . shouldShowYear - Should the year be appended to the date ?
* Defaults to false . Rendering year string is a newer opt - in feature to be enabled where needed .
* /
export default function dateFormatter ( interval , longForm , period , isPeriodFull ) {
export default function dateFormatter ( { interval , longForm , period , isPeriodFull , shouldShowYear = false } ) {
const displayMode = longForm ? 'long' : 'short'
const options = { period : period , interval : interval , isBucketPartial : ! isPeriodFull }
return function ( isoDate , _index , _ticks ) {
const options = { period : period , interval : interval , isBucketPartial : ! isPeriodFull , shouldShowYear }
return function ( isoDate , _index , _ticks ) {
return factory [ interval ] [ displayMode ] ( isoDate , options )
}
}