From 80cce4671ffd38352ea0d3fb66992d298fe12928 Mon Sep 17 00:00:00 2001 From: BlackMark Date: Sat, 16 May 2020 17:01:19 +0200 Subject: [PATCH] Implement optional automatic setting of day of week --- ds3231.hpp | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/ds3231.hpp b/ds3231.hpp index 62bf497..2f48644 100644 --- a/ds3231.hpp +++ b/ds3231.hpp @@ -24,7 +24,7 @@ struct Time { struct DateTime : Date, Time { }; -template +template class DS3231 { public: static constexpr auto I2C_ADDRESS = 0x68; @@ -125,6 +125,13 @@ class DS3231 { static_assert(type::always_false_v, "Invalid register address"); } } + + static uint8_t calcDayOfWeek(uint16_t year, uint8_t month, uint16_t day) + { + day += month < 3 ? year-- : year - 2; + const auto dayOfWeek = (23 * month / 9 + day + 4 + year / 4 - year / 100 + year / 400); + return dayOfWeek % 7; + } }; } // namespace rtc