mirror of
https://github.com/darkrenaissance/darkfi.git
synced 2026-04-28 03:00:18 -04:00
bin/tau: use Timestamp in src/util
This commit is contained in:
@@ -71,8 +71,7 @@ async fn start(mut options: cli::CliTau) -> Result<()> {
|
||||
None => {
|
||||
let task = jsonrpc::get_task_by_id(rpc_addr, id).await?;
|
||||
let taskinfo: TaskInfo = serde_json::from_value(task.clone())?;
|
||||
let default_event =
|
||||
TaskEvent { action: "open".to_string(), timestamp: primitives::Timestamp(0) };
|
||||
let default_event = TaskEvent::default();
|
||||
let state = &taskinfo.events.last().unwrap_or(&default_event).action;
|
||||
println!("Task {}: {}", id, state);
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use super::util::timestamp_to_date;
|
||||
use darkfi::util::Timestamp;
|
||||
|
||||
#[derive(Debug, Clone, Deserialize, Serialize)]
|
||||
pub struct TaskInfo {
|
||||
@@ -51,13 +51,3 @@ impl std::fmt::Display for Comment {
|
||||
write!(f, "{} author: {}, content: {} ", self.timestamp, self.author, self.content)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||||
pub struct Timestamp(pub i64);
|
||||
|
||||
impl std::fmt::Display for Timestamp {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
||||
let date = timestamp_to_date(self.0, "datetime");
|
||||
write!(f, "{}", date)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,7 +5,7 @@ use std::{
|
||||
process::Command,
|
||||
};
|
||||
|
||||
use chrono::{Datelike, Local, NaiveDate, NaiveDateTime};
|
||||
use chrono::{Datelike, Local, NaiveDate};
|
||||
use log::error;
|
||||
use rand::distributions::{Alphanumeric, DistString};
|
||||
|
||||
@@ -77,19 +77,3 @@ pub fn desc_in_editor() -> Result<Option<String>> {
|
||||
|
||||
Ok(Some(description))
|
||||
}
|
||||
|
||||
pub fn timestamp_to_date(timestamp: i64, dt: &str) -> String {
|
||||
if timestamp <= 0 {
|
||||
return "".to_string()
|
||||
}
|
||||
|
||||
match dt {
|
||||
"date" => {
|
||||
NaiveDateTime::from_timestamp(timestamp, 0).date().format("%A %-d %B").to_string()
|
||||
}
|
||||
"datetime" => {
|
||||
NaiveDateTime::from_timestamp(timestamp, 0).format("%H:%M %A %-d %B").to_string()
|
||||
}
|
||||
_ => "".to_string(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,11 +1,10 @@
|
||||
use prettytable::{cell, format, row, table, Cell, Row, Table};
|
||||
|
||||
use darkfi::Result;
|
||||
use darkfi::{util::time::timestamp_to_date, Result};
|
||||
|
||||
use super::{
|
||||
filter::apply_filter,
|
||||
primitives::{Comment, TaskEvent, TaskInfo},
|
||||
util::timestamp_to_date,
|
||||
};
|
||||
|
||||
pub fn print_list_of_task(tasks: &mut Vec<TaskInfo>, filters: Vec<String>) -> Result<()> {
|
||||
@@ -122,7 +121,7 @@ pub fn events_as_string(events: Vec<TaskEvent>) -> String {
|
||||
events_str.push_str("State changed to ");
|
||||
events_str.push_str(&event.action.to_string());
|
||||
events_str.push_str(" at ");
|
||||
events_str.push_str(×tamp_to_date(event.timestamp.0, "datetime"));
|
||||
events_str.push_str(&event.timestamp.to_string());
|
||||
events_str.push('\n');
|
||||
}
|
||||
events_str
|
||||
|
||||
@@ -11,6 +11,7 @@ use darkfi::{
|
||||
jsonrpc::{error as jsonerr, ErrorCode, JsonRequest, JsonResult},
|
||||
rpcserver::RequestHandler,
|
||||
},
|
||||
util::Timestamp,
|
||||
Error,
|
||||
};
|
||||
|
||||
@@ -18,7 +19,6 @@ use crate::{
|
||||
error::{to_json_result, TaudError, TaudResult},
|
||||
month_tasks::MonthTasks,
|
||||
task_info::{Comment, TaskInfo},
|
||||
util::Timestamp,
|
||||
};
|
||||
|
||||
pub struct JsonRpcInterface {
|
||||
|
||||
@@ -7,10 +7,12 @@ use chrono::{TimeZone, Utc};
|
||||
use log::debug;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use darkfi::util::Timestamp;
|
||||
|
||||
use crate::{
|
||||
error::{TaudError, TaudResult},
|
||||
task_info::TaskInfo,
|
||||
util::{get_current_time, load, save, Timestamp},
|
||||
util::{load, save},
|
||||
};
|
||||
|
||||
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
|
||||
@@ -21,7 +23,7 @@ pub struct MonthTasks {
|
||||
|
||||
impl MonthTasks {
|
||||
pub fn new(task_tks: &[String]) -> Self {
|
||||
Self { created_at: get_current_time(), task_tks: task_tks.to_owned() }
|
||||
Self { created_at: Timestamp::current_time(), task_tks: task_tks.to_owned() }
|
||||
}
|
||||
|
||||
pub fn add(&mut self, ref_id: &str) {
|
||||
@@ -180,7 +182,7 @@ mod tests {
|
||||
|
||||
mt.save(&dataset_path)?;
|
||||
|
||||
let mt_load = MonthTasks::load_or_create(Some(&get_current_time()), &dataset_path)?;
|
||||
let mt_load = MonthTasks::load_or_create(Some(&Timestamp::current_time()), &dataset_path)?;
|
||||
|
||||
assert_eq!(mt, mt_load);
|
||||
|
||||
@@ -188,7 +190,7 @@ mod tests {
|
||||
|
||||
mt.save(&dataset_path)?;
|
||||
|
||||
let mt_load = MonthTasks::load_or_create(Some(&get_current_time()), &dataset_path)?;
|
||||
let mt_load = MonthTasks::load_or_create(Some(&Timestamp::current_time()), &dataset_path)?;
|
||||
|
||||
assert_eq!(mt, mt_load);
|
||||
|
||||
@@ -200,7 +202,7 @@ mod tests {
|
||||
|
||||
task.save(&dataset_path)?;
|
||||
|
||||
let mt_load = MonthTasks::load_or_create(Some(&get_current_time()), &dataset_path)?;
|
||||
let mt_load = MonthTasks::load_or_create(Some(&Timestamp::current_time()), &dataset_path)?;
|
||||
|
||||
assert!(mt_load.task_tks.contains(&task.ref_id));
|
||||
|
||||
|
||||
@@ -6,12 +6,16 @@ use std::{
|
||||
use log::debug;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use darkfi::util::{
|
||||
serial::{Decodable, Encodable, SerialDecodable, SerialEncodable, VarInt},
|
||||
Timestamp,
|
||||
};
|
||||
|
||||
use crate::{
|
||||
error::{TaudError, TaudResult},
|
||||
month_tasks::MonthTasks,
|
||||
util::{find_free_id, get_current_time, load, random_ref_id, save, Timestamp},
|
||||
util::{find_free_id, load, random_ref_id, save},
|
||||
};
|
||||
use darkfi::util::serial::{Decodable, Encodable, SerialDecodable, SerialEncodable, VarInt};
|
||||
|
||||
#[derive(Clone, Debug, Serialize, Deserialize, SerialEncodable, SerialDecodable, PartialEq)]
|
||||
struct TaskEvent {
|
||||
@@ -21,7 +25,7 @@ struct TaskEvent {
|
||||
|
||||
impl TaskEvent {
|
||||
fn new(action: String) -> Self {
|
||||
Self { action, timestamp: get_current_time() }
|
||||
Self { action, timestamp: Timestamp::current_time() }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -34,7 +38,11 @@ pub struct Comment {
|
||||
|
||||
impl Comment {
|
||||
pub fn new(content: &str, author: &str) -> Self {
|
||||
Self { content: content.into(), author: author.into(), timestamp: get_current_time() }
|
||||
Self {
|
||||
content: content.into(),
|
||||
author: author.into(),
|
||||
timestamp: Timestamp::current_time(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -75,7 +83,7 @@ impl TaskInfo {
|
||||
// generate ref_id
|
||||
let ref_id = random_ref_id();
|
||||
|
||||
let created_at: Timestamp = get_current_time();
|
||||
let created_at = Timestamp::current_time();
|
||||
|
||||
let task_ids: Vec<u32> =
|
||||
MonthTasks::load_current_open_tasks(dataset_path)?.into_iter().map(|t| t.id).collect();
|
||||
@@ -83,7 +91,7 @@ impl TaskInfo {
|
||||
let id: u32 = find_free_id(&task_ids);
|
||||
|
||||
if let Some(d) = &due {
|
||||
if *d < get_current_time() {
|
||||
if *d < Timestamp::current_time() {
|
||||
return Err(TaudError::InvalidDueTime)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,22 +1,14 @@
|
||||
use std::{fs::File, io::BufReader, path::Path};
|
||||
|
||||
use chrono::Utc;
|
||||
use rand::{distributions::Alphanumeric, thread_rng, Rng};
|
||||
use serde::{de::DeserializeOwned, Deserialize, Serialize};
|
||||
use serde::{de::DeserializeOwned, Serialize};
|
||||
|
||||
use darkfi::{
|
||||
util::serial::{SerialDecodable, SerialEncodable},
|
||||
Result,
|
||||
};
|
||||
use darkfi::Result;
|
||||
|
||||
pub fn random_ref_id() -> String {
|
||||
thread_rng().sample_iter(&Alphanumeric).take(30).map(char::from).collect()
|
||||
}
|
||||
|
||||
pub fn get_current_time() -> Timestamp {
|
||||
Timestamp(Utc::now().timestamp())
|
||||
}
|
||||
|
||||
pub fn find_free_id(task_ids: &[u32]) -> u32 {
|
||||
for i in 1.. {
|
||||
if !task_ids.contains(&i) {
|
||||
@@ -40,11 +32,6 @@ pub fn save<T: Serialize>(path: &Path, value: &T) -> Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[derive(
|
||||
Clone, Debug, Serialize, Deserialize, SerialEncodable, SerialDecodable, PartialEq, PartialOrd,
|
||||
)]
|
||||
pub struct Timestamp(pub i64);
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
@@ -4,6 +4,7 @@ use async_std::{
|
||||
};
|
||||
use chrono::{NaiveDateTime, Utc};
|
||||
use log::debug;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_json::Value;
|
||||
|
||||
use crate::{
|
||||
@@ -12,7 +13,9 @@ use crate::{
|
||||
};
|
||||
|
||||
/// Wrapper struct to represent [`chrono`] UTC timestamps.
|
||||
#[derive(Debug, Copy, Clone, PartialEq, SerialDecodable, SerialEncodable)]
|
||||
#[derive(
|
||||
Clone, Debug, Serialize, Deserialize, SerialEncodable, SerialDecodable, PartialEq, PartialOrd,
|
||||
)]
|
||||
pub struct Timestamp(pub i64);
|
||||
|
||||
impl Timestamp {
|
||||
@@ -35,6 +38,13 @@ impl Timestamp {
|
||||
}
|
||||
}
|
||||
|
||||
impl std::fmt::Display for Timestamp {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
||||
let date = timestamp_to_date(self.0, "datetime");
|
||||
write!(f, "{}", date)
|
||||
}
|
||||
}
|
||||
|
||||
// Clock sync parameters
|
||||
const RETRIES: u8 = 5;
|
||||
const WORLDTIMEAPI_ADDRESS: &str = "worldtimeapi.org";
|
||||
@@ -124,3 +134,19 @@ async fn clock_check() -> Result<()> {
|
||||
false => Err(Error::InvalidClock),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn timestamp_to_date(timestamp: i64, dt: &str) -> String {
|
||||
if timestamp <= 0 {
|
||||
return "".to_string()
|
||||
}
|
||||
|
||||
match dt {
|
||||
"date" => {
|
||||
NaiveDateTime::from_timestamp(timestamp, 0).date().format("%A %-d %B").to_string()
|
||||
}
|
||||
"datetime" => {
|
||||
NaiveDateTime::from_timestamp(timestamp, 0).format("%H:%M %A %-d %B").to_string()
|
||||
}
|
||||
_ => "".to_string(),
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user