Date.mjs (14885B)
1 // generated by diplomat-tool 2 import { Calendar } from "./Calendar.mjs" 3 import { CalendarError } from "./CalendarError.mjs" 4 import { IsoDate } from "./IsoDate.mjs" 5 import { Rfc9557ParseError } from "./Rfc9557ParseError.mjs" 6 import { Weekday } from "./Weekday.mjs" 7 import wasm from "./diplomat-wasm.mjs"; 8 import * as diplomatRuntime from "./diplomat-runtime.mjs"; 9 10 11 /** 12 * An ICU4X Date object capable of containing a date for any calendar. 13 * 14 * See the [Rust documentation for `Date`](https://docs.rs/icu/latest/icu/calendar/struct.Date.html) for more information. 15 */ 16 const Date_box_destroy_registry = new FinalizationRegistry((ptr) => { 17 wasm.icu4x_Date_destroy_mv1(ptr); 18 }); 19 20 export class Date { 21 // Internal ptr reference: 22 #ptr = null; 23 24 // Lifetimes are only to keep dependencies alive. 25 // Since JS won't garbage collect until there are no incoming edges. 26 #selfEdge = []; 27 28 #internalConstructor(symbol, ptr, selfEdge) { 29 if (symbol !== diplomatRuntime.internalConstructor) { 30 console.error("Date is an Opaque type. You cannot call its constructor."); 31 return; 32 } 33 this.#ptr = ptr; 34 this.#selfEdge = selfEdge; 35 36 // Are we being borrowed? If not, we can register. 37 if (this.#selfEdge.length === 0) { 38 Date_box_destroy_registry.register(this, this.#ptr); 39 } 40 41 return this; 42 } 43 get ffiValue() { 44 return this.#ptr; 45 } 46 47 48 /** 49 * Creates a new [`Date`] representing the ISO date 50 * given but in a given calendar 51 * 52 * See the [Rust documentation for `new_from_iso`](https://docs.rs/icu/latest/icu/calendar/struct.Date.html#method.new_from_iso) for more information. 53 */ 54 static fromIsoInCalendar(year, month, day, calendar) { 55 const diplomatReceive = new diplomatRuntime.DiplomatReceiveBuf(wasm, 5, 4, true); 56 57 58 const result = wasm.icu4x_Date_from_iso_in_calendar_mv1(diplomatReceive.buffer, year, month, day, calendar.ffiValue); 59 60 try { 61 if (!diplomatReceive.resultFlag) { 62 const cause = new CalendarError(diplomatRuntime.internalConstructor, diplomatRuntime.enumDiscriminant(wasm, diplomatReceive.buffer)); 63 throw new globalThis.Error('CalendarError: ' + cause.value, { cause }); 64 } 65 return new Date(diplomatRuntime.internalConstructor, diplomatRuntime.ptrRead(wasm, diplomatReceive.buffer), []); 66 } 67 68 finally { 69 diplomatReceive.free(); 70 } 71 } 72 73 /** 74 * Creates a new [`Date`] from the given codes, which are interpreted in the given calendar system 75 * 76 * An empty era code will treat the year as an extended year 77 * 78 * See the [Rust documentation for `try_new_from_codes`](https://docs.rs/icu/latest/icu/calendar/struct.Date.html#method.try_new_from_codes) for more information. 79 */ 80 static fromCodesInCalendar(eraCode, year, monthCode, day, calendar) { 81 let functionCleanupArena = new diplomatRuntime.CleanupArena(); 82 83 const eraCodeSlice = diplomatRuntime.DiplomatBuf.str8(wasm, eraCode); 84 const monthCodeSlice = diplomatRuntime.DiplomatBuf.str8(wasm, monthCode); 85 const diplomatReceive = new diplomatRuntime.DiplomatReceiveBuf(wasm, 5, 4, true); 86 87 88 const result = wasm.icu4x_Date_from_codes_in_calendar_mv1(diplomatReceive.buffer, ...eraCodeSlice.splat(), year, ...monthCodeSlice.splat(), day, calendar.ffiValue); 89 90 try { 91 if (!diplomatReceive.resultFlag) { 92 const cause = new CalendarError(diplomatRuntime.internalConstructor, diplomatRuntime.enumDiscriminant(wasm, diplomatReceive.buffer)); 93 throw new globalThis.Error('CalendarError: ' + cause.value, { cause }); 94 } 95 return new Date(diplomatRuntime.internalConstructor, diplomatRuntime.ptrRead(wasm, diplomatReceive.buffer), []); 96 } 97 98 finally { 99 functionCleanupArena.free(); 100 101 diplomatReceive.free(); 102 } 103 } 104 105 /** 106 * Creates a new [`Date`] from the given Rata Die 107 * 108 * See the [Rust documentation for `from_rata_die`](https://docs.rs/icu/latest/icu/calendar/struct.Date.html#method.from_rata_die) for more information. 109 */ 110 static fromRataDie(rd, calendar) { 111 const diplomatReceive = new diplomatRuntime.DiplomatReceiveBuf(wasm, 5, 4, true); 112 113 114 const result = wasm.icu4x_Date_from_rata_die_mv1(diplomatReceive.buffer, rd, calendar.ffiValue); 115 116 try { 117 if (!diplomatReceive.resultFlag) { 118 const cause = new CalendarError(diplomatRuntime.internalConstructor, diplomatRuntime.enumDiscriminant(wasm, diplomatReceive.buffer)); 119 throw new globalThis.Error('CalendarError: ' + cause.value, { cause }); 120 } 121 return new Date(diplomatRuntime.internalConstructor, diplomatRuntime.ptrRead(wasm, diplomatReceive.buffer), []); 122 } 123 124 finally { 125 diplomatReceive.free(); 126 } 127 } 128 129 /** 130 * Creates a new [`Date`] from an IXDTF string. 131 * 132 * See the [Rust documentation for `try_from_str`](https://docs.rs/icu/latest/icu/calendar/struct.Date.html#method.try_from_str) for more information. 133 */ 134 static fromString(v, calendar) { 135 let functionCleanupArena = new diplomatRuntime.CleanupArena(); 136 137 const vSlice = diplomatRuntime.DiplomatBuf.str8(wasm, v); 138 const diplomatReceive = new diplomatRuntime.DiplomatReceiveBuf(wasm, 5, 4, true); 139 140 141 const result = wasm.icu4x_Date_from_string_mv1(diplomatReceive.buffer, ...vSlice.splat(), calendar.ffiValue); 142 143 try { 144 if (!diplomatReceive.resultFlag) { 145 const cause = new Rfc9557ParseError(diplomatRuntime.internalConstructor, diplomatRuntime.enumDiscriminant(wasm, diplomatReceive.buffer)); 146 throw new globalThis.Error('Rfc9557ParseError: ' + cause.value, { cause }); 147 } 148 return new Date(diplomatRuntime.internalConstructor, diplomatRuntime.ptrRead(wasm, diplomatReceive.buffer), []); 149 } 150 151 finally { 152 functionCleanupArena.free(); 153 154 diplomatReceive.free(); 155 } 156 } 157 158 /** 159 * Convert this date to one in a different calendar 160 * 161 * See the [Rust documentation for `to_calendar`](https://docs.rs/icu/latest/icu/calendar/struct.Date.html#method.to_calendar) for more information. 162 */ 163 toCalendar(calendar) { 164 165 const result = wasm.icu4x_Date_to_calendar_mv1(this.ffiValue, calendar.ffiValue); 166 167 try { 168 return new Date(diplomatRuntime.internalConstructor, result, []); 169 } 170 171 finally { 172 } 173 } 174 175 /** 176 * Converts this date to ISO 177 * 178 * See the [Rust documentation for `to_iso`](https://docs.rs/icu/latest/icu/calendar/struct.Date.html#method.to_iso) for more information. 179 */ 180 toIso() { 181 182 const result = wasm.icu4x_Date_to_iso_mv1(this.ffiValue); 183 184 try { 185 return new IsoDate(diplomatRuntime.internalConstructor, result, []); 186 } 187 188 finally { 189 } 190 } 191 192 /** 193 * Returns this date's Rata Die 194 * 195 * See the [Rust documentation for `to_rata_die`](https://docs.rs/icu/latest/icu/calendar/struct.Date.html#method.to_rata_die) for more information. 196 */ 197 get rataDie() { 198 199 const result = wasm.icu4x_Date_to_rata_die_mv1(this.ffiValue); 200 201 try { 202 return result; 203 } 204 205 finally { 206 } 207 } 208 209 /** 210 * Returns the 1-indexed day in the year for this date 211 * 212 * See the [Rust documentation for `day_of_year`](https://docs.rs/icu/latest/icu/calendar/struct.Date.html#method.day_of_year) for more information. 213 */ 214 get dayOfYear() { 215 216 const result = wasm.icu4x_Date_day_of_year_mv1(this.ffiValue); 217 218 try { 219 return result; 220 } 221 222 finally { 223 } 224 } 225 226 /** 227 * Returns the 1-indexed day in the month for this date 228 * 229 * See the [Rust documentation for `day_of_month`](https://docs.rs/icu/latest/icu/calendar/struct.Date.html#method.day_of_month) for more information. 230 */ 231 get dayOfMonth() { 232 233 const result = wasm.icu4x_Date_day_of_month_mv1(this.ffiValue); 234 235 try { 236 return result; 237 } 238 239 finally { 240 } 241 } 242 243 /** 244 * Returns the day in the week for this day 245 * 246 * See the [Rust documentation for `day_of_week`](https://docs.rs/icu/latest/icu/calendar/struct.Date.html#method.day_of_week) for more information. 247 */ 248 get dayOfWeek() { 249 250 const result = wasm.icu4x_Date_day_of_week_mv1(this.ffiValue); 251 252 try { 253 return new Weekday(diplomatRuntime.internalConstructor, result); 254 } 255 256 finally { 257 } 258 } 259 260 /** 261 * Returns 1-indexed number of the month of this date in its year 262 * 263 * Note that for lunar calendars this may not lead to the same month 264 * having the same ordinal month across years; use month_code if you care 265 * about month identity. 266 * 267 * See the [Rust documentation for `month`](https://docs.rs/icu/latest/icu/calendar/struct.Date.html#method.month) for more information. 268 * 269 * See the [Rust documentation for `ordinal`](https://docs.rs/icu/latest/icu/calendar/types/struct.MonthInfo.html#structfield.ordinal) for more information. 270 */ 271 get ordinalMonth() { 272 273 const result = wasm.icu4x_Date_ordinal_month_mv1(this.ffiValue); 274 275 try { 276 return result; 277 } 278 279 finally { 280 } 281 } 282 283 /** 284 * Returns the month code for this date. Typically something 285 * like "M01", "M02", but can be more complicated for lunar calendars. 286 * 287 * See the [Rust documentation for `standard_code`](https://docs.rs/icu/latest/icu/calendar/types/struct.MonthInfo.html#structfield.standard_code) for more information. 288 * 289 * Additional information: [1](https://docs.rs/icu/latest/icu/calendar/struct.Date.html#method.month) 290 */ 291 get monthCode() { 292 const write = new diplomatRuntime.DiplomatWriteBuf(wasm); 293 294 wasm.icu4x_Date_month_code_mv1(this.ffiValue, write.buffer); 295 296 try { 297 return write.readString8(); 298 } 299 300 finally { 301 write.free(); 302 } 303 } 304 305 /** 306 * Returns the month number of this month. 307 * 308 * See the [Rust documentation for `month_number`](https://docs.rs/icu/latest/icu/calendar/types/struct.MonthInfo.html#method.month_number) for more information. 309 */ 310 get monthNumber() { 311 312 const result = wasm.icu4x_Date_month_number_mv1(this.ffiValue); 313 314 try { 315 return result; 316 } 317 318 finally { 319 } 320 } 321 322 /** 323 * Returns whether the month is a leap month. 324 * 325 * See the [Rust documentation for `is_leap`](https://docs.rs/icu/latest/icu/calendar/types/struct.MonthInfo.html#method.is_leap) for more information. 326 */ 327 get monthIsLeap() { 328 329 const result = wasm.icu4x_Date_month_is_leap_mv1(this.ffiValue); 330 331 try { 332 return result; 333 } 334 335 finally { 336 } 337 } 338 339 /** 340 * Returns the year number in the current era for this date 341 * 342 * For calendars without an era, returns the related ISO year. 343 * 344 * See the [Rust documentation for `era_year_or_related_iso`](https://docs.rs/icu/latest/icu/calendar/types/enum.YearInfo.html#method.era_year_or_related_iso) for more information. 345 * 346 * Additional information: [1](https://docs.rs/icu/latest/icu/calendar/types/struct.EraYear.html#structfield.year), [2](https://docs.rs/icu/latest/icu/calendar/types/struct.CyclicYear.html#structfield.related_iso), [3](https://docs.rs/icu/latest/icu/calendar/struct.Date.html#method.year) 347 */ 348 get eraYearOrRelatedIso() { 349 350 const result = wasm.icu4x_Date_era_year_or_related_iso_mv1(this.ffiValue); 351 352 try { 353 return result; 354 } 355 356 finally { 357 } 358 } 359 360 /** 361 * Returns the extended year in the Date 362 * 363 * See the [Rust documentation for `extended_year`](https://docs.rs/icu/latest/icu/calendar/struct.Date.html#method.extended_year) for more information. 364 */ 365 get extendedYear() { 366 367 const result = wasm.icu4x_Date_extended_year_mv1(this.ffiValue); 368 369 try { 370 return result; 371 } 372 373 finally { 374 } 375 } 376 377 /** 378 * Returns the era for this date, or an empty string 379 * 380 * See the [Rust documentation for `era`](https://docs.rs/icu/latest/icu/calendar/types/struct.EraYear.html#structfield.era) for more information. 381 * 382 * Additional information: [1](https://docs.rs/icu/latest/icu/calendar/struct.Date.html#method.year) 383 */ 384 get era() { 385 const write = new diplomatRuntime.DiplomatWriteBuf(wasm); 386 387 wasm.icu4x_Date_era_mv1(this.ffiValue, write.buffer); 388 389 try { 390 return write.readString8(); 391 } 392 393 finally { 394 write.free(); 395 } 396 } 397 398 /** 399 * Returns the number of months in the year represented by this date 400 * 401 * See the [Rust documentation for `months_in_year`](https://docs.rs/icu/latest/icu/calendar/struct.Date.html#method.months_in_year) for more information. 402 */ 403 get monthsInYear() { 404 405 const result = wasm.icu4x_Date_months_in_year_mv1(this.ffiValue); 406 407 try { 408 return result; 409 } 410 411 finally { 412 } 413 } 414 415 /** 416 * Returns the number of days in the month represented by this date 417 * 418 * See the [Rust documentation for `days_in_month`](https://docs.rs/icu/latest/icu/calendar/struct.Date.html#method.days_in_month) for more information. 419 */ 420 get daysInMonth() { 421 422 const result = wasm.icu4x_Date_days_in_month_mv1(this.ffiValue); 423 424 try { 425 return result; 426 } 427 428 finally { 429 } 430 } 431 432 /** 433 * Returns the number of days in the year represented by this date 434 * 435 * See the [Rust documentation for `days_in_year`](https://docs.rs/icu/latest/icu/calendar/struct.Date.html#method.days_in_year) for more information. 436 */ 437 get daysInYear() { 438 439 const result = wasm.icu4x_Date_days_in_year_mv1(this.ffiValue); 440 441 try { 442 return result; 443 } 444 445 finally { 446 } 447 } 448 449 /** 450 * Returns the [`Calendar`] object backing this date 451 * 452 * See the [Rust documentation for `calendar`](https://docs.rs/icu/latest/icu/calendar/struct.Date.html#method.calendar) for more information. 453 */ 454 get calendar() { 455 456 const result = wasm.icu4x_Date_calendar_mv1(this.ffiValue); 457 458 try { 459 return new Calendar(diplomatRuntime.internalConstructor, result, []); 460 } 461 462 finally { 463 } 464 } 465 466 constructor(symbol, ptr, selfEdge) { 467 return this.#internalConstructor(...arguments) 468 } 469 }