All files / libs/amalia-lang/formula/evaluate/shared/src dates.ts

100% Statements 31/31
100% Branches 13/13
100% Functions 3/3
100% Lines 31/31

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 321x 1x 1x 21x 1x 1x 21x 2x 2x 18x 18x 1x 1x 2x 1x 1x 1x 1x 1x 1x 7x 3x 3x 3x 3x 3x 4x 4x 4x 4x 4x  
import { dayjs } from '@amalia/ext/dayjs';
 
export function convertDateToTimestamp(date: string | null, convertNullToInfinity = false) {
  if (convertNullToInfinity && (date === null || date === '')) {
    return Infinity;
  }
  if (date === null || date === '') {
    return null;
  }
  return dayjs.utc(date, 'YYYY-MM-DD').unix();
}
 
export const convertTimestampToDate = (timestamp: number | null) => {
  if (timestamp) {
    return dayjs.utc(timestamp, 'X').format('YYYY-MM-DD');
  }
  return null;
};
 
export const semesterRange = (date: dayjs.Dayjs): { startDate: dayjs.Dayjs; endDate: dayjs.Dayjs } => {
  if ([1, 2].includes(date.quarter())) {
    return {
      startDate: date.clone().startOf('year'),
      endDate: date.clone().endOf('year').subtract(6, 'month'),
    };
  }
  return {
    startDate: date.clone().startOf('year').add(6, 'month'),
    endDate: date.clone().endOf('year'),
  };
};