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 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 | 20x 166x 166x 166x 166x 166x 166x 166x 166x 59x 107x 66x 64x 64x 64x 576x 64x 641x 26x 148x 68x 80x 80x 26x 54x 1498x 63x 63x 63x 2x 63x 1x 1x 1x 274x 274x 273x 273x 274x 18x 274x | import type {DateTime} from '@gravity-ui/date-utils';
import type {AvailableSections} from './types';
import {getDurationUnitFromSectionType} from './utils';
const dateFields = [
'year',
'quarter',
'month',
'day',
'weekday',
'hour',
'minute',
'second',
'dayPeriod',
] as const;
type Field = (typeof dateFields)[number];
export class IncompleteDate {
year: number | null;
month: number | null;
day: number | null;
weekday: number | null;
dayPeriod: number | null;
hour: number | null;
minute: number | null;
second: number | null;
constructor(date?: DateTime | null) {
this.year = date?.year() ?? null;
this.month = date ? date.month() + 1 : null;
this.day = date?.date() ?? null;
this.weekday = date?.day() ?? null;
this.hour = date?.hour() ?? null;
this.minute = date?.minute() ?? null;
this.second = date?.second() ?? null;
if (date) {
this.dayPeriod = date.hour() >= 12 ? 1 : 0;
} else {
this.dayPeriod = null;
}
}
get quarter() {
return this.month === null ? null : Math.ceil(this.month / 3);
}
set quarter(v: number | null) {
this.month =
v === null
? null
: (v - 1) * 3 + (this.month === null ? 1 : ((this.month - 1) % 3) + 13);
}
copy() {
const copy = new IncompleteDate();
for (const field of dateFields) {
copy[field] = this[field];
}
return copy;
}
isComplete(availableUnits: AvailableSections): boolean {
return dateFields.every((field) => !availableUnits[field] || this[field] !== null);
}
validate(date: DateTime, availableUnits: AvailableSections): boolean {
return dateFields.every((field) => {
if (!availableUnits[field]) {
return true;
}
Iif (field === 'dayPeriod') {
return this.dayPeriod === (date.hour() >= 12 ? 1 : 0);
}
if (field === 'month') {
return date.month() + 1 === this.month;
}
return date[getDurationUnitFromSectionType(field)]() === this[field];
});
}
isCleared(availableUnits: AvailableSections): boolean {
return dateFields.every((field) => !availableUnits[field] || this[field] === null);
}
set(field: Field, value: number): IncompleteDate {
const copy = this.copy();
copy[field] = value;
if (field === 'hour') {
copy.dayPeriod = (copy.hour ?? 0) >= 12 ? 1 : 0;
}
return copy;
}
clear(field: Field): IncompleteDate {
const copy = this.copy();
copy[field] = null;
return copy;
}
toDateTime(
baseValue: DateTime,
{setDate, setTime}: {setDate: boolean; setTime: boolean},
): DateTime {
let nextValue = baseValue;
if (setDate) {
nextValue = nextValue
.set({
year: this.year ?? baseValue.year(),
month: 0, // set January to not overflow day value
date: this.day ?? baseValue.date(),
})
.set({month: this.month === null ? baseValue.month() : this.month - 1});
Iif (this.day === null && this.weekday !== null) {
nextValue = nextValue.set({day: this.weekday});
}
}
if (setTime) {
nextValue = nextValue
.set({
hour: this.hour ?? baseValue.hour(),
minute: this.minute ?? baseValue.minute(),
second: this.second ?? baseValue.second(),
})
.timeZone(nextValue.timeZone());
}
return nextValue;
}
}
|