use crate::util::current_time_unix;
use chrono::{DateTime, Local, NaiveDateTime};
pub fn load_moons() -> Vec<u64> {
let str = include_str!("../resources/full_moon.csv");
let lines = str.split("\n");
let mut i = 0;
let mut timestamps: Vec<u64> = vec![];
for line in lines {
i += 1;
if i == 1 {
continue;
}
let split = line.split(", ").collect::<Vec<&str>>();
let time = split
.get(2)
.unwrap()
.replace("[**]", "")
.replace("[+]", "")
.replace("[*]", "");
let date_str = format!("{} {} +0200", split.get(1).unwrap(), time);
let dt = DateTime::parse_from_str(
&*date_str,
"%d %B %Y %I:%M:%S %P %z", )
.unwrap();
let local = Local::now().timezone();
let dt2 = dt.with_timezone(&local);
let timestamp = dt2.timestamp();
if timestamp > 0 {
timestamps.push(timestamp as u64);
}
}
return timestamps;
}
fn unix_to_local_date(unix: u64) -> DateTime<Local> {
let local = Local::now().offset().clone();
let dt = DateTime::<Local>::from_utc(NaiveDateTime::from_timestamp_opt(unix as i64, 0).unwrap(), local);
return dt;
}
pub fn next_reward_time(vec: Vec<u64>) -> u64 {
let cur = current_time_unix();
let next = vec.iter().filter(|t| **t > cur).min().unwrap().clone();
next
}
#[test]
fn test() {
let vec = load_moons();
let r = next_reward_time(vec);
let dt = unix_to_local_date(r);
println!("Dt {}", dt.to_string());
}