From acc4459f64a02032e5b3d65235039f24291a261b Mon Sep 17 00:00:00 2001 From: Vincent Ollivier Date: Sun, 12 Dec 2021 11:58:02 +0100 Subject: [PATCH] Add time module to API (#284) * Add time module to API * Remove rej files --- src/api/mod.rs | 1 + src/api/time.rs | 23 +++++++++++++++++++++++ src/usr/date.rs | 25 +++++-------------------- src/usr/list.rs | 4 ++-- 4 files changed, 31 insertions(+), 22 deletions(-) create mode 100644 src/api/time.rs diff --git a/src/api/mod.rs b/src/api/mod.rs index 17ab76cda..5d4146857 100644 --- a/src/api/mod.rs +++ b/src/api/mod.rs @@ -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 diff --git a/src/api/time.rs b/src/api/time.rs new file mode 100644 index 000000000..75e5f35c9 --- /dev/null +++ b/src/api/time.rs @@ -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::() { + return UtcOffset::seconds(offset); + } + } + UtcOffset::seconds(0) +} diff --git a/src/usr/date.rs b/src/usr/date.rs index 942710535..524becb07 100644 --- a/src/usr/date.rs +++ b/src/usr/date.rs @@ -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) => { @@ -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::() { - return UtcOffset::seconds(offset); - } - } - UtcOffset::seconds(0) -} diff --git a/src/usr/list.rs b/src/usr/list.rs index c88ea9235..2904b8fc1 100644 --- a/src/usr/list.rs +++ b/src/usr/list.rs @@ -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(); @@ -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() {