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 | 24x 180x 180x 180x 180x 180x 180x 180x 180x 62x 118x 71x 69x 69x 69x 621x 69x 616x 28x 161x 75x 86x 86x 28x 58x 1463x 68x 68x 68x 2x 68x 1x 1x 1x 333x 333x 332x 332x 333x 18x 333x | 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 + 1;
}
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;
}
}
|