Fix memories week span sorting
This commit is contained in:
@@ -1,7 +1,9 @@
|
|||||||
use actix_web::web::Data;
|
use actix_web::web::Data;
|
||||||
use actix_web::{HttpRequest, HttpResponse, Responder, get, web};
|
use actix_web::{HttpRequest, HttpResponse, Responder, get, web};
|
||||||
use chrono::LocalResult::{Ambiguous, Single};
|
use chrono::LocalResult::{Ambiguous, Single};
|
||||||
use chrono::{DateTime, Datelike, FixedOffset, Local, LocalResult, NaiveDate, TimeZone, Utc};
|
use chrono::{
|
||||||
|
DateTime, Datelike, FixedOffset, Local, LocalResult, NaiveDate, TimeZone, Timelike, Utc,
|
||||||
|
};
|
||||||
use log::{debug, trace, warn};
|
use log::{debug, trace, warn};
|
||||||
use opentelemetry::KeyValue;
|
use opentelemetry::KeyValue;
|
||||||
use opentelemetry::trace::{Span, Status, Tracer};
|
use opentelemetry::trace::{Span, Status, Tracer};
|
||||||
@@ -541,7 +543,59 @@ pub async fn list_memories(
|
|||||||
match span_mode {
|
match span_mode {
|
||||||
// Sort by absolute time for a more 'overview'
|
// Sort by absolute time for a more 'overview'
|
||||||
MemoriesSpan::Month => memories_with_dates.sort_by(|a, b| a.1.cmp(&b.1)),
|
MemoriesSpan::Month => memories_with_dates.sort_by(|a, b| a.1.cmp(&b.1)),
|
||||||
_ => {
|
// For week span, sort by day of month, then time of day, then year (oldest first)
|
||||||
|
MemoriesSpan::Week => {
|
||||||
|
memories_with_dates.sort_by(|a, b| {
|
||||||
|
// First, sort by day of month
|
||||||
|
let day_cmp = a.1.day().cmp(&b.1.day());
|
||||||
|
if day_cmp != std::cmp::Ordering::Equal {
|
||||||
|
return day_cmp;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Then sort by time of day
|
||||||
|
match (a.0.created, b.0.created) {
|
||||||
|
(Some(a_time), Some(b_time)) => {
|
||||||
|
// Convert timestamps to DateTime
|
||||||
|
let a_dt_utc = DateTime::<Utc>::from_timestamp(a_time, 0).unwrap();
|
||||||
|
let b_dt_utc = DateTime::<Utc>::from_timestamp(b_time, 0).unwrap();
|
||||||
|
|
||||||
|
// Extract time of day in the appropriate timezone
|
||||||
|
let a_time_of_day = if let Some(ref tz) = client_timezone {
|
||||||
|
let dt = a_dt_utc.with_timezone(tz);
|
||||||
|
(dt.hour(), dt.minute(), dt.second())
|
||||||
|
} else {
|
||||||
|
let dt = a_dt_utc.with_timezone(&Local);
|
||||||
|
(dt.hour(), dt.minute(), dt.second())
|
||||||
|
};
|
||||||
|
|
||||||
|
let b_time_of_day = if let Some(ref tz) = client_timezone {
|
||||||
|
let dt = b_dt_utc.with_timezone(tz);
|
||||||
|
(dt.hour(), dt.minute(), dt.second())
|
||||||
|
} else {
|
||||||
|
let dt = b_dt_utc.with_timezone(&Local);
|
||||||
|
(dt.hour(), dt.minute(), dt.second())
|
||||||
|
};
|
||||||
|
|
||||||
|
// Compare time of day
|
||||||
|
let time_cmp = a_time_of_day.cmp(&b_time_of_day);
|
||||||
|
if time_cmp != std::cmp::Ordering::Equal {
|
||||||
|
return time_cmp;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Finally, sort by year (oldest first)
|
||||||
|
a.1.year().cmp(&b.1.year())
|
||||||
|
}
|
||||||
|
(Some(_), None) => std::cmp::Ordering::Less,
|
||||||
|
(None, Some(_)) => std::cmp::Ordering::Greater,
|
||||||
|
(None, None) => {
|
||||||
|
// If no timestamps, just sort by year (oldest first)
|
||||||
|
a.1.year().cmp(&b.1.year())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
// For day span, sort by day of month then by time
|
||||||
|
MemoriesSpan::Day => {
|
||||||
memories_with_dates.sort_by(|a, b| {
|
memories_with_dates.sort_by(|a, b| {
|
||||||
let day_comparison = a.1.day().cmp(&b.1.day());
|
let day_comparison = a.1.day().cmp(&b.1.day());
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user