mirror of
https://github.com/rustwasm/wasm-bindgen.git
synced 2024-12-25 11:02:11 +03:00
bindings for Date.parse and Date.getXXX (#414)
* binding for Date.prototype.getHours() * binding for Date.prototype.getMilliseconds() * binding for Date.prototype.getMinutes() * binding for Date.prototype.getMonth() * binding for Date.prototype.getSeconds() * binding for Date.prototype.getTime() * binding for Date.parse()
This commit is contained in:
parent
b790856c6e
commit
056b45aeed
46
src/js.rs
46
src/js.rs
@ -828,6 +828,44 @@ extern "C" {
|
||||
#[wasm_bindgen(method, js_name = getFullYear)]
|
||||
pub fn get_full_year(this: &Date) -> u32;
|
||||
|
||||
/// The getHours() method returns the hour for the specified date, according to local time.
|
||||
///
|
||||
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getHours
|
||||
#[wasm_bindgen(method, js_name = getHours)]
|
||||
pub fn get_hours(this: &Date) -> u32;
|
||||
|
||||
/// The getMilliseconds() method returns the milliseconds in the specified date according to local time.
|
||||
///
|
||||
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getMilliseconds
|
||||
#[wasm_bindgen(method, js_name = getMilliseconds)]
|
||||
pub fn get_milliseconds(this: &Date) -> u32;
|
||||
|
||||
/// The getMinutes() method returns the minutes in the specified date according to local time.
|
||||
///
|
||||
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getMinutes
|
||||
#[wasm_bindgen(method, js_name = getMinutes)]
|
||||
pub fn get_minutes(this: &Date) -> u32;
|
||||
|
||||
/// The getMonth() method returns the month in the specified date according to local time,
|
||||
/// as a zero-based value (where zero indicates the first month of the year).
|
||||
///
|
||||
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getMonth
|
||||
#[wasm_bindgen(method, js_name = getMonth)]
|
||||
pub fn get_month(this: &Date) -> u32;
|
||||
|
||||
/// The getSeconds() method returns the seconds in the specified date according to local time.
|
||||
///
|
||||
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getSeconds
|
||||
#[wasm_bindgen(method, js_name = getSeconds)]
|
||||
pub fn get_seconds(this: &Date) -> u32;
|
||||
|
||||
/// The getTime() method returns the numeric value corresponding to the time for the specified date
|
||||
/// according to universal time.
|
||||
///
|
||||
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getTime
|
||||
#[wasm_bindgen(method, js_name = getTime)]
|
||||
pub fn get_time(this: &Date) -> f64;
|
||||
|
||||
/// Creates a JavaScript Date instance that represents
|
||||
/// a single moment in time. Date objects are based on a time value that is
|
||||
/// the number of milliseconds since 1 January 1970 UTC.
|
||||
@ -843,6 +881,14 @@ extern "C" {
|
||||
#[wasm_bindgen(static_method_of = Date)]
|
||||
pub fn now() -> f64;
|
||||
|
||||
/// The Date.parse() method parses a string representation of a date, and returns the number of milliseconds
|
||||
/// since January 1, 1970, 00:00:00 UTC or NaN if the string is unrecognized or, in some cases,
|
||||
/// contains illegal date values (e.g. 2015-02-31).
|
||||
///
|
||||
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/parse
|
||||
#[wasm_bindgen(static_method_of = Date)]
|
||||
pub fn parse(date: JsString) -> f64;
|
||||
|
||||
/// The toDateString() method returns the date portion of a Date object
|
||||
/// in human readable form in American English.
|
||||
///
|
||||
|
@ -106,6 +106,212 @@ fn get_full_year() {
|
||||
.test()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn get_hours() {
|
||||
project()
|
||||
.file(
|
||||
"src/lib.rs",
|
||||
r#"
|
||||
#![feature(proc_macro, wasm_custom_section)]
|
||||
|
||||
extern crate wasm_bindgen;
|
||||
use wasm_bindgen::prelude::*;
|
||||
use wasm_bindgen::js::Date;
|
||||
|
||||
#[wasm_bindgen]
|
||||
pub fn get_hours(this: &Date) -> u32 {
|
||||
this.get_hours()
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.file(
|
||||
"test.js",
|
||||
r#"
|
||||
import * as assert from "assert";
|
||||
import * as wasm from "./out";
|
||||
|
||||
export function test() {
|
||||
let date = new Date('March 13, 08 04:20');
|
||||
|
||||
assert.equal(wasm.get_hours(date), 4);
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.test()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn get_milliseconds() {
|
||||
project()
|
||||
.file(
|
||||
"src/lib.rs",
|
||||
r#"
|
||||
#![feature(proc_macro, wasm_custom_section)]
|
||||
|
||||
extern crate wasm_bindgen;
|
||||
use wasm_bindgen::prelude::*;
|
||||
use wasm_bindgen::js::Date;
|
||||
|
||||
#[wasm_bindgen]
|
||||
pub fn get_milliseconds(this: &Date) -> u32 {
|
||||
this.get_milliseconds()
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.file(
|
||||
"test.js",
|
||||
r#"
|
||||
import * as assert from "assert";
|
||||
import * as wasm from "./out";
|
||||
|
||||
export function test() {
|
||||
let date = new Date('July 20, 69 00:20:18');
|
||||
let dateWithMs = new Date('July 20, 69 00:20:18:123');
|
||||
|
||||
assert.equal(wasm.get_milliseconds(date), 0);
|
||||
assert.equal(wasm.get_milliseconds(dateWithMs), 123);
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.test()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn get_minutes() {
|
||||
project()
|
||||
.file(
|
||||
"src/lib.rs",
|
||||
r#"
|
||||
#![feature(proc_macro, wasm_custom_section)]
|
||||
|
||||
extern crate wasm_bindgen;
|
||||
use wasm_bindgen::prelude::*;
|
||||
use wasm_bindgen::js::Date;
|
||||
|
||||
#[wasm_bindgen]
|
||||
pub fn get_minutes(this: &Date) -> u32 {
|
||||
this.get_minutes()
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.file(
|
||||
"test.js",
|
||||
r#"
|
||||
import * as assert from "assert";
|
||||
import * as wasm from "./out";
|
||||
|
||||
export function test() {
|
||||
let date = new Date('March 13, 08 04:20');
|
||||
|
||||
assert.equal(wasm.get_minutes(date), 20);
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.test()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn get_month() {
|
||||
project()
|
||||
.file(
|
||||
"src/lib.rs",
|
||||
r#"
|
||||
#![feature(proc_macro, wasm_custom_section)]
|
||||
|
||||
extern crate wasm_bindgen;
|
||||
use wasm_bindgen::prelude::*;
|
||||
use wasm_bindgen::js::Date;
|
||||
|
||||
#[wasm_bindgen]
|
||||
pub fn get_month(this: &Date) -> u32 {
|
||||
this.get_month()
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.file(
|
||||
"test.js",
|
||||
r#"
|
||||
import * as assert from "assert";
|
||||
import * as wasm from "./out";
|
||||
|
||||
export function test() {
|
||||
let date = new Date('July 20, 69 00:20:18');
|
||||
|
||||
assert.equal(wasm.get_month(date), 6);
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.test()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn get_seconds() {
|
||||
project()
|
||||
.file(
|
||||
"src/lib.rs",
|
||||
r#"
|
||||
#![feature(proc_macro, wasm_custom_section)]
|
||||
|
||||
extern crate wasm_bindgen;
|
||||
use wasm_bindgen::prelude::*;
|
||||
use wasm_bindgen::js::Date;
|
||||
|
||||
#[wasm_bindgen]
|
||||
pub fn get_seconds(this: &Date) -> u32 {
|
||||
this.get_seconds()
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.file(
|
||||
"test.js",
|
||||
r#"
|
||||
import * as assert from "assert";
|
||||
import * as wasm from "./out";
|
||||
|
||||
export function test() {
|
||||
let date = new Date('July 20, 69 00:20:18');
|
||||
|
||||
assert.equal(wasm.get_seconds(date), 18);
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.test()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn get_time() {
|
||||
project()
|
||||
.file(
|
||||
"src/lib.rs",
|
||||
r#"
|
||||
#![feature(proc_macro, wasm_custom_section)]
|
||||
|
||||
extern crate wasm_bindgen;
|
||||
use wasm_bindgen::prelude::*;
|
||||
use wasm_bindgen::js::Date;
|
||||
|
||||
#[wasm_bindgen]
|
||||
pub fn get_time(this: &Date) -> f64 {
|
||||
this.get_time()
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.file(
|
||||
"test.js",
|
||||
r#"
|
||||
import * as assert from "assert";
|
||||
import * as wasm from "./out";
|
||||
|
||||
export function test() {
|
||||
let date = new Date('July 20, 69 00:20:18 GMT+00:00');
|
||||
|
||||
assert.equal(wasm.get_time(date), -14254782000);
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.test()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn new() {
|
||||
project()
|
||||
@ -164,6 +370,42 @@ fn now() {
|
||||
.test()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse() {
|
||||
project()
|
||||
.file(
|
||||
"src/lib.rs",
|
||||
r#"
|
||||
#![feature(proc_macro, wasm_custom_section)]
|
||||
|
||||
extern crate wasm_bindgen;
|
||||
use wasm_bindgen::prelude::*;
|
||||
use wasm_bindgen::js::{Date, JsString};
|
||||
|
||||
#[wasm_bindgen]
|
||||
pub fn parse(date: JsString) -> f64 {
|
||||
Date::parse(date)
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.file(
|
||||
"test.js",
|
||||
r#"
|
||||
import * as assert from "assert";
|
||||
import * as wasm from "./out";
|
||||
|
||||
export function test() {
|
||||
let date = wasm.parse('04 Dec 1995 00:12:00 GMT');
|
||||
let unixTimeZero = wasm.parse('01 Jan 1970 00:00:00 GMT');
|
||||
|
||||
assert.equal(date, 818035920000);
|
||||
assert.equal(unixTimeZero, 0);
|
||||
}
|
||||
"#,
|
||||
)
|
||||
.test()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn to_date_string() {
|
||||
project()
|
||||
|
Loading…
Reference in New Issue
Block a user