From 292a9bda4b9e7776415e1fca7393a59eea715321 Mon Sep 17 00:00:00 2001 From: EmperorBale Date: Wed, 28 Jul 2021 17:52:59 -0700 Subject: [PATCH] avm2: Implement toLocaleString, toLocaleTimeString, toDateString, toLocaleDateString --- core/src/avm2/globals/date.rs | 76 +++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) diff --git a/core/src/avm2/globals/date.rs b/core/src/avm2/globals/date.rs index d451c79cf..ee70e35d1 100644 --- a/core/src/avm2/globals/date.rs +++ b/core/src/avm2/globals/date.rs @@ -942,6 +942,30 @@ pub fn to_string<'gc>( Ok(Value::Undefined) } +/// Implements the `toLocaleString` method. +pub fn to_locale_string<'gc>( + activation: &mut Activation<'_, 'gc, '_>, + this: Option>, + _args: &[Value<'gc>], +) -> Result, Error> { + if let Some(this) = this.and_then(|this| this.as_date_object()) { + if let Some(date) = this + .date_time() + .map(|date| date.with_timezone(&activation.context.locale.get_timezone())) + { + return Ok(AvmString::new( + activation.context.gc_context, + date.format("%a %b %-d %-Y %T %p").to_string(), + ) + .into()); + } else { + return Ok("Invalid Date".into()); + } + } + + Ok(Value::Undefined) +} + /// Implements the `toTimeString` method. pub fn to_time_string<'gc>( activation: &mut Activation<'_, 'gc, '_>, @@ -966,6 +990,54 @@ pub fn to_time_string<'gc>( Ok(Value::Undefined) } +/// Implements the `toLocaleTimeString` method. +pub fn to_locale_time_string<'gc>( + activation: &mut Activation<'_, 'gc, '_>, + this: Option>, + _args: &[Value<'gc>], +) -> Result, Error> { + if let Some(this) = this.and_then(|this| this.as_date_object()) { + if let Some(date) = this + .date_time() + .map(|date| date.with_timezone(&activation.context.locale.get_timezone())) + { + return Ok(AvmString::new( + activation.context.gc_context, + date.format("%T %p").to_string(), + ) + .into()); + } else { + return Ok("Invalid Date".into()); + } + } + + Ok(Value::Undefined) +} + +/// Implements the `toDateString` & `toLocaleDateString` method. +pub fn to_date_string<'gc>( + activation: &mut Activation<'_, 'gc, '_>, + this: Option>, + _args: &[Value<'gc>], +) -> Result, Error> { + if let Some(this) = this.and_then(|this| this.as_date_object()) { + if let Some(date) = this + .date_time() + .map(|date| date.with_timezone(&activation.context.locale.get_timezone())) + { + return Ok(AvmString::new( + activation.context.gc_context, + date.format("%a %b %-d %-Y").to_string(), + ) + .into()); + } else { + return Ok("Invalid Date".into()); + } + } + + Ok(Value::Undefined) +} + /// Construct `Date`'s class. pub fn create_class<'gc>(mc: MutationContext<'gc, '_>) -> GcCell<'gc, Class<'gc>> { let class = Class::new( @@ -1045,7 +1117,11 @@ pub fn create_class<'gc>(mc: MutationContext<'gc, '_>) -> GcCell<'gc, Class<'gc> ("getTimezoneOffset", timezone_offset), ("valueOf", time), ("toString", to_string), + ("toLocaleString", to_locale_string), ("toTimeString", to_time_string), + ("toLocaleTimeString", to_locale_time_string), + ("toDateString", to_date_string), + ("toLocaleDateString", to_date_string), ]; write.define_public_builtin_instance_methods(mc, PUBLIC_INSTANCE_METHODS);