From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by lists.proxmox.com (Postfix) with UTF8SMTPS id B7B726BBDC for ; Thu, 18 Mar 2021 10:49:33 +0100 (CET) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with UTF8SMTP id ACBB4F21A for ; Thu, 18 Mar 2021 10:49:33 +0100 (CET) Received: from proxmox-new.maurer-it.com (proxmox-new.maurer-it.com [212.186.127.180]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by firstgate.proxmox.com (Proxmox) with UTF8SMTPS id A332BF20D for ; Thu, 18 Mar 2021 10:49:32 +0100 (CET) Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1]) by proxmox-new.maurer-it.com (Proxmox) with UTF8SMTP id 6D174427D1; Thu, 18 Mar 2021 10:49:32 +0100 (CET) Message-ID: Date: Thu, 18 Mar 2021 10:49:31 +0100 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:87.0) Gecko/20100101 Thunderbird/87.0 Content-Language: en-US To: Thomas Lamprecht , Proxmox Backup Server development discussion References: <20210316115623.9368-1-d.csapak@proxmox.com> <4d7e453a-ad33-07a9-a319-07276955fb5f@proxmox.com> From: Dominik Csapak In-Reply-To: <4d7e453a-ad33-07a9-a319-07276955fb5f@proxmox.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-SPAM-LEVEL: Spam detection results: 0 AWL 0.183 Adjusted score from AWL reputation of From: address KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment NICE_REPLY_A -0.001 Looks like a legit reply (A) RCVD_IN_DNSWL_MED -2.3 Sender listed at https://www.dnswl.org/, medium trust SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record URIBL_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to URIBL was blocked. See http://wiki.apache.org/spamassassin/DnsBlocklists#dnsbl-block for more information. [time.rs] Subject: Re: [pbs-devel] [PATCH proxmox-backup 1/3] tools/systemd/time: implement some Traits for TimeSpan X-BeenThere: pbs-devel@lists.proxmox.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Proxmox Backup Server development discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 18 Mar 2021 09:49:33 -0000 On 3/17/21 20:35, Thomas Lamprecht wrote: > On 16.03.21 12:56, Dominik Csapak wrote: >> namely >> * From (to convert easily from duration to timespan) >> * Display (for better formatting) >> >> Signed-off-by: Dominik Csapak >> --- >> 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 for f64 { >> } >> } >> >> +impl From 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 { >> + 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)?; >> >