From 6943ed07553c18460bc7c046f90de43a4e73d94c Mon Sep 17 00:00:00 2001 From: Compey Date: Sat, 26 Aug 2023 20:44:06 +0530 Subject: [PATCH] feat: finish luau side implementation This commit finishes the DateTime implementation for the luau side library. It includes the following methods: * fromLocalTime * toLocalTime --- src/lune/builtins/datetime/mod.rs | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/src/lune/builtins/datetime/mod.rs b/src/lune/builtins/datetime/mod.rs index 3f7a54d..a0bbc63 100644 --- a/src/lune/builtins/datetime/mod.rs +++ b/src/lune/builtins/datetime/mod.rs @@ -7,12 +7,9 @@ use mlua::prelude::*; use once_cell::sync::Lazy; // TODO: Proper error handling and stuff -// TODO: fromLocalTime, toDateTimeBuilder, toLocalTime +// TODO: fromLocalTime, toLocalTime // FIX: DateTime::from_iso_date is broken -// pub fn format_time(&self, timezone: Timezone, fmt_str: T, locale: T) -> String -// where -// T: ToString, pub fn create(lua: &'static Lua) -> LuaResult { TableBuilder::new(lua)? .with_function("now", |_, ()| Ok(DateTime::now()))? @@ -43,6 +40,12 @@ pub fn create(lua: &'static Lua) -> LuaResult { .with_function("toUniversalTime", |_, this: DateTime| { Ok(this.to_universal_time()) })? + .with_function("fromLocalTime", |lua, date_time: LuaValue| { + Ok(DateTime::from_local_time(DateTimeBuilder::from_lua(date_time, lua).ok())) + })? + .with_function("toLocalTime", |_, this: DateTime| { + Ok(this.to_local_time()) + })? .with_function("fromIsoDate", |_, iso_date: LuaString| { Ok(DateTime::from_iso_date(iso_date.to_string_lossy())) })? @@ -330,7 +333,9 @@ impl LuaUserData for DateTime { fn add_methods<'lua, M: LuaUserDataMethods<'lua, Self>>(methods: &mut M) { methods.add_method("now", |_, _this, ()| Ok(DateTime::now())); + methods.add_method("toIsoDate", |_, this, ()| Ok(this.to_iso_date())); + methods.add_method( "formatTime", |_, this, (timezone, fmt_str, locale): (LuaValue, LuaString, LuaString)| { @@ -341,9 +346,14 @@ impl LuaUserData for DateTime { )) }, ); + methods.add_method("toUniversalTime", |_, this: &DateTime, ()| { Ok(this.to_universal_time()) - }) + }); + + methods.add_method("toLocalTime", |_, this: &DateTime, ()| { + Ok(this.to_local_time()) + }); } } @@ -434,7 +444,8 @@ impl DateTimeBuilder { /// Builder method to set the `Month`. pub fn with_month(&mut self, month: Month) -> &mut Self { - self.month = month as u32; + // THe Month enum casts to u32 starting at zero, so we add one to it + self.month = month as u32 + 1; self }