From 5ce424e57b94381bd9908b2a71b377a2b5708672 Mon Sep 17 00:00:00 2001 From: "T. Nagasawa" Date: Wed, 11 Jul 2018 23:27:59 +0900 Subject: [PATCH] bindings for Date.prototype.setUTCXXX() (#456) --- src/js.rs | 46 ++++++ tests/all/js_globals/Date.rs | 273 +++++++++++++++++++++++++++++++++++ 2 files changed, 319 insertions(+) diff --git a/src/js.rs b/src/js.rs index 24789b66..208ffe87 100644 --- a/src/js.rs +++ b/src/js.rs @@ -1082,6 +1082,52 @@ extern "C" { #[wasm_bindgen(method, js_name = setTime)] pub fn set_time(this: &Date, time: f64) -> f64; + /// The setUTCDate() method sets the day of the month for a specified date + /// according to universal time. + /// + /// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/setUTCDate + #[wasm_bindgen(method, js_name = setUTCDate)] + pub fn set_utc_date(this: &Date, day: u32) -> f64; + + /// The setUTCFullYear() method sets the full year for a specified date according to universal time. + /// + /// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/setUTCFullYear + #[wasm_bindgen(method, js_name = setUTCFullYear)] + pub fn set_utc_full_year(this: &Date, year: u32) -> f64; + + /// The setUTCHours() method sets the hour for a specified date according to universal time, + /// and returns the number of milliseconds since January 1, 1970 00:00:00 UTC until the time + /// represented by the updated Date instance. + /// + /// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/setUTCHours + #[wasm_bindgen(method, js_name = setUTCHours)] + pub fn set_utc_hours(this: &Date, hours: u32) -> f64; + + /// The setUTCMilliseconds() method sets the milliseconds for a specified date + /// according to universal time. + /// + /// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/setUTCMilliseconds + #[wasm_bindgen(method, js_name = setUTCMilliseconds)] + pub fn set_utc_milliseconds(this: &Date, milliseconds: u32) -> f64; + + /// The setUTCMinutes() method sets the minutes for a specified date according to universal time. + /// + /// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/setUTCMinutes + #[wasm_bindgen(method, js_name = setUTCMinutes)] + pub fn set_utc_minutes(this: &Date, minutes: u32) -> f64; + + /// The setUTCMonth() method sets the month for a specified date according to universal time. + /// + /// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/setUTCMonth + #[wasm_bindgen(method, js_name = setUTCMonth)] + pub fn set_utc_month(this: &Date, month: u32) -> f64; + + /// The setUTCSeconds() method sets the seconds for a specified date according to universal time. + /// + /// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/setUTCSeconds + #[wasm_bindgen(method, js_name = setUTCSeconds)] + pub fn set_utc_seconds(this: &Date, seconds: u32) -> f64; + /// The toDateString() method returns the date portion of a Date object /// in human readable form in American English. /// diff --git a/tests/all/js_globals/Date.rs b/tests/all/js_globals/Date.rs index 651ea1fd..8e2bbd78 100755 --- a/tests/all/js_globals/Date.rs +++ b/tests/all/js_globals/Date.rs @@ -1035,6 +1035,279 @@ fn set_time() { .test() } +#[test] +fn set_utc_date() { + 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 set_utc_date(this: &Date, day: u32) -> f64 { + this.set_utc_date(day) + } + "#, + ) + .file( + "test.js", + r#" + import * as assert from "assert"; + import * as wasm from "./out"; + + export function test() { + let event1 = new Date('August 19, 1975 23:15:30 GMT-3:00'); + let event2 = new Date('August 19, 1975 02:15:30 GMT'); + + let eventMsFromUnixEpoch = wasm.set_utc_date(event1, 19); + + assert.equal(eventMsFromUnixEpoch, event2.getTime()); + assert.equal(event1.getTime(), event2.valueOf()); + assert.equal(event1.getUTCDate(), 19); + } + "#, + ) + .test() +} + +#[test] +fn set_utc_full_year() { + 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 set_utc_full_year(this: &Date, year: u32) -> f64 { + this.set_utc_full_year(year) + } + "#, + ) + .file( + "test.js", + r#" + import * as assert from "assert"; + import * as wasm from "./out"; + + export function test() { + let event1 = new Date('December 31, 1975 23:15:30 GMT-3:00'); + let event2 = new Date('January 01, 1975 02:15:30 GMT'); + + let eventMsFromUnixEpoch = wasm.set_utc_full_year(event1, 1975); + + assert.equal(eventMsFromUnixEpoch, event2.getTime()); + assert.equal(event1.getTime(), event2.valueOf()); + assert.equal(event1.getUTCFullYear(), 1975); + } + "#, + ) + .test() +} + +#[test] +fn set_utc_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 set_utc_hours(this: &Date, hours: u32) -> f64 { + this.set_utc_hours(hours) + } + "#, + ) + .file( + "test.js", + r#" + import * as assert from "assert"; + import * as wasm from "./out"; + + export function test() { + let event1 = new Date('August 19, 1975 23:15:30 GMT-3:00'); + let event2 = new Date('August 20, 1975 23:15:30 GMT'); + + let eventMsFromUnixEpoch = wasm.set_utc_hours(event1, 23); + + assert.equal(eventMsFromUnixEpoch, event2.getTime()); + assert.equal(event1.getTime(), event2.valueOf()); + assert.equal(event1.getUTCHours(), 23); + } + "#, + ) + .test() +} + +#[test] +fn set_utc_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 set_utc_milliseconds(this: &Date, milliseconds: u32) -> f64 { + this.set_utc_milliseconds(milliseconds) + } + "#, + ) + .file( + "test.js", + r#" + import * as assert from "assert"; + import * as wasm from "./out"; + + export function test() { + let event1 = new Date('August 19, 1975 23:15:30 GMT-3:00'); + let event2 = new Date('August 20, 1975 02:15:30.420Z GMT'); + + let eventMsFromUnixEpoch = wasm.set_utc_milliseconds(event1, 420); + + assert.equal(eventMsFromUnixEpoch, event2.getTime()); + assert.equal(event1.getTime(), event2.valueOf()); + assert.equal(event1.getUTCMilliseconds(), 420); + } + "#, + ) + .test() +} + +#[test] +fn set_utc_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 set_utc_minutes(this: &Date, minutes: u32) -> f64 { + this.set_utc_minutes(minutes) + } + "#, + ) + .file( + "test.js", + r#" + import * as assert from "assert"; + import * as wasm from "./out"; + + export function test() { + let event1 = new Date('December 31, 1975, 23:15:30 GMT-3:00'); + let event2 = new Date('January 01, 1976 02:25:30 GMT'); + + let eventMsFromUnixEpoch = wasm.set_utc_minutes(event1, 25); + + assert.equal(eventMsFromUnixEpoch, event2.getTime()); + assert.equal(event1.getTime(), event2.valueOf()); + assert.equal(event1.getUTCMinutes(), 25); + } + "#, + ) + .test() +} + +#[test] +fn set_utc_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 set_utc_month(this: &Date, minutes: u32) -> f64 { + this.set_utc_month(minutes) + } + "#, + ) + .file( + "test.js", + r#" + import * as assert from "assert"; + import * as wasm from "./out"; + + export function test() { + let event1 = new Date('December 31, 1975 23:15:30 GMT-3:00'); + let event2 = new Date('December 01, 1976 02:15:30 GMT'); + + let eventMsFromUnixEpoch = wasm.set_utc_month(event1, 11); + + assert.equal(eventMsFromUnixEpoch, event2.getTime()); + assert.equal(event1.getTime(), event2.valueOf()); + assert.equal(event1.getUTCMonth(), 11); + } + "#, + ) + .test() +} + +#[test] +fn set_utc_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 set_utc_seconds(this: &Date, seconds: u32) -> f64 { + this.set_utc_seconds(seconds) + } + "#, + ) + .file( + "test.js", + r#" + import * as assert from "assert"; + import * as wasm from "./out"; + + export function test() { + let event1 = new Date('December 31, 1975 23:15:30 GMT-3:00'); + let event2 = new Date('January 01, 1976 02:15:39 GMT'); + + let eventMsFromUnixEpoch = wasm.set_utc_seconds(event1, 39); + + assert.equal(eventMsFromUnixEpoch, event2.getTime()); + assert.equal(event1.getTime(), event2.valueOf()); + assert.equal(event1.getUTCSeconds(), 39); + } + "#, + ) + .test() +} + #[test] fn to_date_string() { project()