From: Tilman Sauerbeck Date: Thu, 2 Jan 2020 12:32:26 +0000 (+0100) Subject: common: Implement Time::fmt_time(). X-Git-Url: http://git.code-monkey.de/?a=commitdiff_plain;h=9ecb8760fe0fd268cf3819fc56d90e6d1e8c300c;p=gps-watch.git common: Implement Time::fmt_time(). Formats time as HH:MM:SS. --- 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'); + } }