153 lines
4.0 KiB
TypeScript
Executable File
153 lines
4.0 KiB
TypeScript
Executable File
export const MONTHS_RU = [
|
||
'Январь',
|
||
'Февраль',
|
||
'Март',
|
||
'Апрель',
|
||
'Май',
|
||
'Июнь',
|
||
'Июль',
|
||
'Август',
|
||
'Сентябрь',
|
||
'Октябрь',
|
||
'Ноябрь',
|
||
'Декабрь',
|
||
];
|
||
|
||
export const WEEKDAYS_RU = ['Пн', 'Вт', 'Ср', 'Чт', 'Пт', 'Сб', 'Вс'];
|
||
|
||
const MONTHS_GENITIVE_RU = [
|
||
'января',
|
||
'февраля',
|
||
'марта',
|
||
'апреля',
|
||
'мая',
|
||
'июня',
|
||
'июля',
|
||
'августа',
|
||
'сентября',
|
||
'октября',
|
||
'ноября',
|
||
'декабря',
|
||
];
|
||
|
||
/** Build an ISO date string (YYYY-MM-DD) without timezone pitfalls. */
|
||
export function isoDate(year: number, month0: number, day: number): string {
|
||
const m = String(month0 + 1).padStart(2, '0');
|
||
const d = String(day).padStart(2, '0');
|
||
|
||
return `${year}-${m}-${d}`;
|
||
}
|
||
|
||
export function todayIso(): string {
|
||
const now = new Date();
|
||
|
||
return isoDate(now.getFullYear(), now.getMonth(), now.getDate());
|
||
}
|
||
|
||
/** Parse an ISO date string into parts. */
|
||
export function parseIso(date: string): {
|
||
year: number;
|
||
month0: number;
|
||
day: number;
|
||
} {
|
||
const [year, month, day] = date.split('-').map(Number);
|
||
|
||
return { year, month0: month - 1, day };
|
||
}
|
||
|
||
export function addDays(date: string, days: number): string {
|
||
const { year, month0, day } = parseIso(date);
|
||
const d = new Date(Date.UTC(year, month0, day + days));
|
||
|
||
return isoDate(d.getUTCFullYear(), d.getUTCMonth(), d.getUTCDate());
|
||
}
|
||
|
||
export function daysInclusive(start: string, end: string): number {
|
||
const a = parseIso(start);
|
||
const b = parseIso(end);
|
||
const ms =
|
||
Date.UTC(b.year, b.month0, b.day) - Date.UTC(a.year, a.month0, a.day);
|
||
|
||
return Math.round(ms / 86_400_000) + 1;
|
||
}
|
||
|
||
/**
|
||
* Build a Monday-first month grid. Returns weeks of cells;
|
||
* each cell is an ISO date string or null (outside the month).
|
||
*/
|
||
export function monthGrid(year: number, month0: number): (string | null)[][] {
|
||
const first = new Date(Date.UTC(year, month0, 1));
|
||
const offset = (first.getUTCDay() + 6) % 7;
|
||
const total = new Date(Date.UTC(year, month0 + 1, 0)).getUTCDate();
|
||
|
||
const cells: (string | null)[] = [];
|
||
|
||
for (let i = 0; i < offset; i++) {
|
||
cells.push(null);
|
||
}
|
||
|
||
for (let day = 1; day <= total; day++) {
|
||
cells.push(isoDate(year, month0, day));
|
||
}
|
||
|
||
while (cells.length % 7 !== 0) {
|
||
cells.push(null);
|
||
}
|
||
|
||
const weeks: (string | null)[][] = [];
|
||
|
||
for (let i = 0; i < cells.length; i += 7) {
|
||
weeks.push(cells.slice(i, i + 7));
|
||
}
|
||
|
||
return weeks;
|
||
}
|
||
|
||
/** "6 июля" or "6 июля 2026" */
|
||
export function formatRu(date: string, withYear = false): string {
|
||
const { year, month0, day } = parseIso(date);
|
||
|
||
return `${day} ${MONTHS_GENITIVE_RU[month0]}${withYear ? ` ${year}` : ''}`;
|
||
}
|
||
|
||
/** "6–19 июля 2026" / "28 декабря 2026 – 8 января 2027" */
|
||
export function formatRangeRu(start: string, end: string): string {
|
||
if (start === end) {
|
||
return formatRu(start, true);
|
||
}
|
||
|
||
const a = parseIso(start);
|
||
const b = parseIso(end);
|
||
|
||
if (a.year === b.year && a.month0 === b.month0) {
|
||
return `${a.day}–${formatRu(end, true)}`;
|
||
}
|
||
|
||
if (a.year === b.year) {
|
||
return `${formatRu(start)} – ${formatRu(end, true)}`;
|
||
}
|
||
|
||
return `${formatRu(start, true)} – ${formatRu(end, true)}`;
|
||
}
|
||
|
||
export function isWeekend(date: string): boolean {
|
||
const { year, month0, day } = parseIso(date);
|
||
const dow = new Date(Date.UTC(year, month0, day)).getUTCDay();
|
||
|
||
return dow === 0 || dow === 6;
|
||
}
|
||
|
||
/** Clip a period to a year and count its days (0 if no overlap). */
|
||
export function daysInYear(start: string, end: string, year: number): number {
|
||
const yearStart = `${year}-01-01`;
|
||
const yearEnd = `${year}-12-31`;
|
||
const s = start > yearStart ? start : yearStart;
|
||
const e = end < yearEnd ? end : yearEnd;
|
||
|
||
if (s > e) {
|
||
return 0;
|
||
}
|
||
|
||
return daysInclusive(s, e);
|
||
}
|