23 if (!value ||
size != 10)
return false;
24 const uint16_t year = uint16_t(value[0]) | (uint16_t(value[1]) << 8);
25 const uint8_t month = value[2], day = value[3];
26 const uint8_t hour = value[4], minute = value[5], second = value[6];
27 if (year < 2024 || year > 2099 || month < 1 || month > 12 ||
28 hour > 23 || minute > 59 || second > 59 || value[7] > 7)
return false;
29 static constexpr uint8_t monthDays[] = {31,28,31,30,31,30,31,31,30,31,30,31};
30 const bool leap = (year % 4 == 0 && year % 100 != 0) || year % 400 == 0;
31 const uint8_t maxDay = monthDays[month - 1] + ((month == 2 && leap) ? 1 : 0);
32 if (day < 1 || day > maxDay)
return false;
35 const unsigned m = month;
37 const int era = (y >= 0 ? y : y - 399) / 400;
38 const unsigned yoe =
static_cast<unsigned>(y - era * 400);
39 const unsigned mp = m > 2 ? m - 3 : m + 9;
40 const unsigned doy = (153 * mp + 2) / 5 + day - 1;
41 const unsigned doe = yoe * 365 + yoe / 4 - yoe / 100 + doy;
42 const int64_t days = int64_t(era) * 146097 + int64_t(doe) - 719468;
43 const int64_t seconds = days * 86400 + hour * 3600 + minute * 60 + second;
44 if (seconds < 0 || seconds > UINT32_MAX)
return false;
45 epoch =
static_cast<uint32_t
>(seconds);