]> git.earman.xyz Git - sensor-watch.git/commitdiff
utility: add function for formatting a duration in days + HMS
authorJoey Castillo <joeycastillo@utexas.edu>
Sun, 6 Feb 2022 20:00:58 +0000 (15:00 -0500)
committerJoey Castillo <joeycastillo@utexas.edu>
Sun, 6 Feb 2022 20:00:58 +0000 (15:00 -0500)
watch-library/shared/watch/watch_utility.c
watch-library/shared/watch/watch_utility.h

index dd11c0eb6c479639c34c575e45a119e5410701af..3f8aa619e0b9f5fc3caf5addb7c0ad395ecb950c 100644 (file)
@@ -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;
index dc0ebb6adc4bbb44cab6cd41d15b1790095fcad8..f4411ce572f605eb73fae2a8bff9e6470e3d92a4 100644 (file)
   * @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.