napi-rs/examples/napi-compat-mode/src/napi5/date.rs

22 lines
608 B
Rust
Raw Normal View History

2020-10-10 15:50:35 +09:00
use std::convert::TryInto;
use napi::{CallContext, JsBoolean, JsDate, JsNumber, JsUnknown, Result};
#[js_function(1)]
pub fn test_object_is_date(ctx: CallContext) -> Result<JsBoolean> {
let obj = ctx.get::<JsUnknown>(0)?;
ctx.env.get_boolean(obj.is_date()?)
}
2020-10-10 15:50:35 +09:00
#[js_function(1)]
pub fn test_create_date(ctx: CallContext) -> Result<JsDate> {
let timestamp: f64 = ctx.get::<JsNumber>(0)?.try_into()?;
ctx.env.create_date(timestamp)
}
#[js_function(1)]
pub fn test_get_date_value(ctx: CallContext) -> Result<JsNumber> {
let date = ctx.get::<JsDate>(0)?;
ctx.env.create_double(date.value_of()?)
}