UNPKG

salat-first

Version:

Islamic prayer times calculation with special support for Moroccan methods and Maliki madhab

89 lines (88 loc) 2.44 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.TimeFormat = exports.Rounding = void 0; exports.format24Hour = format24Hour; exports.format12Hour = format12Hour; exports.formatTime = formatTime; exports.formatPrayerTimes = formatPrayerTimes; /** * Rounding methods for prayer times */ var Rounding; (function (Rounding) { /** * Round to the nearest minute */ Rounding["Nearest"] = "nearest"; /** * Always round up to the next minute */ Rounding["Up"] = "up"; /** * Do not round */ Rounding["None"] = "none"; })(Rounding || (exports.Rounding = Rounding = {})); /** * Time display formats */ var TimeFormat; (function (TimeFormat) { /** * 24-hour format (e.g. "14:30") */ TimeFormat["Hour24"] = "hour24"; /** * 12-hour format with AM/PM (e.g. "2:30 PM") */ TimeFormat["Hour12"] = "hour12"; })(TimeFormat || (exports.TimeFormat = TimeFormat = {})); /** * Formats a date in 24-hour format * @param date The date to format * @returns The formatted time string */ function format24Hour(date) { return `${String(date.getHours()).padStart(2, "0")}:${String(date.getMinutes()).padStart(2, "0")}`; } /** * Formats a date in 12-hour format with AM/PM * @param date The date to format * @returns The formatted time string */ function format12Hour(date) { const hours = date.getHours(); const minutes = date.getMinutes(); const ampm = hours >= 12 ? "PM" : "AM"; const hour12 = hours % 12 || 12; return `${hour12}:${String(minutes).padStart(2, "0")} ${ampm}`; } /** * Formats a date according to the specified format * @param date The date to format * @param format The time format to use * @returns The formatted time string */ function formatTime(date, format = TimeFormat.Hour24) { if (format === TimeFormat.Hour12) { return format12Hour(date); } else { return format24Hour(date); } } /** * Formats all prayer times according to the specified format * @param times Object containing prayer times * @param format The time format to use * @returns Object with formatted prayer times */ function formatPrayerTimes(times, format = TimeFormat.Hour24) { const result = {}; for (const prayer in times) { if (times[prayer] instanceof Date) { result[prayer] = formatTime(times[prayer], format); } } return result; }