avoid chrono dependency, depend on proxmox 0.3.8
- remove chrono dependency - depend on proxmox 0.3.8 - remove epoch_now, epoch_now_u64 and epoch_now_f64 - remove tm_editor (moved to proxmox crate) - use new helpers from proxmox 0.3.8 * epoch_i64 and epoch_f64 * parse_rfc3339 * epoch_to_rfc3339_utc * strftime_local - BackupDir changes: * store epoch and rfc3339 string instead of DateTime * backup_time_to_string now return a Result * remove unnecessary TryFrom<(BackupGroup, i64)> for BackupDir - DynamicIndexHeader: change ctime to i64 - FixedIndexHeader: change ctime to i64
This commit is contained in:
@ -1,5 +1,4 @@
|
||||
use anyhow::{Error};
|
||||
use chrono::Local;
|
||||
use std::io::Write;
|
||||
|
||||
/// Log messages with timestamps into files
|
||||
@ -56,7 +55,10 @@ impl FileLogger {
|
||||
stdout.write_all(b"\n").unwrap();
|
||||
}
|
||||
|
||||
let line = format!("{}: {}\n", Local::now().to_rfc3339(), msg);
|
||||
let now = proxmox::tools::time::epoch_i64();
|
||||
let rfc3339 = proxmox::tools::time::epoch_to_rfc3339(now).unwrap();
|
||||
|
||||
let line = format!("{}: {}\n", rfc3339, msg);
|
||||
self.file.write_all(line.as_bytes()).unwrap();
|
||||
}
|
||||
}
|
||||
|
@ -1,6 +1,5 @@
|
||||
use anyhow::{Error};
|
||||
use serde_json::Value;
|
||||
use chrono::{Local, TimeZone, LocalResult};
|
||||
|
||||
pub fn strip_server_file_expenstion(name: &str) -> String {
|
||||
|
||||
@ -25,9 +24,10 @@ pub fn render_epoch(value: &Value, _record: &Value) -> Result<String, Error> {
|
||||
if value.is_null() { return Ok(String::new()); }
|
||||
let text = match value.as_i64() {
|
||||
Some(epoch) => {
|
||||
match Local.timestamp_opt(epoch, 0) {
|
||||
LocalResult::Single(epoch) => epoch.format("%c").to_string(),
|
||||
_ => epoch.to_string(),
|
||||
if let Ok(epoch_string) = proxmox::tools::time::strftime_local("%c", epoch as i64) {
|
||||
epoch_string
|
||||
} else {
|
||||
epoch.to_string()
|
||||
}
|
||||
},
|
||||
None => {
|
||||
|
@ -2,7 +2,6 @@ pub mod types;
|
||||
pub mod config;
|
||||
|
||||
mod parse_time;
|
||||
pub mod tm_editor;
|
||||
pub mod time;
|
||||
|
||||
use anyhow::{bail, Error};
|
||||
|
@ -3,8 +3,9 @@ use std::convert::TryInto;
|
||||
use anyhow::Error;
|
||||
use bitflags::bitflags;
|
||||
|
||||
use proxmox::tools::time::TmEditor;
|
||||
|
||||
pub use super::parse_time::*;
|
||||
use super::tm_editor::*;
|
||||
|
||||
bitflags!{
|
||||
#[derive(Default)]
|
||||
@ -161,7 +162,7 @@ pub fn compute_next_event(
|
||||
|
||||
let all_days = event.days.is_empty() || event.days.is_all();
|
||||
|
||||
let mut t = TmEditor::new(last, utc)?;
|
||||
let mut t = TmEditor::with_epoch(last, utc)?;
|
||||
|
||||
let mut count = 0;
|
||||
|
||||
|
@ -1,119 +0,0 @@
|
||||
use anyhow::Error;
|
||||
|
||||
use proxmox::tools::time::*;
|
||||
|
||||
pub struct TmEditor {
|
||||
utc: bool,
|
||||
t: libc::tm,
|
||||
}
|
||||
|
||||
impl TmEditor {
|
||||
|
||||
pub fn new(epoch: i64, utc: bool) -> Result<Self, Error> {
|
||||
let t = if utc { gmtime(epoch)? } else { localtime(epoch)? };
|
||||
Ok(Self { utc, t })
|
||||
}
|
||||
|
||||
pub fn into_epoch(mut self) -> Result<i64, Error> {
|
||||
let epoch = if self.utc { timegm(&mut self.t)? } else { timelocal(&mut self.t)? };
|
||||
Ok(epoch)
|
||||
}
|
||||
|
||||
/// increases the year by 'years' and resets all smaller fields to their minimum
|
||||
pub fn add_years(&mut self, years: libc::c_int) -> Result<(), Error> {
|
||||
if years == 0 { return Ok(()); }
|
||||
self.t.tm_mon = 0;
|
||||
self.t.tm_mday = 1;
|
||||
self.t.tm_hour = 0;
|
||||
self.t.tm_min = 0;
|
||||
self.t.tm_sec = 0;
|
||||
self.t.tm_year += years;
|
||||
self.normalize_time()
|
||||
}
|
||||
|
||||
/// increases the month by 'months' and resets all smaller fields to their minimum
|
||||
pub fn add_months(&mut self, months: libc::c_int) -> Result<(), Error> {
|
||||
if months == 0 { return Ok(()); }
|
||||
self.t.tm_mday = 1;
|
||||
self.t.tm_hour = 0;
|
||||
self.t.tm_min = 0;
|
||||
self.t.tm_sec = 0;
|
||||
self.t.tm_mon += months;
|
||||
self.normalize_time()
|
||||
}
|
||||
|
||||
/// increases the day by 'days' and resets all smaller fields to their minimum
|
||||
pub fn add_days(&mut self, days: libc::c_int) -> Result<(), Error> {
|
||||
if days == 0 { return Ok(()); }
|
||||
self.t.tm_hour = 0;
|
||||
self.t.tm_min = 0;
|
||||
self.t.tm_sec = 0;
|
||||
self.t.tm_mday += days;
|
||||
self.normalize_time()
|
||||
}
|
||||
|
||||
pub fn year(&self) -> libc::c_int { self.t.tm_year + 1900 } // see man mktime
|
||||
pub fn month(&self) -> libc::c_int { self.t.tm_mon + 1 }
|
||||
pub fn day(&self) -> libc::c_int { self.t.tm_mday }
|
||||
pub fn hour(&self) -> libc::c_int { self.t.tm_hour }
|
||||
pub fn min(&self) -> libc::c_int { self.t.tm_min }
|
||||
pub fn sec(&self) -> libc::c_int { self.t.tm_sec }
|
||||
|
||||
// Note: tm_wday (0-6, Sunday = 0) => convert to Sunday = 6
|
||||
pub fn day_num(&self) -> libc::c_int {
|
||||
(self.t.tm_wday + 6) % 7
|
||||
}
|
||||
|
||||
pub fn set_time(&mut self, hour: libc::c_int, min: libc::c_int, sec: libc::c_int) -> Result<(), Error> {
|
||||
self.t.tm_hour = hour;
|
||||
self.t.tm_min = min;
|
||||
self.t.tm_sec = sec;
|
||||
self.normalize_time()
|
||||
}
|
||||
|
||||
pub fn set_min_sec(&mut self, min: libc::c_int, sec: libc::c_int) -> Result<(), Error> {
|
||||
self.t.tm_min = min;
|
||||
self.t.tm_sec = sec;
|
||||
self.normalize_time()
|
||||
}
|
||||
|
||||
fn normalize_time(&mut self) -> Result<(), Error> {
|
||||
// libc normalizes it for us
|
||||
if self.utc {
|
||||
timegm(&mut self.t)?;
|
||||
} else {
|
||||
timelocal(&mut self.t)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn set_sec(&mut self, v: libc::c_int) -> Result<(), Error> {
|
||||
self.t.tm_sec = v;
|
||||
self.normalize_time()
|
||||
}
|
||||
|
||||
pub fn set_min(&mut self, v: libc::c_int) -> Result<(), Error> {
|
||||
self.t.tm_min = v;
|
||||
self.normalize_time()
|
||||
}
|
||||
|
||||
pub fn set_hour(&mut self, v: libc::c_int) -> Result<(), Error> {
|
||||
self.t.tm_hour = v;
|
||||
self.normalize_time()
|
||||
}
|
||||
|
||||
pub fn set_mday(&mut self, v: libc::c_int) -> Result<(), Error> {
|
||||
self.t.tm_mday = v;
|
||||
self.normalize_time()
|
||||
}
|
||||
|
||||
pub fn set_mon(&mut self, v: libc::c_int) -> Result<(), Error> {
|
||||
self.t.tm_mon = v - 1;
|
||||
self.normalize_time()
|
||||
}
|
||||
|
||||
pub fn set_year(&mut self, v: libc::c_int) -> Result<(), Error> {
|
||||
self.t.tm_year = v - 1900;
|
||||
self.normalize_time()
|
||||
}
|
||||
}
|
@ -11,7 +11,6 @@ use openssl::sign::{Signer, Verifier};
|
||||
use percent_encoding::{percent_decode_str, percent_encode, AsciiSet};
|
||||
|
||||
use crate::api2::types::Userid;
|
||||
use crate::tools::epoch_now_u64;
|
||||
|
||||
pub const TICKET_LIFETIME: i64 = 3600 * 2; // 2 hours
|
||||
|
||||
@ -69,7 +68,7 @@ where
|
||||
Ok(Self {
|
||||
prefix: Cow::Borrowed(prefix),
|
||||
data: data.to_string(),
|
||||
time: epoch_now_u64()? as i64,
|
||||
time: proxmox::tools::time::epoch_i64(),
|
||||
signature: None,
|
||||
_type_marker: PhantomData,
|
||||
})
|
||||
@ -174,7 +173,7 @@ where
|
||||
None => bail!("invalid ticket without signature"),
|
||||
};
|
||||
|
||||
let age = epoch_now_u64()? as i64 - self.time;
|
||||
let age = proxmox::tools::time::epoch_i64() - self.time;
|
||||
if age < time_frame.start {
|
||||
bail!("invalid ticket - timestamp newer than expected");
|
||||
}
|
||||
@ -272,7 +271,6 @@ mod test {
|
||||
|
||||
use super::Ticket;
|
||||
use crate::api2::types::Userid;
|
||||
use crate::tools::epoch_now_u64;
|
||||
|
||||
fn simple_test<F>(key: &PKey<Private>, aad: Option<&str>, modify: F)
|
||||
where
|
||||
@ -314,7 +312,7 @@ mod test {
|
||||
false
|
||||
});
|
||||
simple_test(&key, None, |t| {
|
||||
t.change_time(epoch_now_u64().unwrap() as i64 + 0x1000_0000);
|
||||
t.change_time(proxmox::tools::time::epoch_i64() + 0x1000_0000);
|
||||
false
|
||||
});
|
||||
}
|
||||
|
Reference in New Issue
Block a user