From 9ecb8760fe0fd268cf3819fc56d90e6d1e8c300c Mon Sep 17 00:00:00 2001 From: Tilman Sauerbeck Date: Thu, 2 Jan 2020 13:32:26 +0100 Subject: [PATCH] common: Implement Time::fmt_time(). Formats time as HH:MM:SS. --- src/common/time.rs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/common/time.rs b/src/common/time.rs index 0413c58..e11dea4 100644 --- a/src/common/time.rs +++ b/src/common/time.rs @@ -24,6 +24,8 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +use fmt::fmt_u32_pad; + pub struct Time { seconds: i32, minutes: i32, @@ -115,4 +117,18 @@ impl Time { }) } } + + pub fn fmt_time(&self, s: &mut [u8]) { + let mut offset = 0; + + offset += fmt_u32_pad(&mut s[offset..], self.hours as u32, 2, b'0'); + s[offset] = b':'; + offset += 1; + + offset += fmt_u32_pad(&mut s[offset..], self.minutes as u32, 2, b'0'); + s[offset] = b':'; + offset += 1; + + fmt_u32_pad(&mut s[offset..], self.seconds as u32, 2, b'0'); + } } -- 2.30.2