add single file tests

This commit is contained in:
2020-01-30 21:14:00 +08:00
parent 44d6207f6a
commit 85b24c3ac6
2 changed files with 51 additions and 0 deletions

26
single_file_tests/time.rs Normal file
View File

@@ -0,0 +1,26 @@
use std::time::SystemTime;
fn get_year_and_left_secs(secs_from_1970: u64) -> (u32, u64) {
let mut t = secs_from_1970;
let mut y = 1970;
loop {
let is_leap_year = y % 4 == 0 && y % 100 != 0;
let secs_per_year = if is_leap_year { 366 } else { 365 } * 24 * 60 * 60;
if t > secs_per_year {
y += 1;
t -= secs_per_year;
} else {
return (y, t);
}
}
}
fn main() {
let secs_from_1970 = SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).unwrap().as_secs();
let (year_gmt, _) = get_year_and_left_secs(secs_from_1970);
let (year_gmt8, _) = get_year_and_left_secs(secs_from_1970 + 8 * 60 * 60);
println!("Secs from 1970: {}", secs_from_1970);
println!("Year (GMT) : {}", year_gmt);
println!("Year (GMT+8) : {}", year_gmt8);
}