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