ref: fc5424cb72e477c5f1bbfaeddb5c50b851a965ae
src/components/datetime/DateTimeController.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
#pragma once #include <cstdint> #include <chrono> #include <ctime> #include <string> #include "components/settings/Settings.h" namespace Pinetime { namespace System { class SystemTask; } namespace Controllers { class DateTime { public: DateTime(Controllers::Settings& settingsController); enum class Days : uint8_t { Unknown, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday }; enum class Months : uint8_t { Unknown, January, February, March, April, May, June, July, August, September, October, November, December }; void SetTime(uint16_t year, uint8_t month, uint8_t day, uint8_t hour, uint8_t minute, uint8_t second); /* * setter corresponding to the BLE Set Local Time characteristic. * * used to update difference between utc and local time (see UtcOffset()) * * parameters are in quarters of an our. Following the BLE CTS specification, * timezone is expected to be constant over DST which will be reported in * dst field. */ void SetTimeZone(int8_t timezone, int8_t dst); void UpdateTime(uint32_t systickCounter); uint16_t Year() const { return 1900 + localTime.tm_year; } Months Month() const { return static_cast<Months>(localTime.tm_mon + 1); } uint8_t Day() const { return localTime.tm_mday; } Days DayOfWeek() const { int daysSinceSunday = localTime.tm_wday; if (daysSinceSunday == 0) { return Days::Sunday; } return static_cast<Days>(daysSinceSunday); } int DayOfYear() const { return localTime.tm_yday + 1; } uint8_t Hours() const { return localTime.tm_hour; } uint8_t Minutes() const { return localTime.tm_min; } uint8_t Seconds() const { return localTime.tm_sec; } /* * returns the offset between local time and UTC in quarters of an hour * * Availability of this field depends on wether the companion app * supports the BLE CTS Local Time Characteristic. Expect it to be 0 * if not. */ int8_t UtcOffset() const { return tzOffset + dstOffset; } /* * returns the offset between the (dst independent) local time zone and UTC * in quarters of an hour * * Availability of this field depends on wether the companion app * supports the BLE CTS Local Time Characteristic. Expect it to be 0 * if not. */ int8_t TzOffset() const { return tzOffset; } /* * returns the offset between the local time zone and local time * in quarters of an hour * if != 0, DST is in effect, if == 0 not. * * Availability of this field depends on wether the companion app * supports the BLE CTS Local Time Characteristic. Expect it to be 0 * if not. */ int8_t DstOffset() const { return dstOffset; } const char* MonthShortToString() const; const char* DayOfWeekShortToString() const; static const char* MonthShortToStringLow(Months month); const char* DayOfWeekShortToStringLow() const; std::chrono::time_point<std::chrono::system_clock, std::chrono::nanoseconds> CurrentDateTime() const { return currentDateTime; } std::chrono::time_point<std::chrono::system_clock, std::chrono::nanoseconds> UTCDateTime() const { return currentDateTime - std::chrono::seconds((tzOffset + dstOffset) * 15 * 60); } std::chrono::seconds Uptime() const { return uptime; } void Register(System::SystemTask* systemTask); void SetCurrentTime(std::chrono::time_point<std::chrono::system_clock, std::chrono::nanoseconds> t); std::string FormattedTime(); private: std::tm localTime; int8_t tzOffset = 0; int8_t dstOffset = 0; uint32_t previousSystickCounter = 0; std::chrono::time_point<std::chrono::system_clock, std::chrono::nanoseconds> currentDateTime; std::chrono::seconds uptime {0}; bool isMidnightAlreadyNotified = false; bool isHourAlreadyNotified = true; bool isHalfHourAlreadyNotified = true; System::SystemTask* systemTask = nullptr; Controllers::Settings& settingsController; }; } } |