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,EAAE,MAAM;IAIjC,CAAC,SAAS,CAAC,MAAM,CAAC,CAAC,KAAK,EAAE,OAAO;IAKjC,CAAC,SAAS,CAAC,IAAI,CAAC;CAYnB","file":"Duration.d.ts","sourcesContent":["/*!\r\n   Copyright 2019 Ron Buckton\r\n\r\n   Licensed under the Apache License, Version 2.0 (the \"License\");\r\n   you may not use this file except in compliance with the License.\r\n   You may obtain a copy of the License at\r\n\r\n       http://www.apache.org/licenses/LICENSE-2.0\r\n\r\n   Unless required by applicable law or agreed to in writing, software\r\n   distributed under the License is distributed on an \"AS IS\" BASIS,\r\n   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r\n   See the License for the specific language governing permissions and\r\n   limitations under the License.\r\n*/\r\nimport { R } from 'retemplate';\r\nimport { normalizeString, DURATION_HASH_SEED } from './internal/core';\r\nimport { getTime, getTimePart, TimePart } from './internal/time';\r\nimport { getInstantPart, InstantPart } from './internal/instant';\r\nimport { NANOS_OF_DAY, NANOS_OF_EPOCH } from './internal/types';\r\nimport { D6, D3 } from \"./internal/formats\";\r\nimport { Components, DateDelta, TimeDelta } from './types';\r\nimport { Equatable, Equaler } from '@esfx/equatable';\r\n\r\nconst durationRegExp = (() => {\r\n    const SIGN = R`[+-]`;\r\n    const DECIMAL = R`${SIGN}?\\d+(?:\\.\\d+)?`;\r\n    const YEARS = R`(?<years>${DECIMAL})Y`;\r\n    const MONTHS = R`(?<months>${DECIMAL})M`;\r\n    const DAYS = R`(?<days>${DECIMAL})D`;\r\n    const HOURS = R`(?<hours>${DECIMAL})H`;\r\n    const MINUTES = R`(?<minutes>${DECIMAL})M`;\r\n    const SECONDS = R`(?<seconds>${DECIMAL})S`;\r\n    return new RegExp(R`^(?<sign>${SIGN})?P(?!$)${YEARS}?${MONTHS}?${DAYS}?(?:T(?!$)${HOURS}?${MINUTES}?${SECONDS}?)?$`);\r\n})();\r\n\r\nconst PRECISION: (DateDelta | TimeDelta)[] = [\"years\", \"months\", \"days\", \"hours\", \"minutes\", \"seconds\", \"milliseconds\", \"nanoseconds\"];\r\n\r\nexport class Duration implements Equatable {\r\n    static readonly ZERO = new Duration();\r\n\r\n    readonly years: number;\r\n    readonly months: number;\r\n    readonly days: number;\r\n    readonly hours: number;\r\n    readonly minutes: number;\r\n    readonly seconds: number;\r\n    readonly milliseconds: number;\r\n    readonly nanoseconds: number;\r\n\r\n    constructor(components: Components<DateDelta | TimeDelta> = {}) {\r\n        let { years = 0, months = 0, days = 0, hours = 0, minutes = 0, seconds = 0, milliseconds = 0, nanoseconds = 0 } = components;\r\n        if (!isFinite(years)) throw new RangeError(`The value ${years} is not valid for 'years' as it is not an finite number.`);\r\n        if (!isFinite(months)) throw new RangeError(`The value ${months} is not valid for 'months' as it is not an finite number.`);\r\n        if (!isFinite(days)) throw new RangeError(`The value ${days} is not valid for 'days' as it is not an finite number.`);\r\n        if (!isFinite(hours)) throw new RangeError(`The value ${hours} is not valid for 'hours' as it is not an finite number.`);\r\n        if (!isFinite(minutes)) throw new RangeError(`The value ${minutes} is not valid for 'minutes' as it is not an finite number.`);\r\n        if (!isFinite(seconds)) throw new RangeError(`The value ${seconds} is not valid for 'seconds' as it is not an finite number.`);\r\n        if (!isFinite(milliseconds)) throw new RangeError(`The value ${milliseconds} is not valid for 'milliseconds' as it is not an finite number.`);\r\n        if (!isFinite(nanoseconds)) throw new RangeError(`The value ${nanoseconds} is not valid for 'nanoseconds' as it is not an finite number.`);\r\n\r\n        // normalize accurate duration\r\n        while (nanoseconds <= -1e6) milliseconds--, nanoseconds += 1e6;\r\n        while (nanoseconds >= 1e6) milliseconds++, nanoseconds -= 1e6;\r\n        while (milliseconds <= -1e3) seconds--, milliseconds += 1e3;\r\n        while (milliseconds >= 1e3) seconds++, milliseconds -= 1e3;\r\n        while (seconds <= -60) minutes--, seconds += 60;\r\n        while (seconds >= 60) minutes++, seconds -= 60;\r\n        while (minutes <= -60) hours--, minutes += 60;\r\n        while (minutes >= 60) hours++, minutes -=60;\r\n\r\n        minutes += (hours % 1) * 60, hours = Math.trunc(hours);\r\n        seconds += (minutes % 1) * 60, minutes = Math.trunc(minutes);\r\n        milliseconds += (seconds % 1) * 1e3, seconds = Math.trunc(seconds);\r\n        nanoseconds += (milliseconds % 1) * 1e6, milliseconds = Math.trunc(milliseconds);\r\n        nanoseconds = Math.trunc(nanoseconds);\r\n\r\n        this.years = years;\r\n        this.months = months;\r\n        this.days = days;\r\n        this.hours = hours;\r\n        this.minutes = minutes;\r\n        this.seconds = seconds;\r\n        this.milliseconds = milliseconds;\r\n        this.nanoseconds = nanoseconds;\r\n    }\r\n\r\n    get sign() {\r\n        if (this.years > 0) return 1;\r\n        if (this.months > 0) return 1;\r\n        if (this.days > 0) return 1;\r\n        if (this.hours > 0) return 1;\r\n        if (this.minutes > 0) return 1;\r\n        if (this.seconds > 0) return 1;\r\n        if (this.milliseconds > 0) return 1;\r\n        if (this.nanoseconds > 0) return 1;\r\n        if (this.years < 0) return -1;\r\n        if (this.months < 0) return -1;\r\n        if (this.days < 0) return -1;\r\n        if (this.hours < 0) return -1;\r\n        if (this.minutes < 0) return -1;\r\n        if (this.seconds < 0) return -1;\r\n        if (this.milliseconds < 0) return -1;\r\n        if (this.nanoseconds < 0) return -1;\r\n        return 0;\r\n    }\r\n\r\n    plus(deltas: Components<DateDelta | TimeDelta> = {}) {\r\n        const { years = 0, months = 0, days = 0, hours = 0, minutes = 0, seconds = 0, milliseconds = 0, nanoseconds = 0 } = deltas;\r\n        if (!isFinite(years)) throw new RangeError(`The value ${years} is not valid for 'years' as it is not an finite number.`);\r\n        if (!isFinite(months)) throw new RangeError(`The value ${months} is not valid for 'months' as it is not an finite number.`);\r\n        if (!isFinite(days)) throw new RangeError(`The value ${days} is not valid for 'days' as it is not an finite number.`);\r\n        if (!isFinite(hours)) throw new RangeError(`The value ${hours} is not valid for 'hours' as it is not an finite number.`);\r\n        if (!isFinite(minutes)) throw new RangeError(`The value ${minutes} is not valid for 'minutes' as it is not an finite number.`);\r\n        if (!isFinite(seconds)) throw new RangeError(`The value ${seconds} is not valid for 'seconds' as it is not an finite number.`);\r\n        if (!isFinite(milliseconds)) throw new RangeError(`The value ${milliseconds} is not valid for 'milliseconds' as it is not an finite number.`);\r\n        if (!isFinite(nanoseconds)) throw new RangeError(`The value ${nanoseconds} is not valid for 'nanoseconds' as it is not an finite number.`);\r\n        return new Duration({\r\n            years: this.years + years,\r\n            months: this.months + months,\r\n            days: this.days + days,\r\n            hours: this.hours + hours,\r\n            minutes: this.minutes + minutes,\r\n            seconds: this.seconds + seconds,\r\n            milliseconds: this.milliseconds + milliseconds,\r\n            nanoseconds: this.nanoseconds + nanoseconds,\r\n        });\r\n    }\r\n\r\n    minus(deltas: Components<DateDelta | TimeDelta> = {}) {\r\n        const { years = 0, months = 0, days = 0, hours = 0, minutes = 0, seconds = 0, milliseconds = 0, nanoseconds = 0 } = deltas;\r\n        if (!isFinite(years)) throw new RangeError(`The value ${years} is not valid for 'years' as it is not an finite number.`);\r\n        if (!isFinite(months)) throw new RangeError(`The value ${months} is not valid for 'months' as it is not an finite number.`);\r\n        if (!isFinite(days)) throw new RangeError(`The value ${days} is not valid for 'days' as it is not an finite number.`);\r\n        if (!isFinite(hours)) throw new RangeError(`The value ${hours} is not valid for 'hours' as it is not an finite number.`);\r\n        if (!isFinite(minutes)) throw new RangeError(`The value ${minutes} is not valid for 'minutes' as it is not an finite number.`);\r\n        if (!isFinite(seconds)) throw new RangeError(`The value ${seconds} is not valid for 'seconds' as it is not an finite number.`);\r\n        if (!isFinite(milliseconds)) throw new RangeError(`The value ${milliseconds} is not valid for 'milliseconds' as it is not an finite number.`);\r\n        if (!isFinite(nanoseconds)) throw new RangeError(`The value ${nanoseconds} is not valid for 'nanoseconds' as it is not an finite number.`);\r\n        return new Duration({\r\n            years: this.years - years,\r\n            months: this.months - months,\r\n            days: this.days - days,\r\n            hours: this.hours - hours,\r\n            minutes: this.minutes - minutes,\r\n            seconds: this.seconds - seconds,\r\n            milliseconds: this.milliseconds - milliseconds,\r\n            nanoseconds: this.nanoseconds - nanoseconds,\r\n        });\r\n    }\r\n\r\n    scale(amount: number) {\r\n        return new Duration({\r\n            years: this.years * amount,\r\n            months: this.months * amount,\r\n            days: this.days * amount,\r\n            hours: this.hours * amount,\r\n            minutes: this.minutes * amount,\r\n            seconds: this.seconds * amount,\r\n            milliseconds: this.milliseconds * amount,\r\n            nanoseconds: this.nanoseconds * amount,\r\n        });\r\n    }\r\n\r\n    with(components: Components<DateDelta | TimeDelta> = {}) {\r\n        const {\r\n            years = this.years,\r\n            months = this.months,\r\n            days = this.days,\r\n            hours = this.hours,\r\n            minutes = this.minutes,\r\n            seconds = this.seconds,\r\n            milliseconds = this.milliseconds,\r\n            nanoseconds = this.nanoseconds,\r\n        } = components;\r\n        return new Duration({\r\n            years,\r\n            months,\r\n            days,\r\n            hours,\r\n            minutes,\r\n            seconds,\r\n            milliseconds,\r\n            nanoseconds,\r\n        });\r\n    }\r\n\r\n    negate() {\r\n        return new Duration({\r\n            years: -this.years,\r\n            months: -this.months,\r\n            days: -this.days,\r\n            hours: -this.hours,\r\n            minutes: -this.minutes,\r\n            seconds: -this.seconds,\r\n            milliseconds: -this.milliseconds,\r\n            nanoseconds: -this.nanoseconds,\r\n        });\r\n    }\r\n\r\n    abs() {\r\n        return new Duration({\r\n            years: Math.abs(this.years),\r\n            months: Math.abs(this.months),\r\n            days: Math.abs(this.days),\r\n            hours: Math.abs(this.hours),\r\n            minutes: Math.abs(this.minutes),\r\n            seconds: Math.abs(this.seconds),\r\n            milliseconds: Math.abs(this.milliseconds),\r\n            nanoseconds: Math.abs(this.nanoseconds),\r\n        });\r\n    }\r\n\r\n    truncate(unit: DateDelta | TimeDelta): Duration {\r\n        const components: Components<DateDelta | TimeDelta> = {};\r\n        for (const key of PRECISION) {\r\n            components[key] = this[key];\r\n            if (key === unit) break;\r\n        }\r\n        return new Duration(components);\r\n    }\r\n\r\n    toTimeAgoString() {\r\n        let duration: Duration = this;\r\n        const sign = duration.sign;\r\n        if (sign === 0) return \"just now\";\r\n        if (sign < 0) duration = duration.negate();\r\n\r\n        const parts: string[] = [];\r\n        if (duration.years !== 0) parts.push(formatUnit(duration.years, \"years\", \"year\"));\r\n        if (duration.months !== 0) parts.push(formatUnit(duration.months, \"months\", \"month\"));\r\n        if (duration.days !== 0) parts.push(formatUnit(duration.days, \"days\", \"day\"));\r\n        if (duration.hours !== 0) parts.push(formatUnit(duration.hours, \"hours\", \"hour\"));\r\n        if (duration.minutes !== 0) parts.push(formatUnit(duration.minutes, \"minutes\", \"minute\"));\r\n\r\n        const seconds = duration.seconds + duration.milliseconds / 1e3 + duration.nanoseconds / 1e9;\r\n        if (seconds !== 0) parts.push(formatUnit(seconds, \"seconds\", \"second\"));\r\n\r\n        let s = \"\";\r\n        if (sign > 0) s += \"in \";\r\n        if (parts.length === 1) {\r\n            s += parts[0];\r\n        }\r\n        else {\r\n            s += parts.slice(0, -1).join(\", \");\r\n            s += \" and \";\r\n            s += parts[parts.length - 1];\r\n        }\r\n        if (sign < 0) s += \" ago\";\r\n        return s;\r\n    }\r\n\r\n    toString() {\r\n        let duration: Duration = this;\r\n        if (duration.sign === 0) return \"P0D\";\r\n\r\n        let s = \"P\";\r\n        if (duration.sign < 0) {\r\n            s = \"-\" + s;\r\n            duration = duration.negate();\r\n        }\r\n        if (duration.years) s += `${duration.years}Y`;\r\n        if (duration.months) s += `${duration.months}M`;\r\n        if (duration.days) s += `${duration.days}D`;\r\n        if (duration.hours || duration.minutes || duration.seconds || duration.milliseconds || duration.nanoseconds) {\r\n            s += `T`;\r\n            if (duration.hours) s += `${duration.hours}H`;\r\n            if (duration.minutes) s += `${duration.minutes}M`;\r\n            let ns = getTime(0, 0, duration.seconds, duration.milliseconds, duration.nanoseconds);\r\n            if (ns) {\r\n                if (ns < 0n) {\r\n                    s += \"-\";\r\n                    ns = -ns as NANOS_OF_DAY;\r\n                }\r\n                const { second, millisecond, nanosecond } = getTimePart(ns, TimePart.all);\r\n                s += `${second}`;\r\n                if (millisecond || nanosecond) {\r\n                    s += `.${D3(millisecond)}`;\r\n                    if (nanosecond) {\r\n                        s += `${D6(nanosecond)}`;\r\n                    }\r\n                }\r\n                s += `S`;\r\n            }\r\n        }\r\n        return s;\r\n    }\r\n\r\n    toJSON() {\r\n        return this.toString();\r\n    }\r\n\r\n    equals(other: Duration) {\r\n        return other !== null\r\n            && other !== undefined\r\n            && this.years === other.years\r\n            && this.months === other.months\r\n            && this.days === other.days\r\n            && this.hours === other.hours\r\n            && this.minutes === other.minutes\r\n            && this.seconds === other.seconds\r\n            && this.milliseconds === other.milliseconds\r\n            && this.nanoseconds === other.nanoseconds;\r\n    }\r\n\r\n    static fromString(text: string) {\r\n        const match = durationRegExp.exec(normalizeString(text));\r\n        if (match) {\r\n            const { sign, years = \"\", months = \"\", days = \"\", hours = \"\", minutes = \"\", seconds = \"\" } = match.groups!;\r\n            const duration = new Duration({ years: +years, months: +months, days: +days, hours: +hours, minutes: +minutes, seconds: +seconds });\r\n            return sign === \"-\" ? duration.negate() : duration;\r\n        }\r\n\r\n        throw new SyntaxError(`Invalid duration string: '${text}'`);\r\n    }\r\n\r\n    static fromMilliseconds(milliseconds: number, nanoseconds: number = 0) {\r\n        return new Duration({ milliseconds, nanoseconds: nanoseconds % 1e6 });\r\n    }\r\n\r\n    static fromNanoseconds(ns: bigint) {\r\n        return new Duration(getInstantPart(ns as NANOS_OF_EPOCH, InstantPart.all));\r\n    }\r\n\r\n    [Equatable.equals](other: unknown) {\r\n        return other instanceof Duration\r\n            && this.equals(other);\r\n    }\r\n\r\n    [Equatable.hash]() {\r\n        let hc = DURATION_HASH_SEED;\r\n        hc = ((hc << 7) | (hc >>> 25)) ^ Equaler.defaultEqualer.hash(this.years);\r\n        hc = ((hc << 7) | (hc >>> 25)) ^ Equaler.defaultEqualer.hash(this.months);\r\n        hc = ((hc << 7) | (hc >>> 25)) ^ Equaler.defaultEqualer.hash(this.days);\r\n        hc = ((hc << 7) | (hc >>> 25)) ^ Equaler.defaultEqualer.hash(this.hours);\r\n        hc = ((hc << 7) | (hc >>> 25)) ^ Equaler.defaultEqualer.hash(this.minutes);\r\n        hc = ((hc << 7) | (hc >>> 25)) ^ Equaler.defaultEqualer.hash(this.seconds);\r\n        hc = ((hc << 7) | (hc >>> 25)) ^ Equaler.defaultEqualer.hash(this.milliseconds);\r\n        hc = ((hc << 7) | (hc >>> 25)) ^ Equaler.defaultEqualer.hash(this.nanoseconds);\r\n        return hc;\r\n    }\r\n}\r\n\r\nObject.defineProperty(Duration.prototype, Symbol.toStringTag, {\r\n    writable: true,\r\n    configurable: true,\r\n    value: \"Duration\"\r\n});\r\n\r\n\r\nfunction formatUnit(value: number, plural: string, singular: string) {\r\n    return `${value} ${Math.abs(value) === 1 ? singular : plural}`;\r\n}"],"sourceRoot":""}