napi-rs/examples/napi/src/date.rs
LongYinan 4719caa643
feat(napi): support Return generic of ThreadsafeFunction (#1997)
* feat(napi): support to use tuple with either (#1993)

`Either` uses `ValidateNapiValue` + `TypeName` to validate and report error on value not being matched. So there's no way to remove these super traits from it. So I implemented these types to `Tuple` types.

* feat(napi): support `Return` generic of ThreadsafeFunction

* depracate JsFunction

* CalleeHandled tsfn should handle Result in callback

* Pass env to call_with_return_value callback

* Fix compile

* clippy fix

* Fix electron test

* Function args

---------

Co-authored-by: Hana <andywangsy@gmail.com>
2024-03-20 21:37:08 +08:00

35 lines
798 B
Rust

use std::str::FromStr;
use chrono::{Duration, Utc};
use napi::bindgen_prelude::*;
#[napi]
fn date_to_number(input: Date) -> Result<f64> {
input.value_of()
}
#[napi]
fn chrono_date_to_millis(input: chrono::DateTime<Utc>) -> i64 {
input.timestamp_millis()
}
#[napi]
fn chrono_date_add_1_minute(input: chrono::DateTime<Utc>) -> chrono::DateTime<Utc> {
Duration::try_minutes(1).map(|d| input + d).unwrap()
}
#[napi(object)]
pub struct Dates {
pub start: chrono::DateTime<Utc>,
pub end: Option<chrono::DateTime<Utc>>,
}
#[napi]
pub fn chrono_native_date_time(date: chrono::NaiveDateTime) -> i64 {
date.and_utc().timestamp_millis()
}
#[napi]
pub fn chrono_native_date_time_return() -> Option<chrono::NaiveDateTime> {
chrono::NaiveDateTime::from_str("2016-12-23T15:25:59.325").ok()
}