/* eslint-disable import/no-unassigned-import */ import { isBrowser } from './environment'; import 'dayjs/locale/ru'; import dayjs from 'dayjs'; import { noop } from 'radashi'; export function combineDateTime(date: Date, time: string) { const [hours = '00', minutes = '00'] = time.split(':'); return new Date( date.getFullYear(), date.getMonth(), date.getDate(), Number.parseInt(hours, 10), Number.parseInt(minutes, 10), ); } export function formatDate(date: Date | string) { if (!date) return { db: noop, user: noop }; return { db: () => dayjs(date).format('YYYY-MM-DD'), user: (template?: string, fallbackLang: string = 'ru') => { const lang = isBrowser() ? document.documentElement.lang || fallbackLang : fallbackLang; dayjs.locale(lang); return dayjs(date).format(template || 'D MMMM YYYY'); }, }; } export function formatTime(time: string) { const [hours = '00', minutes = '00'] = time.split(':'); return { db: () => `${hours}:${minutes}:00`, user: () => `${hours}:${minutes}`, }; } export function getMinutes(time: string) { const [hours = '00', minutes = '00'] = time.split(':'); return Number.parseInt(hours, 10) * 60 + Number.parseInt(minutes, 10); } export function sumTime(time1: string, time2: string) { const [hours1 = '00', minutes1 = '00'] = time1.split(':'); const [hours2 = '00', minutes2 = '00'] = time2.split(':'); let totalMinutes = Number.parseInt(minutes1, 10) + Number.parseInt(minutes2, 10); let totalHours = Number.parseInt(hours1, 10) + Number.parseInt(hours2, 10); totalHours += Math.floor(totalMinutes / 60); totalMinutes %= 60; const paddedHours = totalHours.toString().padStart(2, '0'); const paddedMinutes = totalMinutes.toString().padStart(2, '0'); return `${paddedHours}:${paddedMinutes}`; }