diff --git a/Cargo.toml b/Cargo.toml index 12b1c16..43cfd4f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -107,6 +107,9 @@ reqwest = { version = "0.11", default-features = false, features = [ ] } tokio-tungstenite = { version = "0.20", features = ["rustls-tls-webpki-roots"] } +### DATETIME +chrono = "0.4.26" + ### CLI anyhow = { optional = true, version = "1.0" } @@ -133,4 +136,3 @@ rbx_dom_weak = { optional = true, version = "2.5.0" } rbx_reflection = { optional = true, version = "4.3.0" } rbx_reflection_database = { optional = true, version = "0.2.7" } rbx_xml = { optional = true, version = "0.13.1" } -chrono = "0.4.26" diff --git a/src/lune/builtins/date_time.rs b/src/lune/builtins/date_time.rs index 5538a30..05846f8 100644 --- a/src/lune/builtins/date_time.rs +++ b/src/lune/builtins/date_time.rs @@ -1,5 +1,6 @@ use chrono::prelude::*; use chrono::DateTime as ChronoDateTime; +use once_cell::sync::Lazy; // TODO: Proper error handling and stuff @@ -158,7 +159,23 @@ impl DateTime { } pub fn to_iso_date(&self) -> String { - self.to_universal_time().to_iso_string(Timezone::UTC) + self.to_universal_time() + .to_string::<&str>(Timezone::UTC, None) + } + + // TODO: Implement localization + // There seems to be only one localization crate for chrono, + // which has been committed to last 5 years ago. Thus, this crate doesn't + // work with the version of chrono we're using. I'll be forking this crate + // and making it compatible with our version of chrono for now. + pub fn format_universal_time(&self, fmt_str: T, locale: T) -> String + where + T: ToString, + { + let format = fmt_str.to_string(); + + self.to_universal_time() + .to_string(Timezone::UTC, Some(format)) } } @@ -187,39 +204,6 @@ impl Default for DateTimeConstructor { } } -#[derive(PartialEq, Eq, PartialOrd, Ord)] -pub enum Month { - January, - February, - March, - April, - May, - June, - July, - August, - September, - October, - November, - December, -} - -// fn match_month_to_num(month: Month) { -// match month { -// Month::January => 1, -// Month::February => 2, -// Month::March => 3, -// Month::April => 4, -// Month::May => 5, -// Month::June => 6, -// Month::July => 7, -// Month::August => 8, -// Month::September => 9, -// Month::October => 10, -// Month::November => 11, -// Month::December => 12, -// }; -// } - enum Timezone { UTC, Local, @@ -268,8 +252,17 @@ impl DateTimeConstructor { self } - fn to_iso_string(&self, timezone: Timezone) -> String { - let iso_format = "%Y-%m-%dT%H:%M:%SZ"; + fn to_string(&self, timezone: Timezone, format: Option) -> String + where + T: ToString, + { + let format_lazy: Lazy = Lazy::new(|| { + if let Some(fmt) = format { + fmt.to_string() + } else { + "%Y-%m-%dT%H:%M:%SZ".to_string() + } + }); match timezone { Timezone::UTC => Utc @@ -282,7 +275,7 @@ impl DateTimeConstructor { self.second, ) .unwrap() - .format(iso_format) + .format((*format_lazy).as_str()) .to_string(), Timezone::Local => Local .with_ymd_and_hms( @@ -294,7 +287,7 @@ impl DateTimeConstructor { self.second, ) .unwrap() - .format(iso_format) + .format((*format_lazy).as_str()) .to_string(), } }