Skip to content
This repository has been archived by the owner on Jul 22, 2022. It is now read-only.

Commit

Permalink
complete rktimeconv
Browse files Browse the repository at this point in the history
  • Loading branch information
Lan13 committed Jul 2, 2022
1 parent ba3c7b7 commit 83f431c
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 5 deletions.
89 changes: 84 additions & 5 deletions lib/rktimeconv/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,45 +44,124 @@ pub struct TimePoint {
nanosec: u32,
day_in_week: u8,
day_in_year: u16,
week: u8,
unix_time: Duration,
}

impl TimePoint {
pub fn from_unix_time(unix_time: Duration) -> Self {
todo!()
let unix_time_secs = unix_time.as_secs();
let unix_time_nanos = unix_time.as_nanos() - (unix_time_secs * 1000000000) as u128;
let one_day_secs = 86400;
let mut total_day = unix_time_secs / one_day_secs;
let day_in_week = (total_day + 4) % 7;
let secs_remain = unix_time_secs - total_day * one_day_secs;
let hour = secs_remain / 3600;
let min = (secs_remain - (hour as u64 * 3600)) / 60;
let sec = secs_remain - hour * 3600 - min * 60;
let mut begin_year: u32 = 1970;
loop {
if is_leap_year(begin_year) {
if total_day >= 366 {
total_day -= 366;
begin_year += 1;
} else {
break;
}
} else {
if total_day >= 365 {
total_day -= 365;
begin_year += 1;
} else {
break;
}
}
}
let day_in_year = total_day;
let mut month: u8 = 0;
for mon in 0..=11 {
if total_day >= day_in_month(mon, begin_year) as u64 {
total_day = total_day - day_in_month(mon, begin_year) as u64;
} else {
month = mon;
break;
}
}
TimePoint {
year: begin_year,
mon: month,
day: (total_day + 1) as u8,
hour: hour as u8,
min: min as u8,
sec: sec as u8,
nanosec: unix_time_nanos as u32,
day_in_week: day_in_week as u8,
day_in_year: day_in_year as u16,
unix_time: unix_time
}
}

pub fn year(&self) -> u32 {
self.year
}

pub fn month(&self) -> u32 {
self.mon as u32
}

pub fn day(&self) -> u32 {
self.day as u32
}
pub fn week(&self) -> u32 {
self.week as u32
}

pub fn hour(&self) -> u32 {
self.hour as u32
}

pub fn min(&self) -> u32 {
self.min as u32
}

pub fn second(&self) -> u32 {
self.sec as u32
}

pub fn nanosec(&self) -> u32 {
self.nanosec as u32
}

pub fn day_in_year(&self) -> u32 {
self.day_in_year as u32
}

pub fn day_in_week(&self) -> u32 {
self.day_in_week as u32
}

pub fn to_unix_time(&self) -> Duration {
self.unix_time
}
}

pub fn is_leap_year (year: u32) -> bool {
if year % 400 == 0 {
return true;
} else if year % 100 == 0 {
return false;
} else if year % 4 == 0 {
return true;
} else {
return false;
}
}

pub fn day_in_month(month: u8, year: u32) -> u32 {
let day_in_month = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
if is_leap_year(year) {
if month == 2 {
return day_in_month[month as usize] + 1;
} else {
return day_in_month[month as usize];
}
} else {
return day_in_month[month as usize];
}
}
3 changes: 3 additions & 0 deletions test/timeconv0/.cargo/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[build]
target-dir = "../../build/test"
target = "riscv64gc-unknown-none-elf"
11 changes: 11 additions & 0 deletions test/timeconv0/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[package]
name = "test-timeconv0"
version = "0.1.0"
edition = "2021"

[workspace]

[dependencies]
rkplat = {path = "../../lib/rkplat", features=["driver_ns16550"]}
rkboot = {path = "../../lib/rkboot", features=["alloc_buddy"]}
rktimeconv = {path = "../../lib/rktimeconv"}
50 changes: 50 additions & 0 deletions test/timeconv0/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#![no_std]
#![no_main]

extern crate rkboot;
#[macro_use]
extern crate rkplat;
use rktimeconv::TimePoint;
use core::time::Duration;

#[no_mangle]
fn main(_args: &mut [&str])->i32 {
let tp1 = TimePoint::from_unix_time(Duration::new(1656733945, 1000));
println!("{}, {}, {}, {}, {}, {}", tp1.year(), tp1.month(), tp1.day(), tp1.hour(), tp1.min(), tp1.second());
println!("{}, {}, {}", tp1.nanosec(), tp1.day_in_week(), tp1.day_in_year());
assert_eq!(tp1.year(),2022);
assert_eq!(tp1.month(),7-1);
assert_eq!(tp1.day(),2);
assert_eq!(tp1.hour(), 3);
assert_eq!(tp1.min(), 52);
assert_eq!(tp1.second(), 25);
assert_eq!(tp1.nanosec(), 1000);
assert_eq!(tp1.day_in_week(), 6);
assert_eq!(tp1.day_in_year(), 183-1);
let tp2 = TimePoint::from_unix_time(Duration::new(1645713945, 50000));
println!("{}, {}, {}, {}, {}, {}", tp2.year(), tp2.month(), tp2.day(), tp2.hour(), tp2.min(), tp2.second());
println!("{}, {}, {}", tp2.nanosec(), tp2.day_in_week(), tp2.day_in_year());
assert_eq!(tp2.year(),2022);
assert_eq!(tp2.month(),2-1);
assert_eq!(tp2.day(),24);
assert_eq!(tp2.hour(), 14);
assert_eq!(tp2.min(), 45);
assert_eq!(tp2.second(), 45);
assert_eq!(tp2.nanosec(), 50000);
assert_eq!(tp2.day_in_week(), 4);
assert_eq!(tp2.day_in_year(), 55-1);
let tp3 = TimePoint::from_unix_time(Duration::new(1545713945, 123));
println!("{}, {}, {}, {}, {}, {}", tp3.year(), tp3.month(), tp3.day(), tp3.hour(), tp3.min(), tp3.second());
println!("{}, {}, {}", tp3.nanosec(), tp3.day_in_week(), tp3.day_in_year());
assert_eq!(tp3.year(),2018);
assert_eq!(tp3.month(),12-1);
assert_eq!(tp3.day(),25);
assert_eq!(tp3.hour(), 4);
assert_eq!(tp3.min(), 59);
assert_eq!(tp3.second(), 5);
assert_eq!(tp3.nanosec(), 123);
assert_eq!(tp3.day_in_week(), 2);
assert_eq!(tp3.day_in_year(), 359-1);
rkplat::println!("Test timeconv0 passed!");
0
}

0 comments on commit 83f431c

Please sign in to comment.