ZonedIsoDateTime.mjs (6551B)
1 // generated by diplomat-tool 2 import { IanaParser } from "./IanaParser.mjs" 3 import { IsoDate } from "./IsoDate.mjs" 4 import { Rfc9557ParseError } from "./Rfc9557ParseError.mjs" 5 import { Time } from "./Time.mjs" 6 import { TimeZoneInfo } from "./TimeZoneInfo.mjs" 7 import { UtcOffset } from "./UtcOffset.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 ZonedDateTime object capable of containing a ISO-8601 date, time, and zone. 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 ZonedIsoDateTime { 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("ZonedIsoDateTime's constructor takes an object of ZonedIsoDateTime's fields."); 36 } 37 38 if (internalConstructor !== diplomatRuntime.internalConstructor) { 39 throw new Error("ZonedIsoDateTime 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 ZonedIsoDateTime) { 77 return obj; 78 } 79 80 return ZonedIsoDateTime.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("ZonedIsoDateTime._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 IsoDate(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 ZonedIsoDateTime(structObj, internalConstructor); 112 } 113 114 115 /** 116 * Creates a new [`ZonedIsoDateTime`] 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, 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_ZonedIsoDateTime_full_from_string_mv1(diplomatReceive.buffer, ...vSlice.splat(), 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 ZonedIsoDateTime._fromFFI(diplomatRuntime.internalConstructor, diplomatReceive.buffer); 135 } 136 137 finally { 138 functionCleanupArena.free(); 139 140 diplomatReceive.free(); 141 } 142 } 143 144 /** 145 * Creates a new [`ZonedIsoDateTime`] from milliseconds since epoch (timestamp) and a UTC offset. 146 * 147 * Note: [`ZonedIsoDateTime`]s created with this constructor can only be formatted using localized offset zone styles. 148 * 149 * See the [Rust documentation for `from_epoch_milliseconds_and_utc_offset`](https://docs.rs/icu/latest/icu/time/struct.ZonedDateTime.html#method.from_epoch_milliseconds_and_utc_offset) for more information. 150 */ 151 static fromEpochMillisecondsAndUtcOffset(epochMilliseconds, utcOffset) { 152 const diplomatReceive = new diplomatRuntime.DiplomatReceiveBuf(wasm, 12, 4, false); 153 154 155 const result = wasm.icu4x_ZonedIsoDateTime_from_epoch_milliseconds_and_utc_offset_mv1(diplomatReceive.buffer, epochMilliseconds, utcOffset.ffiValue); 156 157 try { 158 return ZonedIsoDateTime._fromFFI(diplomatRuntime.internalConstructor, diplomatReceive.buffer); 159 } 160 161 finally { 162 diplomatReceive.free(); 163 } 164 } 165 166 constructor(structObj, internalConstructor) { 167 return this.#internalConstructor(...arguments) 168 } 169 }