1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
//! A Rust API wrapper for Boa's `Date` ECMAScript Builtin Object.

use crate::{
    builtins::Date,
    object::{JsObject, JsObjectType},
    value::TryFromJs,
    Context, JsNativeError, JsResult, JsValue,
};
use boa_gc::{Finalize, Trace};
use std::ops::Deref;
use time::{format_description::well_known::Rfc3339, OffsetDateTime};

/// `JsDate` is a wrapper for JavaScript `JsDate` builtin object
///
/// # Example
///
/// Create a `JsDate` object and set date to December 4 1995
///
/// ```
/// use boa_engine::{
///     js_string, object::builtins::JsDate, Context, JsResult, JsValue,
/// };
///
/// fn main() -> JsResult<()> {
///     // JS mutable Context
///     let context = &mut Context::default();
///
///     let date = JsDate::new(context);
///
///     date.set_full_year(&[1995.into(), 11.into(), 4.into()], context)?;
///
///     assert_eq!(
///         date.to_date_string(context)?,
///         JsValue::from(js_string!("Mon Dec 04 1995"))
///     );
///
///     Ok(())
/// }
/// ```
#[derive(Debug, Clone, Trace, Finalize)]
pub struct JsDate {
    inner: JsObject,
}

impl JsDate {
    /// Create a new `Date` object with universal time.
    #[inline]
    pub fn new(context: &mut Context) -> Self {
        let prototype = context.intrinsics().constructors().date().prototype();
        let inner = JsObject::from_proto_and_data_with_shared_shape(
            context.root_shape(),
            prototype,
            Date::utc_now(context.host_hooks()),
        );

        Self { inner }
    }

    /// Create a new `JsDate` object from an existing object.
    #[inline]
    pub fn from_object(object: JsObject) -> JsResult<Self> {
        if object.is::<Date>() {
            Ok(Self { inner: object })
        } else {
            Err(JsNativeError::typ()
                .with_message("object is not a Date")
                .into())
        }
    }

    /// Return a `Number` representing the milliseconds elapsed since the UNIX epoch.
    ///
    /// Same as JavaScript's `Date.now()`
    #[inline]
    pub fn now(context: &mut Context) -> JsResult<JsValue> {
        Date::now(&JsValue::Null, &[JsValue::Null], context)
    }

    // DEBUG: Uses RFC3339 internally therefore could match es6 spec of ISO8601  <========
    /// Parse a `String` representation of date.
    /// String should be ISO 8601 format.
    /// Returns the `Number` of milliseconds since UNIX epoch if `String`
    /// is valid, else return a `NaN`.
    ///
    /// Same as JavaScript's `Date.parse(value)`.
    #[inline]
    pub fn parse(value: JsValue, context: &mut Context) -> JsResult<JsValue> {
        Date::parse(&JsValue::Null, &[value], context)
    }

    /// Takes a [year, month, day, hour, minute, second, millisecond]
    /// Return a `Number` representing the milliseconds elapsed since the UNIX epoch.
    ///
    /// Same as JavaScript's `Date.UTC()`
    #[inline]
    pub fn utc(values: &[JsValue], context: &mut Context) -> JsResult<JsValue> {
        Date::utc(&JsValue::Null, values, context)
    }

    /// Returns the day of the month(1-31) for the specified date
    /// according to local time.
    ///
    /// Same as JavaScript's `Date.prototype.getDate()`.
    #[inline]
    pub fn get_date(&self, context: &mut Context) -> JsResult<JsValue> {
        Date::get_date::<true>(&self.inner.clone().into(), &[JsValue::null()], context)
    }

    /// Returns the day of the week (0–6) for the specified date
    /// according to local time.
    ///
    /// Same as JavaScript's `Date.prototype.getDay()`.
    #[inline]
    pub fn get_day(&self, context: &mut Context) -> JsResult<JsValue> {
        Date::get_day::<true>(&self.inner.clone().into(), &[JsValue::null()], context)
    }

