|
| 1 | +//#include <avr/pgmspace.h> |
| 2 | +#include "DateTime.h" |
| 3 | + |
| 4 | +#define SECONDS_PER_DAY 86400L |
| 5 | +#define SECONDS_FROM_1900_TO_2000 3155673600 |
| 6 | +#define SECONDS_FROM_1970_TO_2000 946684800 |
| 7 | + |
| 8 | +//has to be const or compiler compaints |
| 9 | +const uint16_t daysInMonth [] PROGMEM = {31,28,31,30,31,30,31,31,30,31,30,31}; |
| 10 | + |
| 11 | +// number of days since 2000/01/01, valid for 2001..2099 |
| 12 | +static uint16_t date2days(uint16_t y, uint16_t m, uint16_t d) { |
| 13 | + if (y >= 2000) { |
| 14 | + y -= 2000; |
| 15 | + } |
| 16 | + uint16_t days = d; |
| 17 | + for (uint16_t i = 1; i < m; ++i) { |
| 18 | + days += pgm_read_byte(daysInMonth + i - 1); |
| 19 | + } |
| 20 | + if (m > 2 && y % 4 == 0) { |
| 21 | + ++days; |
| 22 | + } |
| 23 | + return days + 365 * y + (y + 3) / 4 - 1; |
| 24 | +} |
| 25 | + |
| 26 | +static long time2long(uint16_t days, uint16_t h, uint16_t m, uint16_t s) { |
| 27 | + return ((days * 24L + h) * 60 + m) * 60 + s; |
| 28 | +} |
| 29 | + |
| 30 | +//////////////////////////////////////////////////////////////////////////////// |
| 31 | +// DateTime implementation - ignores time zones and DST changes |
| 32 | +// NOTE: also ignores leap seconds, see http://en.wikipedia.org/wiki/Leap_second |
| 33 | + |
| 34 | +DateTime::DateTime(uint32_t t, unsigned long microsfraction) |
| 35 | + : microsfraction_(microsfraction) { |
| 36 | + // bring to 2000 timestamp from 1900 |
| 37 | + t -= SECONDS_FROM_1900_TO_2000; |
| 38 | + |
| 39 | + second_ = t % 60; |
| 40 | + t /= 60; |
| 41 | + minute_ = t % 60; |
| 42 | + t /= 60; |
| 43 | + hour_ = t % 24; |
| 44 | + uint16_t days = t / 24; |
| 45 | + uint16_t leap; |
| 46 | + for (year_ = 0; ; ++year_) { |
| 47 | + leap = year_ % 4 == 0; |
| 48 | + if (days < 365 + leap) { |
| 49 | + break; |
| 50 | + } |
| 51 | + days -= 365 + leap; |
| 52 | + } |
| 53 | + for (month_ = 1; ; ++month_) { |
| 54 | + uint16_t daysPerMonth = pgm_read_byte(daysInMonth + month_ - 1); |
| 55 | + if (leap && month_ == 2) { |
| 56 | + ++daysPerMonth; |
| 57 | + } |
| 58 | + if (days < daysPerMonth) { |
| 59 | + break; |
| 60 | + } |
| 61 | + days -= daysPerMonth; |
| 62 | + } |
| 63 | + day_ = days + 1; |
| 64 | +} |
| 65 | + |
| 66 | +void DateTime::time(uint32_t t) { |
| 67 | + // bring to 2000 timestamp from 1900 |
| 68 | + t -= SECONDS_FROM_1900_TO_2000; |
| 69 | + |
| 70 | + second_ = t % 60; |
| 71 | + t /= 60; |
| 72 | + minute_ = t % 60; |
| 73 | + t /= 60; |
| 74 | + hour_ = t % 24; |
| 75 | + uint16_t days = t / 24; |
| 76 | + uint16_t leap; |
| 77 | + for (year_ = 0; ; ++year_) { |
| 78 | + leap = year_ % 4 == 0; |
| 79 | + if (days < 365 + leap) { |
| 80 | + break; |
| 81 | + } |
| 82 | + days -= 365 + leap; |
| 83 | + } |
| 84 | + for (month_ = 1; ; ++month_) { |
| 85 | + uint16_t daysPerMonth = pgm_read_byte(daysInMonth + month_ - 1); |
| 86 | + if (leap && month_ == 2) { |
| 87 | + ++daysPerMonth; |
| 88 | + } |
| 89 | + if (days < daysPerMonth) { |
| 90 | + break; |
| 91 | + } |
| 92 | + days -= daysPerMonth; |
| 93 | + } |
| 94 | + day_ = days + 1; |
| 95 | +} |
| 96 | + |
| 97 | +DateTime::DateTime( |
| 98 | + uint16_t year, uint16_t month, |
| 99 | + uint16_t day, uint16_t hour, |
| 100 | + uint16_t minute, uint16_t second, |
| 101 | + unsigned long microsfraction) |
| 102 | + : year_( (year >= 2000) ? year - 2000 : year), |
| 103 | + month_(month), |
| 104 | + day_(day), |
| 105 | + hour_(hour), |
| 106 | + minute_(minute), |
| 107 | + second_(second), |
| 108 | + microsfraction_(microsfraction) {} |
| 109 | + |
| 110 | +static uint16_t conv2d(const char *p) { |
| 111 | + uint16_t v = 0; |
| 112 | + if ('0' <= *p && *p <= '9') { |
| 113 | + v = *p - '0'; |
| 114 | + } |
| 115 | + return 10 * v + *++p - '0'; |
| 116 | +} |
| 117 | + |
| 118 | +// A convenient constructor for using "the compiler's time": |
| 119 | +// DateTime now (__DATE__, __TIME__); |
| 120 | +// NOTE: using PSTR would further reduce the RAM footprint |
| 121 | +DateTime::DateTime( |
| 122 | + const char *date, |
| 123 | + const char *time, |
| 124 | + unsigned long microsfraction) |
| 125 | + : microsfraction_(microsfraction) { |
| 126 | + // sample input: date = "Dec 26 2009", time = "12:34:56" |
| 127 | + year_ = conv2d(date + 9); |
| 128 | + // Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec |
| 129 | + switch (date[0]) { |
| 130 | + case 'J': |
| 131 | + month_ = date[1] == 'a' ? 1 : (month_ = date[2] == 'n' ? 6 : 7); |
| 132 | + break; |
| 133 | + case 'F': |
| 134 | + month_ = 2; |
| 135 | + break; |
| 136 | + case 'A': |
| 137 | + month_ = date[2] == 'r' ? 4 : 8; |
| 138 | + break; |
| 139 | + case 'M': |
| 140 | + month_ = date[2] == 'r' ? 3 : 5; |
| 141 | + break; |
| 142 | + case 'S': |
| 143 | + month_ = 9; |
| 144 | + break; |
| 145 | + case 'O': |
| 146 | + month_ = 10; |
| 147 | + break; |
| 148 | + case 'N': |
| 149 | + month_ = 11; |
| 150 | + break; |
| 151 | + case 'D': |
| 152 | + month_ = 12; |
| 153 | + break; |
| 154 | + default: |
| 155 | + break; |
| 156 | + } |
| 157 | + day_ = conv2d(date + 4); |
| 158 | + hour_ = conv2d(time); |
| 159 | + minute_ = conv2d(time + 3); |
| 160 | + second_ = conv2d(time + 6); |
| 161 | + |
| 162 | + |
| 163 | + /*uint16_t DateTime::dayOfWeek() const { |
| 164 | + uint16_t day = date2days(year_, month_, day_); |
| 165 | +
|
| 166 | + // Jan 1, 2000 is a Saturday, i.e. returns 6 |
| 167 | + return (day + 6) % 7; |
| 168 | +
|
| 169 | + */ |
| 170 | +} |
| 171 | + |
| 172 | +uint32_t DateTime::ntptime(void) const |
| 173 | +{ |
| 174 | + uint32_t t; |
| 175 | + uint16_t days = date2days(year_, month_, day_); |
| 176 | + t = time2long(days, hour_, minute_, second_); |
| 177 | + t += SECONDS_FROM_1900_TO_2000; |
| 178 | + |
| 179 | + return t; |
| 180 | +} |
| 181 | + |
| 182 | +uint32_t DateTime::unixtime(void) const |
| 183 | +{ |
| 184 | + uint32_t t; |
| 185 | + uint16_t days = date2days(year_, month_, day_); |
| 186 | + t = time2long(days, hour_, minute_, second_); |
| 187 | + t += SECONDS_FROM_1970_TO_2000; |
| 188 | + |
| 189 | + return t; |
| 190 | +} |
| 191 | + |
| 192 | +String DateTime::toStringTime(void) const |
| 193 | +{ |
| 194 | + char chartime[9]; |
| 195 | + sprintf(chartime, "%02d:%02d:%02d", hour(), minute(), second() ); |
| 196 | + return chartime; |
| 197 | +} |
| 198 | + |
| 199 | + |
| 200 | +String DateTime::toStringDate(void) const |
| 201 | +{ |
| 202 | + char charDate[11]; |
| 203 | + sprintf(charDate, "%02d/%02d/%04d", day(), month(), year() ); |
| 204 | + return charDate; |
| 205 | +} |
| 206 | + |
0 commit comments