summaryrefslogtreecommitdiffstats
path: root/watch-library
diff options
context:
space:
mode:
authorSlim <slim@pirate.tn>2022-12-02 19:45:53 +0100
committerSlim <slim@pirate.tn>2022-12-02 19:45:53 +0100
commita69547805b54c0c986241386213948416ac0fa1d (patch)
tree2a0be15507fe8f3df434468199da5fec7bc6eacb /watch-library
parent852099e1abeb4886eaa87da71a6735175084c4d5 (diff)
downloadSensor-Watch-a69547805b54c0c986241386213948416ac0fa1d.tar.gz
Sensor-Watch-a69547805b54c0c986241386213948416ac0fa1d.tar.bz2
Sensor-Watch-a69547805b54c0c986241386213948416ac0fa1d.zip
Weeknumber watch face
Same as simple clock, but has iso 8601 week number instead of seconds counter
Diffstat (limited to 'watch-library')
-rw-r--r--watch-library/shared/watch/watch_utility.c67
-rw-r--r--watch-library/shared/watch/watch_utility.h27
2 files changed, 84 insertions, 10 deletions
diff --git a/watch-library/shared/watch/watch_utility.c b/watch-library/shared/watch/watch_utility.c
index 252624b3..6a8b408a 100644
--- a/watch-library/shared/watch/watch_utility.c
+++ b/watch-library/shared/watch/watch_utility.c
@@ -26,16 +26,60 @@
#include "watch_utility.h"
const char * watch_utility_get_weekday(watch_date_time date_time) {
- static const char weekdays[7][3] = {"SA", "SU", "MO", "TU", "WE", "TH", "FR"};
- date_time.unit.year += 20;
- if (date_time.unit.month <= 2) {
- date_time.unit.month += 12;
- date_time.unit.year--;
+ static const char weekdays[7][3] = {"MO", "TU", "WE", "TH", "FR", "SA", "SU"};
+ return weekdays[watch_utility_get_iso8601_weekday_number(date_time.unit.year + WATCH_RTC_REFERENCE_YEAR, date_time.unit.month, date_time.unit.day) - 1];
+}
+
+// Per ISO8601 week starts on Monday with index 1
+uint8_t watch_utility_get_iso8601_weekday_number(uint16_t year, uint8_t month, uint8_t day) {
+ year -= WATCH_RTC_REFERENCE_YEAR;
+ year += 20;
+ if (month <= 2) {
+ month += 12;
+ year--;
}
- return weekdays[(date_time.unit.day + 13 * (date_time.unit.month + 1) / 5 + date_time.unit.year + date_time.unit.year / 4 + 525) % 7];
+ return ((day + (13 * (month + 1) / 5) + year + (year / 4) + 5) % 7) + 1;
}
-uint32_t watch_utility_convert_to_unix_time(uint16_t year, uint8_t month, uint8_t day, uint8_t hour, uint8_t minute, uint8_t second, uint32_t utc_offset) {
+// this function is from the excellent musl c library
+uint8_t watch_utility_get_weeknumber(uint16_t year, uint8_t month, uint8_t day) {
+ uint8_t weekday;
+ uint16_t days;
+
+ weekday = watch_utility_get_iso8601_weekday_number(year, month, day) % 7;
+ days = watch_utility_days_since_new_year(year, month, day);
+
+ int val = (days + 7U - (weekday+6U)%7) / 7;
+ /* If 1 Jan is just 1-3 days past Monday,
+ * the previous week is also in this year. */
+ if ((weekday + 371U - days - 2) % 7 <= 2)
+ val++;
+ if (!val) {
+ val = 52;
+ /* If 31 December of prev year a Thursday,
+ * or Friday of a leap year, then the
+ * prev year has 53 weeks. */
+ int dec31 = (weekday + 7U - days - 1) % 7;
+ if (dec31 == 4 || (dec31 == 5 && is_leap(year%400-1)))
+ val++;
+ } else if (val == 53) {
+ /* If 1 January is not a Thursday, and not
+ * a Wednesday of a leap year, then this
+ * year has only 52 weeks. */
+ int jan1 = (weekday + 371U - days) % 7;
+ if (jan1 != 4 && (jan1 != 3 || !is_leap(year)))
+ val = 1;
+ }
+ return val;
+}
+
+uint8_t is_leap(uint16_t y)
+{
+ y += 1900;
+ return !(y%4) && ((y%100) || !(y%400));
+}
+
+uint16_t watch_utility_days_since_new_year(uint16_t year, uint8_t month, uint8_t day) {
uint16_t DAYS_SO_FAR[] = {
0, // Jan
31, // Feb
@@ -51,10 +95,13 @@ uint32_t watch_utility_convert_to_unix_time(uint16_t year, uint8_t month, uint8_
334 // December
};
+ return (is_leap(year) && (month > 2) ? 1 : 0) + DAYS_SO_FAR[month - 1] + day;
+}
+
+uint32_t watch_utility_convert_to_unix_time(uint16_t year, uint8_t month, uint8_t day, uint8_t hour, uint8_t minute, uint8_t second, uint32_t utc_offset) {
uint32_t year_adj = year + 4800;
- uint32_t febs = year_adj - (month <= 2 ? 1 : 0); /* Februaries since base. */
- uint32_t leap_days = 1 + (febs / 4) - (febs / 100) + (febs / 400);
- uint32_t days = 365 * year_adj + leap_days + DAYS_SO_FAR[month - 1] + day - 1;
+ uint32_t leap_days = 1 + (year_adj / 4) - (year_adj / 100) + (year_adj / 400);
+ uint32_t days = 365 * year_adj + leap_days + watch_utility_days_since_new_year(year, month, day) - 1;
days -= 2472692; /* Adjust to Unix epoch. */
uint32_t timestamp = days * 86400;
diff --git a/watch-library/shared/watch/watch_utility.h b/watch-library/shared/watch/watch_utility.h
index 57bcbed7..f96bd8c1 100644
--- a/watch-library/shared/watch/watch_utility.h
+++ b/watch-library/shared/watch/watch_utility.h
@@ -46,6 +46,33 @@ typedef struct {
*/
const char * watch_utility_get_weekday(watch_date_time date_time);
+/** @brief Returns a number between 1-7 representing the weekday according to ISO8601 : week starts on Monday and has index 1, Sunday has index 7
+ * @param year The year of the date
+ * @param month The month of the date (1-12)
+ * @param day The day of the date (1-31)
+ */
+uint8_t watch_utility_get_iso8601_weekday_number(uint16_t year, uint8_t month, uint8_t day);
+
+
+/** @brief Returns a number between 1-53 representing the weeknumber according to ISO8601 : First week of the year always contains Thursday. If it overlaps on previous year, current year has 53 weeks
+ * @param year The year of the date
+ * @param month The month of the date (1-12)
+ * @param day The day of the date (1-31)
+ */
+uint8_t watch_utility_get_weeknumber(uint16_t year, uint8_t month, uint8_t day);
+
+/** @brief Returns a number between 1-366 representing the elapsed days since January 1st the same year.
+ * @param year The year of the date
+ * @param month The month of the date (1-12)
+ * @param day The day of the date (1-31)
+ */
+uint16_t watch_utility_days_since_new_year(uint16_t year, uint8_t month, uint8_t day);
+
+/** @brief Returns 1 if year is leap and 0 otherwise
+ * @param year The year (ex. 2022)
+ */
+uint8_t is_leap(uint16_t year);
+
/** @brief Returns the UNIX time (seconds since 1970) for a given date/time in UTC.
* @param date_time The watch_date_time that you wish to convert.
* @param year The year of the date you wish to convert.