use discord timestamp formatting

This commit is contained in:
jellywx 2021-07-02 17:57:18 +01:00
parent 320060b1bd
commit f02c04b313

View File

@ -1,7 +1,5 @@
use regex_command_attr::command;
use chrono_tz::Tz;
use serenity::{
cache::Cache,
client::Context,
@ -49,18 +47,9 @@ use std::{
};
use regex::Captures;
use ring::hmac;
fn shorthand_displacement(seconds: u64) -> String {
let (days, seconds) = seconds.div_rem(&DAY);
let (hours, seconds) = seconds.div_rem(&HOUR);
let (minutes, seconds) = seconds.div_rem(&MINUTE);
let time_repr = format!("{:02}:{:02}:{:02}", hours, minutes, seconds);
format!("{} days, {}", days, time_repr)
}
fn longhand_displacement(seconds: u64) -> String {
let (days, seconds) = seconds.div_rem(&DAY);
let (hours, seconds) = seconds.div_rem(&HOUR);
@ -397,27 +386,11 @@ impl LookReminder {
}
}
fn display(
&self,
flags: &LookFlags,
meridian: &MeridianType,
timezone: &Tz,
inter: &str,
) -> String {
fn display(&self, flags: &LookFlags, inter: &str) -> String {
let time_display = match flags.time_display {
TimeDisplayType::Absolute => timezone
.timestamp(self.time.timestamp(), 0)
.format(meridian.fmt_str())
.to_string(),
TimeDisplayType::Absolute => format!("<t:{}>", self.time.timestamp()),
TimeDisplayType::Relative => {
let now = SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap()
.as_secs();
longhand_displacement((self.time.timestamp() as u64).checked_sub(now).unwrap_or(1))
}
TimeDisplayType::Relative => format!("<t:{}:R>", self.time.timestamp()),
};
if let Some(interval) = self.interval {
@ -451,8 +424,6 @@ async fn look(ctx: &Context, msg: &Message, args: String) {
let (pool, lm) = get_ctx_data(&ctx).await;
let language = UserData::language_of(&msg.author, &pool).await;
let timezone = UserData::timezone_of(&msg.author, &pool).await;
let meridian = UserData::meridian_of(&msg.author, &pool).await;
let flags = LookFlags::from_string(&args);
@ -519,7 +490,7 @@ LIMIT
let display = reminders
.iter()
.map(|reminder| reminder.display(&flags, &meridian, &timezone, &inter));
.map(|reminder| reminder.display(&flags, &inter));
let _ = msg.channel_id.say_lines(&ctx, display).await;
}
@ -643,14 +614,13 @@ WHERE
let enumerated_reminders = reminders.iter().enumerate().map(|(count, reminder)| {
reminder_ids.push(reminder.id);
let time = user_data.timezone().timestamp(reminder.time.timestamp(), 0);
format!(
"**{}**: '{}' *<#{}>* at {}",
"**{}**: '{}' *<#{}>* at <t:{}>",
count + 1,
reminder.display_content(),
reminder.channel,
time.format(user_data.meridian().fmt_str())
reminder.time.timestamp()
)
});
@ -1292,9 +1262,7 @@ async fn remind_command(ctx: &Context, msg: &Message, args: String, command: Rem
.replace("{location}", &ok_locations[0].mention())
.replace(
"{offset}",
&shorthand_displacement(
time_parser.displacement().unwrap() as u64,
),
&format!("<t:{}:R>", time_parser.timestamp().unwrap()),
),
n => lm
.get(&language, "remind/success_bulk")
@ -1309,9 +1277,7 @@ async fn remind_command(ctx: &Context, msg: &Message, args: String, command: Rem
)
.replace(
"{offset}",
&shorthand_displacement(
time_parser.displacement().unwrap() as u64,
),
&format!("<t:{}:R>", time_parser.timestamp().unwrap()),
),
};
@ -1418,11 +1384,6 @@ async fn remind_command(ctx: &Context, msg: &Message, args: String, command: Rem
async fn natural(ctx: &Context, msg: &Message, args: String) {
let (pool, lm) = get_ctx_data(&ctx).await;
let now = SystemTime::now();
let since_epoch = now
.duration_since(UNIX_EPOCH)
.expect("Time calculated as going backwards. Very bad");
let user_data = UserData::from_user(&msg.author, &ctx, &pool).await.unwrap();
match REGEX_NATURAL_COMMAND_1.captures(&args) {
@ -1486,8 +1447,6 @@ async fn natural(ctx: &Context, msg: &Message, args: String) {
match content_res {
Ok(mut content) => {
let offset = timestamp as u64 - since_epoch.as_secs();
let mut ok_locations = vec![];
let mut err_locations = vec![];
let mut err_types = HashSet::new();
@ -1519,7 +1478,7 @@ async fn natural(ctx: &Context, msg: &Message, args: String) {
1 => lm
.get(&user_data.language, "remind/success")
.replace("{location}", &ok_locations[0].mention())
.replace("{offset}", &shorthand_displacement(offset)),
.replace("{offset}", &format!("<t:{}:R>", timestamp)),
n => lm
.get(&user_data.language, "remind/success_bulk")
.replace("{number}", &n.to_string())
@ -1531,7 +1490,7 @@ async fn natural(ctx: &Context, msg: &Message, args: String) {
.collect::<Vec<String>>()
.join(", "),
)
.replace("{offset}", &shorthand_displacement(offset)),
.replace("{offset}", &format!("<t:{}:R>", timestamp)),
};
let error_part = format!(