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AAC,YAAY,CAAC,IAAI,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM;IASvE,MAAM,CAAC,eAAe,CAAC,IAAI,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM;IAQtD,MAAM,CAAC,UAAU,CAAC,IAAI,EAAE,MAAM;IAY9B,MAAM,CAAC,GAAG,CAAC,KAAK,EAAE,KAAK;IAIvB,CAAC,SAAS,CAAC,MAAM,CAAC,CAAC,KAAK,EAAE,OAAO;IAKjC,CAAC,SAAS,CAAC,IAAI,CAAC;IAMhB,CAAC,UAAU,CAAC,SAAS,CAAC,CAAC,KAAK,EAAE,OAAO;CAIxC","file":"CivilDate.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 { Equatable, Equaler, Comparable } from '@esfx/equatable';\r\nimport { isInt, compareNumbers, CIVILDATE_HASH_SEED } from './internal/core';\r\nimport { getDaysInMonth, getDate, getDatePart, getWeeksInYear, getCalendarDateFromWeekDate, getDaysInYear, getCalendarDateFromOrdinalDate, DatePart } from './internal/date';\r\nimport { dateDiff, datePlus } from './internal/math';\r\nimport { DATE } from './internal/symbols';\r\nimport { EPOCH_DAYS } from './internal/types';\r\nimport { DATE_EXTENDED, DATE_BASIC } from './internal/patterns';\r\nimport { D2, D3, D4 } from './internal/formats';\r\nimport { Temporal, Components, DateDelta, DateUnit } from './types';\r\nimport { Clock } from './Clock';\r\nimport { CivilTime } from './CivilTime';\r\nimport { CivilDateTime } from './CivilDateTime';\r\nimport { Duration } from './Duration';\r\n\r\nconst dateExtendedRegExp = new RegExp(R`^${DATE_EXTENDED}$`);\r\nconst dateBasicRegExp = new RegExp(R`^${DATE_BASIC}$`);\r\n\r\nexport class CivilDate implements Temporal<CivilDate>, Equatable, Comparable {\r\n    /* @internal */ private [DATE]: EPOCH_DAYS; // days since epoch\r\n\r\n    constructor(year: number, month: number, day: number) {\r\n        if (!isInt(year)) throw new RangeError(`The value ${year} is not a valid year.`);\r\n        if (!isInt(month) || month < 1 || month > 12) throw new RangeError(`The value ${month} is not a valid month.`);\r\n        if (!isInt(day) || day < 1 || day > getDaysInMonth(year, month)) throw new RangeError(`The value ${day} is not a valid day.`);\r\n        this[DATE] = getDate(year, month, day);\r\n    }\r\n\r\n    get year() { return getDatePart(this[DATE], DatePart.year); }\r\n    get month() { return getDatePart(this[DATE], DatePart.month); }\r\n    get day() { return getDatePart(this[DATE], DatePart.day); }\r\n    get dayOfYear() { return getDatePart(this[DATE], DatePart.dayOfYear); }\r\n    get dayOfWeek() { return getDatePart(this[DATE], DatePart.dayOfWeek); }\r\n    get weekOfYear() { return getDatePart(this[DATE], DatePart.weekOfYear); }\r\n\r\n    plus(deltas: Components<DateDelta> = {}) {\r\n        const { years = 0, months = 0, days = 0 } = deltas;\r\n        if (!isInt(years)) throw new RangeError(`The value ${years} is not valid for 'years' as it is not an integer.`);\r\n        if (!isInt(months)) throw new RangeError(`The value ${months} is not valid for 'months' as it is not an integer.`);\r\n        if (!isInt(days)) throw new RangeError(`The value ${days} is not valid for 'days' as it is not an integer.`);\r\n        return CivilDate.fromDayOfEpoch(datePlus(this[DATE], years, months, days));\r\n    }\r\n\r\n    minus(deltas: Components<DateDelta> = {}) {\r\n        const { years = 0, months = 0, days = 0 } = deltas;\r\n        if (!isInt(years)) throw new RangeError(`The value ${years} is not valid for 'years' as it is not an integer.`);\r\n        if (!isInt(months)) throw new RangeError(`The value ${months} is not valid for 'months' as it is not an integer.`);\r\n        if (!isInt(days)) throw new RangeError(`The value ${days} is not valid for 'days' as it is not an integer.`);\r\n        return CivilDate.fromDayOfEpoch(datePlus(this[DATE], -years, -months, -days));\r\n    }\r\n\r\n    with(components: Components<DateUnit> = {}) {\r\n        const { year = this.year, month = this.month, day = this.day } = components;\r\n        return new CivilDate(year, month, day);\r\n    }\r\n\r\n    withTime(time: CivilTime) {\r\n        if (!(time instanceof CivilTime)) throw new TypeError(\"CivilTime expected: time\");\r\n        return CivilDateTime.from(this, time);\r\n    }\r\n\r\n    until(other: CivilDate) {\r\n        if (!(other instanceof CivilDate)) throw new TypeError(\"CivilDate expected: other\");\r\n        return new Duration(dateDiff(this[DATE], other[DATE]));\r\n    }\r\n\r\n    since(other: CivilDate) {\r\n        if (!(other instanceof CivilDate)) throw new TypeError(\"CivilDate expected: other\");\r\n        return other.until(this);\r\n    }\r\n\r\n    toCalendarDateString() {\r\n        return `${D4(this.year)}-${D2(this.month)}-${D2(this.day)}`;\r\n    }\r\n\r\n    toWeekDateString() {\r\n        let { year, weekOfYear } = this;\r\n        while (weekOfYear < 1) year--, weekOfYear += getWeeksInYear(year);\r\n        while (weekOfYear > getWeeksInYear(year)) weekOfYear -= getWeeksInYear(year), year++;\r\n        return `${D4(year)}-W${D2(weekOfYear)}-${this.dayOfWeek}`;\r\n    }\r\n\r\n    toOrdinalDateString() {\r\n        return `${D4(this.year)}-${D3(this.dayOfYear)}`;\r\n    }\r\n\r\n    toString() {\r\n        return this.toCalendarDateString();\r\n    }\r\n\r\n    toISOString() {\r\n        return this.toCalendarDateString();\r\n    }\r\n\r\n    toJSON() {\r\n        return this.toCalendarDateString();\r\n    }\r\n\r\n    equals(other: CivilDate) {\r\n        return other !== null\r\n            && other !== undefined\r\n            && this[DATE] === other[DATE];\r\n    }\r\n\r\n    compareTo(other: CivilDate): number {\r\n        return compareNumbers(this[DATE], other[DATE]);\r\n    }\r\n\r\n    static fromDayOfEpoch(day: number): CivilDate {\r\n        if (!isInt(day)) throw new RangeError(`The value ${day} is not valid for the number of days.`);\r\n        return Object.create(CivilDate.prototype, {\r\n            [DATE]: { value: day }\r\n        });\r\n    }\r\n\r\n    static fromCalendarDate(year: number, month: number, day: number) {\r\n        return new CivilDate(year, month, day);\r\n    }\r\n\r\n    static fromWeekDate(year: number, weekOfYear: number, dayOfWeek: number) {\r\n        if (!isInt(year)) throw new RangeError(`The value ${year} is not a valid year.`);\r\n        if (!isInt(weekOfYear) || weekOfYear < 1 || weekOfYear > getWeeksInYear(year)) throw new RangeError(`The value ${weekOfYear} is not a valid week for the year ${year}.`);\r\n        if (!isInt(dayOfWeek) || dayOfWeek < 1 || dayOfWeek > 7) throw new RangeError(`The value ${dayOfWeek} is not a valid day of the week.`);\r\n\r\n        const components = getCalendarDateFromWeekDate(year, weekOfYear, dayOfWeek);\r\n        return CivilDate.fromCalendarDate(components.year, components.month, components.day);\r\n    }\r\n\r\n    static fromOrdinalDate(year: number, dayOfYear: number) {\r\n        if (!isInt(year)) throw new RangeError(`The value ${year} is not a valid year.`);\r\n        if (!isInt(dayOfYear) || dayOfYear < 1 || dayOfYear > getDaysInYear(year)) throw new RangeError(`The value ${dayOfYear} is not a valid day for the year ${year}.`);\r\n\r\n        const components = getCalendarDateFromOrdinalDate(year, dayOfYear);\r\n        return CivilDate.fromCalendarDate(components.year, components.month, components.day);\r\n    }\r\n\r\n    static fromString(text: string) {\r\n        const match = dateExtendedRegExp.exec(text) || dateBasicRegExp.exec(text);\r\n        if (match) {\r\n            const { year, month, day, weekOfYear, dayOfWeek, dayOfYear } = match.groups!;\r\n            if (year && month && day) return CivilDate.fromCalendarDate(+year, +month, +day);\r\n            if (year && weekOfYear && dayOfWeek) return CivilDate.fromWeekDate(+year, +weekOfYear, + dayOfWeek);\r\n            if (year && dayOfYear) return CivilDate.fromOrdinalDate(+year, + dayOfYear);\r\n        }\r\n\r\n        throw new SyntaxError(`Invalid date string '${text}'.`);\r\n    }\r\n\r\n    static now(clock: Clock) {\r\n        return clock.currentInstant().withZone(clock.zone).toCivilDate();\r\n    }\r\n\r\n    [Equatable.equals](other: unknown) {\r\n        return other instanceof CivilDate\r\n            && this.equals(other);\r\n    }\r\n\r\n    [Equatable.hash]() {\r\n        let hc = CIVILDATE_HASH_SEED;\r\n        hc = ((hc << 7) | (hc >>> 25)) ^ Equaler.defaultEqualer.hash(this[DATE]);\r\n        return hc;\r\n    }\r\n\r\n    [Comparable.compareTo](other: unknown) {\r\n        if (!(other instanceof CivilDate)) throw new TypeError();\r\n        return this.compareTo(other);\r\n    }\r\n}\r\n\r\nObject.defineProperty(CivilDate.prototype, Symbol.toStringTag, {\r\n    writable: true,\r\n    configurable: true,\r\n    value: \"CivilDate\"\r\n});"],"sourceRoot":""}