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