use git2::{Oid, Repository};
use std::path::Path;
use anyhow::Result;
pub fn open_repo(path: &Path) -> Result<Repository> {
Ok(Repository::discover(path)?)
}
pub fn short_oid(oid: &Oid) -> String {
let s = oid.to_string();
s[..7.min(s.len())].to_string()
}
pub fn format_timestamp(secs: i64, offset_minutes: i32) -> String {
let local_secs = secs + (offset_minutes as i64) * 60;
let days_since_epoch = local_secs.div_euclid(86400);
let time_of_day = local_secs.rem_euclid(86400);
let hours = time_of_day / 3600;
let minutes = (time_of_day % 3600) / 60;
let seconds = time_of_day % 60;
let mut days = days_since_epoch;
let mut year = 1970i64;
loop {
let days_in_year = if year % 4 == 0 && (year % 100 != 0 || year % 400 == 0) {
366
} else {
365
};
if days < days_in_year {
break;
}
days -= days_in_year;
year += 1;
}
let is_leap = year % 4 == 0 && (year % 100 != 0 || year % 400 == 0);
let month_days = [
31,
if is_leap { 29 } else { 28 },
31,
30,
31,
30,
31,
31,
30,
31,
30,
31,
];
let month_names = [
"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
];
let mut month = 0;
for (i, &md) in month_days.iter().enumerate() {
if days < md as i64 {
month = i;
break;
}
days -= md as i64;
}
let offset_sign = if offset_minutes >= 0 { '+' } else { '-' };
let abs_offset = offset_minutes.unsigned_abs();
let offset_hours = abs_offset / 60;
let offset_mins = abs_offset % 60;
format!(
"{} {} {} {:02}:{:02}:{:02} {}{:02}{:02}",
month_names[month],
days + 1,
year,
hours,
minutes,
seconds,
offset_sign,
offset_hours,
offset_mins
)
}