#pragma once #include #include namespace BoomRtc { inline int bcd(uint8_t value) { return (value & 15) <= 9 && (value >> 4) <= 9 ? (value >> 4) * 10 + (value & 15) : -1; } inline bool decode(uint8_t control, const uint8_t bytes[7], time_t& result) { if ((control & 0x28) || (bytes[0] & 0x80)) return false; tm utc{}; utc.tm_sec = bcd(bytes[0]); utc.tm_min = bcd(bytes[1]); utc.tm_hour = bcd(bytes[2]); utc.tm_mday = bcd(bytes[3]); utc.tm_mon = bcd(bytes[5]) - 1; utc.tm_year = bcd(bytes[6]) + 100; if (utc.tm_sec < 0 || utc.tm_sec > 59 || utc.tm_min < 0 || utc.tm_min > 59 || utc.tm_hour < 0 || utc.tm_hour > 23 || utc.tm_mday < 1 || utc.tm_mday > 31 || utc.tm_mon < 0 || utc.tm_mon > 11 || utc.tm_year < 124 || utc.tm_year > 199) return false; // UTC conversion without changing the process TZ used by other tasks. int year = utc.tm_year + 1900, month = utc.tm_mon + 1; year -= month <= 2; int era = year / 400, yoe = year - era * 400; int doy = (153 * (month + (month > 2 ? -3 : 9)) + 2) / 5 + utc.tm_mday - 1; int days = era * 146097 + yoe * 365 + yoe / 4 - yoe / 100 + doy - 719468; result = time_t(days) * 86400 + utc.tm_hour * 3600 + utc.tm_min * 60 + utc.tm_sec; tm check{}; gmtime_r(&result, &check); return check.tm_mon == utc.tm_mon && check.tm_mday == utc.tm_mday; } }