Skip to content

Commit c185897

Browse files
committed
binding for Date.prototype.setSeconds()
1 parent 609d457 commit c185897

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

src/js.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1060,6 +1060,12 @@ extern "C" {
10601060
#[wasm_bindgen(method, js_name = setMonth)]
10611061
pub fn set_month(this: &Date, month: u32) -> f64;
10621062

1063+
/// The setSeconds() method sets the seconds for a specified date according to local time.
1064+
///
1065+
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/setSeconds
1066+
#[wasm_bindgen(method, js_name = setSeconds)]
1067+
pub fn set_seconds(this: &Date, seconds: u32) -> f64;
1068+
10631069
/// The toDateString() method returns the date portion of a Date object
10641070
/// in human readable form in American English.
10651071
///

tests/all/js_globals/Date.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -958,6 +958,45 @@ fn set_month() {
958958
.test()
959959
}
960960

961+
#[test]
962+
fn set_seconds() {
963+
project()
964+
.file(
965+
"src/lib.rs",
966+
r#"
967+
#![feature(proc_macro, wasm_custom_section)]
968+
969+
extern crate wasm_bindgen;
970+
use wasm_bindgen::prelude::*;
971+
use wasm_bindgen::js::Date;
972+
973+
#[wasm_bindgen]
974+
pub fn set_seconds(this: &Date, seconds: u32) -> f64 {
975+
this.set_seconds(seconds)
976+
}
977+
"#,
978+
)
979+
.file(
980+
"test.js",
981+
r#"
982+
import * as assert from "assert";
983+
import * as wasm from "./out";
984+
985+
export function test() {
986+
let event1 = new Date('August 19, 1975 23:15:30');
987+
let event2 = new Date('August 19, 1975 23:15:42');
988+
989+
let eventMsFromUnixEpoch = wasm.set_seconds(event1, 42);
990+
991+
assert.equal(eventMsFromUnixEpoch, 177689742000);
992+
assert.equal(event1.getTime(), event2.valueOf());
993+
assert.equal(event1.getSeconds(), 42);
994+
}
995+
"#,
996+
)
997+
.test()
998+
}
999+
9611000
#[test]
9621001
fn to_date_string() {
9631002
project()

0 commit comments

Comments
 (0)