
fn main() {
    // 这里添加的环境变量，代码里访问  env!("BUILD_TIME")
    let timestamp = std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).expect("时间计算错误");
    let (年,月,日,时,分,秒) = timestamp_to_datetime(timestamp.as_millis());
    println!("cargo:rustc-env=BUILD_TIME={}", format!("{年}/{月}/{日} {时}:{分}:{秒}"));
    
    #[cfg(all(feature = "mod_slint" ))]
    slint_build::compile("src/mod_ui/ui/scene.slint").unwrap();


    // let current_dir = std::env::current_dir().expect("无法获取当前目录");
    // let 父目录 = current_dir.parent().unwrap();
    // let ffmpeg目录 = 父目录.join("lib-ffmpeg/ffmpeg-sys-next/dist").display().to_string();


    // println!("cargo:rustc-link-search=native={ffmpeg目录}/lib");
    // println!("cargo:rustc-link-lib=static=avcodec");
    // println!("cargo:rustc-link-lib=static=avdevice");
    // println!("cargo:rustc-link-lib=static=avfilter");
    // println!("cargo:rustc-link-lib=static=avformat");
    // println!("cargo:rustc-link-lib=static=avutil");
    // println!("cargo:rustc-link-lib=static=swresample");
    // println!("cargo:rustc-link-lib=static=swscale");
    // println!("cargo:rustc-link-arg=-Wl,--no-as-needed");
    // println!("cargo:rustc-env=FFMPEG_DIR={ffmpeg目录}"); 
    // println!("cargo:rustc-link-lib=dylib=ffmpeg");
}


fn timestamp_to_datetime(ms: u128) -> (i32, u32, u32, u32, u32, u32) {
    let seconds = ms / 1000;
    let mut total_days = seconds / 86400; // 86400秒 = 1天
    
    // 判断是否为闰年
    fn is_leap_year(year: i32) -> bool {
        (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)
    }
    // 计算年份
    let mut year = 1970;
    loop {
        let days_in_year = if is_leap_year(year) { 366 } else { 365 };
        if total_days < days_in_year as u128 {
            break;
        }
        total_days -= days_in_year as u128;
        year += 1;
    }
    
    // 计算月份和日期
    let mut month = 1;
    let mut day = 0;
    let days_in_months = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
    
    for &days in days_in_months.iter() {
        let adj_days = if month == 2 && is_leap_year(year) {
            days + 1
        } else {
            days
        };
        
        if total_days < adj_days as u128 {
            day = total_days as u32 + 1; // 日期从1开始
            break;
        }
        
        total_days -= adj_days as u128;
        month += 1;
    }
    
    // 计算时分秒
    let remaining_seconds = ((seconds + 8*3600)  % 86400) as u32;   // 中国时区
    let hour = remaining_seconds / 3600;
    let remaining_seconds = remaining_seconds % 3600;
    let minute = remaining_seconds / 60;
    let second = remaining_seconds % 60;
    
    (year, month, day, hour, minute, second)
}

