use std::{time::{SystemTime, UNIX_EPOCH}};


/// 时间戳对象，存储从 UNIX 纪元起经过的秒数、微秒数和纳秒数。
//时间戳对象
pub struct Timestamp {
    pub timestamp_s: u64,  // 存数值而不是 SystemTime
    pub timestamp_ms:u128,
    pub timestamp_ns:u128,
}

impl Default for Timestamp {
    fn default() -> Self {
        Timestamp {
            timestamp_s: 0,
            timestamp_ms: 0,
            timestamp_ns: 0,
        }
    }
}

impl Timestamp {
    /// 创建一个默认的 `Timestamp` 实例（所有字段初始化为 0）。
    //使用默认宏赋予实例参数
    pub fn new() -> Self {Timestamp::default()}

    /// 获取当前时间戳，填充秒、微秒、纳秒字段。
    //获取时间戳
    pub fn get_timestamp(mut self) -> Self{
        let d_timestamp =SystemTime::now().duration_since(UNIX_EPOCH).unwrap();
        let timestamp_s_temp=d_timestamp.as_secs();
        let timestamp_ms_temp =d_timestamp.as_micros();
        let timestamp_ns_temp =d_timestamp.as_nanos();

        self.timestamp_s=timestamp_s_temp;
        self.timestamp_ms=timestamp_ms_temp;
        self.timestamp_ns=timestamp_ns_temp;

        self
    }
}

/// 日期时间对象，存储年、月、日、时、分、秒及 UTC+8 时区偏移后的本地时间。
//datetime对象
pub struct Time {
    pub year:i32,
    pub mouth:i8,
    pub day:i8,
    pub hour:i8,
    pub minute:i8,
    pub secend:i8,
    timezone_offset:i32,  // 时区偏移量（秒），默认 UTC+8
}

impl Default for Time {
    fn default() -> Self {
        Time {
            year: 0,
            mouth: 0,
            day: 0,
            hour: 0,
            minute: 0,
            secend: 0,
            timezone_offset: 8 * 3600,
        }
    }
}

impl Time {
    /// 创建一个默认的 `Time` 实例（所有字段初始化为 0，时区默认 UTC+8）。
    //创建实例
    pub fn new () ->Self{Time::default()}
    /// 判断当前年份是否为闰年。
    //判断是否为闰年
    pub fn is_leap_year(&self) -> bool {
        if self.year % 400 == 0 {
            true
        } else if self.year % 100 == 0 {
            false
        } else if self.year % 4 == 0 {
            true
        } else {
            false
        }
    }

    /// 修改时区偏移量，单位为小时（如传入 `8` 表示 UTC+8，传入 `-5` 表示 UTC-5）。
    //修改时区偏移量，单位为小时
    pub fn set_timezone(mut self, hours: i32) -> Self {
        self.timezone_offset = hours * 3600;
        self
    }

    /// 获取当前日期时间，按配置的时区转换为年/月/日/时/分/秒。
    pub fn get_time(mut self) -> Self{
        let t = Timestamp::new().get_timestamp().timestamp_s as i128;

        // 加上时区偏移
        let t = t + self.timezone_offset as i128;

        //时间戳里完整的天数
        let mut days = t / 86400;
        //除下完整天数，余下的秒数
        let other_s = t % 86400;

        // 时分秒
        let hour = (other_s / 3600) as i8;
        let minute = ((other_s % 3600) / 60) as i8;
        let second = (other_s % 60) as i8;

        // 从1970年开始算年
        let mut year: i32 = 1970;
        loop {
            self.year = year;
            //根据是否为闰年，给年中天数赋值
            let days_in_year = if self.is_leap_year() { 366 } else { 365 };
            //如果剩下的天数少于这一年的年中天数，停止，否则天数-该年中天数，年份+1
            if days < days_in_year { break; }
            days -= days_in_year;
            year += 1;
        }

        // 算月日
        self.year = year;
        let month_days = if self.is_leap_year() {
            [31,29,31,30,31,30,31,31,30,31,30,31]
        } else {
            [31,28,31,30,31,30,31,31,30,31,30,31]
        };

        let mut month: i8 = 1;
        for &md in &month_days {
            //如果剩下的天数少于这一月的月中天数，停止，否则，天数-该月中天数，月份+1
            if days < md { break; }
            days -= md;
            month += 1;
        }

        let day = (days + 1) as i8;

        self.year = year;
        self.mouth = month;
        self.day = day;
        self.hour = hour;
        self.minute = minute;
        self.secend = second;

        self
    }
}