    /// Returns the year (4 digits for 4-digit years) of the specified date
    /// according to local time.
    ///
    /// Same as JavaScript's `Date.prototype.getFullYear()`.
    #[inline]
    pub fn get_full_year(&self, context: &mut Context) -> JsResult<JsValue> {
        Date::get_full_year::<true>(&self.inner.clone().into(), &[JsValue::null()], context)
    }

    /// Returns the hour (0–23) in the specified date according to local time.
    ///
    /// Same as JavaScript's `Date.prototype.getHours()`.
    #[inline]
    pub fn get_hours(&self, context: &mut Context) -> JsResult<JsValue> {
        Date::get_hours::<true>(&self.inner.clone().into(), &[JsValue::null()], context)
    }

    /// Returns the milliseconds (0–999) in the specified date according
    /// to local time.
    ///
    /// Same as JavaScript's `Date.prototype.getMilliseconds()`.
    #[inline]
    pub fn get_milliseconds(&self, context: &mut Context) -> JsResult<JsValue> {
        Date::get_milliseconds::<true>(&self.inner.clone().into(), &[JsValue::null()], context)
    }

    /// Returns the minutes (0–59) in the specified date according to local time.
    ///
    /// Same as JavaScript's `Date.prototype.getMinutes()`.
    #[inline]
    pub fn get_minutes(&self, context: &mut Context) -> JsResult<JsValue> {
        Date::get_minutes::<true>(&self.inner.clone().into(), &[JsValue::null()], context)
    }

    /// Returns the month (0–11) in the specified date according to local time.
    ///
    /// Same as JavaScript's `Date.prototype.getMonth()`.
    #[inline]
    pub fn get_month(&self, context: &mut Context) -> JsResult<JsValue> {
        Date::get_month::<true>(&self.inner.clone().into(), &[JsValue::null()], context)
    }

    /// Returns the seconds (0–59) in the specified date according to local time.
    ///
    /// Same as JavaScript's `Date.prototype.getSeconds()`.
    #[inline]
    pub fn get_seconds(&self, context: &mut Context) -> JsResult<JsValue> {
        Date::get_seconds::<true>(&self.inner.clone().into(), &[JsValue::null()], context)
    }

    /// Returns the numeric value of the specified date as the number
    /// of milliseconds since UNIX epoch.
    /// Negative values are returned for prior times.
    ///
    /// Same as JavaScript's `Date.prototype.getTime()`.
    #[inline]
    pub fn get_time(&self, context: &mut Context) -> JsResult<JsValue> {
        Date::get_time(&self.inner.clone().into(), &[JsValue::null()], context)
    }

    /// Returns the time-zone offset in minutes for the current locale.
    ///
    /// Same as JavaScript's `Date.prototype.getTimezoneOffset()`.
    #[inline]
    pub fn get_timezone_offset(&self, context: &mut Context) -> JsResult<JsValue> {
        Date::get_timezone_offset(&self.inner.clone().into(), &[JsValue::Null], context)
    }

    /// Returns the day (date) of the month (1–31) in the specified
    /// date according to universal time.
    ///
    /// Same as JavaScript's `Date.prototype.getUTCDate()`.
    #[inline]
    pub fn get_utc_date(&self, context: &mut Context) -> JsResult<JsValue> {
        Date::get_date::<false>(&self.inner.clone().into(), &[JsValue::null()], context)
    }

    /// Returns the day of the week (0–6) in the specified
    /// date according to universal time.
    ///
    /// Same as JavaScript's `Date.prototype.getUTCDay()`.
    #[inline]
    pub fn get_utc_day(&self, context: &mut Context) -> JsResult<JsValue> {
        Date::get_day::<false>(&self.inner.clone().into(), &[JsValue::null()], context)
    }

    /// Returns the year (4 digits for 4-digit years) in the specified
    /// date according to universal time.
    ///
    /// Same as JavaScript's `Date.prototype.getUTCFullYear()`.
    #[inline]
    pub fn get_utc_full_year(&self, context: &mut Context) -> JsResult<JsValue> {
        Date::get_full_year::<false>(&self.inner.clone().into(), &[JsValue::null()], context)
    }

