/** * Bunch of miscellaneous constants and utility functions related to handling * date and time durations. * * Note that month and year do not have fixed durations, and hence are excluded * from this file. Weeks have fixed durations, but are excluded because we * use days as the max duration supported. */ type Duration = { days?: number; hours?: number; minutes?: number; seconds?: number; milliseconds?: number; }; /** * One of: days, hours, minutes, seconds, milliseconds */ type DurationType = keyof Duration; /** * Order in which the duration type appears in the duration string. */ declare const DURATION_TYPE_SEQUENCE: DurationType[]; /** * Follows the same format as Intl.DurationFormat.prototype.format(). * * Short: 1 yr, 2 mths, 3 wks, 3 days, 4 hr, 5 min, 6 sec, 7 ms, 8 μs, 9 ns * Long: 1 year, 2 months, 3 weeks, 3 days, 4 hours, 5 minutes, 6 seconds, * 7 milliseconds, 8 microseconds, 9 nanoseconds * Narrow: 1y 2mo 3w 3d 4h 5m 6s 7ms 8μs 9ns */ type DurationStyle = "short" | "long" | "narrow"; type DurationSuffixMap = { short: string; shorts: string; long: string; longs: string; narrow: string; }; type DurationSuffixType = keyof DurationSuffixMap; declare const DURATION_STYLE_SUFFIX_MAP: Record; /** * Convert a milliseconds duration into a Duration object. If the given ms is * zero, then return an object with a single field of zero with duration type * of durationTypeForZero. * * @param durationTypeForZero Defaults to 'milliseconds' */ declare function msToDuration(ms: number, durationTypeForZero?: DurationType): Duration; /** * Returns the number of milliseconds for the given duration. */ declare function durationToMs(duration: Duration): number; /** * Convenience function to return a duration given an ms or Duration. */ declare function durationOrMsToMs(duration: number | Duration): number; /** * Format a given Duration object into a string. If the object has no fields, * then returns an empty string. * * @param style Defaults to 'short' */ declare function formatDuration(duration: Duration, style?: DurationStyle): string; /** * Convert a millisecond duration into a human-readable duration string. * * @param options.durationTypeForZero - Defaults to 'milliseconds' * @param options.style - Defaults to 'short' */ declare function readableDuration(ms: number, options?: { durationTypeForZero?: DurationType; style?: DurationStyle; }): string; /** A shortened duration string useful for logging timings. */ declare function elapsed(ms: number): string; export { DURATION_STYLE_SUFFIX_MAP, DURATION_TYPE_SEQUENCE, type Duration, type DurationStyle, type DurationSuffixMap, type DurationSuffixType, type DurationType, durationOrMsToMs, durationToMs, elapsed, formatDuration, msToDuration, readableDuration };