Skip to content

Commit 907996b

Browse files
committed
Refactor
1 parent 4572a7f commit 907996b

3 files changed

Lines changed: 21 additions & 11 deletions

File tree

src/core/database.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@
44
mod data;
55

66
use self::data::Data;
7-
use self::data::epochrealtime::EpochRealtime;
8-
use self::data::epochseconds::EpochSeconds;
7+
use self::data::epochtime::EpochTime;
98
use self::data::random::RandomVar;
109
use self::data::seconds::Seconds;
1110
use self::data::srandom::SRandomVar;
1211
use self::data::single::SingleData;
1312
use crate::error::exec::ExecError;
1413
use crate::elements::command::function_def::FunctionDefinition;
14+
use crate::utils::clock;
1515
use std::collections::{HashMap, HashSet};
1616
use std::env;
1717

@@ -33,8 +33,10 @@ impl DataBase {
3333
ans.params[0].insert("RANDOM".to_string(), Box::new(RandomVar::new()));
3434
ans.params[0].insert("SRANDOM".to_string(), Box::new(SRandomVar::new()));
3535
ans.params[0].insert("SECONDS".to_string(), Box::new(Seconds::new()));
36-
ans.params[0].insert("EPOCHREALTIME".to_string(), Box::new(EpochRealtime::default()));
37-
ans.params[0].insert("EPOCHSECONDS".to_string(), Box::new(EpochSeconds::default()));
36+
ans.params[0].insert("EPOCHSECONDS".to_string(),
37+
Box::new(EpochTime::new(clock::get_epochseconds)));
38+
ans.params[0].insert("EPOCHREALTIME".to_string(),
39+
Box::new(EpochTime::new(clock::get_epochrealtime)));
3840
ans
3941
}
4042

src/core/database/data.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
//SPDXFileCopyrightText: 2025 Ryuichi Ueda ryuichiueda@gmail.com
22
//SPDXLicense-Identifier: BSD-3-Clause
33

4-
pub mod epochrealtime;
5-
pub mod epochseconds;
4+
pub mod epochtime;
65
pub mod random;
76
pub mod srandom;
87
pub mod seconds;
Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22
//SPDXLicense-Identifier: BSD-3-Clause
33

44
use super::{Data, ExecError};
5-
use crate::utils::clock;
65

7-
#[derive(Debug, Clone, Default)]
8-
pub struct EpochSeconds {
6+
#[derive(Debug, Clone)]
7+
pub struct EpochTime {
8+
timefunc: fn() -> String,
99
flags: String,
1010
}
1111

12-
impl Data for EpochSeconds {
12+
impl Data for EpochTime {
1313
fn boxed_clone(&self) -> Box<dyn Data> {
1414
Box::new(self.clone())
1515
}
@@ -19,7 +19,7 @@ impl Data for EpochSeconds {
1919
}
2020

2121
fn get_as_single(&mut self) -> Result<String, ExecError> {
22-
Ok(clock::get_epochseconds())
22+
Ok((self.timefunc)())
2323
}
2424

2525
fn set_as_single(&mut self, name: &str, _: &str) -> Result<(), ExecError> {
@@ -36,3 +36,12 @@ impl Data for EpochSeconds {
3636
&self.flags
3737
}
3838
}
39+
40+
impl EpochTime {
41+
pub fn new(timefn: fn() -> String) -> Self {
42+
Self {
43+
timefunc: timefn,
44+
flags: "".to_string(),
45+
}
46+
}
47+
}

0 commit comments

Comments
 (0)