1// generated by diplomat-tool 2import { Calendar } from "./Calendar.mjs" 3import { CalendarError } from "./CalendarError.mjs" 4import { CalendarParseError } from "./CalendarParseError.mjs" 5import { IsoDate } from "./IsoDate.mjs" 6import { WeekCalculator } from "./WeekCalculator.mjs" 7import { WeekOf } from "./WeekOf.mjs" 8import { Weekday } from "./Weekday.mjs" 9import wasm from "./diplomat-wasm.mjs"; 10import * as diplomatRuntime from "./diplomat-runtime.mjs"; 11 12 13/** An ICU4X Date object capable of containing a date and time for any calendar. 14* 15*See the [Rust documentation for `Date`](https://docs.rs/icu/latest/icu/calendar/struct.Date.html) for more information. 16*/ 17const Date_box_destroy_registry = new FinalizationRegistry((ptr) => { 18 wasm.icu4x_Date_destroy_mv1(ptr); 19}); 20 21export class Date { 22 23 // Internal ptr reference: 24 #ptr = null; 25 26 // Lifetimes are only to keep dependencies alive. 27 // Since JS won't garbage collect until there are no incoming edges. 28 #selfEdge = []; 29 30 #internalConstructor(symbol, ptr, selfEdge) { 31 if (symbol !== diplomatRuntime.internalConstructor) { 32 console.error("Date is an Opaque type. You cannot call its constructor."); 33 return; 34 } 35 36 this.#ptr = ptr; 37 this.#selfEdge = selfEdge; 38 39 // Are we being borrowed? If not, we can register. 40 if (this.#selfEdge.length === 0) { 41 Date_box_destroy_registry.register(this, this.#ptr); 42 } 43 44 return this; 45 } 46 get ffiValue() { 47 return this.#ptr; 48 } 49 50 static fromIsoInCalendar(year, month, day, calendar) { 51 const diplomatReceive = new diplomatRuntime.DiplomatReceiveBuf(wasm, 5, 4, true); 52 53 const result = wasm.icu4x_Date_from_iso_in_calendar_mv1(diplomatReceive.buffer, year, month, day, calendar.ffiValue); 54 55 try { 56 if (!diplomatReceive.resultFlag) { 57 const cause = new CalendarError(diplomatRuntime.internalConstructor, diplomatRuntime.enumDiscriminant(wasm, diplomatReceive.buffer)); 58 throw new globalThis.Error('CalendarError: ' + cause.value, { cause }); 59 } 60 return new Date(diplomatRuntime.internalConstructor, diplomatRuntime.ptrRead(wasm, diplomatReceive.buffer), []); 61 } 62 63 finally { 64 diplomatReceive.free(); 65 } 66 } 67 68 static fromCodesInCalendar(eraCode, year, monthCode, day, calendar) { 69 let functionCleanupArena = new diplomatRuntime.CleanupArena(); 70 71 const eraCodeSlice = functionCleanupArena.alloc(diplomatRuntime.DiplomatBuf.str8(wasm, eraCode)); 72 73 const monthCodeSlice = functionCleanupArena.alloc(diplomatRuntime.DiplomatBuf.str8(wasm, monthCode)); 74 75 const diplomatReceive = new diplomatRuntime.DiplomatReceiveBuf(wasm, 5, 4, true); 76 77 const result = wasm.icu4x_Date_from_codes_in_calendar_mv1(diplomatReceive.buffer, ...eraCodeSlice.splat(), year, ...monthCodeSlice.splat(), day, calendar.ffiValue); 78 79 try { 80 if (!diplomatReceive.resultFlag) { 81 const cause = new CalendarError(diplomatRuntime.internalConstructor, diplomatRuntime.enumDiscriminant(wasm, diplomatReceive.buffer)); 82 throw new globalThis.Error('CalendarError: ' + cause.value, { cause }); 83 } 84 return new Date(diplomatRuntime.internalConstructor, diplomatRuntime.ptrRead(wasm, diplomatReceive.buffer), []); 85 } 86 87 finally { 88 functionCleanupArena.free(); 89 90 diplomatReceive.free(); 91 } 92 } 93 94 static fromString(v, calendar) { 95 let functionCleanupArena = new diplomatRuntime.CleanupArena(); 96 97 const vSlice = functionCleanupArena.alloc(diplomatRuntime.DiplomatBuf.str8(wasm, v)); 98 99 const diplomatReceive = new diplomatRuntime.DiplomatReceiveBuf(wasm, 5, 4, true); 100 101 const result = wasm.icu4x_Date_from_string_mv1(diplomatReceive.buffer, ...vSlice.splat(), calendar.ffiValue); 102 103 try { 104 if (!diplomatReceive.resultFlag) { 105 const cause = new CalendarParseError(diplomatRuntime.internalConstructor, diplomatRuntime.enumDiscriminant(wasm, diplomatReceive.buffer)); 106 throw new globalThis.Error('CalendarParseError: ' + cause.value, { cause }); 107 } 108 return new Date(diplomatRuntime.internalConstructor, diplomatRuntime.ptrRead(wasm, diplomatReceive.buffer), []); 109 } 110 111 finally { 112 functionCleanupArena.free(); 113 114 diplomatReceive.free(); 115 } 116 } 117 118 toCalendar(calendar) { 119 const result = wasm.icu4x_Date_to_calendar_mv1(this.ffiValue, calendar.ffiValue); 120 121 try { 122 return new Date(diplomatRuntime.internalConstructor, result, []); 123 } 124 125 finally {} 126 } 127 128 toIso() { 129 const result = wasm.icu4x_Date_to_iso_mv1(this.ffiValue); 130 131 try { 132 return new IsoDate(diplomatRuntime.internalConstructor, result, []); 133 } 134 135 finally {} 136 } 137 138 get dayOfYear() { 139 const result = wasm.icu4x_Date_day_of_year_mv1(this.ffiValue); 140 141 try { 142 return result; 143 } 144 145 finally {} 146 } 147 148 get dayOfMonth() { 149 const result = wasm.icu4x_Date_day_of_month_mv1(this.ffiValue); 150 151 try { 152 return result; 153 } 154 155 finally {} 156 } 157 158 get dayOfWeek() { 159 const result = wasm.icu4x_Date_day_of_week_mv1(this.ffiValue); 160 161 try { 162 return new Weekday(diplomatRuntime.internalConstructor, result); 163 } 164 165 finally {} 166 } 167 168 weekOfMonth(firstWeekday) { 169 const result = wasm.icu4x_Date_week_of_month_mv1(this.ffiValue, firstWeekday.ffiValue); 170 171 try { 172 return result; 173 } 174 175 finally {} 176 } 177 178 weekOfYear(calculator) { 179 const diplomatReceive = new diplomatRuntime.DiplomatReceiveBuf(wasm, 8, 4, false); 180 181 const result = wasm.icu4x_Date_week_of_year_mv1(diplomatReceive.buffer, this.ffiValue, calculator.ffiValue); 182 183 try { 184 return WeekOf._fromFFI(diplomatRuntime.internalConstructor, diplomatReceive.buffer); 185 } 186 187 finally { 188 diplomatReceive.free(); 189 } 190 } 191 192 get ordinalMonth() { 193 const result = wasm.icu4x_Date_ordinal_month_mv1(this.ffiValue); 194 195 try { 196 return result; 197 } 198 199 finally {} 200 } 201 202 get monthCode() { 203 const write = new diplomatRuntime.DiplomatWriteBuf(wasm); 204 wasm.icu4x_Date_month_code_mv1(this.ffiValue, write.buffer); 205 206 try { 207 return write.readString8(); 208 } 209 210 finally { 211 write.free(); 212 } 213 } 214 215 get monthNumber() { 216 const result = wasm.icu4x_Date_month_number_mv1(this.ffiValue); 217 218 try { 219 return result; 220 } 221 222 finally {} 223 } 224 225 get monthIsLeap() { 226 const result = wasm.icu4x_Date_month_is_leap_mv1(this.ffiValue); 227 228 try { 229 return result; 230 } 231 232 finally {} 233 } 234 235 get yearInEra() { 236 const result = wasm.icu4x_Date_year_in_era_mv1(this.ffiValue); 237 238 try { 239 return result; 240 } 241 242 finally {} 243 } 244 245 get extendedYear() { 246 const result = wasm.icu4x_Date_extended_year_mv1(this.ffiValue); 247 248 try { 249 return result; 250 } 251 252 finally {} 253 } 254 255 get era() { 256 const write = new diplomatRuntime.DiplomatWriteBuf(wasm); 257 wasm.icu4x_Date_era_mv1(this.ffiValue, write.buffer); 258 259 try { 260 return write.readString8(); 261 } 262 263 finally { 264 write.free(); 265 } 266 } 267 268 get monthsInYear() { 269 const result = wasm.icu4x_Date_months_in_year_mv1(this.ffiValue); 270 271 try { 272 return result; 273 } 274 275 finally {} 276 } 277 278 get daysInMonth() { 279 const result = wasm.icu4x_Date_days_in_month_mv1(this.ffiValue); 280 281 try { 282 return result; 283 } 284 285 finally {} 286 } 287 288 get daysInYear() { 289 const result = wasm.icu4x_Date_days_in_year_mv1(this.ffiValue); 290 291 try { 292 return result; 293 } 294 295 finally {} 296 } 297 298 get calendar() { 299 const result = wasm.icu4x_Date_calendar_mv1(this.ffiValue); 300 301 try { 302 return new Calendar(diplomatRuntime.internalConstructor, result, []); 303 } 304 305 finally {} 306 } 307 308 constructor(symbol, ptr, selfEdge) { 309 return this.#internalConstructor(...arguments) 310 } 311}