Skip to content

Commit 890f467

Browse files
committed
updated sessions command for cleaner output (no multi day)
1 parent d0876d3 commit 890f467

File tree

2 files changed

+29
-11
lines changed

2 files changed

+29
-11
lines changed

server/src/core/time.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,11 @@ pub fn format_date(secs: i64, tz: &Tz) -> String {
3737
dt_local.format("%B %-d").to_string()
3838
}
3939

40-
/// Format seconds since epoch as a short datetime (e.g. "Thu Feb 19, 3:00PM")
40+
/// Format seconds since epoch as a short datetime (e.g. "Thu, Feb 19, 3:00PM")
4141
pub fn format_datetime(secs: i64, tz: &Tz) -> String {
4242
let dt_utc = Utc.timestamp_opt(secs, 0).single().unwrap_or_else(|| Utc.timestamp_opt(0, 0).single().unwrap());
4343
let dt_local = tz.from_utc_datetime(&dt_utc.naive_utc());
44-
dt_local.format("%a %b %e, %-I:%M%p").to_string()
44+
dt_local.format("%a, %b %e, %-I:%M%p").to_string()
4545
}
4646

4747
/// Format a start and end time as a human-readable range (e.g. "3:00PM to 8:00PM")

server/src/modules/discord/commands.rs

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use chrono::Utc;
22
use serenity::model::channel::Message;
33
use serenity::prelude::*;
44

5-
use crate::core::time::{format_datetime, format_time_range, parse_tz};
5+
use crate::core::time::{format_date, format_datetime, format_time, parse_tz};
66
use crate::{
77
generated::{
88
common::Timestamp,
@@ -148,17 +148,20 @@ fn leaderboard(args: &str) -> String {
148148
}
149149

150150
pub fn sessions() -> String {
151+
// Load settings
151152
let settings = match Settings::get() {
152153
Ok(s) => s,
153154
Err(e) => return format!("Error loading settings: {e}"),
154155
};
155156
let tz = parse_tz(&settings.timezone);
156157

158+
// Load sessions
157159
let sessions = match Session::get_all() {
158160
Ok(s) => s,
159161
Err(e) => return format!("Error loading sessions: {e}"),
160162
};
161163

164+
// Load locations
162165
let locations_map = match Location::get_all() {
163166
Ok(l) => l,
164167
Err(e) => return format!("Error loading locations: {e}"),
@@ -169,16 +172,17 @@ pub fn sessions() -> String {
169172
let mut active = Vec::new();
170173
let mut upcoming = Vec::new();
171174

172-
for (id, session) in &sessions {
175+
// Categorize sessions
176+
for (_id, session) in &sessions {
173177
let start = session.start_time.as_ref().map_or(0, |t| t.seconds);
174178
let end = session.end_time.as_ref().map_or(0, |t| t.seconds);
175179
let location =
176180
session.location_id.as_str().pipe_ref(|lid| locations_map.get(*lid)).map_or("Unknown", |l| l.location.as_str());
177181

178182
if !session.finished && start <= now_secs {
179-
active.push((id, start, end, location.to_string()));
183+
active.push((start, end, location.to_string()));
180184
} else if start > now_secs {
181-
upcoming.push((id, start, end, location.to_string()));
185+
upcoming.push((start, end, location.to_string()));
182186
}
183187
}
184188

@@ -188,18 +192,32 @@ pub fn sessions() -> String {
188192

189193
let mut lines = Vec::new();
190194

195+
// Format active sessions
191196
if !active.is_empty() {
192197
lines.push("**Active Sessions**".to_string());
193-
for (_, start, end, loc) in &active {
194-
lines.push(format!("- {} @ {}", format_time_range(*start, *end, &tz), loc));
198+
for (start, end, loc) in &active {
199+
let dt_string = format_datetime(*start, &tz);
200+
let start_day = dt_string.split(',').next().unwrap_or("").to_string(); // own the String now
201+
let start_date = format_date(*start, &tz);
202+
let start_time = format_time(*start, &tz);
203+
let end_time = format_time(*end, &tz);
204+
205+
lines.push(format!("[{start_day}, {start_date}]: {start_time}–{end_time} @ {loc}"));
195206
}
196207
}
197208

209+
// Format upcoming sessions
198210
if !upcoming.is_empty() {
199-
upcoming.sort_by_key(|(_, start, _, _)| *start);
211+
upcoming.sort_by_key(|(start, _, _)| *start);
200212
lines.push("**Upcoming Sessions**".to_string());
201-
for (_, start, end, loc) in upcoming.iter().take(5) {
202-
lines.push(format!("- {} @ {}", format_time_range(*start, *end, &tz), loc));
213+
for (start, end, loc) in upcoming.iter().take(5) {
214+
let dt_string = format_datetime(*start, &tz);
215+
let start_day = dt_string.split(',').next().unwrap_or("").to_string(); // own the String now
216+
let start_date = format_date(*start, &tz);
217+
let start_time = format_time(*start, &tz);
218+
let end_time = format_time(*end, &tz);
219+
220+
lines.push(format!("[{start_day}, {start_date}]: {start_time}–{end_time} @ {loc}"));
203221
}
204222
}
205223

0 commit comments

Comments
 (0)