use chrono::{DateTime, Datelike, Local, Offset};
use std::{path::Path, time::UNIX_EPOCH};
pub fn convert_tag_characters(path: &Path, tag: &str) -> String {
let mut new_tag: String = tag.to_owned();
if new_tag.contains("%p") {
let parent_dirname: String = path
.parent()
.unwrap()
.file_name()
.unwrap()
.to_str()
.unwrap()
.to_owned();
let split_tag: Vec<&str> = new_tag.split("%p").collect();
if split_tag.is_empty() {
new_tag = parent_dirname;
} else {
new_tag = split_tag.join(parent_dirname.as_str());
}
}
if new_tag.contains("%y")
|| new_tag.contains("%m")
|| new_tag.contains("%d")
|| new_tag.contains("%w")
{
let timestamp = std::fs::metadata(path)
.unwrap()
.modified()
.unwrap()
.duration_since(UNIX_EPOCH)
.unwrap()
.as_secs();
let as_sec = i64::try_from(timestamp).ok().unwrap();
let date_time_utc = DateTime::from_timestamp(as_sec, 0).unwrap().naive_utc();
let local_timezone = Local::now().offset().fix();
let date_time_local =
DateTime::<Local>::from_naive_utc_and_offset(date_time_utc, local_timezone);
if new_tag.contains("%y") {
let split_tag: Vec<&str> = new_tag.split("%y").collect();
if split_tag.is_empty() {
new_tag = date_time_local.year().to_string();
} else {
new_tag = split_tag.join(date_time_local.year().to_string().as_str());
}
}
if new_tag.contains("%m") {
let split_tag: Vec<&str> = new_tag.split("%m").collect();
if split_tag.is_empty() {
new_tag = date_time_local.month().to_string();
} else {
new_tag = split_tag.join(date_time_local.month().to_string().as_str());
}
}
if new_tag.contains("%d") {
let split_tag: Vec<&str> = new_tag.split("%d").collect();
if split_tag.is_empty() {
new_tag = date_time_local.day().to_string();
} else {
new_tag = split_tag.join(date_time_local.day().to_string().as_str());
}
}
if new_tag.contains("%w") {
let split_tag: Vec<&str> = new_tag.split("%w").collect();
if split_tag.is_empty() {
new_tag = date_time_local.weekday().to_string();
} else {
new_tag = split_tag.join(date_time_local.weekday().to_string().as_str());
}
}
}
new_tag
}