Skip to content

Commit

Permalink
Add time module to API (#284)
Browse files Browse the repository at this point in the history
* Add time module to API

* Remove rej files
  • Loading branch information
vinc authored Dec 12, 2021
1 parent 313f90e commit acc4459
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 22 deletions.
1 change: 1 addition & 0 deletions src/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,6 @@ pub mod prompt;
pub mod random;
pub mod regex;
pub mod syscall;
pub mod time;
pub mod vga;
// TODO: add mod wildcard
23 changes: 23 additions & 0 deletions src/api/time.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
use crate::sys;
use crate::api::syscall;

use time::{OffsetDateTime, Duration, UtcOffset};

pub fn now() -> OffsetDateTime {
let s = syscall::realtime(); // Since Unix Epoch
let ns = Duration::nanoseconds(libm::floor(1e9 * (s - libm::floor(s))) as i64);
OffsetDateTime::from_unix_timestamp(s as i64).to_offset(offset()) + ns
}

pub fn from_timestamp(ts: i64) -> OffsetDateTime {
OffsetDateTime::from_unix_timestamp(ts).to_offset(offset())
}

fn offset() -> UtcOffset {
if let Some(tz) = sys::process::env("TZ") {
if let Ok(offset) = tz.parse::<i32>() {
return UtcOffset::seconds(offset);
}
}
UtcOffset::seconds(0)
}
25 changes: 5 additions & 20 deletions src/usr/date.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
use crate::{sys, usr};
use crate::api::syscall;
use time::{OffsetDateTime, Duration, UtcOffset};
use crate::{api, usr};
use time::validate_format_string;

pub fn main(args: &[&str]) -> usr::shell::ExitCode {
let seconds = syscall::realtime(); // Since Unix Epoch
let nanoseconds = libm::floor(1e9 * (seconds - libm::floor(seconds))) as i64;
let date = OffsetDateTime::from_unix_timestamp(seconds as i64).to_offset(offset())
+ Duration::nanoseconds(nanoseconds);

let format = if args.len() > 1 { args[1] } else { "%FT%H:%M:%S" };
match time::util::validate_format_string(format) {
let format = if args.len() > 1 { args[1] } else { "%F %H:%M:%S" };
match validate_format_string(format) {
Ok(()) => {
println!("{}", date.format(format));
println!("{}", api::time::now().format(format));
usr::shell::ExitCode::CommandSuccessful
}
Err(e) => {
Expand All @@ -20,12 +14,3 @@ pub fn main(args: &[&str]) -> usr::shell::ExitCode {
}
}
}

fn offset() -> UtcOffset {
if let Some(tz) = sys::process::env("TZ") {
if let Ok(offset) = tz.parse::<i32>() {
return UtcOffset::seconds(offset);
}
}
UtcOffset::seconds(0)
}
4 changes: 2 additions & 2 deletions src/usr/list.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::{sys, usr};
use crate::api::console::Style;
use crate::api::time;
use alloc::string::ToString;
use alloc::vec::Vec;
use time::OffsetDateTime;

pub fn main(args: &[&str]) -> usr::shell::ExitCode {
let mut path: &str = &sys::process::dir();
Expand Down Expand Up @@ -59,7 +59,7 @@ pub fn main(args: &[&str]) -> usr::shell::ExitCode {
let csi_reset = Style::reset();

for file in files {
let date = OffsetDateTime::from_unix_timestamp(file.time() as i64);
let date = time::from_timestamp(file.time() as i64);
let color = if file.is_dir() {
csi_dir_color
} else if file.is_device() {
Expand Down

0 comments on commit acc4459

Please sign in to comment.