From: Dominik Csapak <d.csapak@proxmox.com>
To: Thomas Lamprecht <t.lamprecht@proxmox.com>,
Proxmox Backup Server development discussion
<pbs-devel@lists.proxmox.com>
Subject: Re: [pbs-devel] [PATCH proxmox-backup 1/3] tools/systemd/time: implement some Traits for TimeSpan
Date: Thu, 18 Mar 2021 10:49:31 +0100 [thread overview]
Message-ID: <d4e77a90-198b-b59d-4bf4-74af5710c9d7@proxmox.com> (raw)
In-Reply-To: <4d7e453a-ad33-07a9-a319-07276955fb5f@proxmox.com>
On 3/17/21 20:35, Thomas Lamprecht wrote:
> On 16.03.21 12:56, Dominik Csapak wrote:
>> namely
>> * From<Duration> (to convert easily from duration to timespan)
>> * Display (for better formatting)
>>
>> Signed-off-by: Dominik Csapak <d.csapak@proxmox.com>
>> ---
>> if wanted, we can optimize the display trait a bit further, e.g.
>> only showing the biggest two units instead
>>
>> src/tools/systemd/time.rs | 84 +++++++++++++++++++++++++++++++++++++++
>> 1 file changed, 84 insertions(+)
>>
>> diff --git a/src/tools/systemd/time.rs b/src/tools/systemd/time.rs
>> index 7cc42415..75fb0ea2 100644
>> --- a/src/tools/systemd/time.rs
>> +++ b/src/tools/systemd/time.rs
>> @@ -141,6 +141,90 @@ impl From<TimeSpan> for f64 {
>> }
>> }
>>
>> +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,
>> + msec,
>> + usec,
>
> nit:
> nano -> milli -> micro? While it works correct, that order above seems just wrong...
yeah sorry...
>
>> + 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 single = true;
>> + if self.years > 0 {
>> + write!(f, "{}y", self.years)?;
>> + single = false;
>> + }
>> + let write_space = |single: bool, f: &mut std::fmt::Formatter| -> Result<bool, std::fmt::Error> {
>> + if !single {
>> + write!(f, " ")?;
>> + }
>> + Ok(false)
>> + };
>
> No sure if something like (untested):
>
> let mut first = true;
> let mut do_write = |v: u64, unit: &str| -> Result<(), std::fmt::Error> {
> if !first {
> write!(f, " ")?;
> first = false;
> }
> write!(f, "{}{}", v, unit)
> }
>
> if self.months > 0 {
> do_write(self.months, "m")?;
> }
>
> ...
>
> would be nicer IMO, especially as the "single" handling reads a bit weird.
> Maybe the seconds field makes this not really feasible though...
mhm.. i can try to improve that a bit
>
>> + if self.months > 0 {
>> + single = write_space(single, f)?;
>> + write!(f, "{}m", self.months)?;
>> + }
>> + if self.weeks > 0 {
>> + single = write_space(single, f)?;
>> + write!(f, "{}w", self.weeks)?;
>> + }
>> + if self.days > 0 {
>> + single = write_space(single, f)?;
>> + write!(f, "{}d", self.days)?;
>> + }
>> + if self.hours > 0 {
>> + single = write_space(single, f)?;
>> + write!(f, "{}h", self.hours)?;
>> + }
>> + if self.minutes > 0 {
>> + single = write_space(single, f)?;
>> + write!(f, "{}min", self.minutes)?;
>> + }
>> + let seconds = self.seconds as f64 + (self.msec as f64 / 1000.0);
>> + if seconds >= 0.1 {
>> + write_space(single, f)?;
>> + if seconds >= 1.0 || !single {
>> + write!(f, "{:.0}s", seconds)?;
>> + } else {
>> + write!(f, "{:.1}s", seconds)?;
>> + }
>> + } else if single {
>> + write_space(single, f)?;
>> + write!(f, "<0.1s")?;
>> + }
>> + Ok(())
>> + }
>> +}
>>
>> pub fn verify_time_span(i: &str) -> Result<(), Error> {
>> parse_time_span(i)?;
>>
>
prev parent reply other threads:[~2021-03-18 9:49 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-03-16 11:56 Dominik Csapak
2021-03-16 11:56 ` [pbs-devel] [PATCH proxmox-backup 2/3] server/email_notifications: do not panic on template registration Dominik Csapak
2021-03-17 19:38 ` Thomas Lamprecht
2021-03-18 9:57 ` Dominik Csapak
2021-03-18 10:21 ` Thomas Lamprecht
2021-03-18 10:31 ` Dominik Csapak
2021-03-18 11:13 ` Thomas Lamprecht
2021-03-16 11:56 ` [pbs-devel] [PATCH proxmox-backup 3/3] api2/tape/backup: include a summary on notification e-mails Dominik Csapak
2021-03-17 19:35 ` [pbs-devel] [PATCH proxmox-backup 1/3] tools/systemd/time: implement some Traits for TimeSpan Thomas Lamprecht
2021-03-18 9:49 ` Dominik Csapak [this message]
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=d4e77a90-198b-b59d-4bf4-74af5710c9d7@proxmox.com \
--to=d.csapak@proxmox.com \
--cc=pbs-devel@lists.proxmox.com \
--cc=t.lamprecht@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