Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new date command #112

Merged
merged 8 commits into from
Nov 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Changelog

## Unreleased
- Add new `date` and `env` commands (#112)
- Add ACPI shutdown (#111)
- Improve text editor (#109)
- Add pcnet driver (#82)
Expand Down
74 changes: 74 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,4 @@ uart_16550 = "0.2.7"
volatile = "0.2.6"
vte = "0.8.0"
x86_64 = "0.12.2"
time = { version = "0.2.22", default-features = false }
4 changes: 4 additions & 0 deletions src/kernel/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ pub fn env(key: &str) -> Option<String> {
}
}

pub fn envs() -> BTreeMap<String, String> {
PROCESS.lock().env.clone()
}

pub fn dir() -> String {
PROCESS.lock().dir.clone()
}
Expand Down
60 changes: 22 additions & 38 deletions src/user/date.rs
Original file line number Diff line number Diff line change
@@ -1,46 +1,30 @@
use crate::kernel::cmos::CMOS;
use crate::{kernel, print, user};
use time::{OffsetDateTime, Duration, UtcOffset};

pub fn print_time_in_seconds(time: f64) {
if time < 1.0e3 {
print!("{:.3} seconds\n", time);
} else if time < 1.0e6 {
print!("{:.3} kiloseconds\n", time / 1.0e3);
} else if time < 1.0e9 {
print!("{:.3} megaseconds\n", time / 1.0e6);
} else {
print!("{:.3} gigaseconds\n", time / 1.0e9);
}
}
pub fn main(args: &[&str]) -> user::shell::ExitCode {
let seconds = kernel::clock::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);

pub fn print_time_in_days(time: f64) {
if time < 0.01 {
print!("{:.2} dimidays\n", time * 10_000.0);
} else if time < 1.0 {
print!("{:.2} centidays\n", time * 100.0);
} else {
print!("{:.2} days\n", time);
let format = if args.len() > 1 { args[1] } else { "%FT%H:%M:%S" };
match time::util::validate_format_string(format) {
Ok(()) => {
print!("{}\n", date.format(format));
user::shell::ExitCode::CommandSuccessful
}
Err(e) => {
print!("Error: {}\n", e);
user::shell::ExitCode::CommandError
}
}
}

pub fn main(args: &[&str]) -> user::shell::ExitCode {
if args.len() == 2 && args[1] == "--raw" {
print!("{:.6}\n", kernel::clock::realtime());
} else if args.len() == 2 && args[1] == "--metric" {
print_time_in_seconds(kernel::clock::realtime());
} else if args.len() == 2 && args[1] == "--iso-8601" {
let rtc = CMOS::new().rtc();
print!(
"{:04}-{:02}-{:02}T{:02}:{:02}:{:02}\n",
rtc.year, rtc.month, rtc.day,
rtc.hour, rtc.minute, rtc.second
);
} else {
let rtc = CMOS::new().rtc();
let seconds_since_midnight = rtc.hour as f64 * 3600.0
+ rtc.minute as f64 * 60.0
+ rtc.second as f64;
print_time_in_days(seconds_since_midnight / 86400.0);
fn offset() -> UtcOffset {
if let Some(tz) = kernel::process::env("TZ") {
if let Ok(offset) = tz.parse::<i32>() {
return UtcOffset::seconds(offset);
}
}
user::shell::ExitCode::CommandSuccessful
UtcOffset::seconds(0)
}
22 changes: 22 additions & 0 deletions src/user/env.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
use crate::{kernel, print, user};

pub fn main(args: &[&str]) -> user::shell::ExitCode {
if args.len() == 1 {
for (key, val) in kernel::process::envs() {
print!("{}={}\n", key, val);
}
} else {
for arg in args[1..].iter() {
if let Some(i) = arg.find('=') {
let (key, mut val) = arg.split_at(i);
val = &val[1..];
kernel::process::set_env(key, val);
print!("{}={}\n", key, val);
} else {
print!("Error: could not parse '{}'\n", arg);
return user::shell::ExitCode::CommandError;
}
}
}
user::shell::ExitCode::CommandSuccessful
}
51 changes: 51 additions & 0 deletions src/user/help.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ pub fn main(args: &[&str]) -> user::shell::ExitCode {

fn help_command(cmd: &str) -> user::shell::ExitCode {
match cmd {
"date" => help_date(),
"edit" => help_edit(),
_ => help_unknown(cmd),
}
Expand Down Expand Up @@ -79,3 +80,53 @@ fn help_edit() -> user::shell::ExitCode {
}
user::shell::ExitCode::CommandSuccessful
}

fn help_date() -> user::shell::ExitCode {
let csi_color = Style::color("Yellow");
let csi_reset = Style::reset();
print!("The date command's formatting behavior is based on strftime in C\n");
print!("\n");
print!("{}Specifiers:{}\n", csi_color, csi_reset);
print!("\n");

let specifiers = [
("%a", "Abbreviated weekday name", "Thu"),
("%A", "Full weekday name", "Thursday"),
("%b", "Abbreviated month name", "Aug"),
("%B", "Full month name", "August"),
("%c", "Date and time representation, equivalent to %a %b %-d %-H:%M:%S %-Y", "Thu Aug 23 14:55:02 2001"),
("%C", "Year divided by 100 and truncated to integer (00-99)", "20"),
("%d", "Day of the month, zero-padded (01-31)", "23"),
("%D", "Short MM/DD/YY date, equivalent to %-m/%d/%y", "8/23/01"),
("%F", "Short YYYY-MM-DD date, equivalent to %-Y-%m-%d", "2001-08-23"),
("%g", "Week-based year, last two digits (00-99)", "01"),
("%G", "Week-based year", "2001"),
("%H", "Hour in 24h format (00-23)", "14"),
("%I", "Hour in 12h format (01-12)", "02"),
("%j", "Day of the year (001-366)", "235"),
("%m", "Month as a decimal number (01-12)", "08"),
("%M", "Minute (00-59)", "55"),
("%N", "Subsecond nanoseconds. Always 9 digits", "012345678"),
("%p", "am or pm designation", "pm"),
("%P", "AM or PM designation", "PM"),
("%r", "12-hour clock time, equivalent to %-I:%M:%S %p", "2:55:02 pm"),
("%R", "24-hour HH:MM time, equivalent to %-H:%M", "14:55"),
("%S", "Second (00-59)", "02"),
("%T", "24-hour clock time with seconds, equivalent to %-H:%M:%S", "14:55:02"),
("%u", "ISO 8601 weekday as number with Monday as 1 (1-7)", "4"),
("%U", "Week number with the first Sunday as the start of week one (00-53)", "33"),
("%V", "ISO 8601 week number (01-53)", "34"),
("%w", "Weekday as a decimal number with Sunday as 0 (0-6)", "4"),
("%W", "Week number with the first Monday as the start of week one (00-53)", "34"),
("%y", "Year, last two digits (00-99)", "01"),
("%Y", "Full year, including + if ≥10,000", "2001"),
("%z", "ISO 8601 offset from UTC in timezone (+HHMM)", "+0100"),
("%%", "Literal %", "%"),
];
for (specifier, usage, _exemple) in &specifiers {
let csi_color = Style::color("LightGreen");
let csi_reset = Style::reset();
print!(" {}{}{} {}\n", csi_color, specifier, csi_reset, usage);
}
user::shell::ExitCode::CommandSuccessful
}
1 change: 1 addition & 0 deletions src/user/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ pub mod delete;
pub mod dhcp;
pub mod disk;
pub mod editor;
pub mod env;
pub mod geotime;
pub mod halt;
pub mod help;
Expand Down
15 changes: 12 additions & 3 deletions src/user/read.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::{kernel, print, user};
use crate::kernel::cmos::CMOS;
use alloc::borrow::ToOwned;
use alloc::vec::Vec;

Expand All @@ -11,13 +12,21 @@ pub fn main(args: &[&str]) -> user::shell::ExitCode {

match pathname {
"/dev/rtc" => {
user::date::main(&["date", "--iso-8601"])
let rtc = CMOS::new().rtc();
print!(
"{:04}-{:02}-{:02}T{:02}:{:02}:{:02}\n",
rtc.year, rtc.month, rtc.day,
rtc.hour, rtc.minute, rtc.second
);
user::shell::ExitCode::CommandSuccessful
},
"/dev/clk/realtime" => {
user::date::main(&["date", "--raw"])
print!("{:.6}\n", kernel::clock::realtime());
user::shell::ExitCode::CommandSuccessful
},
"/dev/clk/uptime" => {
user::uptime::main(&["uptime", "--raw"])
print!("{:.6}\n", kernel::clock::uptime());
user::shell::ExitCode::CommandSuccessful
},
_ => {
if pathname.starts_with("/net/") {
Expand Down
2 changes: 2 additions & 0 deletions src/user/shell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,8 @@ impl Shell {
"sleep" => user::sleep::main(&args),
"clear" => user::clear::main(&args),
"base64" => user::base64::main(&args),
"date" => user::date::main(&args),
"env" => user::env::main(&args),
"halt" => user::halt::main(&args),
"hex" => user::hex::main(&args), // TODO: Rename to `dump`
"net" => user::net::main(&args),
Expand Down
11 changes: 2 additions & 9 deletions src/user/uptime.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@
use crate::{kernel, print, user};

pub fn main(args: &[&str]) -> user::shell::ExitCode {
let time = kernel::clock::uptime();
if args.len() == 2 && args[1] == "--raw" {
print!("{:.6}\n", time);
} else if args.len() == 2 && args[1] == "--metric" {
user::date::print_time_in_seconds(time);
} else {
user::date::print_time_in_days(time / 86400.0);
}
pub fn main(_args: &[&str]) -> user::shell::ExitCode {
print!("{:.6}\n", kernel::clock::uptime());
user::shell::ExitCode::CommandSuccessful
}