mirror of
https://github.com/lune-org/lune.git
synced 2025-05-04 10:43:57 +01:00
refactor: implement requested changes
This commit is contained in:
parent
09063e2279
commit
2b46958007
2 changed files with 12 additions and 32 deletions
|
@ -97,21 +97,15 @@ impl DateTimeBuilder {
|
||||||
where
|
where
|
||||||
T: ToString,
|
T: ToString,
|
||||||
{
|
{
|
||||||
let format_lazy: Lazy<String, _> = Lazy::new(|| {
|
let format = match format {
|
||||||
if let Some(fmt) = format {
|
Some(fmt) => fmt.to_string(),
|
||||||
fmt.to_string()
|
None => "%Y-%m-%dT%H:%M:%SZ".to_string(),
|
||||||
} else {
|
};
|
||||||
"%Y-%m-%dT%H:%M:%SZ".to_string()
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
let locale_lazy: Lazy<String, _> = Lazy::new(|| {
|
let locale = match locale {
|
||||||
if let Some(locale) = locale {
|
Some(locale) => locale.to_string(),
|
||||||
locale.to_string()
|
None => "en".to_string(),
|
||||||
} else {
|
};
|
||||||
"en".to_string()
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
Ok(match timezone {
|
Ok(match timezone {
|
||||||
Timezone::Utc => Utc
|
Timezone::Utc => Utc
|
||||||
|
@ -125,7 +119,7 @@ impl DateTimeBuilder {
|
||||||
)
|
)
|
||||||
.single()
|
.single()
|
||||||
.ok_or(())?
|
.ok_or(())?
|
||||||
.formatl((*format_lazy).as_str(), (*locale_lazy).as_str())
|
.formatl((format).as_str(), locale.as_str())
|
||||||
.to_string(),
|
.to_string(),
|
||||||
Timezone::Local => Local
|
Timezone::Local => Local
|
||||||
.with_ymd_and_hms(
|
.with_ymd_and_hms(
|
||||||
|
@ -138,7 +132,7 @@ impl DateTimeBuilder {
|
||||||
)
|
)
|
||||||
.single()
|
.single()
|
||||||
.ok_or(())?
|
.ok_or(())?
|
||||||
.formatl((*format_lazy).as_str(), (*locale_lazy).as_str())
|
.formatl((format).as_str(), locale.as_str())
|
||||||
.to_string(),
|
.to_string(),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
|
@ -22,13 +22,12 @@ pub fn create(lua: &'static Lua) -> LuaResult<LuaTable> {
|
||||||
TimestampType::Seconds => timestamp_cloned.as_i64().ok_or(LuaError::external("invalid float integer timestamp supplied"))?,
|
TimestampType::Seconds => timestamp_cloned.as_i64().ok_or(LuaError::external("invalid float integer timestamp supplied"))?,
|
||||||
TimestampType::Millis => {
|
TimestampType::Millis => {
|
||||||
let timestamp = timestamp_cloned.as_f64().ok_or(LuaError::external("invalid float timestamp with millis component supplied"))?;
|
let timestamp = timestamp_cloned.as_f64().ok_or(LuaError::external("invalid float timestamp with millis component supplied"))?;
|
||||||
|
|
||||||
((((timestamp - timestamp.fract()) as u64) * 1000_u64) // converting the whole seconds part to millis
|
((((timestamp - timestamp.fract()) as u64) * 1000_u64) // converting the whole seconds part to millis
|
||||||
// the ..3 gets a &str of the first 3 chars of the digits after the decimals, ignoring
|
// the ..3 gets a &str of the first 3 chars of the digits after the decimals, ignoring
|
||||||
// additional floating point accuracy digits
|
// additional floating point accuracy digits
|
||||||
+ (timestamp.fract() * (10_u64.pow(timestamp.fract().to_string().split('.').collect::<Vec<&str>>()[1][..3].len() as u32)) as f64) as u64) as i64
|
+ (timestamp.fract() * (10_u64.pow(timestamp.fract().to_string().split('.').collect::<Vec<&str>>()[1][..3].len() as u32)) as f64) as u64) as i64
|
||||||
// adding the millis to the fract as a whole number
|
// adding the millis to the fract as a whole number
|
||||||
// HACK: 10 * (timestamp.fract().to_string().len() - 2) gives us the number of digits
|
// HACK: 10 ** (timestamp.fract().to_string().len() - 2) gives us the number of digits
|
||||||
// after the decimal
|
// after the decimal
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -187,7 +186,6 @@ impl<'lua> FromLua<'lua> for DateTimeBuilder {
|
||||||
.with_minute(t.get("minute")?)
|
.with_minute(t.get("minute")?)
|
||||||
.with_second(t.get("second")?)
|
.with_second(t.get("second")?)
|
||||||
.with_millisecond(t.get("millisecond")?)
|
.with_millisecond(t.get("millisecond")?)
|
||||||
// TODO: millisecond support
|
|
||||||
.build()),
|
.build()),
|
||||||
_ => Err(LuaError::external(
|
_ => Err(LuaError::external(
|
||||||
"expected type table for DateTimeBuilder",
|
"expected type table for DateTimeBuilder",
|
||||||
|
@ -198,25 +196,13 @@ impl<'lua> FromLua<'lua> for DateTimeBuilder {
|
||||||
|
|
||||||
impl<'lua> FromLua<'lua> for Timezone {
|
impl<'lua> FromLua<'lua> for Timezone {
|
||||||
fn from_lua(value: LuaValue<'lua>, _: &'lua Lua) -> LuaResult<Self> {
|
fn from_lua(value: LuaValue<'lua>, _: &'lua Lua) -> LuaResult<Self> {
|
||||||
fn num_to_enum(num: i32) -> LuaResult<Timezone> {
|
|
||||||
match num {
|
|
||||||
1 => Ok(Timezone::Utc),
|
|
||||||
2 => Ok(Timezone::Local),
|
|
||||||
_ => Err(LuaError::external("Invalid enum member!")),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
match value {
|
match value {
|
||||||
LuaValue::Integer(num) => num_to_enum(num),
|
|
||||||
LuaValue::Number(num) => num_to_enum(num as i32),
|
|
||||||
LuaValue::String(str) => match str.to_str()?.to_lowercase().as_str() {
|
LuaValue::String(str) => match str.to_str()?.to_lowercase().as_str() {
|
||||||
"utc" => Ok(Timezone::Utc),
|
"utc" => Ok(Timezone::Utc),
|
||||||
"local" => Ok(Timezone::Local),
|
"local" => Ok(Timezone::Local),
|
||||||
&_ => Err(LuaError::external("Invalid enum member!")),
|
&_ => Err(LuaError::external("Invalid enum member!")),
|
||||||
},
|
},
|
||||||
_ => Err(LuaError::external(
|
_ => Err(LuaError::external("Invalid enum type, string expected")),
|
||||||
"Invalid enum type, number or string expected",
|
|
||||||
)),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue