nodex_api/value/
date.rs

1use crate::{api, prelude::*};
2use std::{mem::MaybeUninit, os::raw::c_char};
3
4#[derive(Copy, Clone, Debug)]
5pub struct JsDate(pub(crate) JsValue);
6
7impl JsDate {
8    pub(crate) fn from_value(value: JsValue) -> JsDate {
9        JsDate(value)
10    }
11
12    #[cfg(feature = "v5")]
13    /// This API does not observe leap seconds; they are ignored, as ECMAScript aligns with POSIX time specification.
14    /// This API allocates a JavaScript Date object.
15    /// JavaScript Date objects are described in Section 20.3 of the ECMAScript Language Specification.
16    pub fn new(env: NapiEnv, time: f64) -> NapiResult<JsDate> {
17        let value = napi_call!(=napi_create_date, env, time);
18        Ok(JsDate::from_raw(env, value))
19    }
20
21    #[cfg(feature = "v5")]
22    /// This API does not observe leap seconds; they are ignored, as ECMAScript aligns with POSIX time specification.
23    /// Returns napi_ok if the API succeeded. If a non-date napi_value is passed in it returns napi_date_expected.
24    /// This API returns the C double primitive of time value for the given JavaScript Date.
25    pub fn get(&self) -> NapiResult<f64> {
26        Ok(napi_call!(=napi_get_date_value, self.env(), self.raw()))
27    }
28}
29
30napi_value_t!(JsDate);
31
32impl NapiValueCheck for JsDate {
33    #[cfg(feature = "v5")]
34    fn check(&self) -> NapiResult<bool> {
35        Ok(napi_call!(=napi_is_date, self.env(), self.raw()))
36    }
37
38    #[cfg(not(feature = "v5"))]
39    fn check(&self) -> NapiResult<bool> {
40        Err(NapiStatus::GenericFailure)
41    }
42}