Skip to content

Commit 7200d36

Browse files
authored
Merge pull request #438 from toVersus/js_date
bindings for Date.setXXX
2 parents 8be0a98 + d2feddc commit 7200d36

File tree

2 files changed

+361
-0
lines changed

2 files changed

+361
-0
lines changed

src/js.rs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1020,6 +1020,58 @@ extern "C" {
10201020
#[wasm_bindgen(static_method_of = Date)]
10211021
pub fn parse(date: JsString) -> f64;
10221022

1023+
/// The setDate() method sets the day of the Date object relative to the beginning of the currently set month.
1024+
///
1025+
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/setDate
1026+
#[wasm_bindgen(method, js_name = setDate)]
1027+
pub fn set_date(this: &Date, day: u32) -> f64;
1028+
1029+
/// The setFullYear() method sets the full year for a specified date according to local time.
1030+
/// Returns new timestamp.
1031+
///
1032+
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/setFullYear
1033+
#[wasm_bindgen(method, js_name = setFullYear)]
1034+
pub fn set_full_year(this: &Date, year: u32) -> f64;
1035+
1036+
/// The setHours() method sets the hours for a specified date according to local time,
1037+
/// and returns the number of milliseconds since January 1, 1970 00:00:00 UTC until the time represented
1038+
/// by the updated Date instance.
1039+
///
1040+
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/setHours
1041+
#[wasm_bindgen(method, js_name = setHours)]
1042+
pub fn set_hours(this: &Date, hours: u32) -> f64;
1043+
1044+
/// The setMilliseconds() method sets the milliseconds for a specified date according to local time.
1045+
///
1046+
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/setMilliseconds
1047+
#[wasm_bindgen(method, js_name = setMilliseconds)]
1048+
pub fn set_milliseconds(this: &Date, milliseconds: u32) -> f64;
1049+
1050+
/// The setMinutes() method sets the minutes for a specified date according to local time.
1051+
///
1052+
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/setMinutes
1053+
#[wasm_bindgen(method, js_name = setMinutes)]
1054+
pub fn set_minutes(this: &Date, minutes: u32) -> f64;
1055+
1056+
/// The setMonth() method sets the month for a specified date according to the currently set year.
1057+
///
1058+
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/setMonth
1059+
#[wasm_bindgen(method, js_name = setMonth)]
1060+
pub fn set_month(this: &Date, month: u32) -> f64;
1061+
1062+
/// The setSeconds() method sets the seconds for a specified date according to local time.
1063+
///
1064+
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/setSeconds
1065+
#[wasm_bindgen(method, js_name = setSeconds)]
1066+
pub fn set_seconds(this: &Date, seconds: u32) -> f64;
1067+
1068+
/// The setTime() method sets the Date object to the time represented by a number of milliseconds
1069+
/// since January 1, 1970, 00:00:00 UTC.
1070+
///
1071+
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/setTime
1072+
#[wasm_bindgen(method, js_name = setTime)]
1073+
pub fn set_time(this: &Date, time: f64) -> f64;
1074+
10231075
/// The toDateString() method returns the date portion of a Date object
10241076
/// in human readable form in American English.
10251077
///

tests/all/js_globals/Date.rs

Lines changed: 309 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -726,6 +726,315 @@ fn parse() {
726726
.test()
727727
}
728728

729+
#[test]
730+
fn set_date() {
731+
project()
732+
.file(
733+
"src/lib.rs",
734+
r#"
735+
#![feature(proc_macro, wasm_custom_section)]
736+
737+
extern crate wasm_bindgen;
738+
use wasm_bindgen::prelude::*;
739+
use wasm_bindgen::js::Date;
740+
741+
#[wasm_bindgen]
742+
pub fn set_date(this: &Date, day: u32) -> f64 {
743+
this.set_date(day)
744+
}
745+
"#,
746+
)
747+
.file(
748+
"test.js",
749+
r#"
750+
import * as assert from "assert";
751+
import * as wasm from "./out";
752+
753+
export function test() {
754+
let event1 = new Date('August 19, 1975 23:15:30');
755+
let event2 = new Date('August 24, 1975 23:15:30');
756+
757+
let eventMsFromUnixEpoch = wasm.set_date(event1, 24);
758+
759+
assert.equal(eventMsFromUnixEpoch, event2.getTime());
760+
assert.equal(event1.getTime(), event2.valueOf());
761+
assert.equal(event1.getDate(), 24);
762+
}
763+
"#,
764+
)
765+
.test()
766+
}
767+
768+
#[test]
769+
fn set_full_year() {
770+
project()
771+
.file(
772+
"src/lib.rs",
773+
r#"
774+
#![feature(proc_macro, wasm_custom_section)]
775+
776+
extern crate wasm_bindgen;
777+
use wasm_bindgen::prelude::*;
778+
use wasm_bindgen::js::Date;
779+
780+
#[wasm_bindgen]
781+
pub fn set_full_year(this: &Date, year: u32) -> f64 {
782+
this.set_full_year(year)
783+
}
784+
"#,
785+
)
786+
.file(
787+
"test.js",
788+
r#"
789+
import * as assert from "assert";
790+
import * as wasm from "./out";
791+
792+
export function test() {
793+
let event1 = new Date('August 19, 1975 23:15:30');
794+
let event2 = new Date('August 19, 1976 23:15:30');
795+
796+
let eventMsFromUnixEpoch = wasm.set_full_year(event1, 1976);
797+
798+
assert.equal(eventMsFromUnixEpoch, event2.getTime());
799+
assert.equal(event1.getTime(), event2.valueOf());
800+
assert.equal(event1.getFullYear(), 1976);
801+
}
802+
"#,
803+
)
804+
.test()
805+
}
806+
807+
#[test]
808+
fn set_hours() {
809+
project()
810+
.file(
811+
"src/lib.rs",
812+
r#"
813+
#![feature(proc_macro, wasm_custom_section)]
814+
815+
extern crate wasm_bindgen;
816+
use wasm_bindgen::prelude::*;
817+
use wasm_bindgen::js::Date;
818+
819+
#[wasm_bindgen]
820+
pub fn set_hours(this: &Date, hours: u32) -> f64 {
821+
this.set_hours(hours)
822+
}
823+
"#,
824+
)
825+
.file(
826+
"test.js",
827+
r#"
828+
import * as assert from "assert";
829+
import * as wasm from "./out";
830+
831+
export function test() {
832+
let event1 = new Date('August 19, 1975 23:15:30');
833+
let event2 = new Date('August 19, 1975 20:15:30');
834+
835+
let eventMsFromUnixEpoch = wasm.set_hours(event1, 20);
836+
837+
assert.equal(eventMsFromUnixEpoch, event2.getTime());
838+
assert.equal(event1.getTime(), event2.valueOf());
839+
assert.equal(event1.getHours(), 20);
840+
}
841+
"#,
842+
)
843+
.test()
844+
}
845+
846+
#[test]
847+
fn set_milliseconds() {
848+
project()
849+
.file(
850+
"src/lib.rs",
851+
r#"
852+
#![feature(proc_macro, wasm_custom_section)]
853+
854+
extern crate wasm_bindgen;
855+
use wasm_bindgen::prelude::*;
856+
use wasm_bindgen::js::Date;
857+
858+
#[wasm_bindgen]
859+
pub fn set_milliseconds(this: &Date, milliseconds: u32) -> f64 {
860+
this.set_milliseconds(milliseconds)
861+
}
862+
"#,
863+
)
864+
.file(
865+
"test.js",
866+
r#"
867+
import * as assert from "assert";
868+
import * as wasm from "./out";
869+
870+
export function test() {
871+
let event = new Date('August 19, 1975 23:15:30');
872+
873+
let eventMsFromUnixEpoch = wasm.set_milliseconds(event, 456);
874+
875+
assert.equal(eventMsFromUnixEpoch, event.getTime());
876+
assert.equal(event.getMilliseconds(), 456);
877+
}
878+
"#,
879+
)
880+
.test()
881+
}
882+
883+
#[test]
884+
fn set_minutes() {
885+
project()
886+
.file(
887+
"src/lib.rs",
888+
r#"
889+
#![feature(proc_macro, wasm_custom_section)]
890+
891+
extern crate wasm_bindgen;
892+
use wasm_bindgen::prelude::*;
893+
use wasm_bindgen::js::Date;
894+
895+
#[wasm_bindgen]
896+
pub fn set_minutes(this: &Date, minutes: u32) -> f64 {
897+
this.set_minutes(minutes)
898+
}
899+
"#,
900+
)
901+
.file(
902+
"test.js",
903+
r#"
904+
import * as assert from "assert";
905+
import * as wasm from "./out";
906+
907+
export function test() {
908+
let event1 = new Date('August 19, 1975 23:15:30');
909+
let event2 = new Date('August 19, 1975 23:45:30');
910+
911+
let eventMsFromUnixEpoch = wasm.set_minutes(event1, 45);
912+
913+
assert.equal(eventMsFromUnixEpoch, event2.getTime());
914+
assert.equal(event1.getTime(), event2.valueOf());
915+
assert.equal(event1.getMinutes(), 45);
916+
}
917+
"#,
918+
)
919+
.test()
920+
}
921+
922+
#[test]
923+
fn set_month() {
924+
project()
925+
.file(
926+
"src/lib.rs",
927+
r#"
928+
#![feature(proc_macro, wasm_custom_section)]
929+
930+
extern crate wasm_bindgen;
931+
use wasm_bindgen::prelude::*;
932+
use wasm_bindgen::js::Date;
933+
934+
#[wasm_bindgen]
935+
pub fn set_month(this: &Date, month: u32) -> f64 {
936+
this.set_month(month)
937+
}
938+
"#,
939+
)
940+
.file(
941+
"test.js",
942+
r#"
943+
import * as assert from "assert";
944+
import * as wasm from "./out";
945+
946+
export function test() {
947+
let event1 = new Date('August 19, 1975 23:15:30');
948+
let event2 = new Date('April 19, 1975 23:15:30');
949+
950+
let eventMsFromUnixEpoch = wasm.set_month(event1, 3);
951+
952+
assert.equal(eventMsFromUnixEpoch, event2.getTime());
953+
assert.equal(event1.getTime(), event2.valueOf());
954+
assert.equal(event1.getMonth(), 3);
955+
}
956+
"#,
957+
)
958+
.test()
959+
}
960+
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, event2.getTime());
992+
assert.equal(event1.getTime(), event2.valueOf());
993+
assert.equal(event1.getSeconds(), 42);
994+
}
995+
"#,
996+
)
997+
.test()
998+
}
999+
1000+
#[test]
1001+
fn set_time() {
1002+
project()
1003+
.file(
1004+
"src/lib.rs",
1005+
r#"
1006+
#![feature(proc_macro, wasm_custom_section)]
1007+
1008+
extern crate wasm_bindgen;
1009+
use wasm_bindgen::prelude::*;
1010+
use wasm_bindgen::js::Date;
1011+
1012+
#[wasm_bindgen]
1013+
pub fn set_time(this: &Date, time: f64) -> f64 {
1014+
this.set_time(time)
1015+
}
1016+
"#,
1017+
)
1018+
.file(
1019+
"test.js",
1020+
r#"
1021+
import * as assert from "assert";
1022+
import * as wasm from "./out";
1023+
1024+
export function test() {
1025+
let event1 = new Date('July 1, 1999');
1026+
let event2 = new Date();
1027+
1028+
let eventMsFromUnixEpoch = wasm.set_time(event2, event1.getTime());
1029+
1030+
assert.equal(eventMsFromUnixEpoch, event2.getTime());
1031+
assert.equal(event1.valueOf(), event2.getTime());
1032+
}
1033+
"#,
1034+
)
1035+
.test()
1036+
}
1037+
7291038
#[test]
7301039
fn to_date_string() {
7311040
project()

0 commit comments

Comments
 (0)