From 4e05bc470f75561cc6623a18c9229b452af4e9c2 Mon Sep 17 00:00:00 2001 From: Sendil Kumar Date: Mon, 25 Jun 2018 10:15:02 +0200 Subject: [PATCH] Add date to locale string --- src/js.rs | 14 +++++++++++++- tests/all/js_globals/Date.rs | 28 ++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/src/js.rs b/src/js.rs index 4fedc27a4..f6c489e8f 100644 --- a/src/js.rs +++ b/src/js.rs @@ -376,7 +376,19 @@ extern { #[wasm_bindgen] extern { pub type Date; - + + /// The toLocaleString() method returns a string with a language sensitive + /// representation of this date. The new locales and options arguments + /// let applications specify the language whose formatting conventions + /// should be used and customize the behavior of the function. + /// In older implementations, which ignore the locales + /// and options arguments, the locale used and the form of the string + /// returned are entirely implementation dependent. + /// + /// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleString + #[wasm_bindgen(method, js_name = toLocaleString)] + pub fn to_locale_string(this: &Date, locale: JsString, options: JsValue) -> JsString; + /// The toLocaleTimeString() method returns a string with a language sensitive /// representation of the time portion of this date. The new locales and options /// arguments let applications specify the language whose formatting conventions should be diff --git a/tests/all/js_globals/Date.rs b/tests/all/js_globals/Date.rs index 732df26e3..536f3a71b 100644 --- a/tests/all/js_globals/Date.rs +++ b/tests/all/js_globals/Date.rs @@ -2,6 +2,34 @@ use super::project; +#[test] +fn to_locale_string() { + project() + .file("src/lib.rs", r#" + #![feature(proc_macro, wasm_custom_section)] + + extern crate wasm_bindgen; + use JsValue; + use wasm_bindgen::prelude::*; + use wasm_bindgen::js::{Date, JsString}; + + #[wasm_bindgen] + pub fn to_locale_string(this: &Date, locale: JsString, options: JsValue) -> JsString { + this.to_locale_string(locale, options) + } + "#) + .file("test.ts", r#" + import * as assert from "assert"; + import * as wasm from "./out"; + + export function test() { + let date = new Date(Date.UTC(2012, 11, 20, 3, 0, 0)); + assert.equal(wasm.to_locale_string(date, 'en-GB', { timeZone: 'UTC' }), "12/20/2012, 3:00:00 AM"); + } + "#) + .test() +} + #[test] fn to_locale_time_string() { project()