UtcOffset.mjs (6868B)
1 // generated by diplomat-tool 2 import { TimeZoneInvalidOffsetError } from "./TimeZoneInvalidOffsetError.mjs" 3 import wasm from "./diplomat-wasm.mjs"; 4 import * as diplomatRuntime from "./diplomat-runtime.mjs"; 5 6 7 /** 8 * See the [Rust documentation for `UtcOffset`](https://docs.rs/icu/latest/icu/time/zone/struct.UtcOffset.html) for more information. 9 */ 10 const UtcOffset_box_destroy_registry = new FinalizationRegistry((ptr) => { 11 wasm.icu4x_UtcOffset_destroy_mv1(ptr); 12 }); 13 14 export class UtcOffset { 15 // Internal ptr reference: 16 #ptr = null; 17 18 // Lifetimes are only to keep dependencies alive. 19 // Since JS won't garbage collect until there are no incoming edges. 20 #selfEdge = []; 21 22 #internalConstructor(symbol, ptr, selfEdge) { 23 if (symbol !== diplomatRuntime.internalConstructor) { 24 console.error("UtcOffset is an Opaque type. You cannot call its constructor."); 25 return; 26 } 27 this.#ptr = ptr; 28 this.#selfEdge = selfEdge; 29 30 // Are we being borrowed? If not, we can register. 31 if (this.#selfEdge.length === 0) { 32 UtcOffset_box_destroy_registry.register(this, this.#ptr); 33 } 34 35 return this; 36 } 37 get ffiValue() { 38 return this.#ptr; 39 } 40 41 42 /** 43 * Creates an offset from seconds. 44 * 45 * Errors if the offset seconds are out of range. 46 * 47 * See the [Rust documentation for `try_from_seconds`](https://docs.rs/icu/latest/icu/time/zone/struct.UtcOffset.html#method.try_from_seconds) for more information. 48 */ 49 static fromSeconds(seconds) { 50 const diplomatReceive = new diplomatRuntime.DiplomatReceiveBuf(wasm, 5, 4, true); 51 52 53 const result = wasm.icu4x_UtcOffset_from_seconds_mv1(diplomatReceive.buffer, seconds); 54 55 try { 56 if (!diplomatReceive.resultFlag) { 57 const cause = TimeZoneInvalidOffsetError.fromFields({}, diplomatRuntime.internalConstructor); 58 throw new globalThis.Error('TimeZoneInvalidOffsetError', { cause }); 59 } 60 return new UtcOffset(diplomatRuntime.internalConstructor, diplomatRuntime.ptrRead(wasm, diplomatReceive.buffer), []); 61 } 62 63 finally { 64 diplomatReceive.free(); 65 } 66 } 67 68 /** 69 * Creates an offset from a string. 70 * 71 * See the [Rust documentation for `try_from_str`](https://docs.rs/icu/latest/icu/time/zone/struct.UtcOffset.html#method.try_from_str) for more information. 72 * 73 * Additional information: [1](https://docs.rs/icu/latest/icu/time/zone/struct.UtcOffset.html) 74 */ 75 static fromString(offset) { 76 let functionCleanupArena = new diplomatRuntime.CleanupArena(); 77 78 const offsetSlice = diplomatRuntime.DiplomatBuf.str8(wasm, offset); 79 const diplomatReceive = new diplomatRuntime.DiplomatReceiveBuf(wasm, 5, 4, true); 80 81 82 const result = wasm.icu4x_UtcOffset_from_string_mv1(diplomatReceive.buffer, ...offsetSlice.splat()); 83 84 try { 85 if (!diplomatReceive.resultFlag) { 86 const cause = TimeZoneInvalidOffsetError.fromFields({}, diplomatRuntime.internalConstructor); 87 throw new globalThis.Error('TimeZoneInvalidOffsetError', { cause }); 88 } 89 return new UtcOffset(diplomatRuntime.internalConstructor, diplomatRuntime.ptrRead(wasm, diplomatReceive.buffer), []); 90 } 91 92 finally { 93 functionCleanupArena.free(); 94 95 diplomatReceive.free(); 96 } 97 } 98 99 /** 100 * Returns the value as offset seconds. 101 * 102 * See the [Rust documentation for `offset`](https://docs.rs/icu/latest/icu/time/struct.TimeZoneInfo.html#method.offset) for more information. 103 * 104 * See the [Rust documentation for `to_seconds`](https://docs.rs/icu/latest/icu/time/zone/struct.UtcOffset.html#method.to_seconds) for more information. 105 * 106 * Additional information: [1](https://docs.rs/icu/latest/icu/time/zone/struct.UtcOffset.html) 107 */ 108 get seconds() { 109 110 const result = wasm.icu4x_UtcOffset_seconds_mv1(this.ffiValue); 111 112 try { 113 return result; 114 } 115 116 finally { 117 } 118 } 119 120 /** 121 * Returns whether the offset is positive. 122 * 123 * See the [Rust documentation for `is_non_negative`](https://docs.rs/icu/latest/icu/time/zone/struct.UtcOffset.html#method.is_non_negative) for more information. 124 * 125 * Additional information: [1](https://docs.rs/icu/latest/icu/time/zone/struct.UtcOffset.html) 126 */ 127 get isNonNegative() { 128 129 const result = wasm.icu4x_UtcOffset_is_non_negative_mv1(this.ffiValue); 130 131 try { 132 return result; 133 } 134 135 finally { 136 } 137 } 138 139 /** 140 * Returns whether the offset is zero. 141 * 142 * See the [Rust documentation for `is_zero`](https://docs.rs/icu/latest/icu/time/zone/struct.UtcOffset.html#method.is_zero) for more information. 143 * 144 * Additional information: [1](https://docs.rs/icu/latest/icu/time/zone/struct.UtcOffset.html) 145 */ 146 get isZero() { 147 148 const result = wasm.icu4x_UtcOffset_is_zero_mv1(this.ffiValue); 149 150 try { 151 return result; 152 } 153 154 finally { 155 } 156 } 157 158 /** 159 * Returns the hours part of the offset. 160 * 161 * See the [Rust documentation for `hours_part`](https://docs.rs/icu/latest/icu/time/zone/struct.UtcOffset.html#method.hours_part) for more information. 162 * 163 * Additional information: [1](https://docs.rs/icu/latest/icu/time/zone/struct.UtcOffset.html) 164 */ 165 get hoursPart() { 166 167 const result = wasm.icu4x_UtcOffset_hours_part_mv1(this.ffiValue); 168 169 try { 170 return result; 171 } 172 173 finally { 174 } 175 } 176 177 /** 178 * Returns the minutes part of the offset. 179 * 180 * See the [Rust documentation for `minutes_part`](https://docs.rs/icu/latest/icu/time/zone/struct.UtcOffset.html#method.minutes_part) for more information. 181 * 182 * Additional information: [1](https://docs.rs/icu/latest/icu/time/zone/struct.UtcOffset.html) 183 */ 184 get minutesPart() { 185 186 const result = wasm.icu4x_UtcOffset_minutes_part_mv1(this.ffiValue); 187 188 try { 189 return result; 190 } 191 192 finally { 193 } 194 } 195 196 /** 197 * Returns the seconds part of the offset. 198 * 199 * See the [Rust documentation for `seconds_part`](https://docs.rs/icu/latest/icu/time/zone/struct.UtcOffset.html#method.seconds_part) for more information. 200 * 201 * Additional information: [1](https://docs.rs/icu/latest/icu/time/zone/struct.UtcOffset.html) 202 */ 203 get secondsPart() { 204 205 const result = wasm.icu4x_UtcOffset_seconds_part_mv1(this.ffiValue); 206 207 try { 208 return result; 209 } 210 211 finally { 212 } 213 } 214 215 constructor(symbol, ptr, selfEdge) { 216 return this.#internalConstructor(...arguments) 217 } 218 }