import { dayjs , applyDuration , stringifyDuration } from './dayjs' ; import { TimeQuery } from './defs' ; export const QUERY_KEY = 'interval' ; export function getDefaultQuery ( now = dayjs ( ) ) { return { base : now . startOf ( 'day' ) , duration : dayjs . duration ( 'P1D' ) } ; } export function parseTimeQuery ( query ) { const [ base , duration ] = query . split ( '--' ) ; return { base : dayjs ( base ) , duration : dayjs . duration ( duration ) } ; } export function stringifyTimeQuery ( { base , duration } ) { const isDST = base . utcOffset ( ) > base . add ( 1 , 'hour' ) . utcOffset ( ) ; let format = 'YYYY-MM-DD' ; if ( base . get ( 'hour' ) > 0 ) { format = isDST ? 'YYYY-MM-DDTHH:mmZ' : 'YYYY-MM-DDTHH' ; } return `${ base . format ( format ) }--${ stringifyDuration ( duration ) }` ; } export function queryToTimestamps ( { base , duration } ) { return { from : base . valueOf ( ) , to : applyDuration ( { base , duration } ) . valueOf ( ) } ; }