UNPKG

@sixbell-telco/sdk

Version:

A collection of reusable components designed for use in Sixbell Telco Angular projects

243 lines (239 loc) 8.33 kB
import dayjs from 'dayjs'; /** * Supported display / parsing formats for date utilities. * Internally all calculations use ISO `YYYY-MM-DD` and convert to the chosen format for display. */ const SUPPORTED_DATE_FORMATS = [ 'DD/MM/YYYY', // Day/Month/Year (European) 'MM/DD/YYYY', // Month/Day/Year (US) 'YYYY-MM-DD', // Year-Month-Day (ISO) 'DD-MM-YYYY', // Day-Month-Year (European with dashes) 'MM-DD-YYYY', // Month-Day-Year (US with dashes) 'YYYY/MM/DD', // Year/Month/Day (ISO with slashes) 'DD.MM.YYYY', // Day.Month.Year (German style) 'MM.DD.YYYY', // Month.Day.Year (US with dots) 'YYYY.MM.DD', // Year.Month.Day (ISO with dots) ]; /** * Supported time-only formats */ const SUPPORTED_TIME_FORMATS = ['HH:mm', 'HH:mm:ss']; // Commonly used format constants const ISO_DATE_FORMAT = 'YYYY-MM-DD'; const TIME_MINUTES_FORMAT = 'HH:mm'; const TIME_SECONDS_FORMAT = 'HH:mm:ss'; // 12-hour time format constants const TIME_12H_MINUTES_FORMAT = 'h:mm A'; const TIME_12H_SECONDS_FORMAT = 'h:mm:ss A'; /** * Reusable date utility service for date operations across the SDK. * Provides consistent date handling, formatting, and range operations. */ class DateUtils { /** * Convert a date string to ISO format (YYYY-MM-DD) */ static toISODate(date, format) { if (!date) return ''; // If already ISO, return as-is to avoid misparsing when a display format is provided if (/^\d{4}-\d{2}-\d{2}$/.test(date)) return date; if (format && format !== 'YYYY-MM-DD') { const parsed = dayjs(date, format, true); return parsed.isValid() ? parsed.format('YYYY-MM-DD') : dayjs(date).format('YYYY-MM-DD'); } return dayjs(date).format('YYYY-MM-DD'); } /** * Convert ISO date to display format */ static toDisplayDate(isoDate, format) { if (!isoDate) return ''; return dayjs(isoDate).format(format); } /** Build a combined date-time format string, e.g. `YYYY-MM-DD HH:mm:ss` */ static buildDateTimeFormat(dateFmt, timeFmt) { return `${dateFmt} ${timeFmt}`; } /** Infer time format from a time string (returns seconds precision if length is 8) */ static inferTimeFormat(time) { return time?.length === 8 ? TIME_SECONDS_FORMAT : TIME_MINUTES_FORMAT; } /** Current time formatted per the provided time format */ static timeNow(format = TIME_SECONDS_FORMAT) { return dayjs().format(format); } /** Milliseconds since epoch for now */ static nowMillis() { return dayjs().valueOf(); } /** Convert ISO date + time string into epoch milliseconds using provided time format */ static toMillis(isoDate, time, timeFmt) { if (!isoDate || !time) return null; const fmt = this.buildDateTimeFormat(ISO_DATE_FORMAT, timeFmt); const dt = dayjs(`${isoDate} ${time}`, fmt); return dt.isValid() ? dt.valueOf() : null; } /** Whether a given ISO date equals today (local) */ static isTodayISO(isoDate) { return !!isoDate && isoDate === this.today(); } /** End-of-day string for the given time format */ static endOfDay(format) { return format === TIME_SECONDS_FORMAT ? '23:59:59' : '23:59'; } /** Start-of-day string for the given time format */ static startOfDay(format) { return format === TIME_SECONDS_FORMAT ? '00:00:00' : '00:00'; } /** Checks if the provided time string represents end-of-day */ static isEndOfDay(time) { return time === '23:59' || time === '23:59:59'; } /** * Convert 24-hour time format to 12-hour format with AM/PM * @param time24h Time in HH:mm or HH:mm:ss format * @param includeSeconds Whether to include seconds in the output * @returns Time in 12-hour format (h:mm A or h:mm:ss A) */ static to12HourFormat(time24h, includeSeconds = false) { if (!time24h) return ''; // Parse the time using dayjs const timeFormat = includeSeconds ? TIME_SECONDS_FORMAT : TIME_MINUTES_FORMAT; const timeObj = dayjs(`2000-01-01 ${time24h}`, `YYYY-MM-DD ${timeFormat}`); if (!timeObj.isValid()) return time24h; const outputFormat = includeSeconds ? TIME_12H_SECONDS_FORMAT : TIME_12H_MINUTES_FORMAT; return timeObj.format(outputFormat); } /** * Generate range value string for calendar components (ISO/ISO format) */ static toRangeValue(range) { if (!range?.start || !range?.end) return ''; return `${range.start}/${range.end}`; } /** * Parse range value string from calendar components (ISO/ISO format) */ static parseRangeValue(value) { if (!value?.includes('/')) return null; const [start, end] = value.split('/'); if (!start || !end) return null; return { start, end }; } /** * Generate quick range based on preset */ static generateQuickRange(preset) { const today = dayjs(); let startDate; switch (preset) { case 'last7Days': startDate = today.subtract(6, 'day'); // inclusive of today => 7 days break; case 'last30Days': startDate = today.subtract(29, 'day'); break; case 'last6Months': startDate = today.subtract(6, 'month'); break; case 'lastYear': startDate = today.subtract(1, 'year'); break; default: throw new Error(`Unknown preset: ${preset}`); } return { start: startDate.format('YYYY-MM-DD'), end: today.format('YYYY-MM-DD'), }; } /** * Generate a range representing the last N hours relative to now, including times. * Returns ISO date range plus HH:mm:ss start/end time strings. */ static generateLastHoursRange(hours) { const now = dayjs(); const start = now.subtract(hours, 'hour'); return { range: { start: start.format('YYYY-MM-DD'), end: now.format('YYYY-MM-DD'), }, startTime: start.format('HH:mm:ss'), endTime: now.format('HH:mm:ss'), }; } /** * Validate date range */ static isValidRange(range) { if (!range?.start || !range?.end) return false; return dayjs(range.start).isValid() && dayjs(range.end).isValid(); } /** * Check if a date is valid */ static isValidDate(date) { return dayjs(date).isValid(); } /** * Get today's date in ISO format */ static today() { return dayjs().format('YYYY-MM-DD'); } /** * Get yesterday's date in ISO format */ static yesterday() { return dayjs().subtract(1, 'day').format('YYYY-MM-DD'); } /** * Get tomorrow's date in ISO format */ static tomorrow() { return dayjs().add(1, 'day').format('YYYY-MM-DD'); } /** * Add/subtract time from a date */ static addTime(date, amount, unit) { return dayjs(date).add(amount, unit).format('YYYY-MM-DD'); } /** * Get difference between two dates */ static diff(date1, date2, unit = 'day') { return dayjs(date1).diff(dayjs(date2), unit); } /** * Check if date is between two dates (inclusive) */ static isBetween(date, start, end) { const dateObj = dayjs(date); const startObj = dayjs(start); const endObj = dayjs(end); return (dateObj.isAfter(startObj) || dateObj.isSame(startObj)) && (dateObj.isBefore(endObj) || dateObj.isSame(endObj)); } /** * Format date with custom pattern */ static format(date, pattern) { return dayjs(date).format(pattern); } } /** * Generated bundle index. Do not edit. */ export { DateUtils, ISO_DATE_FORMAT, SUPPORTED_DATE_FORMATS, SUPPORTED_TIME_FORMATS, TIME_12H_MINUTES_FORMAT, TIME_12H_SECONDS_FORMAT, TIME_MINUTES_FORMAT, TIME_SECONDS_FORMAT }; //# sourceMappingURL=sixbell-telco-sdk-utils-date.mjs.map