All files / src/components/RelativeRangeDatePicker utils.ts

63.63% Statements 14/22
63.88% Branches 23/36
25% Functions 1/4
63.63% Lines 14/22

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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 843x                                                                                               12x 5x     7x   12x 12x 7x         7x 7x 5x               7x         2x     5x   5x    
import {dateTime, dateTimeParse} from '@gravity-ui/date-utils';
 
import type {Value} from '../RelativeDatePicker';
import type {ExtractFunctionType, RangeValue} from '../types';
 
import type {Preset} from './components/Presets/defaultPresets';
import {i18n} from './components/Presets/i18n';
import {getPresetTitle} from './components/Presets/utils';
 
export function resolveTimeZone(timeZone: string) {
    if (timeZone === 'default' || timeZone === 'system') {
        return dateTime({timeZone}).timeZone();
    }
 
    return timeZone;
}
 
export function normalizeTimeZone(timeZone: string) {
    const lowered = timeZone.toLowerCase();
    if (lowered === 'default' || lowered === 'system') {
        return lowered;
    }
 
    return resolveTimeZone(timeZone);
}
 
export function getTimeZoneOffset(timeZone: string) {
    return `UTC ${dateTime({timeZone}).format('Z')}`;
}
 
interface GetDefaultTitleArgs {
    value: RangeValue<Value | null> | null;
    timeZone: string;
    alwaysShowAsAbsolute?: boolean;
    format?: string;
    presets?: Preset[];
    presetsTranslations?: ExtractFunctionType<typeof i18n>;
    lang?: string;
}
export function getDefaultTitle({
    value,
    timeZone,
    alwaysShowAsAbsolute,
    format = 'L',
    presets,
    presetsTranslations = i18n,
    lang = 'en',
}: GetDefaultTitleArgs): string {
    if (!value) {
        return '';
    }
 
    const tz = timeZone === 'default' ? '' : ` (${getTimeZoneOffset(timeZone)})`;
 
    let from = '';
    if (value.start) {
        from =
            value.start.type === 'relative' && !alwaysShowAsAbsolute
                ? value.start.value
                : (dateTimeParse(value.start.value, {timeZone, lang})?.format(format) ?? '');
    }
    let to = '';
    if (value.end) {
        to =
            value.end.type === 'relative' && !alwaysShowAsAbsolute
                ? value.end.value
                : (dateTimeParse(value.end.value, {timeZone, roundUp: true, lang})?.format(
                      format,
                  ) ?? '');
    }
 
    if (
        !alwaysShowAsAbsolute &&
        value.start?.type === 'relative' &&
        value.end?.type === 'relative'
    ) {
        return getPresetTitle(value.start.value, value.end.value, presets, presetsTranslations);
    }
 
    const delimiter = ' — ';
 
    return `${from}${delimiter}${to}${tz}`;
}