• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// generated by diplomat-tool
2import { Calendar } from "./Calendar.mjs"
3import { CalendarParseError } from "./CalendarParseError.mjs"
4import { Date } from "./Date.mjs"
5import { IanaParser } from "./IanaParser.mjs"
6import { Time } from "./Time.mjs"
7import { TimeZoneInfo } from "./TimeZoneInfo.mjs"
8import { UtcOffsetCalculator } from "./UtcOffsetCalculator.mjs"
9import wasm from "./diplomat-wasm.mjs";
10import * as diplomatRuntime from "./diplomat-runtime.mjs";
11
12
13/** An ICU4X DateTime object capable of containing a date, time, and zone for any calendar.
14*
15*See the [Rust documentation for `ZonedDateTime`](https://docs.rs/icu/latest/icu/time/struct.ZonedDateTime.html) for more information.
16*/
17
18
19export class ZonedDateTime {
20
21    #date;
22
23    get date()  {
24        return this.#date;
25    }
26
27    #time;
28
29    get time()  {
30        return this.#time;
31    }
32
33    #zone;
34
35    get zone()  {
36        return this.#zone;
37    }
38
39    #internalConstructor(structObj, internalConstructor) {
40        if (typeof structObj !== "object") {
41            throw new Error("ZonedDateTime's constructor takes an object of ZonedDateTime's fields.");
42        }
43
44        if (internalConstructor !== diplomatRuntime.internalConstructor) {
45            throw new Error("ZonedDateTime is an out struct and can only be created internally.");
46        }
47        if ("date" in structObj) {
48            this.#date = structObj.date;
49        } else {
50            throw new Error("Missing required field date.");
51        }
52
53        if ("time" in structObj) {
54            this.#time = structObj.time;
55        } else {
56            throw new Error("Missing required field time.");
57        }
58
59        if ("zone" in structObj) {
60            this.#zone = structObj.zone;
61        } else {
62            throw new Error("Missing required field zone.");
63        }
64
65        return this;
66    }
67
68    // Return this struct in FFI function friendly format.
69    // Returns an array that can be expanded with spread syntax (...)
70
71    _intoFFI(
72        functionCleanupArena,
73        appendArrayMap
74    ) {
75        return [this.#date.ffiValue, this.#time.ffiValue, this.#zone.ffiValue]
76    }
77
78    static _fromSuppliedValue(internalConstructor, obj) {
79        if (internalConstructor !== diplomatRuntime.internalConstructor) {
80            throw new Error("_fromSuppliedValue cannot be called externally.");
81        }
82
83        if (obj instanceof ZonedDateTime) {
84            return obj;
85        }
86
87        return ZonedDateTime.fromFields(obj);
88    }
89
90    _writeToArrayBuffer(
91        arrayBuffer,
92        offset,
93        functionCleanupArena,
94        appendArrayMap
95    ) {
96        diplomatRuntime.writeToArrayBuffer(arrayBuffer, offset + 0, this.#date.ffiValue, Uint32Array);
97        diplomatRuntime.writeToArrayBuffer(arrayBuffer, offset + 4, this.#time.ffiValue, Uint32Array);
98        diplomatRuntime.writeToArrayBuffer(arrayBuffer, offset + 8, this.#zone.ffiValue, Uint32Array);
99    }
100
101    // This struct contains borrowed fields, so this takes in a list of
102    // "edges" corresponding to where each lifetime's data may have been borrowed from
103    // and passes it down to individual fields containing the borrow.
104    // This method does not attempt to handle any dependencies between lifetimes, the caller
105    // should handle this when constructing edge arrays.
106    static _fromFFI(internalConstructor, ptr) {
107        if (internalConstructor !== diplomatRuntime.internalConstructor) {
108            throw new Error("ZonedDateTime._fromFFI is not meant to be called externally. Please use the default constructor.");
109        }
110        let structObj = {};
111        const dateDeref = diplomatRuntime.ptrRead(wasm, ptr);
112        structObj.date = new Date(diplomatRuntime.internalConstructor, dateDeref, []);
113        const timeDeref = diplomatRuntime.ptrRead(wasm, ptr + 4);
114        structObj.time = new Time(diplomatRuntime.internalConstructor, timeDeref, []);
115        const zoneDeref = diplomatRuntime.ptrRead(wasm, ptr + 8);
116        structObj.zone = new TimeZoneInfo(diplomatRuntime.internalConstructor, zoneDeref, []);
117
118        return new ZonedDateTime(structObj, internalConstructor);
119    }
120
121    static fromString(v, calendar, ianaParser, offsetCalculator) {
122        let functionCleanupArena = new diplomatRuntime.CleanupArena();
123
124        const vSlice = functionCleanupArena.alloc(diplomatRuntime.DiplomatBuf.str8(wasm, v));
125
126        const diplomatReceive = new diplomatRuntime.DiplomatReceiveBuf(wasm, 13, 4, true);
127
128        const result = wasm.icu4x_ZonedDateTime_from_string_mv1(diplomatReceive.buffer, ...vSlice.splat(), calendar.ffiValue, ianaParser.ffiValue, offsetCalculator.ffiValue);
129
130        try {
131            if (!diplomatReceive.resultFlag) {
132                const cause = new CalendarParseError(diplomatRuntime.internalConstructor, diplomatRuntime.enumDiscriminant(wasm, diplomatReceive.buffer));
133                throw new globalThis.Error('CalendarParseError: ' + cause.value, { cause });
134            }
135            return ZonedDateTime._fromFFI(diplomatRuntime.internalConstructor, diplomatReceive.buffer);
136        }
137
138        finally {
139            functionCleanupArena.free();
140
141            diplomatReceive.free();
142        }
143    }
144
145    static locationOnlyFromString(v, calendar, ianaParser) {
146        let functionCleanupArena = new diplomatRuntime.CleanupArena();
147
148        const vSlice = functionCleanupArena.alloc(diplomatRuntime.DiplomatBuf.str8(wasm, v));
149
150        const diplomatReceive = new diplomatRuntime.DiplomatReceiveBuf(wasm, 13, 4, true);
151
152        const result = wasm.icu4x_ZonedDateTime_location_only_from_string_mv1(diplomatReceive.buffer, ...vSlice.splat(), calendar.ffiValue, ianaParser.ffiValue);
153
154        try {
155            if (!diplomatReceive.resultFlag) {
156                const cause = new CalendarParseError(diplomatRuntime.internalConstructor, diplomatRuntime.enumDiscriminant(wasm, diplomatReceive.buffer));
157                throw new globalThis.Error('CalendarParseError: ' + cause.value, { cause });
158            }
159            return ZonedDateTime._fromFFI(diplomatRuntime.internalConstructor, diplomatReceive.buffer);
160        }
161
162        finally {
163            functionCleanupArena.free();
164
165            diplomatReceive.free();
166        }
167    }
168
169    static offsetOnlyFromString(v, calendar) {
170        let functionCleanupArena = new diplomatRuntime.CleanupArena();
171
172        const vSlice = functionCleanupArena.alloc(diplomatRuntime.DiplomatBuf.str8(wasm, v));
173
174        const diplomatReceive = new diplomatRuntime.DiplomatReceiveBuf(wasm, 13, 4, true);
175
176        const result = wasm.icu4x_ZonedDateTime_offset_only_from_string_mv1(diplomatReceive.buffer, ...vSlice.splat(), calendar.ffiValue);
177
178        try {
179            if (!diplomatReceive.resultFlag) {
180                const cause = new CalendarParseError(diplomatRuntime.internalConstructor, diplomatRuntime.enumDiscriminant(wasm, diplomatReceive.buffer));
181                throw new globalThis.Error('CalendarParseError: ' + cause.value, { cause });
182            }
183            return ZonedDateTime._fromFFI(diplomatRuntime.internalConstructor, diplomatReceive.buffer);
184        }
185
186        finally {
187            functionCleanupArena.free();
188
189            diplomatReceive.free();
190        }
191    }
192
193    static looseFromString(v, calendar, ianaParser) {
194        let functionCleanupArena = new diplomatRuntime.CleanupArena();
195
196        const vSlice = functionCleanupArena.alloc(diplomatRuntime.DiplomatBuf.str8(wasm, v));
197
198        const diplomatReceive = new diplomatRuntime.DiplomatReceiveBuf(wasm, 13, 4, true);
199
200        const result = wasm.icu4x_ZonedDateTime_loose_from_string_mv1(diplomatReceive.buffer, ...vSlice.splat(), calendar.ffiValue, ianaParser.ffiValue);
201
202        try {
203            if (!diplomatReceive.resultFlag) {
204                const cause = new CalendarParseError(diplomatRuntime.internalConstructor, diplomatRuntime.enumDiscriminant(wasm, diplomatReceive.buffer));
205                throw new globalThis.Error('CalendarParseError: ' + cause.value, { cause });
206            }
207            return ZonedDateTime._fromFFI(diplomatRuntime.internalConstructor, diplomatReceive.buffer);
208        }
209
210        finally {
211            functionCleanupArena.free();
212
213            diplomatReceive.free();
214        }
215    }
216
217    constructor(structObj, internalConstructor) {
218        return this.#internalConstructor(...arguments)
219    }
220}