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

Improve HTTP server #391

Merged
merged 21 commits into from Aug 25, 2022
Merged
5 changes: 3 additions & 2 deletions Makefile
Expand Up @@ -43,8 +43,9 @@ image: $(img)
cargo bootimage --no-default-features --features $(output) --release --bin moros
dd conv=notrunc if=$(bin) of=$(img)

opts = -m 32 -nic model=$(nic) -drive file=$(img),format=raw \
-audiodev driver=sdl,id=a0 -machine pcspk-audiodev=a0
opts = -m 32 -drive file=$(img),format=raw \
-audiodev driver=sdl,id=a0 -machine pcspk-audiodev=a0 \
-netdev user,id=e0,hostfwd=tcp::8080-:80 -device $(nic),netdev=e0
ifeq ($(kvm),true)
opts += -cpu host -accel kvm
else
Expand Down
5 changes: 4 additions & 1 deletion src/api/clock.rs
@@ -1,7 +1,10 @@
use crate::api::fs;

use core::convert::TryInto;

pub const DATE_TIME_ZONE: &str = "%Y-%m-%d %H:%M:%S %z";
pub const DATE_TIME: &str = "%Y-%m-%d %H:%M:%S";
pub const DATE: &str = "%Y-%m-%d";

fn read_float(path: &str) -> f64 {
if let Ok(bytes) = fs::read_to_bytes(path) {
if bytes.len() == 8 {
Expand Down
8 changes: 6 additions & 2 deletions src/api/time.rs
Expand Up @@ -4,9 +4,13 @@ use crate::api::clock;
use time::{OffsetDateTime, Duration, UtcOffset};

pub fn now() -> OffsetDateTime {
now_utc().to_offset(offset())
}

pub fn now_utc() -> OffsetDateTime {
let s = clock::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
OffsetDateTime::from_unix_timestamp(s as i64) + ns
}

pub fn from_timestamp(ts: i64) -> OffsetDateTime {
Expand All @@ -19,5 +23,5 @@ fn offset() -> UtcOffset {
return UtcOffset::seconds(offset);
}
}
UtcOffset::seconds(0)
UtcOffset::UTC
}
3 changes: 2 additions & 1 deletion src/sys/clock.rs
@@ -1,3 +1,4 @@
use crate::api::clock::DATE_TIME_ZONE;
use crate::sys;
use crate::sys::cmos::CMOS;
use crate::sys::fs::FileIO;
Expand Down Expand Up @@ -105,7 +106,7 @@ pub fn init() {
let s = realtime();
let ns = Duration::nanoseconds(libm::floor(1e9 * (s - libm::floor(s))) as i64);
let dt = OffsetDateTime::from_unix_timestamp(s as i64) + ns;
let rtc = dt.format("%F %H:%M:%S UTC");
let rtc = dt.format(DATE_TIME_ZONE);
log!("RTC {}\n", rtc);
}

Expand Down
3 changes: 2 additions & 1 deletion src/usr/date.rs
@@ -1,9 +1,10 @@
use crate::api;
use crate::api::clock::DATE_TIME_ZONE;
use crate::api::process::ExitCode;
use time::validate_format_string;

pub fn main(args: &[&str]) -> Result<(), ExitCode> {
let format = if args.len() > 1 { args[1] } else { "%F %H:%M:%S" };
let format = if args.len() > 1 { args[1] } else { DATE_TIME_ZONE };
match validate_format_string(format) {
Ok(()) => {
println!("{}", api::time::now().format(format));
Expand Down
4 changes: 2 additions & 2 deletions src/usr/find.rs
Expand Up @@ -44,7 +44,7 @@ pub fn main(args: &[&str]) -> Result<(), ExitCode> {
error!("Missing name");
return Err(ExitCode::UsageError);
}
},
}
"-l" | "--line" => {
if i + 1 < n {
line = Some(args[i + 1]);
Expand All @@ -53,7 +53,7 @@ pub fn main(args: &[&str]) -> Result<(), ExitCode> {
error!("Missing line");
return Err(ExitCode::UsageError);
}
},
}
_ => path = args[i],
}
i += 1;
Expand Down