    /// Returns the hours (0–23) in the specified date according
    /// to universal time.
    ///
    /// Same as JavaScript's `Date.prototype.getUTCHours()`.
    #[inline]
    pub fn get_utc_hours(&self, context: &mut Context) -> JsResult<JsValue> {
        Date::get_hours::<false>(&self.inner.clone().into(), &[JsValue::null()], context)
    }

    /// Returns the milliseconds (0–999) in the specified date
    /// according to universal time.
    ///
    /// Same as JavaScript's `Date.prototype.getUTCMilliseconds()`.
    #[inline]
    pub fn get_utc_milliseconds(&self, context: &mut Context) -> JsResult<JsValue> {
        Date::get_milliseconds::<false>(&self.inner.clone().into(), &[JsValue::null()], context)
    }

    /// Returns the minutes (0–59) in the specified date according
    /// to universal time.
    ///
    /// Same as JavaScript's `Date.prototype.getUTCMinutes()`.
    #[inline]
    pub fn get_utc_minutes(&self, context: &mut Context) -> JsResult<JsValue> {
        Date::get_minutes::<false>(&self.inner.clone().into(), &[JsValue::null()], context)
    }

    /// Returns the month (0–11) in the specified date according
    /// to universal time.
    ///
    /// Same as JavaScript's `Date.prototype.getUTCMonth()`.
    #[inline]
    pub fn get_utc_month(&self, context: &mut Context) -> JsResult<JsValue> {
        Date::get_month::<false>(&self.inner.clone().into(), &[JsValue::null()], context)
    }

    /// Returns the seconds (0–59) in the specified date according
    /// to universal time.
    ///
    /// Same as JavaScript's `Date.prototype.getUTCSeconds()`.
    #[inline]
    pub fn get_utc_seconds(&self, context: &mut Context) -> JsResult<JsValue> {
        Date::get_seconds::<false>(&self.inner.clone().into(), &[JsValue::null()], context)
    }

    /// Sets the day of the month for a specified date according
    /// to local time.
    /// Takes a `month_value`.
    /// Return a `Number` representing the milliseconds elapsed between
    /// the UNIX epoch and the given date.
    ///
    /// Same as JavaScript's `Date.prototype.setDate()`.
    pub fn set_date<T>(&self, value: T, context: &mut Context) -> JsResult<JsValue>
    where
        T: Into<JsValue>,
    {
        Date::set_date::<true>(&self.inner.clone().into(), &[value.into()], context)
    }

    /// Sets the full year (e.g. 4 digits for 4-digit years) for a
    /// specified date according to local time.
    /// Takes [`year_value`, `month_value`, `date_value`]
    /// Return a `Number` representing the milliseconds elapsed between
    /// the UNIX epoch and updated date.
    ///
    /// Same as JavaScript's `Date.prototype.setFullYear()`.
    #[inline]
    pub fn set_full_year(&self, values: &[JsValue], context: &mut Context) -> JsResult<JsValue> {
        Date::set_full_year::<true>(&self.inner.clone().into(), values, context)
    }

    /// Sets the hours for a specified date according to local time.
    /// Takes [`hours_value`, `minutes_value`, `seconds_value`, `ms_value`]
    /// Return a `Number` representing the milliseconds elapsed between
    /// the UNIX epoch and the updated date.
    ///
    /// Same as JavaScript's `Date.prototype.setHours()`.
    #[inline]
    pub fn set_hours(&self, values: &[JsValue], context: &mut Context) -> JsResult<JsValue> {
        Date::set_hours::<true>(&self.inner.clone().into(), values, context)
    }

    /// Sets the milliseconds for a specified date according to local time.
    /// Takes a `milliseconds_value`
    /// Return a `Number` representing the milliseconds elapsed between
    /// the UNIX epoch and updated date.
    ///
    /// Same as JavaScript's `Date.prototype.setMilliseconds()`.
    pub fn set_milliseconds<T>(&self, value: T, context: &mut Context) -> JsResult<JsValue>
    where
        T: Into<JsValue>,
    {
        Date::set_milliseconds::<true>(&self.inner.clone().into(), &[value.into()], context)
    }

