/** * Time unit constants and helper functions for duration calculations. * * Provides millisecond values for common time units (seconds, minutes, hours, etc.) * and functions to multiply these units. Useful for setTimeout, interval calculations, * cache TTLs, and any duration-based logic. * * WHY: Improves code readability by using human-friendly time units instead of raw milliseconds. * Instead of writing `setTimeout(fn, 300000)`, you can write `setTimeout(fn, timeUnits.min15)`. * * @example * // Using predefined constants * setTimeout(cleanup, timeUnits.min15); // 15 minutes * cache.set('key', value, { ttl: timeUnits.hour }); // 1 hour * * @example * // Using helper functions * const backoffDelay = timeUnits.secs(30); // 30 seconds * const cacheExpiry = timeUnits.days(7); // 7 days * const sessionTimeout = timeUnits.hours(2); // 2 hours */ export declare const timeUnits: { sec: number; min: number; min15: number; min30: number; hour: number; hour2: number; hour4: number; hour8: number; hour12: number; day: number; week: number; secs(n: number): number; mins(n: number): number; hours(n: number): number; days(n: number): number; weeks(n: number): number; months(n: number): number; years(n: number): number; }; /** * Creates a duration in seconds (converted to milliseconds). * * WHY: Provides a more readable way to specify durations in seconds. * * @param n number of seconds * @returns milliseconds equivalent * * @example * setTimeout(fn, seconds(30)); // 30 seconds = 30000ms * cache.set('key', value, { ttl: seconds(120) }); // 2 minutes */ export declare const seconds: (n: number) => number; /** * Creates a duration in minutes (converted to milliseconds). * * WHY: Provides a more readable way to specify durations in minutes. * * @param n number of minutes * @returns milliseconds equivalent * * @example * setTimeout(fn, minutes(5)); // 5 minutes = 300000ms * const pollInterval = minutes(2); // Poll every 2 minutes */ export declare const minutes: (n: number) => number; /** * Creates a duration in hours (converted to milliseconds). * * WHY: Provides a more readable way to specify durations in hours. * * @param n number of hours * @returns milliseconds equivalent * * @example * setTimeout(fn, hours(1)); // 1 hour = 3600000ms * const sessionExpiry = hours(24); // 24 hour session */ export declare const hours: (n: number) => number; /** * Creates a duration in days (converted to milliseconds). * * WHY: Provides a more readable way to specify durations in days. * * @param n number of days * @returns milliseconds equivalent * * @example * const cacheExpiry = days(7); // 7 days = 604800000ms * setTimeout(cleanup, days(30)); // 30 day cleanup cycle */ export declare const days: (n: number) => number; /** * Creates a duration in weeks (converted to milliseconds). * * WHY: Provides a more readable way to specify durations in weeks. * * @param n number of weeks * @returns milliseconds equivalent * * @example * const backupInterval = weeks(2); // Backup every 2 weeks * setTimeout(archive, weeks(4)); // Archive after 4 weeks */ export declare const weeks: (n: number) => number; /** * Creates a duration in months (converted to milliseconds). * * WHY: Provides a more readable way to specify durations in months. * Uses 30.4 days as average month length for accuracy. * * @param n number of months * @returns milliseconds equivalent (rounded) * * @example * const subscriptionExpiry = months(1); // 1 month subscription * setTimeout(fn, months(3)); // Quarterly task */ export declare const months: (n: number) => number; /** * Creates a duration in years (converted to milliseconds). * * WHY: Provides a more readable way to specify durations in years. * Uses 365 days (does not account for leap years). * * @param n number of years * @returns milliseconds equivalent * * @example * const licenseExpiry = years(1); // 1 year license * const longTermCache = years(5); // 5 year retention */ export declare const years: (n: number) => number; /** * Parses a time duration string into milliseconds. * * WHY: Allows reading durations from configuration files or user input in a human-friendly format. * Supports decimal values, plurals, full words, and is case-insensitive. * * Accepts formats: * - Short: "30sec", "5min", "2hour" * - Plural: "30secs", "5mins", "2hours" * - Full words: "30 seconds", "5 minutes", "2 hours" * * @param str time duration string (format: " ") * @returns milliseconds as number * @throws Error if string format is invalid or unit is unknown * * @example * parseTimeDuration('30sec'); // 30000 * parseTimeDuration('30 secs'); // 30000 * parseTimeDuration('30 seconds'); // 30000 * parseTimeDuration('5 min'); // 300000 * parseTimeDuration('5 minutes'); // 300000 * parseTimeDuration('2.5 hours'); // 9000000 * * @example * // Using in configuration * const config = { * sessionTimeout: parseTimeDuration(process.env.SESSION_TIMEOUT || '1hour'), * cacheExpiry: parseTimeDuration(process.env.CACHE_TTL || '15min') * }; */ export declare const parseTimeDuration: (str: string) => number; /** * Formats a millisecond duration into a human-readable string. * * WHY: Converts raw millisecond values into user-friendly strings for display. * Automatically selects the most appropriate unit (sec, min, hour, day, etc.). * * @param ms number of milliseconds to format * @param opts optional formatting options * @param opts.decimals number of decimal places (default: 0 for whole units, 1 for fractional) * @param opts.unit force a specific unit instead of auto-selecting * @returns formatted string (e.g., "30sec", "5min", "2hour") * * @example * formatTimeDuration(1000); // "1sec" * formatTimeDuration(30000); // "30sec" * formatTimeDuration(90000); // "1.5min" * formatTimeDuration(3600000); // "1hour" * formatTimeDuration(86400000); // "1day" * * @example * // Force specific unit * formatTimeDuration(90000, { unit: 'sec' }); // "90sec" * formatTimeDuration(90000, { unit: 'min' }); // "1.5min" * * @example * // Control decimals * formatTimeDuration(90000, { decimals: 0 }); // "2min" * formatTimeDuration(90000, { decimals: 2 }); // "1.50min" * * @example * // Display cache expiry times * console.log(`Cache expires in: ${formatTimeDuration(cache.ttl)}`); */ export declare const formatTimeDuration: (ms: number, opts?: { decimals?: number; unit?: "sec" | "min" | "hour" | "day" | "week" | "month" | "year"; }) => string;