Mirror of https://github.com/roostorg/osprey github.com/roostorg/osprey
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

at main 44 lines 1.5 kB view raw
1import moment, { Moment } from 'moment-timezone'; 2 3import { IntervalOptions, MomentRangeValues } from '../types/QueryTypes'; 4 5import { DATE_FORMAT } from '../Constants'; 6 7const DURATIONS = ['month', 'week', 'day', 'hour']; 8 9export function formatUtcTimestamp(timestamp: string | Moment): string { 10 return moment.utc(timestamp).format(DATE_FORMAT); 11} 12 13export function localizeAndFormatTimestamp(timestamp: string | Moment): string { 14 return moment.utc(timestamp).tz(moment.tz.guess()).format(DATE_FORMAT); 15} 16 17export function isTimestampPast(timestamp: string): boolean { 18 return moment.utc(timestamp).isBefore(moment.utc()); 19} 20 21export function getIntervalFromDateRange({ start, end }: { start: string; end: string }): MomentRangeValues | null { 22 const momentDuration = moment.duration(moment(start).diff(moment(end))); 23 let unit: moment.unitOfTime.Base | null = null; 24 let numUnits = 0; 25 26 for (const timeUnit of DURATIONS) { 27 const unitOfTime = timeUnit as moment.unitOfTime.Base; 28 const num = Math.abs(momentDuration.as(unitOfTime)); 29 if (num % 1 === 0) { 30 unit = unitOfTime; 31 numUnits = num; 32 break; 33 } 34 } 35 36 if (unit == null) return null; 37 38 const intervalOption = Object.keys(IntervalOptions).find((key) => { 39 const { durationConstructor: option } = IntervalOptions[key as MomentRangeValues]; 40 return option[0] === numUnits && option[1] === unit; 41 }); 42 43 return (intervalOption as MomentRangeValues) ?? null; 44}