    /// Sets the minutes for a specified date according to local time.
    /// Takes [`minutes_value`, `seconds_value`, `ms_value`]
    /// Return a `Number` representing the milliseconds elapsed between
    /// the UNIX epoch and the updated date.
    ///
    /// Same as JavaScript's `Date.prototype.setMinutes()`.
    #[inline]
    pub fn set_minutes(&self, values: &[JsValue], context: &mut Context) -> JsResult<JsValue> {
        Date::set_minutes::<true>(&self.inner.clone().into(), values, context)
    }

    /// Sets the month for a specified date according to local time.
    /// Takes [`month_value`, `day_value`]
    /// Return a `Number` representing the milliseconds elapsed between
    /// the UNIX epoch and the updated date.
    ///
    /// Same as JavaScript's `Date.prototype.setMonth()`.
    #[inline]
    pub fn set_month(&self, values: &[JsValue], context: &mut Context) -> JsResult<JsValue> {
        Date::set_month::<true>(&self.inner.clone().into(), values, context)
    }

    /// Sets the seconds for a specified date according to local time.
    /// Takes [`seconds_value`, `ms_value`]
    /// Return a `Number` representing the milliseconds elapsed between
    /// the UNIX epoch and the updated date.
    ///
    /// Same as JavaScript's `Date.prototype.setSeconds()`.
    #[inline]
    pub fn set_seconds(&self, values: &[JsValue], context: &mut Context) -> JsResult<JsValue> {
        Date::set_seconds::<true>(&self.inner.clone().into(), values, context)
    }

    /// Sets the Date object to the time represented by a number
    /// of milliseconds since UNIX epoch.
    /// Takes number of milliseconds since UNIX epoch.
    /// Use negative numbers for times prior.
    /// Return a `Number` representing the milliseconds elapsed between
    /// the UNIX epoch and the updated date.
    ///
    /// Same as JavaScript's `Date.prototype.setTime()`.
    pub fn set_time<T>(&self, value: T, context: &mut Context) -> JsResult<JsValue>
    where
        T: Into<JsValue>,
    {
        Date::set_time(&self.inner.clone().into(), &[value.into()], context)
    }

    /// Sets the day of the month for a specified date according
    /// to universal time.
    /// Takes a `month_value`.
    /// Return a `Number` representing the milliseconds elapsed between
    /// the UNIX epoch and the updated date.
    ///
    /// Same as JavaScript's `Date.prototype.setUTCDate()`.
    pub fn set_utc_date<T>(&self, value: T, context: &mut Context) -> JsResult<JsValue>
    where
        T: Into<JsValue>,
    {
        Date::set_date::<false>(&self.inner.clone().into(), &[value.into()], context)
    }

    /// Sets the full year (e.g. 4 digits for 4-digit years) for a
    /// specified date according to universal time.
    /// Takes [`year_value`, `month_value`, `date_value`]
    /// Return a `Number` representing the milliseconds elapsed between
    /// the UNIX epoch and the updated date.
    ///
    /// Same as JavaScript's `Date.prototype.setUTCFullYear()`.
    #[inline]
    pub fn set_utc_full_year(
        &self,
        values: &[JsValue],
        context: &mut Context,
    ) -> JsResult<JsValue> {
        Date::set_full_year::<false>(&self.inner.clone().into(), values, context)
    }

    /// Sets the hours for a specified date according to universal time.
    /// Takes [`hours_value`, `minutes_value`, `seconds_value`, `ms_value`]
    /// Return a `Number` representing the milliseconds elapsed between
    /// the UNIX epoch and the updated dated.
    ///
    /// Same as JavaScript's `Date.prototype.setUTCHours()`.
    #[inline]
    pub fn set_utc_hours(&self, values: &[JsValue], context: &mut Context) -> JsResult<JsValue> {
        Date::set_hours::<false>(&self.inner.clone().into(), values, context)
    }

