Skip to content

Commit 5ce424e

Browse files
toVersusalexcrichton
authored andcommitted
bindings for Date.prototype.setUTCXXX() (#456)
1 parent 77b86f4 commit 5ce424e

File tree

2 files changed

+319
-0
lines changed

2 files changed

+319
-0
lines changed

src/js.rs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1082,6 +1082,52 @@ extern "C" {
10821082
#[wasm_bindgen(method, js_name = setTime)]
10831083
pub fn set_time(this: &Date, time: f64) -> f64;
10841084

1085+
/// The setUTCDate() method sets the day of the month for a specified date
1086+
/// according to universal time.
1087+
///
1088+
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/setUTCDate
1089+
#[wasm_bindgen(method, js_name = setUTCDate)]
1090+
pub fn set_utc_date(this: &Date, day: u32) -> f64;
1091+
1092+
/// The setUTCFullYear() method sets the full year for a specified date according to universal time.
1093+
///
1094+
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/setUTCFullYear
1095+
#[wasm_bindgen(method, js_name = setUTCFullYear)]
1096+
pub fn set_utc_full_year(this: &Date, year: u32) -> f64;
1097+
1098+
/// The setUTCHours() method sets the hour for a specified date according to universal time,
1099+
/// and returns the number of milliseconds since January 1, 1970 00:00:00 UTC until the time
1100+
/// represented by the updated Date instance.
1101+
///
1102+
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/setUTCHours
1103+
#[wasm_bindgen(method, js_name = setUTCHours)]
1104+
pub fn set_utc_hours(this: &Date, hours: u32) -> f64;
1105+
1106+
/// The setUTCMilliseconds() method sets the milliseconds for a specified date
1107+
/// according to universal time.
1108+
///
1109+
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/setUTCMilliseconds
1110+
#[wasm_bindgen(method, js_name = setUTCMilliseconds)]
1111+
pub fn set_utc_milliseconds(this: &Date, milliseconds: u32) -> f64;
1112+
1113+
/// The setUTCMinutes() method sets the minutes for a specified date according to universal time.
1114+
///
1115+
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/setUTCMinutes
1116+
#[wasm_bindgen(method, js_name = setUTCMinutes)]
1117+
pub fn set_utc_minutes(this: &Date, minutes: u32) -> f64;
1118+
1119+
/// The setUTCMonth() method sets the month for a specified date according to universal time.
1120+
///
1121+
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/setUTCMonth
1122+
#[wasm_bindgen(method, js_name = setUTCMonth)]
1123+
pub fn set_utc_month(this: &Date, month: u32) -> f64;
1124+
1125+
/// The setUTCSeconds() method sets the seconds for a specified date according to universal time.
1126+
///
1127+
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/setUTCSeconds
1128+
#[wasm_bindgen(method, js_name = setUTCSeconds)]
1129+
pub fn set_utc_seconds(this: &Date, seconds: u32) -> f64;
1130+
10851131
/// The toDateString() method returns the date portion of a Date object
10861132
/// in human readable form in American English.
10871133
///

tests/all/js_globals/Date.rs

Lines changed: 273 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1035,6 +1035,279 @@ fn set_time() {
10351035
.test()
10361036
}
10371037

1038+
#[test]
1039+
fn set_utc_date() {
1040+
project()
1041+
.file(
1042+
"src/lib.rs",
1043+
r#"
1044+
#![feature(proc_macro, wasm_custom_section)]
1045+
1046+
extern crate wasm_bindgen;
1047+
use wasm_bindgen::prelude::*;
1048+
use wasm_bindgen::js::Date;
1049+
1050+
#[wasm_bindgen]
1051+
pub fn set_utc_date(this: &Date, day: u32) -> f64 {
1052+
this.set_utc_date(day)
1053+
}
1054+
"#,
1055+
)
1056+
.file(
1057+
"test.js",
1058+
r#"
1059+
import * as assert from "assert";
1060+
import * as wasm from "./out";
1061+
1062+
export function test() {
1063+
let event1 = new Date('August 19, 1975 23:15:30 GMT-3:00');
1064+
let event2 = new Date('August 19, 1975 02:15:30 GMT');
1065+
1066+
let eventMsFromUnixEpoch = wasm.set_utc_date(event1, 19);
1067+
1068+
assert.equal(eventMsFromUnixEpoch, event2.getTime());
1069+
assert.equal(event1.getTime(), event2.valueOf());
1070+
assert.equal(event1.getUTCDate(), 19);
1071+
}
1072+
"#,
1073+
)
1074+
.test()
1075+
}
1076+
1077+
#[test]
1078+
fn set_utc_full_year() {
1079+
project()
1080+
.file(
1081+
"src/lib.rs",
1082+
r#"
1083+
#![feature(proc_macro, wasm_custom_section)]
1084+
1085+
extern crate wasm_bindgen;
1086+
use wasm_bindgen::prelude::*;
1087+
use wasm_bindgen::js::Date;
1088+
1089+
#[wasm_bindgen]
1090+
pub fn set_utc_full_year(this: &Date, year: u32) -> f64 {
1091+
this.set_utc_full_year(year)
1092+
}
1093+
"#,
1094+
)
1095+
.file(
1096+
"test.js",
1097+
r#"
1098+
import * as assert from "assert";
1099+
import * as wasm from "./out";
1100+
1101+
export function test() {
1102+
let event1 = new Date('December 31, 1975 23:15:30 GMT-3:00');
1103+
let event2 = new Date('January 01, 1975 02:15:30 GMT');
1104+
1105+
let eventMsFromUnixEpoch = wasm.set_utc_full_year(event1, 1975);
1106+
1107+
assert.equal(eventMsFromUnixEpoch, event2.getTime());
1108+
assert.equal(event1.getTime(), event2.valueOf());
1109+
assert.equal(event1.getUTCFullYear(), 1975);
1110+
}
1111+
"#,
1112+
)
1113+
.test()
1114+
}
1115+
1116+
#[test]
1117+
fn set_utc_hours() {
1118+
project()
1119+
.file(
1120+
"src/lib.rs",
1121+
r#"
1122+
#![feature(proc_macro, wasm_custom_section)]
1123+
1124+
extern crate wasm_bindgen;
1125+
use wasm_bindgen::prelude::*;
1126+
use wasm_bindgen::js::Date;
1127+
1128+
#[wasm_bindgen]
1129+
pub fn set_utc_hours(this: &Date, hours: u32) -> f64 {
1130+
this.set_utc_hours(hours)
1131+
}
1132+
"#,
1133+
)
1134+
.file(
1135+
"test.js",
1136+
r#"
1137+
import * as assert from "assert";
1138+
import * as wasm from "./out";
1139+
1140+
export function test() {
1141+
let event1 = new Date('August 19, 1975 23:15:30 GMT-3:00');
1142+
let event2 = new Date('August 20, 1975 23:15:30 GMT');
1143+
1144+
let eventMsFromUnixEpoch = wasm.set_utc_hours(event1, 23);
1145+
1146+
assert.equal(eventMsFromUnixEpoch, event2.getTime());
1147+
assert.equal(event1.getTime(), event2.valueOf());
1148+
assert.equal(event1.getUTCHours(), 23);
1149+
}
1150+
"#,
1151+
)
1152+
.test()
1153+
}
1154+
1155+
#[test]
1156+
fn set_utc_milliseconds() {
1157+
project()
1158+
.file(
1159+
"src/lib.rs",
1160+
r#"
1161+
#![feature(proc_macro, wasm_custom_section)]
1162+
1163+
extern crate wasm_bindgen;
1164+
use wasm_bindgen::prelude::*;
1165+
use wasm_bindgen::js::Date;
1166+
1167+
#[wasm_bindgen]
1168+
pub fn set_utc_milliseconds(this: &Date, milliseconds: u32) -> f64 {
1169+
this.set_utc_milliseconds(milliseconds)
1170+
}
1171+
"#,
1172+
)
1173+
.file(
1174+
"test.js",
1175+
r#"
1176+
import * as assert from "assert";
1177+
import * as wasm from "./out";
1178+
1179+
export function test() {
1180+
let event1 = new Date('August 19, 1975 23:15:30 GMT-3:00');
1181+
let event2 = new Date('August 20, 1975 02:15:30.420Z GMT');
1182+
1183+
let eventMsFromUnixEpoch = wasm.set_utc_milliseconds(event1, 420);
1184+
1185+
assert.equal(eventMsFromUnixEpoch, event2.getTime());
1186+
assert.equal(event1.getTime(), event2.valueOf());
1187+
assert.equal(event1.getUTCMilliseconds(), 420);
1188+
}
1189+
"#,
1190+
)
1191+
.test()
1192+
}
1193+
1194+
#[test]
1195+
fn set_utc_minutes() {
1196+
project()
1197+
.file(
1198+
"src/lib.rs",
1199+
r#"
1200+
#![feature(proc_macro, wasm_custom_section)]
1201+
1202+
extern crate wasm_bindgen;
1203+
use wasm_bindgen::prelude::*;
1204+
use wasm_bindgen::js::Date;
1205+
1206+
#[wasm_bindgen]
1207+
pub fn set_utc_minutes(this: &Date, minutes: u32) -> f64 {
1208+
this.set_utc_minutes(minutes)
1209+
}
1210+
"#,
1211+
)
1212+
.file(
1213+
"test.js",
1214+
r#"
1215+
import * as assert from "assert";
1216+
import * as wasm from "./out";
1217+
1218+
export function test() {
1219+
let event1 = new Date('December 31, 1975, 23:15:30 GMT-3:00');
1220+
let event2 = new Date('January 01, 1976 02:25:30 GMT');
1221+
1222+
let eventMsFromUnixEpoch = wasm.set_utc_minutes(event1, 25);
1223+
1224+
assert.equal(eventMsFromUnixEpoch, event2.getTime());
1225+
assert.equal(event1.getTime(), event2.valueOf());
1226+
assert.equal(event1.getUTCMinutes(), 25);
1227+
}
1228+
"#,
1229+
)
1230+
.test()
1231+
}
1232+
1233+
#[test]
1234+
fn set_utc_month() {
1235+
project()
1236+
.file(
1237+
"src/lib.rs",
1238+
r#"
1239+
#![feature(proc_macro, wasm_custom_section)]
1240+
1241+
extern crate wasm_bindgen;
1242+
use wasm_bindgen::prelude::*;
1243+
use wasm_bindgen::js::Date;
1244+
1245+
#[wasm_bindgen]
1246+
pub fn set_utc_month(this: &Date, minutes: u32) -> f64 {
1247+
this.set_utc_month(minutes)
1248+
}
1249+
"#,
1250+
)
1251+
.file(
1252+
"test.js",
1253+
r#"
1254+
import * as assert from "assert";
1255+
import * as wasm from "./out";
1256+
1257+
export function test() {
1258+
let event1 = new Date('December 31, 1975 23:15:30 GMT-3:00');
1259+
let event2 = new Date('December 01, 1976 02:15:30 GMT');
1260+
1261+
let eventMsFromUnixEpoch = wasm.set_utc_month(event1, 11);
1262+
1263+
assert.equal(eventMsFromUnixEpoch, event2.getTime());
1264+
assert.equal(event1.getTime(), event2.valueOf());
1265+
assert.equal(event1.getUTCMonth(), 11);
1266+
}
1267+
"#,
1268+
)
1269+
.test()
1270+
}
1271+
1272+
#[test]
1273+
fn set_utc_seconds() {
1274+
project()
1275+
.file(
1276+
"src/lib.rs",
1277+
r#"
1278+
#![feature(proc_macro, wasm_custom_section)]
1279+
1280+
extern crate wasm_bindgen;
1281+
use wasm_bindgen::prelude::*;
1282+
use wasm_bindgen::js::Date;
1283+
1284+
#[wasm_bindgen]
1285+
pub fn set_utc_seconds(this: &Date, seconds: u32) -> f64 {
1286+
this.set_utc_seconds(seconds)
1287+
}
1288+
"#,
1289+
)
1290+
.file(
1291+
"test.js",
1292+
r#"
1293+
import * as assert from "assert";
1294+
import * as wasm from "./out";
1295+
1296+
export function test() {
1297+
let event1 = new Date('December 31, 1975 23:15:30 GMT-3:00');
1298+
let event2 = new Date('January 01, 1976 02:15:39 GMT');
1299+
1300+
let eventMsFromUnixEpoch = wasm.set_utc_seconds(event1, 39);
1301+
1302+
assert.equal(eventMsFromUnixEpoch, event2.getTime());
1303+
assert.equal(event1.getTime(), event2.valueOf());
1304+
assert.equal(event1.getUTCSeconds(), 39);
1305+
}
1306+
"#,
1307+
)
1308+
.test()
1309+
}
1310+
10381311
#[test]
10391312
fn to_date_string() {
10401313
project()

0 commit comments

Comments
 (0)