Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of WIZwiki-7500_Blynk by
BlynkDateTime.h
00001 /** 00002 * @file BlynkDateTime.h 00003 * @author Volodymyr Shymanskyy 00004 * @license This project is released under the MIT License (MIT) 00005 * @copyright Copyright (c) 2016 Volodymyr Shymanskyy 00006 * @date Aug 2016 00007 * @brief DateTime implementation 00008 * 00009 */ 00010 00011 #ifndef BlynkDateTime_h 00012 #define BlynkDateTime_h 00013 00014 typedef long blynk_time_t; 00015 00016 struct blynk_tm *blynk_gmtime_r(const blynk_time_t *time, struct blynk_tm *tm); 00017 blynk_time_t blynk_mk_gmtime(struct blynk_tm *tm); 00018 00019 struct blynk_tm { 00020 int8_t tm_sec; 00021 int8_t tm_min; 00022 int8_t tm_hour; 00023 int8_t tm_mday; 00024 int8_t tm_wday; 00025 int8_t tm_mon; 00026 int16_t tm_year; 00027 int16_t tm_yday; 00028 int16_t tm_isdst; 00029 }; 00030 00031 class BlynkTime { 00032 00033 public: 00034 static const uint32_t MAX_TIME = 86400L; 00035 00036 BlynkTime() : mTime(-1) {} 00037 00038 BlynkTime(const BlynkTime& t) : mTime(t.mTime) {} 00039 00040 BlynkTime(long seconds) : mTime(seconds % MAX_TIME) {} 00041 00042 BlynkTime(int hour, int minute, int second) 00043 { 00044 mTime = (hour * 3600 + minute * 60 + second) % MAX_TIME; 00045 } 00046 00047 int second() const { return mTime % 60; } 00048 int minute() const { return (mTime / 60) % 60; } 00049 int hour() const { return mTime / 3600; } 00050 00051 int hour12() const { 00052 int h = hour(); 00053 if (h == 0) 00054 return 12; // 12 midnight 00055 else if (h > 12) 00056 return h - 12; 00057 return h; 00058 } 00059 00060 bool isAM() const { return !isPM(); } 00061 bool isPM() const { return (hour() >= 12); } 00062 00063 void adjustSeconds(int sec) { 00064 if (isValid()) { 00065 mTime = (mTime + sec) % MAX_TIME; 00066 } 00067 } 00068 00069 blynk_time_t getUnixOffset() const { return mTime; } 00070 00071 bool isValid() const { return mTime < MAX_TIME; } 00072 operator bool() const { return isValid(); } 00073 00074 bool operator == (const BlynkTime& t) const { return mTime == t.mTime; } 00075 bool operator >= (const BlynkTime& t) const { return mTime >= t.mTime; } 00076 bool operator <= (const BlynkTime& t) const { return mTime <= t.mTime; } 00077 bool operator > (const BlynkTime& t) const { return mTime > t.mTime; } 00078 bool operator < (const BlynkTime& t) const { return mTime < t.mTime; } 00079 00080 private: 00081 uint32_t mTime; 00082 }; 00083 00084 class BlynkDateTime { 00085 00086 public: 00087 BlynkDateTime() : mTime(0) {} 00088 00089 BlynkDateTime(const BlynkDateTime& t) 00090 { 00091 mTime = t.mTime; 00092 blynk_gmtime_r(&mTime, &mTm); 00093 } 00094 00095 BlynkDateTime(blynk_time_t t) 00096 { 00097 mTime = t; 00098 blynk_gmtime_r(&mTime, &mTm); 00099 } 00100 00101 BlynkDateTime(int hour, int minute, int second, int day, int month, int year) 00102 { 00103 mTm.tm_hour = hour; 00104 mTm.tm_min = minute; 00105 mTm.tm_sec = second; 00106 00107 mTm.tm_mday = day; 00108 mTm.tm_mon = month - 1; 00109 mTm.tm_year = year - 1900; 00110 00111 mTm.tm_isdst = 0; 00112 00113 mTime = blynk_mk_gmtime(&mTm); 00114 } 00115 00116 int second() const { return mTm.tm_sec; } 00117 int minute() const { return mTm.tm_min; } 00118 int hour() const { return mTm.tm_hour; } 00119 int day() const { return mTm.tm_mday; } 00120 int month() const { return 1 + mTm.tm_mon; } 00121 int year() const { return 1900 + mTm.tm_year; } 00122 00123 int day_of_year() const { return 1 + mTm.tm_yday; } 00124 int day_of_week() const { return mTm.tm_wday == 0 ? 7 : mTm.tm_wday; } 00125 00126 /*int weak_of_year() const { 00127 int julian = day_of_year(); 00128 int dow = day_of_week(); 00129 int dowJan1 = BlynkDateTime(0,0,0, 1,1,year()).day_of_week(); 00130 int weekNum = ((julian + 6) / 7); 00131 if (dow < dowJan1) 00132 ++weekNum; 00133 return (weekNum); 00134 }*/ 00135 00136 int hour12() const { 00137 int h = hour(); 00138 if (h == 0) 00139 return 12; // 12 midnight 00140 else if (h > 12) 00141 return h - 12; 00142 return h; 00143 } 00144 00145 bool isAM() const { return !isPM(); } 00146 bool isPM() const { return (hour() >= 12); } 00147 00148 void adjustSeconds(int sec) { 00149 if (isValid()) { 00150 mTime += sec; 00151 blynk_gmtime_r(&mTime, &mTm); 00152 } 00153 } 00154 00155 //tm& getTm() { return mTm; } 00156 blynk_time_t getUnix() const { return mTime; } 00157 00158 bool isValid() const { return mTime != 0; } 00159 operator bool() const { return isValid(); } 00160 00161 bool operator == (const BlynkDateTime& t) const { return mTime == t.mTime; } 00162 bool operator >= (const BlynkDateTime& t) const { return mTime >= t.mTime; } 00163 bool operator <= (const BlynkDateTime& t) const { return mTime <= t.mTime; } 00164 bool operator > (const BlynkDateTime& t) const { return mTime > t.mTime; } 00165 bool operator < (const BlynkDateTime& t) const { return mTime < t.mTime; } 00166 00167 private: 00168 blynk_tm mTm; 00169 blynk_time_t mTime; 00170 }; 00171 00172 00173 #endif
Generated on Thu Jul 14 2022 00:21:50 by
1.7.2
