blynk & neopixelring & w7500
Fork of WIZwiki-7500_Blynk by
utility/BlynkDateTime.h@18:bc3d69c01bf2, 2017-11-27 (annotated)
- Committer:
- jcm931213
- Date:
- Mon Nov 27 11:49:08 2017 +0000
- Revision:
- 18:bc3d69c01bf2
- Parent:
- 8:a34aa310ec48
NeoPixel Ring Blynk Example final!!!
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Volodymyr Shymanskyy |
8:a34aa310ec48 | 1 | /** |
Volodymyr Shymanskyy |
8:a34aa310ec48 | 2 | * @file BlynkDateTime.h |
Volodymyr Shymanskyy |
8:a34aa310ec48 | 3 | * @author Volodymyr Shymanskyy |
Volodymyr Shymanskyy |
8:a34aa310ec48 | 4 | * @license This project is released under the MIT License (MIT) |
Volodymyr Shymanskyy |
8:a34aa310ec48 | 5 | * @copyright Copyright (c) 2016 Volodymyr Shymanskyy |
Volodymyr Shymanskyy |
8:a34aa310ec48 | 6 | * @date Aug 2016 |
Volodymyr Shymanskyy |
8:a34aa310ec48 | 7 | * @brief DateTime implementation |
Volodymyr Shymanskyy |
8:a34aa310ec48 | 8 | * |
Volodymyr Shymanskyy |
8:a34aa310ec48 | 9 | */ |
Volodymyr Shymanskyy |
8:a34aa310ec48 | 10 | |
Volodymyr Shymanskyy |
8:a34aa310ec48 | 11 | #ifndef BlynkDateTime_h |
Volodymyr Shymanskyy |
8:a34aa310ec48 | 12 | #define BlynkDateTime_h |
Volodymyr Shymanskyy |
8:a34aa310ec48 | 13 | |
Volodymyr Shymanskyy |
8:a34aa310ec48 | 14 | typedef long blynk_time_t; |
Volodymyr Shymanskyy |
8:a34aa310ec48 | 15 | |
Volodymyr Shymanskyy |
8:a34aa310ec48 | 16 | struct blynk_tm *blynk_gmtime_r(const blynk_time_t *time, struct blynk_tm *tm); |
Volodymyr Shymanskyy |
8:a34aa310ec48 | 17 | blynk_time_t blynk_mk_gmtime(struct blynk_tm *tm); |
Volodymyr Shymanskyy |
8:a34aa310ec48 | 18 | |
Volodymyr Shymanskyy |
8:a34aa310ec48 | 19 | struct blynk_tm { |
Volodymyr Shymanskyy |
8:a34aa310ec48 | 20 | int8_t tm_sec; |
Volodymyr Shymanskyy |
8:a34aa310ec48 | 21 | int8_t tm_min; |
Volodymyr Shymanskyy |
8:a34aa310ec48 | 22 | int8_t tm_hour; |
Volodymyr Shymanskyy |
8:a34aa310ec48 | 23 | int8_t tm_mday; |
Volodymyr Shymanskyy |
8:a34aa310ec48 | 24 | int8_t tm_wday; |
Volodymyr Shymanskyy |
8:a34aa310ec48 | 25 | int8_t tm_mon; |
Volodymyr Shymanskyy |
8:a34aa310ec48 | 26 | int16_t tm_year; |
Volodymyr Shymanskyy |
8:a34aa310ec48 | 27 | int16_t tm_yday; |
Volodymyr Shymanskyy |
8:a34aa310ec48 | 28 | int16_t tm_isdst; |
Volodymyr Shymanskyy |
8:a34aa310ec48 | 29 | }; |
Volodymyr Shymanskyy |
8:a34aa310ec48 | 30 | |
Volodymyr Shymanskyy |
8:a34aa310ec48 | 31 | class BlynkTime { |
Volodymyr Shymanskyy |
8:a34aa310ec48 | 32 | |
Volodymyr Shymanskyy |
8:a34aa310ec48 | 33 | public: |
Volodymyr Shymanskyy |
8:a34aa310ec48 | 34 | static const uint32_t MAX_TIME = 86400L; |
Volodymyr Shymanskyy |
8:a34aa310ec48 | 35 | |
Volodymyr Shymanskyy |
8:a34aa310ec48 | 36 | BlynkTime() : mTime(-1) {} |
Volodymyr Shymanskyy |
8:a34aa310ec48 | 37 | |
Volodymyr Shymanskyy |
8:a34aa310ec48 | 38 | BlynkTime(const BlynkTime& t) : mTime(t.mTime) {} |
Volodymyr Shymanskyy |
8:a34aa310ec48 | 39 | |
Volodymyr Shymanskyy |
8:a34aa310ec48 | 40 | BlynkTime(long seconds) : mTime(seconds % MAX_TIME) {} |
Volodymyr Shymanskyy |
8:a34aa310ec48 | 41 | |
Volodymyr Shymanskyy |
8:a34aa310ec48 | 42 | BlynkTime(int hour, int minute, int second) |
Volodymyr Shymanskyy |
8:a34aa310ec48 | 43 | { |
Volodymyr Shymanskyy |
8:a34aa310ec48 | 44 | mTime = (hour * 3600 + minute * 60 + second) % MAX_TIME; |
Volodymyr Shymanskyy |
8:a34aa310ec48 | 45 | } |
Volodymyr Shymanskyy |
8:a34aa310ec48 | 46 | |
Volodymyr Shymanskyy |
8:a34aa310ec48 | 47 | int second() const { return mTime % 60; } |
Volodymyr Shymanskyy |
8:a34aa310ec48 | 48 | int minute() const { return (mTime / 60) % 60; } |
Volodymyr Shymanskyy |
8:a34aa310ec48 | 49 | int hour() const { return mTime / 3600; } |
Volodymyr Shymanskyy |
8:a34aa310ec48 | 50 | |
Volodymyr Shymanskyy |
8:a34aa310ec48 | 51 | int hour12() const { |
Volodymyr Shymanskyy |
8:a34aa310ec48 | 52 | int h = hour(); |
Volodymyr Shymanskyy |
8:a34aa310ec48 | 53 | if (h == 0) |
Volodymyr Shymanskyy |
8:a34aa310ec48 | 54 | return 12; // 12 midnight |
Volodymyr Shymanskyy |
8:a34aa310ec48 | 55 | else if (h > 12) |
Volodymyr Shymanskyy |
8:a34aa310ec48 | 56 | return h - 12; |
Volodymyr Shymanskyy |
8:a34aa310ec48 | 57 | return h; |
Volodymyr Shymanskyy |
8:a34aa310ec48 | 58 | } |
Volodymyr Shymanskyy |
8:a34aa310ec48 | 59 | |
Volodymyr Shymanskyy |
8:a34aa310ec48 | 60 | bool isAM() const { return !isPM(); } |
Volodymyr Shymanskyy |
8:a34aa310ec48 | 61 | bool isPM() const { return (hour() >= 12); } |
Volodymyr Shymanskyy |
8:a34aa310ec48 | 62 | |
Volodymyr Shymanskyy |
8:a34aa310ec48 | 63 | void adjustSeconds(int sec) { |
Volodymyr Shymanskyy |
8:a34aa310ec48 | 64 | if (isValid()) { |
Volodymyr Shymanskyy |
8:a34aa310ec48 | 65 | mTime = (mTime + sec) % MAX_TIME; |
Volodymyr Shymanskyy |
8:a34aa310ec48 | 66 | } |
Volodymyr Shymanskyy |
8:a34aa310ec48 | 67 | } |
Volodymyr Shymanskyy |
8:a34aa310ec48 | 68 | |
Volodymyr Shymanskyy |
8:a34aa310ec48 | 69 | blynk_time_t getUnixOffset() const { return mTime; } |
Volodymyr Shymanskyy |
8:a34aa310ec48 | 70 | |
Volodymyr Shymanskyy |
8:a34aa310ec48 | 71 | bool isValid() const { return mTime < MAX_TIME; } |
Volodymyr Shymanskyy |
8:a34aa310ec48 | 72 | operator bool() const { return isValid(); } |
Volodymyr Shymanskyy |
8:a34aa310ec48 | 73 | |
Volodymyr Shymanskyy |
8:a34aa310ec48 | 74 | bool operator == (const BlynkTime& t) const { return mTime == t.mTime; } |
Volodymyr Shymanskyy |
8:a34aa310ec48 | 75 | bool operator >= (const BlynkTime& t) const { return mTime >= t.mTime; } |
Volodymyr Shymanskyy |
8:a34aa310ec48 | 76 | bool operator <= (const BlynkTime& t) const { return mTime <= t.mTime; } |
Volodymyr Shymanskyy |
8:a34aa310ec48 | 77 | bool operator > (const BlynkTime& t) const { return mTime > t.mTime; } |
Volodymyr Shymanskyy |
8:a34aa310ec48 | 78 | bool operator < (const BlynkTime& t) const { return mTime < t.mTime; } |
Volodymyr Shymanskyy |
8:a34aa310ec48 | 79 | |
Volodymyr Shymanskyy |
8:a34aa310ec48 | 80 | private: |
Volodymyr Shymanskyy |
8:a34aa310ec48 | 81 | uint32_t mTime; |
Volodymyr Shymanskyy |
8:a34aa310ec48 | 82 | }; |
Volodymyr Shymanskyy |
8:a34aa310ec48 | 83 | |
Volodymyr Shymanskyy |
8:a34aa310ec48 | 84 | class BlynkDateTime { |
Volodymyr Shymanskyy |
8:a34aa310ec48 | 85 | |
Volodymyr Shymanskyy |
8:a34aa310ec48 | 86 | public: |
Volodymyr Shymanskyy |
8:a34aa310ec48 | 87 | BlynkDateTime() : mTime(0) {} |
Volodymyr Shymanskyy |
8:a34aa310ec48 | 88 | |
Volodymyr Shymanskyy |
8:a34aa310ec48 | 89 | BlynkDateTime(const BlynkDateTime& t) |
Volodymyr Shymanskyy |
8:a34aa310ec48 | 90 | { |
Volodymyr Shymanskyy |
8:a34aa310ec48 | 91 | mTime = t.mTime; |
Volodymyr Shymanskyy |
8:a34aa310ec48 | 92 | blynk_gmtime_r(&mTime, &mTm); |
Volodymyr Shymanskyy |
8:a34aa310ec48 | 93 | } |
Volodymyr Shymanskyy |
8:a34aa310ec48 | 94 | |
Volodymyr Shymanskyy |
8:a34aa310ec48 | 95 | BlynkDateTime(blynk_time_t t) |
Volodymyr Shymanskyy |
8:a34aa310ec48 | 96 | { |
Volodymyr Shymanskyy |
8:a34aa310ec48 | 97 | mTime = t; |
Volodymyr Shymanskyy |
8:a34aa310ec48 | 98 | blynk_gmtime_r(&mTime, &mTm); |
Volodymyr Shymanskyy |
8:a34aa310ec48 | 99 | } |
Volodymyr Shymanskyy |
8:a34aa310ec48 | 100 | |
Volodymyr Shymanskyy |
8:a34aa310ec48 | 101 | BlynkDateTime(int hour, int minute, int second, int day, int month, int year) |
Volodymyr Shymanskyy |
8:a34aa310ec48 | 102 | { |
Volodymyr Shymanskyy |
8:a34aa310ec48 | 103 | mTm.tm_hour = hour; |
Volodymyr Shymanskyy |
8:a34aa310ec48 | 104 | mTm.tm_min = minute; |
Volodymyr Shymanskyy |
8:a34aa310ec48 | 105 | mTm.tm_sec = second; |
Volodymyr Shymanskyy |
8:a34aa310ec48 | 106 | |
Volodymyr Shymanskyy |
8:a34aa310ec48 | 107 | mTm.tm_mday = day; |
Volodymyr Shymanskyy |
8:a34aa310ec48 | 108 | mTm.tm_mon = month - 1; |
Volodymyr Shymanskyy |
8:a34aa310ec48 | 109 | mTm.tm_year = year - 1900; |
Volodymyr Shymanskyy |
8:a34aa310ec48 | 110 | |
Volodymyr Shymanskyy |
8:a34aa310ec48 | 111 | mTm.tm_isdst = 0; |
Volodymyr Shymanskyy |
8:a34aa310ec48 | 112 | |
Volodymyr Shymanskyy |
8:a34aa310ec48 | 113 | mTime = blynk_mk_gmtime(&mTm); |
Volodymyr Shymanskyy |
8:a34aa310ec48 | 114 | } |
Volodymyr Shymanskyy |
8:a34aa310ec48 | 115 | |
Volodymyr Shymanskyy |
8:a34aa310ec48 | 116 | int second() const { return mTm.tm_sec; } |
Volodymyr Shymanskyy |
8:a34aa310ec48 | 117 | int minute() const { return mTm.tm_min; } |
Volodymyr Shymanskyy |
8:a34aa310ec48 | 118 | int hour() const { return mTm.tm_hour; } |
Volodymyr Shymanskyy |
8:a34aa310ec48 | 119 | int day() const { return mTm.tm_mday; } |
Volodymyr Shymanskyy |
8:a34aa310ec48 | 120 | int month() const { return 1 + mTm.tm_mon; } |
Volodymyr Shymanskyy |
8:a34aa310ec48 | 121 | int year() const { return 1900 + mTm.tm_year; } |
Volodymyr Shymanskyy |
8:a34aa310ec48 | 122 | |
Volodymyr Shymanskyy |
8:a34aa310ec48 | 123 | int day_of_year() const { return 1 + mTm.tm_yday; } |
Volodymyr Shymanskyy |
8:a34aa310ec48 | 124 | int day_of_week() const { return mTm.tm_wday == 0 ? 7 : mTm.tm_wday; } |
Volodymyr Shymanskyy |
8:a34aa310ec48 | 125 | |
Volodymyr Shymanskyy |
8:a34aa310ec48 | 126 | /*int weak_of_year() const { |
Volodymyr Shymanskyy |
8:a34aa310ec48 | 127 | int julian = day_of_year(); |
Volodymyr Shymanskyy |
8:a34aa310ec48 | 128 | int dow = day_of_week(); |
Volodymyr Shymanskyy |
8:a34aa310ec48 | 129 | int dowJan1 = BlynkDateTime(0,0,0, 1,1,year()).day_of_week(); |
Volodymyr Shymanskyy |
8:a34aa310ec48 | 130 | int weekNum = ((julian + 6) / 7); |
Volodymyr Shymanskyy |
8:a34aa310ec48 | 131 | if (dow < dowJan1) |
Volodymyr Shymanskyy |
8:a34aa310ec48 | 132 | ++weekNum; |
Volodymyr Shymanskyy |
8:a34aa310ec48 | 133 | return (weekNum); |
Volodymyr Shymanskyy |
8:a34aa310ec48 | 134 | }*/ |
Volodymyr Shymanskyy |
8:a34aa310ec48 | 135 | |
Volodymyr Shymanskyy |
8:a34aa310ec48 | 136 | int hour12() const { |
Volodymyr Shymanskyy |
8:a34aa310ec48 | 137 | int h = hour(); |
Volodymyr Shymanskyy |
8:a34aa310ec48 | 138 | if (h == 0) |
Volodymyr Shymanskyy |
8:a34aa310ec48 | 139 | return 12; // 12 midnight |
Volodymyr Shymanskyy |
8:a34aa310ec48 | 140 | else if (h > 12) |
Volodymyr Shymanskyy |
8:a34aa310ec48 | 141 | return h - 12; |
Volodymyr Shymanskyy |
8:a34aa310ec48 | 142 | return h; |
Volodymyr Shymanskyy |
8:a34aa310ec48 | 143 | } |
Volodymyr Shymanskyy |
8:a34aa310ec48 | 144 | |
Volodymyr Shymanskyy |
8:a34aa310ec48 | 145 | bool isAM() const { return !isPM(); } |
Volodymyr Shymanskyy |
8:a34aa310ec48 | 146 | bool isPM() const { return (hour() >= 12); } |
Volodymyr Shymanskyy |
8:a34aa310ec48 | 147 | |
Volodymyr Shymanskyy |
8:a34aa310ec48 | 148 | void adjustSeconds(int sec) { |
Volodymyr Shymanskyy |
8:a34aa310ec48 | 149 | if (isValid()) { |
Volodymyr Shymanskyy |
8:a34aa310ec48 | 150 | mTime += sec; |
Volodymyr Shymanskyy |
8:a34aa310ec48 | 151 | blynk_gmtime_r(&mTime, &mTm); |
Volodymyr Shymanskyy |
8:a34aa310ec48 | 152 | } |
Volodymyr Shymanskyy |
8:a34aa310ec48 | 153 | } |
Volodymyr Shymanskyy |
8:a34aa310ec48 | 154 | |
Volodymyr Shymanskyy |
8:a34aa310ec48 | 155 | //tm& getTm() { return mTm; } |
Volodymyr Shymanskyy |
8:a34aa310ec48 | 156 | blynk_time_t getUnix() const { return mTime; } |
Volodymyr Shymanskyy |
8:a34aa310ec48 | 157 | |
Volodymyr Shymanskyy |
8:a34aa310ec48 | 158 | bool isValid() const { return mTime != 0; } |
Volodymyr Shymanskyy |
8:a34aa310ec48 | 159 | operator bool() const { return isValid(); } |
Volodymyr Shymanskyy |
8:a34aa310ec48 | 160 | |
Volodymyr Shymanskyy |
8:a34aa310ec48 | 161 | bool operator == (const BlynkDateTime& t) const { return mTime == t.mTime; } |
Volodymyr Shymanskyy |
8:a34aa310ec48 | 162 | bool operator >= (const BlynkDateTime& t) const { return mTime >= t.mTime; } |
Volodymyr Shymanskyy |
8:a34aa310ec48 | 163 | bool operator <= (const BlynkDateTime& t) const { return mTime <= t.mTime; } |
Volodymyr Shymanskyy |
8:a34aa310ec48 | 164 | bool operator > (const BlynkDateTime& t) const { return mTime > t.mTime; } |
Volodymyr Shymanskyy |
8:a34aa310ec48 | 165 | bool operator < (const BlynkDateTime& t) const { return mTime < t.mTime; } |
Volodymyr Shymanskyy |
8:a34aa310ec48 | 166 | |
Volodymyr Shymanskyy |
8:a34aa310ec48 | 167 | private: |
Volodymyr Shymanskyy |
8:a34aa310ec48 | 168 | blynk_tm mTm; |
Volodymyr Shymanskyy |
8:a34aa310ec48 | 169 | blynk_time_t mTime; |
Volodymyr Shymanskyy |
8:a34aa310ec48 | 170 | }; |
Volodymyr Shymanskyy |
8:a34aa310ec48 | 171 | |
Volodymyr Shymanskyy |
8:a34aa310ec48 | 172 | |
Volodymyr Shymanskyy |
8:a34aa310ec48 | 173 | #endif |