IsoDate.mjs (10543B)
1 // generated by diplomat-tool 2 import { Calendar } from "./Calendar.mjs" 3 import { CalendarError } from "./CalendarError.mjs" 4 import { Date } from "./Date.mjs" 5 import { IsoWeekOfYear } from "./IsoWeekOfYear.mjs" 6 import { Rfc9557ParseError } from "./Rfc9557ParseError.mjs" 7 import { Weekday } from "./Weekday.mjs" 8 import wasm from "./diplomat-wasm.mjs"; 9 import * as diplomatRuntime from "./diplomat-runtime.mjs"; 10 11 12 /** 13 * An ICU4X Date object capable of containing a ISO-8601 date 14 * 15 * See the [Rust documentation for `Date`](https://docs.rs/icu/latest/icu/calendar/struct.Date.html) for more information. 16 */ 17 const IsoDate_box_destroy_registry = new FinalizationRegistry((ptr) => { 18 wasm.icu4x_IsoDate_destroy_mv1(ptr); 19 }); 20 21 export class IsoDate { 22 // Internal ptr reference: 23 #ptr = null; 24 25 // Lifetimes are only to keep dependencies alive. 26 // Since JS won't garbage collect until there are no incoming edges. 27 #selfEdge = []; 28 29 #internalConstructor(symbol, ptr, selfEdge) { 30 if (symbol !== diplomatRuntime.internalConstructor) { 31 console.error("IsoDate is an Opaque type. You cannot call its constructor."); 32 return; 33 } 34 this.#ptr = ptr; 35 this.#selfEdge = selfEdge; 36 37 // Are we being borrowed? If not, we can register. 38 if (this.#selfEdge.length === 0) { 39 IsoDate_box_destroy_registry.register(this, this.#ptr); 40 } 41 42 return this; 43 } 44 get ffiValue() { 45 return this.#ptr; 46 } 47 48 49 /** 50 * Creates a new [`IsoDate`] from the specified date. 51 * 52 * See the [Rust documentation for `try_new_iso`](https://docs.rs/icu/latest/icu/calendar/struct.Date.html#method.try_new_iso) for more information. 53 */ 54 #defaultConstructor(year, month, day) { 55 const diplomatReceive = new diplomatRuntime.DiplomatReceiveBuf(wasm, 5, 4, true); 56 57 58 const result = wasm.icu4x_IsoDate_create_mv1(diplomatReceive.buffer, year, month, day); 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 IsoDate(diplomatRuntime.internalConstructor, diplomatRuntime.ptrRead(wasm, diplomatReceive.buffer), []); 66 } 67 68 finally { 69 diplomatReceive.free(); 70 } 71 } 72 73 /** 74 * Creates a new [`IsoDate`] from the given Rata Die 75 * 76 * 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. 77 */ 78 static fromRataDie(rd) { 79 80 const result = wasm.icu4x_IsoDate_from_rata_die_mv1(rd); 81 82 try { 83 return new IsoDate(diplomatRuntime.internalConstructor, result, []); 84 } 85 86 finally { 87 } 88 } 89 90 /** 91 * Creates a new [`IsoDate`] from an IXDTF string. 92 * 93 * 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. 94 */ 95 static fromString(v) { 96 let functionCleanupArena = new diplomatRuntime.CleanupArena(); 97 98 const vSlice = diplomatRuntime.DiplomatBuf.str8(wasm, v); 99 const diplomatReceive = new diplomatRuntime.DiplomatReceiveBuf(wasm, 5, 4, true); 100 101 102 const result = wasm.icu4x_IsoDate_from_string_mv1(diplomatReceive.buffer, ...vSlice.splat()); 103 104 try { 105 if (!diplomatReceive.resultFlag) { 106 const cause = new Rfc9557ParseError(diplomatRuntime.internalConstructor, diplomatRuntime.enumDiscriminant(wasm, diplomatReceive.buffer)); 107 throw new globalThis.Error('Rfc9557ParseError: ' + cause.value, { cause }); 108 } 109 return new IsoDate(diplomatRuntime.internalConstructor, diplomatRuntime.ptrRead(wasm, diplomatReceive.buffer), []); 110 } 111 112 finally { 113 functionCleanupArena.free(); 114 115 diplomatReceive.free(); 116 } 117 } 118 119 /** 120 * Convert this date to one in a different calendar 121 * 122 * See the [Rust documentation for `to_calendar`](https://docs.rs/icu/latest/icu/calendar/struct.Date.html#method.to_calendar) for more information. 123 */ 124 toCalendar(calendar) { 125 126 const result = wasm.icu4x_IsoDate_to_calendar_mv1(this.ffiValue, calendar.ffiValue); 127 128 try { 129 return new Date(diplomatRuntime.internalConstructor, result, []); 130 } 131 132 finally { 133 } 134 } 135 136 /** 137 * See the [Rust documentation for `to_any`](https://docs.rs/icu/latest/icu/calendar/struct.Date.html#method.to_any) for more information. 138 */ 139 toAny() { 140 141 const result = wasm.icu4x_IsoDate_to_any_mv1(this.ffiValue); 142 143 try { 144 return new Date(diplomatRuntime.internalConstructor, result, []); 145 } 146 147 finally { 148 } 149 } 150 151 /** 152 * Returns this date's Rata Die 153 * 154 * 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. 155 */ 156 get rataDie() { 157 158 const result = wasm.icu4x_IsoDate_to_rata_die_mv1(this.ffiValue); 159 160 try { 161 return result; 162 } 163 164 finally { 165 } 166 } 167 168 /** 169 * Returns the 1-indexed day in the year for this date 170 * 171 * 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. 172 */ 173 get dayOfYear() { 174 175 const result = wasm.icu4x_IsoDate_day_of_year_mv1(this.ffiValue); 176 177 try { 178 return result; 179 } 180 181 finally { 182 } 183 } 184 185 /** 186 * Returns the 1-indexed day in the month for this date 187 * 188 * 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. 189 */ 190 get dayOfMonth() { 191 192 const result = wasm.icu4x_IsoDate_day_of_month_mv1(this.ffiValue); 193 194 try { 195 return result; 196 } 197 198 finally { 199 } 200 } 201 202 /** 203 * Returns the day in the week for this day 204 * 205 * 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. 206 */ 207 get dayOfWeek() { 208 209 const result = wasm.icu4x_IsoDate_day_of_week_mv1(this.ffiValue); 210 211 try { 212 return new Weekday(diplomatRuntime.internalConstructor, result); 213 } 214 215 finally { 216 } 217 } 218 219 /** 220 * Returns the week number in this year, using week data 221 * 222 * See the [Rust documentation for `week_of_year`](https://docs.rs/icu/latest/icu/calendar/struct.Date.html#method.week_of_year) for more information. 223 */ 224 weekOfYear() { 225 const diplomatReceive = new diplomatRuntime.DiplomatReceiveBuf(wasm, 8, 4, false); 226 227 228 const result = wasm.icu4x_IsoDate_week_of_year_mv1(diplomatReceive.buffer, this.ffiValue); 229 230 try { 231 return IsoWeekOfYear._fromFFI(diplomatRuntime.internalConstructor, diplomatReceive.buffer); 232 } 233 234 finally { 235 diplomatReceive.free(); 236 } 237 } 238 239 /** 240 * Returns 1-indexed number of the month of this date in its year 241 * 242 * See the [Rust documentation for `ordinal`](https://docs.rs/icu/latest/icu/calendar/types/struct.MonthInfo.html#structfield.ordinal) for more information. 243 * 244 * Additional information: [1](https://docs.rs/icu/latest/icu/calendar/struct.Date.html#method.month) 245 */ 246 get month() { 247 248 const result = wasm.icu4x_IsoDate_month_mv1(this.ffiValue); 249 250 try { 251 return result; 252 } 253 254 finally { 255 } 256 } 257 258 /** 259 * Returns the year number in the current era for this date 260 * 261 * For calendars without an era, returns the extended year 262 * 263 * See the [Rust documentation for `year`](https://docs.rs/icu/latest/icu/calendar/struct.Date.html#method.year) for more information. 264 */ 265 get year() { 266 267 const result = wasm.icu4x_IsoDate_year_mv1(this.ffiValue); 268 269 try { 270 return result; 271 } 272 273 finally { 274 } 275 } 276 277 /** 278 * Returns if the year is a leap year for this date 279 * 280 * See the [Rust documentation for `is_in_leap_year`](https://docs.rs/icu/latest/icu/calendar/struct.Date.html#method.is_in_leap_year) for more information. 281 */ 282 get isInLeapYear() { 283 284 const result = wasm.icu4x_IsoDate_is_in_leap_year_mv1(this.ffiValue); 285 286 try { 287 return result; 288 } 289 290 finally { 291 } 292 } 293 294 /** 295 * Returns the number of months in the year represented by this date 296 * 297 * 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. 298 */ 299 get monthsInYear() { 300 301 const result = wasm.icu4x_IsoDate_months_in_year_mv1(this.ffiValue); 302 303 try { 304 return result; 305 } 306 307 finally { 308 } 309 } 310 311 /** 312 * Returns the number of days in the month represented by this date 313 * 314 * 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. 315 */ 316 get daysInMonth() { 317 318 const result = wasm.icu4x_IsoDate_days_in_month_mv1(this.ffiValue); 319 320 try { 321 return result; 322 } 323 324 finally { 325 } 326 } 327 328 /** 329 * Returns the number of days in the year represented by this date 330 * 331 * 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. 332 */ 333 get daysInYear() { 334 335 const result = wasm.icu4x_IsoDate_days_in_year_mv1(this.ffiValue); 336 337 try { 338 return result; 339 } 340 341 finally { 342 } 343 } 344 345 constructor(year, month, day) { 346 if (arguments[0] === diplomatRuntime.exposeConstructor) { 347 return this.#internalConstructor(...Array.prototype.slice.call(arguments, 1)); 348 } else if (arguments[0] === diplomatRuntime.internalConstructor) { 349 return this.#internalConstructor(...arguments); 350 } else { 351 return this.#defaultConstructor(...arguments); 352 } 353 } 354 }