    /// Sets the milliseconds for a specified date according to universal time.
    /// Takes a `milliseconds_value`
    /// Return a `Number` representing the milliseconds elapsed between
    /// the UNIX epoch and the updated date.
    ///
    /// Same as JavaScript's `Date.prototype.setUTCMilliseconds()`.
    pub fn set_utc_milliseconds<T>(&self, value: T, context: &mut Context) -> JsResult<JsValue>
    where
        T: Into<JsValue>,
    {
        Date::set_milliseconds::<false>(&self.inner.clone().into(), &[value.into()], context)
    }

    /// Sets the minutes for a specified date according to universal time.
    /// Takes [`minutes_value`, `seconds_value`, `ms_value`]
    /// Return a `Number` representing the milliseconds elapsed between
    /// the UNIX epoch and the updated date.
    ///
    /// Same as JavaScript's `Date.prototype.setUTCMinutes()`.
    #[inline]
    pub fn set_utc_minutes(&self, values: &[JsValue], context: &mut Context) -> JsResult<JsValue> {
        Date::set_minutes::<false>(&self.inner.clone().into(), values, context)
    }

    /// Sets the month for a specified date according to universal time.
    /// Takes [`month_value`, `day_value`]
    /// Return a `Number` representing the milliseconds elapsed between
    /// the UNIX epoch and the updated date.
    ///
    /// Same as JavaScript's `Date.prototype.setUTCMonth()`.
    #[inline]
    pub fn set_utc_month(&self, values: &[JsValue], context: &mut Context) -> JsResult<JsValue> {
        Date::set_month::<false>(&self.inner.clone().into(), values, context)
    }

    /// Sets the seconds for a specified date according to universal time.
    /// Takes [`seconds_value`, `ms_value`]
    /// Return a `Number` representing the milliseconds elapsed between
    /// the UNIX epoch and the updated date.
    ///
    /// Same as JavaScript's `Date.prototype.setUTCSeconds()`.
    #[inline]
    pub fn set_utc_seconds(&self, values: &[JsValue], context: &mut Context) -> JsResult<JsValue> {
        Date::set_seconds::<false>(&self.inner.clone().into(), values, context)
    }

    /// Returns the "date" portion of the Date as a human-readable string.
    ///
    /// Same as JavaScript's `Date.prototype.toDateString()`.
    #[inline]
    pub fn to_date_string(&self, context: &mut Context) -> JsResult<JsValue> {
        Date::to_date_string(&self.inner.clone().into(), &[JsValue::Null], context)
    }

    /// DEPRECATED: This feature is no longer recommended.
    /// USE: `to_utc_string()` instead.
    /// Returns a string representing the Date based on the GMT timezone.
    ///
    /// Same as JavaScript's legacy `Date.prototype.toGMTString()`
    #[deprecated]
    #[inline]
    pub fn to_gmt_string(&self, context: &mut Context) -> JsResult<JsValue> {
        Date::to_utc_string(&self.inner.clone().into(), &[JsValue::Null], context)
    }

    /// Returns the given date in the ISO 8601 format according to universal
    /// time.
    ///
    /// Same as JavaScript's `Date.prototype.toISOString()`.
    #[inline]
    pub fn to_iso_string(&self, context: &mut Context) -> JsResult<JsValue> {
        Date::to_iso_string(&self.inner.clone().into(), &[JsValue::Null], context)
    }

    /// Returns a string representing the Date using `to_iso_string()`.
    ///
    /// Same as JavaScript's `Date.prototype.toJSON()`.
    #[inline]
    pub fn to_json(&self, context: &mut Context) -> JsResult<JsValue> {
        Date::to_json(&self.inner.clone().into(), &[JsValue::Null], context)
    }

