| 74 | static const uint8_t monthDays[]={31,28,31,30,31,30,31,31,30,31,30,31}; // API starts months from 1, this array starts from 0 |
| 75 | |
| 76 | void breakTime(time_t timeInput, tmElements_t &tm){ |
| 77 | // break the given time_t into time components |
| 78 | // this is a more compact version of the C library localtime function |
| 79 | // note that year is offset from 1970 !!! |
| 80 | |
| 81 | uint8_t year; |
| 82 | uint8_t month, monthLength; |
| 83 | uint32_t time; |
| 84 | unsigned long days; |
| 85 | |
| 86 | time = (uint32_t)timeInput; |
| 87 | tm.Second = time % 60; |
| 88 | time /= 60; // now it is minutes |
| 89 | tm.Minute = time % 60; |
| 90 | time /= 60; // now it is hours |
| 91 | tm.Hour = time % 24; |
| 92 | time /= 24; // now it is days |
| 93 | tm.Wday = ((time + 4) % 7) + 1; // Sunday is day 1 |
| 94 | |
| 95 | year = 0; |
| 96 | days = 0; |
| 97 | while((unsigned)(days += (LEAP_YEAR(year) ? 366 : 365)) <= time) { |
| 98 | year++; |
| 99 | } |
| 100 | tm.Year = year; // year is offset from 1970 |
| 101 | |
| 102 | days -= LEAP_YEAR(year) ? 366 : 365; |
| 103 | time -= days; // now it is days in this year, starting at 0 |
| 104 | |
| 105 | days=0; |
| 106 | month=0; |
| 107 | monthLength=0; |
| 108 | for (month=0; month<12; month++) { |
| 109 | if (month==1) { // february |
| 110 | if (LEAP_YEAR(year)) { |
| 111 | monthLength=29; |
| 112 | } else { |
| 113 | monthLength=28; |
| 114 | } |
| 115 | } else { |
| 116 | monthLength = monthDays[month]; |
| 117 | } |
| 118 | |
| 119 | if (time >= monthLength) { |
| 120 | time -= monthLength; |
| 121 | } else { |
| 122 | break; |
| 123 | } |
| 124 | } |
| 125 | tm.Month = month + 1; // jan is month 1 |
| 126 | tm.Day = time + 1; // day of month |
| 127 | } |
| 128 | |
| 129 | time_t makeTime(tmElements_t &tm){ |
| 130 | // assemble time elements into time_t |