{"version":3,"sources":["../src/Clock.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;EAcE;AACF,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AACpC,OAAO,EAAE,QAAQ,EAAE,MAAM,SAAS,CAAC;AACnC,OAAO,EAAE,IAAI,EAAE,MAAM,QAAQ,CAAC;AAK9B,8BAAsB,KAAK;IACvB,QAAQ,CAAC,IAAI,EAAE,IAAI,CAAC;gBAER,IAAI,EAAE,IAAI;oBAIX,OAAO;oBACP,GAAG;IAEd,QAAQ,CAAC,cAAc,IAAI,OAAO;IAElC,QAAQ,CAAC,QAAQ,CAAC,IAAI,EAAE,IAAI,GAAG,MAAM,GAAG,KAAK;IAE7C,aAAa,CAAC,SAAS,EAAE,QAAQ,GAAG,KAAK;IAczC,MAAM,CAAC,SAAS,CAAC,IAAI,EAAE,IAAI,GAAG,KAAK;IAInC,MAAM,CAAC,QAAQ,CAAC,IAAI,EAAE,IAAI,EAAE,OAAO,EAAE,OAAO,GAAG,KAAK;CAGvD","file":"Clock.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 { Instant } from './Instant';\r\nimport { TimeUnit } from './types';\r\nimport { Zone } from './Zone';\r\n\r\nconst PRECISION = [\"hour\", \"minute\", \"second\", \"millisecond\", \"nanosecond\"];\r\nconst INSTANT = Symbol(\"[[Time]]\");\r\n\r\nexport abstract class Clock {\r\n    readonly zone: Zone;\r\n\r\n    constructor(zone: Zone) {\r\n        this.zone = zone;\r\n    }\r\n\r\n    static get default() { return Clock.getSystem(Zone.SYSTEM); }\r\n    static get UTC() { return Clock.getSystem(Zone.UTC); }\r\n\r\n    abstract currentInstant(): Instant;\r\n\r\n    abstract withZone(zone: Zone | string): Clock;\r\n\r\n    withPrecision(precision: TimeUnit): Clock {\r\n        const newPrecision = PRECISION.indexOf(precision);\r\n        if (newPrecision === -1) throw new RangeError();\r\n\r\n        if (this instanceof TruncatingClock) {\r\n            const oldPrecision = PRECISION.indexOf(this._precision);\r\n            if (oldPrecision <= newPrecision) return this;\r\n\r\n            return new TruncatingClock(this._clock, precision);\r\n        }\r\n\r\n        return new TruncatingClock(this, precision);\r\n    }\r\n\r\n    static getSystem(zone: Zone): Clock {\r\n        return new SystemClock(zone);\r\n    }\r\n\r\n    static getFixed(zone: Zone, instant: Instant): Clock {\r\n        return new FixedClock(zone, instant);\r\n    }\r\n}\r\n\r\nObject.defineProperty(Clock.prototype, Symbol.toStringTag, {\r\n    writable: true,\r\n    configurable: true,\r\n    value: \"Clock\"\r\n});\r\n\r\nclass SystemClock extends Clock {\r\n    constructor(zone: Zone) {\r\n        super(zone);\r\n    }\r\n\r\n    currentInstant() {\r\n        return Instant.fromMilliseconds(Date.now());\r\n    }\r\n\r\n    withZone(zone: Zone | string) {\r\n        if (typeof zone === \"string\") zone = Zone.fromString(zone);\r\n        return new SystemClock(zone);\r\n    }\r\n}\r\n\r\nclass FixedClock extends Clock {\r\n    private [INSTANT]: Instant;\r\n\r\n    constructor(zone: Zone, instant: Instant) {\r\n        super(zone);\r\n        this[INSTANT] = instant;\r\n    }\r\n\r\n    currentInstant() {\r\n        return this[INSTANT];\r\n    }\r\n\r\n    withZone(zone: Zone | string) {\r\n        if (typeof zone === \"string\") zone = Zone.fromString(zone);\r\n        return new FixedClock(zone, this[INSTANT]);\r\n    }\r\n}\r\n\r\nclass TruncatingClock extends Clock {\r\n    readonly _precision: TimeUnit;\r\n    readonly _clock: Clock;\r\n\r\n    constructor(clock: Clock, precision: TimeUnit) {\r\n        super(clock.zone);\r\n        this._clock = clock;\r\n        this._precision = precision;\r\n    }\r\n\r\n    currentInstant() {\r\n        return this._clock.currentInstant().truncate(this._precision);\r\n    }\r\n\r\n    withPrecision(precision: TimeUnit): Clock {\r\n        const newPrecision = PRECISION.indexOf(precision);\r\n        if (newPrecision === -1) throw new RangeError();\r\n\r\n        const oldPrecision = PRECISION.indexOf(this._precision);\r\n        if (oldPrecision <= newPrecision) return this;\r\n\r\n        return new TruncatingClock(this._clock, precision);\r\n    }\r\n\r\n    withZone(zone: Zone | string) {\r\n        return new TruncatingClock(this._clock.withZone(zone), this._precision);\r\n    }\r\n}\r\n"],"sourceRoot":""}