synpad 0.1.0

A full-featured Matrix chat client built with Dioxus
use chrono::{DateTime, Local, Utc};

/// Format a Unix timestamp (milliseconds) into a time string.
pub fn format_time(timestamp_ms: u64) -> String {
    let secs = (timestamp_ms / 1000) as i64;
    let dt = DateTime::<Utc>::from_timestamp(secs, 0);

    match dt {
        Some(utc_dt) => {
            let local: DateTime<Local> = utc_dt.with_timezone(&Local);
            local.format("%H:%M").to_string()
        }
        None => String::new(),
    }
}

/// Format a Unix timestamp into a full date-time string.
pub fn format_datetime(timestamp_ms: u64) -> String {
    let secs = (timestamp_ms / 1000) as i64;
    let dt = DateTime::<Utc>::from_timestamp(secs, 0);

    match dt {
        Some(utc_dt) => {
            let local: DateTime<Local> = utc_dt.with_timezone(&Local);
            local.format("%b %d, %Y at %H:%M").to_string()
        }
        None => String::new(),
    }
}

/// Format a timestamp into a relative date (Today, Yesterday, or date).
pub fn format_relative_date(timestamp_ms: u64) -> String {
    let secs = (timestamp_ms / 1000) as i64;
    let dt = DateTime::<Utc>::from_timestamp(secs, 0);

    match dt {
        Some(utc_dt) => {
            let local: DateTime<Local> = utc_dt.with_timezone(&Local);
            let now = Local::now();
            let today = now.date_naive();
            let msg_date = local.date_naive();

            if msg_date == today {
                "Today".to_string()
            } else if msg_date == today.pred_opt().unwrap_or(today) {
                "Yesterday".to_string()
            } else {
                let days_ago = (today - msg_date).num_days();
                if days_ago < 7 {
                    local.format("%A").to_string() // Day name
                } else {
                    local.format("%b %d, %Y").to_string()
                }
            }
        }
        None => String::new(),
    }
}

/// Format a timestamp for the room list (short format).
pub fn format_room_list_time(timestamp_ms: u64) -> String {
    let secs = (timestamp_ms / 1000) as i64;
    let dt = DateTime::<Utc>::from_timestamp(secs, 0);

    match dt {
        Some(utc_dt) => {
            let local: DateTime<Local> = utc_dt.with_timezone(&Local);
            let now = Local::now();
            let today = now.date_naive();
            let msg_date = local.date_naive();

            if msg_date == today {
                local.format("%H:%M").to_string()
            } else if msg_date == today.pred_opt().unwrap_or(today) {
                "Yesterday".to_string()
            } else {
                let days_ago = (today - msg_date).num_days();
                if days_ago < 7 {
                    local.format("%a").to_string() // Short day name
                } else {
                    local.format("%d/%m/%y").to_string()
                }
            }
        }
        None => String::new(),
    }
}