From aae91604224bb0f10096b41a23b01654a2e46823 Mon Sep 17 00:00:00 2001 From: Joey Castillo Date: Sun, 6 Feb 2022 15:00:58 -0500 Subject: utility: add function for formatting a duration in days + HMS --- watch-library/shared/watch/watch_utility.c | 11 +++++++++++ watch-library/shared/watch/watch_utility.h | 14 ++++++++++++++ 2 files changed, 25 insertions(+) (limited to 'watch-library/shared/watch') diff --git a/watch-library/shared/watch/watch_utility.c b/watch-library/shared/watch/watch_utility.c index dd11c0eb..3f8aa619 100644 --- a/watch-library/shared/watch/watch_utility.c +++ b/watch-library/shared/watch/watch_utility.c @@ -151,6 +151,17 @@ watch_date_time watch_utility_date_time_convert_zone(watch_date_time date_time, return watch_utility_date_time_from_unix_time(timestamp, destination_utc_offset); } +watch_duration_t watch_utility_seconds_to_duration(uint32_t seconds) { + watch_duration_t retval; + + retval.seconds = (seconds % 60); + retval.minutes = (seconds % 3600) / 60; + retval.hours = (seconds % 86400) / 3600; + retval.days = seconds / 86400; + + return retval; +} + bool watch_utility_convert_to_12_hour(watch_date_time *date_time) { bool is_pm = date_time->unit.hour > 11; date_time->unit.hour %= 12; diff --git a/watch-library/shared/watch/watch_utility.h b/watch-library/shared/watch/watch_utility.h index dc0ebb6a..f4411ce5 100644 --- a/watch-library/shared/watch/watch_utility.h +++ b/watch-library/shared/watch/watch_utility.h @@ -32,6 +32,14 @@ * @brief This section covers various useful functions that don't fit anywhere else. **/ /// @{ + +typedef struct { + uint8_t seconds; // 0-59 + uint8_t minutes; // 0-59 + uint8_t hours; // 0-23 + uint32_t days; // 0-4294967295 +} watch_duration_t; + /** @brief Returns a two-letter weekday for the given timestamp, suitable for display * in positions 0-1 of the watch face * @param date_time The watch_date_time whose weekday you want. @@ -60,6 +68,12 @@ uint32_t watch_utility_convert_to_unix_time(uint16_t year, uint8_t month, uint8_ */ uint32_t watch_utility_date_time_to_unix_time(watch_date_time date_time, uint32_t utc_offset); +/** @brief Converts a duration in seconds to a watch_duration_t struct. + * @param seconds A positive number of seconds that you wish to convert to a formatted duration. + * @return A populated struct with the number of days, hours, minutes and seconds elapsed. + */ +watch_duration_t watch_utility_seconds_to_duration(uint32_t seconds); + /** @brief Returns the UNIX time (seconds since 1970) for a given watch_date_time struct. * @param timestamp The UNIX timestamp that you wish to convert. * @param utc_offset The number of seconds that you wish date_time to be offset from UTC. -- cgit v1.2.3