core/clock/progress.c
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /** | ||
| 2 | * @file progress.c | ||
| 3 | * @brief The progress sums: seconds gone over seconds in the period, as a whole percent. | ||
| 4 | * | ||
| 5 | * @ingroup lib_core | ||
| 6 | */ | ||
| 7 | #include "clock/progress.h" | ||
| 8 | |||
| 9 | |||
| 10 | #include "clock/date.h" | ||
| 11 | |||
| 12 | /// Seconds in an hour | ||
| 13 | #define SECS_PER_HOUR 3600 | ||
| 14 | |||
| 15 | /// Seconds in a day on the wall clock, which is always 24 hours | ||
| 16 | #define SECS_PER_DAY 86400 | ||
| 17 | |||
| 18 | /** | ||
| 19 | * @brief Seconds gone over seconds in the period, as a whole percent rounded down. | ||
| 20 | * | ||
| 21 | * Every period is a whole number of hours, so a hundredth of one is a whole number of seconds, 36 | ||
| 22 | * an hour. Dividing by that hundredth gives the same answer as times 100 over the total, with no | ||
| 23 | * product that could run past a 32 bit int on a year of seconds. It also keeps 64 bit division, | ||
| 24 | * about 750 bytes of library code, off the watch. | ||
| 25 | * | ||
| 26 | * @param elapsed Seconds gone so far. | ||
| 27 | * @param total Seconds in the whole period, a whole number of hours. | ||
| 28 | * @return Whole percent from 0 to 99, or 0 when either count is not positive. | ||
| 29 | */ | ||
| 30 | 22 | static int pct_of_seconds(int elapsed, int total) | |
| 31 | { | ||
| 32 |
3/4✓ Branch 2 → 3 taken 22 times.
✗ Branch 2 → 4 not taken.
✓ Branch 3 → 4 taken 5 times.
✓ Branch 3 → 5 taken 17 times.
|
22 | if (total <= 0 || elapsed <= 0) |
| 33 | { | ||
| 34 | 5 | return 0; | |
| 35 | } | ||
| 36 | 17 | int pct = elapsed / (total / 100); | |
| 37 | 17 | return pct > 99 ? 99 : pct; | |
| 38 | } | ||
| 39 | |||
| 40 | /** | ||
| 41 | * @brief How many seconds of the day have gone by on the wall clock. | ||
| 42 | * | ||
| 43 | * @param hour The hour, 0 to 23. | ||
| 44 | * @param min The minute, 0 to 59. | ||
| 45 | * @param sec The second, 0 to 59. | ||
| 46 | * @return Seconds since midnight. | ||
| 47 | */ | ||
| 48 | 18 | static int seconds_into_day(int hour, int min, int sec) | |
| 49 | { | ||
| 50 | 18 | return hour * SECS_PER_HOUR + min * 60 + sec; | |
| 51 | } | ||
| 52 | |||
| 53 | /** | ||
| 54 | * @brief How many days a year has, 365 or 366. | ||
| 55 | * | ||
| 56 | * January 1st has every day of the year still ahead of it bar itself, so that count plus one is | ||
| 57 | * the year's length, with the leap rule kept in date.c alone. | ||
| 58 | * | ||
| 59 | * @param year The full year (2026, not 126). | ||
| 60 | * @return The day count, 365 or 366. | ||
| 61 | */ | ||
| 62 | 8 | static int days_in_year(int year) | |
| 63 | { | ||
| 64 | 8 | return date_days_left_in_year(year, 0) + 1; | |
| 65 | } | ||
| 66 | |||
| 67 | 11 | int progress_week_day(int wday, int week_start) | |
| 68 | { | ||
| 69 | // C keeps the sign of a negative remainder, so the second % 7 is what lands a week start past | ||
| 70 | // either end of 0 to 6 back inside the week rather than on day 0 or below | ||
| 71 | 11 | return ((wday - week_start) % 7 + 7) % 7 + 1; | |
| 72 | } | ||
| 73 | |||
| 74 | 4 | int progress_hour_pct(int min, int sec) | |
| 75 | { | ||
| 76 | 4 | return pct_of_seconds(min * 60 + sec, SECS_PER_HOUR); | |
| 77 | } | ||
| 78 | |||
| 79 | 4 | int progress_day_pct(int hour, int min, int sec) | |
| 80 | { | ||
| 81 | 4 | return pct_of_seconds(seconds_into_day(hour, min, sec), SECS_PER_DAY); | |
| 82 | } | ||
| 83 | |||
| 84 | 6 | int progress_week_pct(int wday, int week_start, int hour, int min, int sec) | |
| 85 | { | ||
| 86 | 6 | int elapsed = (progress_week_day(wday, week_start) - 1) * SECS_PER_DAY + seconds_into_day(hour, min, sec); | |
| 87 | 6 | return pct_of_seconds(elapsed, 7 * SECS_PER_DAY); | |
| 88 | } | ||
| 89 | |||
| 90 | 8 | int progress_year_pct(int year, int yday, int hour, int min, int sec) | |
| 91 | { | ||
| 92 | 8 | int elapsed = yday * SECS_PER_DAY + seconds_into_day(hour, min, sec); | |
| 93 | 8 | return pct_of_seconds(elapsed, days_in_year(year) * SECS_PER_DAY); | |
| 94 | } | ||
| 95 |