    /// Returns a string representing the date portion of the given Date instance
    /// according to language-specific conventions.
    /// Takes [locales, options]
    ///
    /// Same as JavaScript's `Date.prototype.toLocaleDateString()`.
    #[inline]
    pub fn to_local_date_string(
        &self,
        values: &[JsValue],
        context: &mut Context,
    ) -> JsResult<JsValue> {
        Date::to_locale_date_string(&self.inner.clone().into(), values, context)
    }

    /// Returns a string representing the given date according to language-specific conventions.
    /// Takes [locales, options]
    ///
    /// Same as JavaScript's `Date.prototype.toLocaleDateString()`.
    #[inline]
    pub fn to_locale_string(&self, values: &[JsValue], context: &mut Context) -> JsResult<JsValue> {
        Date::to_locale_string(&self.inner.clone().into(), values, context)
    }

    /// Returns the "time" portion of the Date as human-readable string.
    ///
    /// Same as JavaScript's `Date.prototype.toTimeString()`.
    #[inline]
    pub fn to_locale_time_string(
        &self,
        values: &[JsValue],
        context: &mut Context,
    ) -> JsResult<JsValue> {
        Date::to_locale_time_string(&self.inner.clone().into(), values, context)
    }

    /// Returns a string representing the specified Date object.
    ///
    /// Same as JavaScript's `Date.prototype.toString()`.
    #[inline]
    pub fn to_string(&self, context: &mut Context) -> JsResult<JsValue> {
        Date::to_string(&self.inner.clone().into(), &[JsValue::Null], context)
    }

    /// Returns the "time" portion of the Date as human-readable string.
    ///
    /// Same as JavaScript's `Date.prototype.toTimeString()`.
    #[inline]
    pub fn to_time_string(&self, context: &mut Context) -> JsResult<JsValue> {
        Date::to_time_string(&self.inner.clone().into(), &[JsValue::Null], context)
    }

    /// Returns a string representing the given date using the UTC time zone.
    ///
    /// Same as JavaScript's `Date.prototype.toUTCString()`.
    #[inline]
    pub fn to_utc_string(&self, context: &mut Context) -> JsResult<JsValue> {
        Date::to_utc_string(&self.inner.clone().into(), &[JsValue::Null], context)
    }

    /// Returns the primitive value pf Date object.
    ///
    /// Same as JavaScript's `Date.prototype.valueOf()`.
    #[inline]
    pub fn value_of(&self, context: &mut Context) -> JsResult<JsValue> {
        Date::value_of(&self.inner.clone().into(), &[JsValue::Null], context)
    }

    /// Utility create a `Date` object from RFC3339 string
    pub fn new_from_parse(value: &JsValue, context: &mut Context) -> JsResult<Self> {
        let prototype = context.intrinsics().constructors().date().prototype();
        let string = value
            .to_string(context)?
            .to_std_string()
            .map_err(|_| JsNativeError::typ().with_message("unpaired surrogate on date string"))?;
        let t = OffsetDateTime::parse(&string, &Rfc3339)
            .map_err(|err| JsNativeError::typ().with_message(err.to_string()))?;
        let date_time = Date::new((t.unix_timestamp() * 1000 + i64::from(t.millisecond())) as f64);

        Ok(Self {
            inner: JsObject::from_proto_and_data_with_shared_shape(
                context.root_shape(),
                prototype,
                date_time,
            ),
        })
    }
}

impl From<JsDate> for JsObject {
    #[inline]
    fn from(o: JsDate) -> Self {
        o.inner.clone()
    }
}

impl From<JsDate> for JsValue {
    #[inline]
    fn from(o: JsDate) -> Self {
        o.inner.clone().into()
    }
}

impl Deref for JsDate {
    type Target = JsObject;

    #[inline]
    fn deref(&self) -> &Self::Target {
        &self.inner
    }
}

impl JsObjectType for JsDate {}

impl TryFromJs for JsDate {
    fn try_from_js(value: &JsValue, _context: &mut Context) -> JsResult<Self> {
        match value {
            JsValue::Object(o) => Self::from_object(o.clone()),
            _ => Err(JsNativeError::typ()
                .with_message("value is not a Date object")
                .into()),
        }
    }
}