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) server-digest SHA256) (No client certificate requested) by lists.proxmox.com (Postfix) with ESMTPS id 00A206B949 for ; Wed, 17 Mar 2021 20:35:21 +0100 (CET) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id E15DBA2DF for ; Wed, 17 Mar 2021 20:35:21 +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) server-digest SHA256) (No client certificate requested) by firstgate.proxmox.com (Proxmox) with ESMTPS id 98395A2CF for ; Wed, 17 Mar 2021 20:35:20 +0100 (CET) Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1]) by proxmox-new.maurer-it.com (Proxmox) with ESMTP id 5C7D04196D for ; Wed, 17 Mar 2021 20:35:20 +0100 (CET) Message-ID: <4d7e453a-ad33-07a9-a319-07276955fb5f@proxmox.com> Date: Wed, 17 Mar 2021 20:35:19 +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: Proxmox Backup Server development discussion , Dominik Csapak References: <20210316115623.9368-1-d.csapak@proxmox.com> From: Thomas Lamprecht In-Reply-To: <20210316115623.9368-1-d.csapak@proxmox.com> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-SPAM-LEVEL: Spam detection results: 0 AWL -0.046 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: Wed, 17 Mar 2021 19:35:22 -0000 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... > + 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... > + 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)?; >