use chrono::{Duration, DateTime, Utc, NaiveDateTime, NaiveDate, NaiveTime};
use crate::protocol::tables::GPSTimeTable;
use std::error::Error;
pub fn gps_week2time(
week: i64,
time: i64) -> Result<DateTime<Utc>,Box<dyn Error>>
{
let date = NaiveDate::from_ymd_opt(1980, 1, 6).unwrap();
let dtime = NaiveTime::from_hms_opt(0,0,0).unwrap();
let start_date = DateTime::<Utc>::from_utc(NaiveDateTime::new(date,dtime), Utc);
let final_data = start_date + Duration::weeks(week) + Duration::milliseconds(time);
Ok(final_data)
}
pub fn gpstime(
table_time:&GPSTimeTable,
leap: i64) -> Result<DateTime<Utc>,Box<dyn std::error::Error>>
{
let time = table_time.gps_time as i64;
let week = table_time.gps_week as i64;
let final_date = gps_week2time(week, time)? - Duration::seconds(leap);
Ok(final_date)
}