Skip to content
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
75 changes: 75 additions & 0 deletions cwa/libcwa/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 cwa/libcwa/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ repository = "https://github.com/Xe/olin"
homepage = "https://github.com/Xe/olin"

[dependencies]
chrono = "0.4"
16 changes: 16 additions & 0 deletions cwa/libcwa/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ pub mod sys {
pub fn resource_write(id: i32, data_ptr: *const u8, data_len: usize) -> i32;
pub fn resource_close(id: i32);
pub fn resource_flush(id: i32) -> i32;

pub fn time_now() -> i64;
}
}

Expand Down Expand Up @@ -267,3 +269,17 @@ impl Write for Resource {
.unwrap_err());
}
}

pub mod time {
extern crate chrono;

use time::chrono::TimeZone;

pub fn now() -> chrono::DateTime<chrono::Utc> {
return chrono::Utc.timestamp(ts(), 0);
}

pub fn ts() -> i64 {
unsafe { ::sys::time_now() }
}
}
75 changes: 75 additions & 0 deletions cwa/tests/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 cwa/tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ authors = ["Christine Dodrill <[email protected]>"]
[dependencies]
libcwa = { path = "../libcwa" }
httparse = "1.3.2"
chrono = "0.4"
12 changes: 12 additions & 0 deletions cwa/tests/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ extern crate libcwa;
use libcwa::*;

mod http;
mod time;

#[no_mangle]
pub extern "C" fn cwa_main() -> i32 {
Expand All @@ -29,6 +30,17 @@ pub extern "C" fn cwa_main() -> i32 {
Err(e) => result = e as i32,
}

if result != 0 {
return result;
}

log::info("time test");
let result: i32;
match time::test() {
Ok(()) => result = 0,
Err(e) => result = e as i32,
}

result
}

Expand Down
19 changes: 19 additions & 0 deletions cwa/tests/src/time.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
extern crate chrono;
extern crate libcwa;

use time::chrono::TimeZone;
use std::io;
use libcwa::*;

pub fn test() -> Result<(), i32> {
let now: i64 = time::ts();
let dt = chrono::Utc.timestamp(now, 0);

log::info(&format!("ts: {}, dt: {}", now, dt.to_rfc3339()));

let now = time::now();
log::info(&format!("time::now(): {}", now.to_rfc3339()));

log::info("time test passed");
Ok(())
}
6 changes: 6 additions & 0 deletions internal/abi/cwa/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,12 @@ func (p *Process) ResolveFunc(module, field string) exec.FunctionImport {

return 0
}
case "time_now":
return func(vm *exec.VirtualMachine) int64 {
p.SetVM(vm)

return p.timeNow()
}
default:
log.Panicf("unknown import %s::%s", module, field)
}
Expand Down
7 changes: 7 additions & 0 deletions internal/abi/cwa/time.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package cwa

import "time"

func (p *Process) timeNow() int64 {
return time.Now().UTC().Unix()
}