Ersa Wearable Platform
Embedded smartwatch firmware and application framework
Loading...
Searching...
No Matches
ble_current_time.h
Go to the documentation of this file.
1#pragma once
2
3#include <stddef.h>
4#include <stdint.h>
5
6namespace ersa {
7namespace protocols {
8
22inline bool decodeCurrentTime(const uint8_t* value, size_t size, uint32_t& epoch) {
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;
33
34 int y = year;
35 const unsigned m = month;
36 y -= m <= 2;
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);
46 return true;
47}
48
49} // namespace protocols
50} // namespace ersa
bool decodeCurrentTime(const uint8_t *value, size_t size, uint32_t &epoch)
Definition ble_current_time.h:22
Definition application.h:10
size_t size
Definition ota_service.cpp:47