Skip to content

Commit

Permalink
Add: NASL builtin function gettimeofday
Browse files Browse the repository at this point in the history
There are no tests, as the time is system specific
  • Loading branch information
Kraemii authored and ArnoStiefvater committed May 15, 2024
1 parent c1d4337 commit 8abd9c9
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
2 changes: 1 addition & 1 deletion rust/nasl-builtin-misc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
- gzip
- gunzip
- defined_func
- gettimeofday

## Missing
- dump_ctxt
- gettimeofday
- index
- keys
- make_array
Expand Down
24 changes: 23 additions & 1 deletion rust/nasl-builtin-misc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use std::{
fs::File,
io::{Read, Write},
thread,
time::{Duration, UNIX_EPOCH},
time::{self, Duration, UNIX_EPOCH},
};

use chrono::{
Expand Down Expand Up @@ -294,6 +294,27 @@ where
})
}

/// Returns the seconds and microseconds counted from 1st January 1970. It formats a string
/// containing the seconds separated by a `.` followed by the microseconds.
///
/// For example: “1067352015.030757” means 1067352015 seconds and 30757 microseconds.
fn gettimeofday<K>(_: &Register, _: &Context<K>) -> Result<NaslValue, FunctionErrorKind>
where
K: AsRef<str>,
{
match time::SystemTime::now().duration_since(time::SystemTime::UNIX_EPOCH) {
Ok(time) => {
let time = time.as_micros();
Ok(NaslValue::String(format!(
"{}.{:06}",
time / 1000000,
time % 1000000
)))
}
Err(e) => Err(FunctionErrorKind::Dirty(format!("{e}"))),
}
}

/// Returns found function for key or None when not found
fn lookup<K>(key: &str) -> Option<NaslFunction<K>>
where
Expand All @@ -313,6 +334,7 @@ where
"gzip" => Some(gzip),
"gunzip" => Some(gunzip),
"defined_func" => Some(defined_func),
"gettimeofday" => Some(gettimeofday),
_ => None,
}
}
Expand Down

0 comments on commit 8abd9c9

Please sign in to comment.