1// generated by diplomat-tool 2import { DataProvider } from "./DataProvider.mjs" 3import { Date } from "./Date.mjs" 4import { DateTimeFormatError } from "./DateTimeFormatError.mjs" 5import { DateTimeFormatterLoadError } from "./DateTimeFormatterLoadError.mjs" 6import { DateTimeLength } from "./DateTimeLength.mjs" 7import { IsoDate } from "./IsoDate.mjs" 8import { Locale } from "./Locale.mjs" 9import { Time } from "./Time.mjs" 10import { TimeZoneInfo } from "./TimeZoneInfo.mjs" 11import wasm from "./diplomat-wasm.mjs"; 12import * as diplomatRuntime from "./diplomat-runtime.mjs"; 13 14 15/** An object capable of formatting a date time with time zone to a string. 16* 17*See the [Rust documentation for `DateTimeFormatter`](https://docs.rs/icu/latest/icu/datetime/struct.DateTimeFormatter.html) for more information. 18* 19*Additional information: [1](https://docs.rs/icu/latest/icu/datetime/fieldsets/struct.YMDT.html), [2](https://docs.rs/icu/latest/icu/datetime/fieldsets/zone/struct.GenericShort.html) 20*/ 21const ZonedDateTimeFormatter_box_destroy_registry = new FinalizationRegistry((ptr) => { 22 wasm.icu4x_ZonedDateTimeFormatter_destroy_mv1(ptr); 23}); 24 25export class ZonedDateTimeFormatter { 26 27 // Internal ptr reference: 28 #ptr = null; 29 30 // Lifetimes are only to keep dependencies alive. 31 // Since JS won't garbage collect until there are no incoming edges. 32 #selfEdge = []; 33 34 #internalConstructor(symbol, ptr, selfEdge) { 35 if (symbol !== diplomatRuntime.internalConstructor) { 36 console.error("ZonedDateTimeFormatter is an Opaque type. You cannot call its constructor."); 37 return; 38 } 39 40 this.#ptr = ptr; 41 this.#selfEdge = selfEdge; 42 43 // Are we being borrowed? If not, we can register. 44 if (this.#selfEdge.length === 0) { 45 ZonedDateTimeFormatter_box_destroy_registry.register(this, this.#ptr); 46 } 47 48 return this; 49 } 50 get ffiValue() { 51 return this.#ptr; 52 } 53 54 static createWithLength(locale, length) { 55 const diplomatReceive = new diplomatRuntime.DiplomatReceiveBuf(wasm, 5, 4, true); 56 57 const result = wasm.icu4x_ZonedDateTimeFormatter_create_with_length_mv1(diplomatReceive.buffer, locale.ffiValue, length.ffiValue); 58 59 try { 60 if (!diplomatReceive.resultFlag) { 61 const cause = new DateTimeFormatterLoadError(diplomatRuntime.internalConstructor, diplomatRuntime.enumDiscriminant(wasm, diplomatReceive.buffer)); 62 throw new globalThis.Error('DateTimeFormatterLoadError: ' + cause.value, { cause }); 63 } 64 return new ZonedDateTimeFormatter(diplomatRuntime.internalConstructor, diplomatRuntime.ptrRead(wasm, diplomatReceive.buffer), []); 65 } 66 67 finally { 68 diplomatReceive.free(); 69 } 70 } 71 72 static createWithLengthAndProvider(provider, locale, length) { 73 const diplomatReceive = new diplomatRuntime.DiplomatReceiveBuf(wasm, 5, 4, true); 74 75 const result = wasm.icu4x_ZonedDateTimeFormatter_create_with_length_and_provider_mv1(diplomatReceive.buffer, provider.ffiValue, locale.ffiValue, length.ffiValue); 76 77 try { 78 if (!diplomatReceive.resultFlag) { 79 const cause = new DateTimeFormatterLoadError(diplomatRuntime.internalConstructor, diplomatRuntime.enumDiscriminant(wasm, diplomatReceive.buffer)); 80 throw new globalThis.Error('DateTimeFormatterLoadError: ' + cause.value, { cause }); 81 } 82 return new ZonedDateTimeFormatter(diplomatRuntime.internalConstructor, diplomatRuntime.ptrRead(wasm, diplomatReceive.buffer), []); 83 } 84 85 finally { 86 diplomatReceive.free(); 87 } 88 } 89 90 format(date, time, zone) { 91 const diplomatReceive = new diplomatRuntime.DiplomatReceiveBuf(wasm, 5, 4, true); 92 93 const write = new diplomatRuntime.DiplomatWriteBuf(wasm); 94 95 const result = wasm.icu4x_ZonedDateTimeFormatter_format_mv1(diplomatReceive.buffer, this.ffiValue, date.ffiValue, time.ffiValue, zone.ffiValue, write.buffer); 96 97 try { 98 if (!diplomatReceive.resultFlag) { 99 const cause = new DateTimeFormatError(diplomatRuntime.internalConstructor, diplomatRuntime.enumDiscriminant(wasm, diplomatReceive.buffer)); 100 throw new globalThis.Error('DateTimeFormatError: ' + cause.value, { cause }); 101 } 102 return write.readString8(); 103 } 104 105 finally { 106 diplomatReceive.free(); 107 108 write.free(); 109 } 110 } 111 112 formatIso(date, time, zone) { 113 const diplomatReceive = new diplomatRuntime.DiplomatReceiveBuf(wasm, 5, 4, true); 114 115 const write = new diplomatRuntime.DiplomatWriteBuf(wasm); 116 117 const result = wasm.icu4x_ZonedDateTimeFormatter_format_iso_mv1(diplomatReceive.buffer, this.ffiValue, date.ffiValue, time.ffiValue, zone.ffiValue, write.buffer); 118 119 try { 120 if (!diplomatReceive.resultFlag) { 121 const cause = new DateTimeFormatError(diplomatRuntime.internalConstructor, diplomatRuntime.enumDiscriminant(wasm, diplomatReceive.buffer)); 122 throw new globalThis.Error('DateTimeFormatError: ' + cause.value, { cause }); 123 } 124 return write.readString8(); 125 } 126 127 finally { 128 diplomatReceive.free(); 129 130 write.free(); 131 } 132 } 133 134 constructor(symbol, ptr, selfEdge) { 135 return this.#internalConstructor(...arguments) 136 } 137}