Implement optional automatic setting of day of week

This commit is contained in:
BlackMark 2020-05-16 17:01:19 +02:00
parent c728d99f97
commit 80cce4671f

View File

@ -24,7 +24,7 @@ struct Time {
struct DateTime : Date, Time {
};
template <typename I2cDriver>
template <typename I2cDriver, bool SetDayOfWeek = true>
class DS3231 {
public:
static constexpr auto I2C_ADDRESS = 0x68;
@ -125,6 +125,13 @@ class DS3231 {
static_assert(type::always_false_v<decltype(Address)>, "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