From: Dominik Csapak <d.csapak@proxmox.com>
To: pbs-devel@lists.proxmox.com
Subject: [pbs-devel] [PATCH proxmox 09/14] proxmox-time: move TimeSpan into time_span.rs
Date: Tue, 30 Nov 2021 13:12:03 +0100 [thread overview]
Message-ID: <20211130121209.216846-10-d.csapak@proxmox.com> (raw)
In-Reply-To: <20211130121209.216846-1-d.csapak@proxmox.com>
and related parsing functions
Signed-off-by: Dominik Csapak <d.csapak@proxmox.com>
---
proxmox-time/src/lib.rs | 6 +-
proxmox-time/src/parse_time.rs | 157 -------------------
proxmox-time/src/time.rs | 129 +---------------
proxmox-time/src/time_span.rs | 265 +++++++++++++++++++++++++++++++++
4 files changed, 269 insertions(+), 288 deletions(-)
delete mode 100644 proxmox-time/src/parse_time.rs
create mode 100644 proxmox-time/src/time_span.rs
diff --git a/proxmox-time/src/lib.rs b/proxmox-time/src/lib.rs
index 0436628..cbb7008 100644
--- a/proxmox-time/src/lib.rs
+++ b/proxmox-time/src/lib.rs
@@ -7,9 +7,6 @@ use anyhow::{bail, format_err, Error};
mod tm_editor;
pub use tm_editor::*;
-mod parse_time;
-pub use parse_time::*;
-
mod time;
pub use time::*;
@@ -20,6 +17,9 @@ pub(crate) mod date_time_value;
mod calendar_event;
pub use calendar_event::*;
+mod time_span;
+pub use time_span::*;
+
mod week_days;
pub use week_days::*;
diff --git a/proxmox-time/src/parse_time.rs b/proxmox-time/src/parse_time.rs
deleted file mode 100644
index 51c0486..0000000
--- a/proxmox-time/src/parse_time.rs
+++ /dev/null
@@ -1,157 +0,0 @@
-use std::collections::HashMap;
-
-use anyhow::{bail, Error};
-use lazy_static::lazy_static;
-
-use super::time::*;
-use super::daily_duration::*;
-
-use nom::{
- error::{context, ParseError, VerboseError},
- bytes::complete::{tag, take_while1},
- combinator::{map_res, all_consuming, opt, recognize},
- sequence::{pair, preceded, terminated, tuple},
- character::complete::{alpha1, space0, digit1},
- multi::separated_nonempty_list,
-};
-
-use crate::parse_helpers::{parse_complete_line, parse_error, parse_hm_time, parse_time_comp, parse_u64, IResult};
-use crate::{parse_weekdays_range, WeekDays};
-use crate::date_time_value::DateTimeValue;
-
-lazy_static! {
- static ref TIME_SPAN_UNITS: HashMap<&'static str, f64> = {
- let mut map = HashMap::new();
-
- let second = 1.0;
-
- map.insert("seconds", second);
- map.insert("second", second);
- map.insert("sec", second);
- map.insert("s", second);
-
- let msec = second / 1000.0;
-
- map.insert("msec", msec);
- map.insert("ms", msec);
-
- let usec = msec / 1000.0;
-
- map.insert("usec", usec);
- map.insert("us", usec);
- map.insert("µs", usec);
-
- let nsec = usec / 1000.0;
-
- map.insert("nsec", nsec);
- map.insert("ns", nsec);
-
- let minute = second * 60.0;
-
- map.insert("minutes", minute);
- map.insert("minute", minute);
- map.insert("min", minute);
- map.insert("m", minute);
-
- let hour = minute * 60.0;
-
- map.insert("hours", hour);
- map.insert("hour", hour);
- map.insert("hr", hour);
- map.insert("h", hour);
-
- let day = hour * 24.0 ;
-
- map.insert("days", day);
- map.insert("day", day);
- map.insert("d", day);
-
- let week = day * 7.0;
-
- map.insert("weeks", week);
- map.insert("week", week);
- map.insert("w", week);
-
- let month = 30.44 * day;
-
- map.insert("months", month);
- map.insert("month", month);
- map.insert("M", month);
-
- let year = 365.25 * day;
-
- map.insert("years", year);
- map.insert("year", year);
- map.insert("y", year);
-
- map
- };
-}
-
-fn parse_time_unit(i: &str) -> IResult<&str, &str> {
- let (n, text) = take_while1(|c: char| char::is_ascii_alphabetic(&c) || c == 'µ')(i)?;
- if TIME_SPAN_UNITS.contains_key(&text) {
- Ok((n, text))
- } else {
- Err(parse_error(text, "time unit"))
- }
-}
-
-
-/// Parse a [TimeSpan]
-pub fn parse_time_span(i: &str) -> Result<TimeSpan, Error> {
- parse_complete_line("time span", i, parse_time_span_incomplete)
-}
-
-fn parse_time_span_incomplete(mut i: &str) -> IResult<&str, TimeSpan> {
-
- let mut ts = TimeSpan::default();
-
- loop {
- i = space0(i)?.0;
- if i.is_empty() { break; }
- let (n, num) = parse_u64(i)?;
- i = space0(n)?.0;
-
- if let (n, Some(unit)) = opt(parse_time_unit)(i)? {
- i = n;
- match unit {
- "seconds" | "second" | "sec" | "s" => {
- ts.seconds += num;
- }
- "msec" | "ms" => {
- ts.msec += num;
- }
- "usec" | "us" | "µs" => {
- ts.usec += num;
- }
- "nsec" | "ns" => {
- ts.nsec += num;
- }
- "minutes" | "minute" | "min" | "m" => {
- ts.minutes += num;
- }
- "hours" | "hour" | "hr" | "h" => {
- ts.hours += num;
- }
- "days" | "day" | "d" => {
- ts.days += num;
- }
- "weeks" | "week" | "w" => {
- ts.weeks += num;
- }
- "months" | "month" | "M" => {
- ts.months += num;
- }
- "years" | "year" | "y" => {
- ts.years += num;
- }
- _ => return Err(parse_error(unit, "internal error")),
- }
- } else {
- ts.seconds += num;
- }
- }
-
- Ok((i, ts))
-}
diff --git a/proxmox-time/src/time.rs b/proxmox-time/src/time.rs
index 1c45a09..ac2f510 100644
--- a/proxmox-time/src/time.rs
+++ b/proxmox-time/src/time.rs
@@ -1,136 +1,9 @@
-use anyhow::Error;
-
-use crate::date_time_value::DateTimeValue;
-use crate::TmEditor;
-use crate::WeekDays;
-
-use crate::{compute_next_event, parse_calendar_event, parse_time_span};
-
-/// A time spans defines a time duration
-#[derive(Default, Clone, Debug)]
-pub struct TimeSpan {
- pub nsec: u64,
- pub usec: u64,
- pub msec: u64,
- pub seconds: u64,
- pub minutes: u64,
- pub hours: u64,
- pub days: u64,
- pub weeks: u64,
- pub months: u64,
- pub years: u64,
-}
-
-impl From<TimeSpan> for f64 {
- fn from(ts: TimeSpan) -> Self {
- (ts.seconds as f64) +
- ((ts.nsec as f64) / 1_000_000_000.0) +
- ((ts.usec as f64) / 1_000_000.0) +
- ((ts.msec as f64) / 1_000.0) +
- ((ts.minutes as f64) * 60.0) +
- ((ts.hours as f64) * 3600.0) +
- ((ts.days as f64) * 3600.0 * 24.0) +
- ((ts.weeks as f64) * 3600.0 * 24.0 * 7.0) +
- ((ts.months as f64) * 3600.0 * 24.0 * 30.44) +
- ((ts.years as f64) * 3600.0 * 24.0 * 365.25)
- }
-}
-
-impl From<std::time::Duration> for TimeSpan {
- fn from(duration: std::time::Duration) -> Self {
- let mut duration = duration.as_nanos();
- let nsec = (duration % 1000) as u64;
- duration /= 1000;
- let usec = (duration % 1000) as u64;
- duration /= 1000;
- let msec = (duration % 1000) as u64;
- duration /= 1000;
- let seconds = (duration % 60) as u64;
- duration /= 60;
- let minutes = (duration % 60) as u64;
- duration /= 60;
- let hours = (duration % 24) as u64;
- duration /= 24;
- let years = (duration as f64 / 365.25) as u64;
- let ydays = (duration as f64 % 365.25) as u64;
- let months = (ydays as f64 / 30.44) as u64;
- let mdays = (ydays as f64 % 30.44) as u64;
- let weeks = mdays / 7;
- let days = mdays % 7;
- Self {
- nsec,
- usec,
- msec,
- seconds,
- minutes,
- hours,
- days,
- weeks,
- months,
- years,
- }
- }
-}
-
-impl std::fmt::Display for TimeSpan {
- fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
- let mut first = true;
- { // block scope for mutable borrows
- let mut do_write = |v: u64, unit: &str| -> Result<(), std::fmt::Error> {
- if !first {
- write!(f, " ")?;
- }
- first = false;
- write!(f, "{}{}", v, unit)
- };
- if self.years > 0 {
- do_write(self.years, "y")?;
- }
- if self.months > 0 {
- do_write(self.months, "m")?;
- }
- if self.weeks > 0 {
- do_write(self.weeks, "w")?;
- }
- if self.days > 0 {
- do_write(self.days, "d")?;
- }
- if self.hours > 0 {
- do_write(self.hours, "h")?;
- }
- if self.minutes > 0 {
- do_write(self.minutes, "min")?;
- }
- }
- if !first {
- write!(f, " ")?;
- }
- let seconds = self.seconds as f64 + (self.msec as f64 / 1000.0);
- if seconds >= 0.1 {
- if seconds >= 1.0 || !first {
- write!(f, "{:.0}s", seconds)?;
- } else {
- write!(f, "{:.1}s", seconds)?;
- }
- } else if first {
- write!(f, "<0.1s")?;
- }
- Ok(())
- }
-}
-
-/// Verify the format of the [TimeSpan]
-pub fn verify_time_span(i: &str) -> Result<(), Error> {
- parse_time_span(i)?;
- Ok(())
-}
-
#[cfg(test)]
mod test {
use anyhow::bail;
+ use crate::*;
- use super::*;
fn test_event(v: &'static str) -> Result<(), Error> {
match parse_calendar_event(v) {
diff --git a/proxmox-time/src/time_span.rs b/proxmox-time/src/time_span.rs
new file mode 100644
index 0000000..2f8cf59
--- /dev/null
+++ b/proxmox-time/src/time_span.rs
@@ -0,0 +1,265 @@
+use std::collections::HashMap;
+
+use anyhow::Error;
+use lazy_static::lazy_static;
+use nom::{bytes::complete::take_while1, character::complete::space0, combinator::opt};
+
+use crate::parse_helpers::{parse_complete_line, parse_error, parse_u64, IResult};
+
+lazy_static! {
+ static ref TIME_SPAN_UNITS: HashMap<&'static str, f64> = {
+ let mut map = HashMap::new();
+
+ let second = 1.0;
+
+ map.insert("seconds", second);
+ map.insert("second", second);
+ map.insert("sec", second);
+ map.insert("s", second);
+
+ let msec = second / 1000.0;
+
+ map.insert("msec", msec);
+ map.insert("ms", msec);
+
+ let usec = msec / 1000.0;
+
+ map.insert("usec", usec);
+ map.insert("us", usec);
+ map.insert("µs", usec);
+
+ let nsec = usec / 1000.0;
+
+ map.insert("nsec", nsec);
+ map.insert("ns", nsec);
+
+ let minute = second * 60.0;
+
+ map.insert("minutes", minute);
+ map.insert("minute", minute);
+ map.insert("min", minute);
+ map.insert("m", minute);
+
+ let hour = minute * 60.0;
+
+ map.insert("hours", hour);
+ map.insert("hour", hour);
+ map.insert("hr", hour);
+ map.insert("h", hour);
+
+ let day = hour * 24.0;
+
+ map.insert("days", day);
+ map.insert("day", day);
+ map.insert("d", day);
+
+ let week = day * 7.0;
+
+ map.insert("weeks", week);
+ map.insert("week", week);
+ map.insert("w", week);
+
+ let month = 30.44 * day;
+
+ map.insert("months", month);
+ map.insert("month", month);
+ map.insert("M", month);
+
+ let year = 365.25 * day;
+
+ map.insert("years", year);
+ map.insert("year", year);
+ map.insert("y", year);
+
+ map
+ };
+}
+
+/// A time spans defines a time duration
+#[derive(Default, Clone, Debug)]
+pub struct TimeSpan {
+ pub nsec: u64,
+ pub usec: u64,
+ pub msec: u64,
+ pub seconds: u64,
+ pub minutes: u64,
+ pub hours: u64,
+ pub days: u64,
+ pub weeks: u64,
+ pub months: u64,
+ pub years: u64,
+}
+
+impl From<TimeSpan> for f64 {
+ fn from(ts: TimeSpan) -> Self {
+ (ts.seconds as f64)
+ + ((ts.nsec as f64) / 1_000_000_000.0)
+ + ((ts.usec as f64) / 1_000_000.0)
+ + ((ts.msec as f64) / 1_000.0)
+ + ((ts.minutes as f64) * 60.0)
+ + ((ts.hours as f64) * 3600.0)
+ + ((ts.days as f64) * 3600.0 * 24.0)
+ + ((ts.weeks as f64) * 3600.0 * 24.0 * 7.0)
+ + ((ts.months as f64) * 3600.0 * 24.0 * 30.44)
+ + ((ts.years as f64) * 3600.0 * 24.0 * 365.25)
+ }
+}
+
+impl From<std::time::Duration> for TimeSpan {
+ fn from(duration: std::time::Duration) -> Self {
+ let mut duration = duration.as_nanos();
+ let nsec = (duration % 1000) as u64;
+ duration /= 1000;
+ let usec = (duration % 1000) as u64;
+ duration /= 1000;
+ let msec = (duration % 1000) as u64;
+ duration /= 1000;
+ let seconds = (duration % 60) as u64;
+ duration /= 60;
+ let minutes = (duration % 60) as u64;
+ duration /= 60;
+ let hours = (duration % 24) as u64;
+ duration /= 24;
+ let years = (duration as f64 / 365.25) as u64;
+ let ydays = (duration as f64 % 365.25) as u64;
+ let months = (ydays as f64 / 30.44) as u64;
+ let mdays = (ydays as f64 % 30.44) as u64;
+ let weeks = mdays / 7;
+ let days = mdays % 7;
+ Self {
+ nsec,
+ usec,
+ msec,
+ seconds,
+ minutes,
+ hours,
+ days,
+ weeks,
+ months,
+ years,
+ }
+ }
+}
+
+impl std::fmt::Display for TimeSpan {
+ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
+ let mut first = true;
+ {
+ // block scope for mutable borrows
+ let mut do_write = |v: u64, unit: &str| -> Result<(), std::fmt::Error> {
+ if !first {
+ write!(f, " ")?;
+ }
+ first = false;
+ write!(f, "{}{}", v, unit)
+ };
+ if self.years > 0 {
+ do_write(self.years, "y")?;
+ }
+ if self.months > 0 {
+ do_write(self.months, "m")?;
+ }
+ if self.weeks > 0 {
+ do_write(self.weeks, "w")?;
+ }
+ if self.days > 0 {
+ do_write(self.days, "d")?;
+ }
+ if self.hours > 0 {
+ do_write(self.hours, "h")?;
+ }
+ if self.minutes > 0 {
+ do_write(self.minutes, "min")?;
+ }
+ }
+ if !first {
+ write!(f, " ")?;
+ }
+ let seconds = self.seconds as f64 + (self.msec as f64 / 1000.0);
+ if seconds >= 0.1 {
+ if seconds >= 1.0 || !first {
+ write!(f, "{:.0}s", seconds)?;
+ } else {
+ write!(f, "{:.1}s", seconds)?;
+ }
+ } else if first {
+ write!(f, "<0.1s")?;
+ }
+ Ok(())
+ }
+}
+
+fn parse_time_unit(i: &str) -> IResult<&str, &str> {
+ let (n, text) = take_while1(|c: char| char::is_ascii_alphabetic(&c) || c == 'µ')(i)?;
+ if TIME_SPAN_UNITS.contains_key(&text) {
+ Ok((n, text))
+ } else {
+ Err(parse_error(text, "time unit"))
+ }
+}
+
+/// Parse a [TimeSpan]
+pub fn parse_time_span(i: &str) -> Result<TimeSpan, Error> {
+ parse_complete_line("time span", i, parse_time_span_incomplete)
+}
+
+fn parse_time_span_incomplete(mut i: &str) -> IResult<&str, TimeSpan> {
+ let mut ts = TimeSpan::default();
+
+ loop {
+ i = space0(i)?.0;
+ if i.is_empty() {
+ break;
+ }
+ let (n, num) = parse_u64(i)?;
+ i = space0(n)?.0;
+
+ if let (n, Some(unit)) = opt(parse_time_unit)(i)? {
+ i = n;
+ match unit {
+ "seconds" | "second" | "sec" | "s" => {
+ ts.seconds += num;
+ }
+ "msec" | "ms" => {
+ ts.msec += num;
+ }
+ "usec" | "us" | "µs" => {
+ ts.usec += num;
+ }
+ "nsec" | "ns" => {
+ ts.nsec += num;
+ }
+ "minutes" | "minute" | "min" | "m" => {
+ ts.minutes += num;
+ }
+ "hours" | "hour" | "hr" | "h" => {
+ ts.hours += num;
+ }
+ "days" | "day" | "d" => {
+ ts.days += num;
+ }
+ "weeks" | "week" | "w" => {
+ ts.weeks += num;
+ }
+ "months" | "month" | "M" => {
+ ts.months += num;
+ }
+ "years" | "year" | "y" => {
+ ts.years += num;
+ }
+ _ => return Err(parse_error(unit, "internal error")),
+ }
+ } else {
+ ts.seconds += num;
+ }
+ }
+
+ Ok((i, ts))
+}
+
+/// Verify the format of the [TimeSpan]
+pub fn verify_time_span(i: &str) -> Result<(), Error> {
+ parse_time_span(i)?;
+ Ok(())
+}
+
--
2.30.2
next prev parent reply other threads:[~2021-11-30 12:13 UTC|newest]
Thread overview: 18+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-11-30 12:11 [pbs-devel] [PATCH proxmox/proxmox-backup 00/14] improve & cleanup proxmox-time Dominik Csapak
2021-11-30 12:11 ` [pbs-devel] [PATCH proxmox 01/14] proxmox-time: calendar-events: implement repeated ranges Dominik Csapak
2021-11-30 12:11 ` [pbs-devel] [PATCH proxmox 02/14] proxmox-time: calendar-events: make hour optional Dominik Csapak
2021-11-30 12:11 ` [pbs-devel] [PATCH proxmox 03/14] proxmox-time: move common parse functions to parse_helpers Dominik Csapak
2021-11-30 12:11 ` [pbs-devel] [PATCH proxmox 04/14] proxmox-time: move WeekDays into own file Dominik Csapak
2021-11-30 12:11 ` [pbs-devel] [PATCH proxmox 05/14] proxmox-time: split DateTimeValue " Dominik Csapak
2021-11-30 12:12 ` [pbs-devel] [PATCH proxmox 06/14] proxmox-time: move parse_daily_duration to daily_duration.rs Dominik Csapak
2021-11-30 12:12 ` [pbs-devel] [PATCH proxmox 07/14] proxmox-time: daily_duration.rs: rustfmt Dominik Csapak
2021-11-30 12:12 ` [pbs-devel] [PATCH proxmox 08/14] proxmox-time: move CalendarEvent into calendar_events.rs Dominik Csapak
2021-11-30 12:12 ` Dominik Csapak [this message]
2021-11-30 12:12 ` [pbs-devel] [PATCH proxmox 10/14] proxmox-time: move tests from time.rs to test.rs Dominik Csapak
2021-11-30 12:12 ` [pbs-devel] [PATCH proxmox 11/14] proxmox-time: lib.rs: rustfmt Dominik Csapak
2021-11-30 12:12 ` [pbs-devel] [PATCH proxmox 12/14] proxmox-time: calendar-events: make compute_next_event a method Dominik Csapak
2021-11-30 12:12 ` [pbs-devel] [PATCH proxmox 13/14] proxmox-time: calendar_events: implement FromStr Dominik Csapak
2021-11-30 12:12 ` [pbs-devel] [PATCH proxmox 14/14] proxmox-time: time-span: " Dominik Csapak
2021-11-30 12:12 ` [pbs-devel] [PATCH proxmox-backup 1/1] remove use of deprecated functions from proxmox-time Dominik Csapak
2021-12-01 6:26 ` [pbs-devel] applied: " Dietmar Maurer
2021-12-01 6:20 ` [pbs-devel] applied: [PATCH proxmox/proxmox-backup 00/14] improve & cleanup proxmox-time Dietmar Maurer
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20211130121209.216846-10-d.csapak@proxmox.com \
--to=d.csapak@proxmox.com \
--cc=pbs-devel@lists.proxmox.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal