Andrew Boyson / clock

Dependents:   oldheating gps motorhome heating

Committer:
andrewboyson
Date:
Thu Feb 21 21:08:03 2019 +0000
Revision:
57:4daf2e423b27
Parent:
44:aa45226d118e
Child:
58:ad2bfd0345de
Two changes: defined typedef for clktime from int64_t; created new typedef time64 from int64_t to replace time_t to avoid the 2038 problem.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
andrewboyson 26:0421132e6eaf 1 #include <stdlib.h>
andrewboyson 26:0421132e6eaf 2 #include <stdio.h>
andrewboyson 26:0421132e6eaf 3 #include <string.h>
andrewboyson 26:0421132e6eaf 4 #include <stdbool.h>
andrewboyson 57:4daf2e423b27 5 #include <time.h>
andrewboyson 57:4daf2e423b27 6
andrewboyson 57:4daf2e423b27 7 #include "time64.h"
andrewboyson 26:0421132e6eaf 8
andrewboyson 26:0421132e6eaf 9 #define STD_OFFSET 0
andrewboyson 26:0421132e6eaf 10 #define DST_OFFSET 1
andrewboyson 26:0421132e6eaf 11
andrewboyson 37:330b844f54b6 12
andrewboyson 26:0421132e6eaf 13 static bool isLeapYear(int year)
andrewboyson 26:0421132e6eaf 14 {
andrewboyson 26:0421132e6eaf 15 year += 1900;
andrewboyson 26:0421132e6eaf 16 bool leapYear = !(year & 0x3);
andrewboyson 26:0421132e6eaf 17 if (year >= 2100)
andrewboyson 26:0421132e6eaf 18 {
andrewboyson 26:0421132e6eaf 19 if (year % 100 == 0) leapYear = false;
andrewboyson 26:0421132e6eaf 20 if (year % 400 == 0) leapYear = true;
andrewboyson 26:0421132e6eaf 21 }
andrewboyson 26:0421132e6eaf 22 return leapYear;
andrewboyson 26:0421132e6eaf 23
andrewboyson 26:0421132e6eaf 24 }
andrewboyson 26:0421132e6eaf 25 static int monthLength(int year, int month)
andrewboyson 26:0421132e6eaf 26 {
andrewboyson 26:0421132e6eaf 27 static char monthlengths[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
andrewboyson 26:0421132e6eaf 28 int daysInMonth = monthlengths[month];
andrewboyson 26:0421132e6eaf 29 if (month == 1 && isLeapYear(year)) daysInMonth++; //February is month 1 of months 0 to 11
andrewboyson 26:0421132e6eaf 30 return daysInMonth;
andrewboyson 26:0421132e6eaf 31 }
andrewboyson 26:0421132e6eaf 32 static bool isDst(int year, int month, int dayOfMonth, int dayOfWeek, int hours)
andrewboyson 26:0421132e6eaf 33 {
andrewboyson 26:0421132e6eaf 34 //Find the last Sunday in the month
andrewboyson 26:0421132e6eaf 35 int lastDayOfMonth = monthLength(year, month);
andrewboyson 26:0421132e6eaf 36 int daysToEndOfMonth = lastDayOfMonth - dayOfMonth;
andrewboyson 26:0421132e6eaf 37 int dayOfWeekOfLastDayOfMonth = (dayOfWeek + daysToEndOfMonth) % 7;
andrewboyson 26:0421132e6eaf 38 int lastSundayDayOfMonth = lastDayOfMonth - dayOfWeekOfLastDayOfMonth;
andrewboyson 26:0421132e6eaf 39
andrewboyson 26:0421132e6eaf 40 //Check each month
andrewboyson 26:0421132e6eaf 41 if (month <= 1) return false; //Jan, Feb
andrewboyson 26:0421132e6eaf 42 if (month == 2) //Mar - DST true after 1am UTC on the last Sunday in March
andrewboyson 26:0421132e6eaf 43 {
andrewboyson 26:0421132e6eaf 44 if (dayOfMonth < lastSundayDayOfMonth) return false;
andrewboyson 26:0421132e6eaf 45 if (dayOfMonth == lastSundayDayOfMonth) return hours >= 1;
andrewboyson 26:0421132e6eaf 46 if (dayOfMonth > lastSundayDayOfMonth) return true;
andrewboyson 26:0421132e6eaf 47 }
andrewboyson 26:0421132e6eaf 48 if (month >= 3 && month <= 8) return true; //Apr, May, Jun, Jul, Aug, Sep
andrewboyson 26:0421132e6eaf 49 if (month == 9) //Oct - DST false after 1am UTC on the last Sunday in October
andrewboyson 26:0421132e6eaf 50 {
andrewboyson 26:0421132e6eaf 51 if (dayOfMonth < lastSundayDayOfMonth) return true;
andrewboyson 26:0421132e6eaf 52 if (dayOfMonth == lastSundayDayOfMonth) return hours < 1;
andrewboyson 26:0421132e6eaf 53 if (dayOfMonth > lastSundayDayOfMonth) return false;
andrewboyson 26:0421132e6eaf 54 }
andrewboyson 26:0421132e6eaf 55 if (month >= 10) return false; //Nov, Dec
andrewboyson 26:0421132e6eaf 56 return false;
andrewboyson 26:0421132e6eaf 57 }
andrewboyson 26:0421132e6eaf 58 static void calculateDayOfYearAndWeek(int thisYear, int thisMonth, int thisMonthDay, int* pDayOfYear, int* pDayOfWeek)
andrewboyson 26:0421132e6eaf 59 {
andrewboyson 26:0421132e6eaf 60 int dayOfYear = 0; //1 Jan is day 0
andrewboyson 26:0421132e6eaf 61 int dayOfWeek = 4; //1 Jan 1970 is a Thursday
andrewboyson 26:0421132e6eaf 62
andrewboyson 26:0421132e6eaf 63 //Add days of each whole year
andrewboyson 26:0421132e6eaf 64 for (int y = 70; y < thisYear; y++)
andrewboyson 26:0421132e6eaf 65 {
andrewboyson 26:0421132e6eaf 66 int lengthOfYear = isLeapYear(y) ? 366 : 365;
andrewboyson 26:0421132e6eaf 67 dayOfWeek += lengthOfYear;
andrewboyson 26:0421132e6eaf 68 }
andrewboyson 26:0421132e6eaf 69
andrewboyson 26:0421132e6eaf 70 //Add days of each whole month
andrewboyson 26:0421132e6eaf 71 for (int m = 0; m < thisMonth; m++)
andrewboyson 26:0421132e6eaf 72 {
andrewboyson 26:0421132e6eaf 73 int lengthOfMonth = monthLength(thisYear, m);
andrewboyson 26:0421132e6eaf 74 dayOfYear += lengthOfMonth;
andrewboyson 26:0421132e6eaf 75 dayOfWeek += lengthOfMonth;
andrewboyson 26:0421132e6eaf 76 }
andrewboyson 26:0421132e6eaf 77
andrewboyson 26:0421132e6eaf 78 //Add days of part month
andrewboyson 26:0421132e6eaf 79 thisMonthDay--; //thisMonthDay is 01 to 31 where we need 00 to 30
andrewboyson 26:0421132e6eaf 80 dayOfYear += thisMonthDay;
andrewboyson 26:0421132e6eaf 81 dayOfWeek += thisMonthDay;
andrewboyson 26:0421132e6eaf 82
andrewboyson 26:0421132e6eaf 83 //Update the day of year and day of week parts of the struct tm
andrewboyson 26:0421132e6eaf 84 *pDayOfYear = dayOfYear; // 0 --> 365
andrewboyson 26:0421132e6eaf 85 *pDayOfWeek = dayOfWeek % 7; // 0 --> 6
andrewboyson 26:0421132e6eaf 86 }
andrewboyson 26:0421132e6eaf 87 static void normalise(int* pHours, int* pDayOfWeek, int* pDayOfMonth, int* pMonth, int * pDayOfYear, int* pYear)
andrewboyson 26:0421132e6eaf 88 {
andrewboyson 26:0421132e6eaf 89 if (*pHours > 23)
andrewboyson 26:0421132e6eaf 90 {
andrewboyson 26:0421132e6eaf 91 *pHours -= 24;
andrewboyson 26:0421132e6eaf 92 ++*pDayOfWeek;
andrewboyson 26:0421132e6eaf 93 if (*pDayOfWeek > 6) *pDayOfWeek = 0;
andrewboyson 26:0421132e6eaf 94 ++*pDayOfYear;
andrewboyson 26:0421132e6eaf 95 ++*pDayOfMonth;
andrewboyson 26:0421132e6eaf 96 if (*pDayOfMonth > monthLength(*pYear, *pMonth))
andrewboyson 26:0421132e6eaf 97 {
andrewboyson 26:0421132e6eaf 98 ++*pMonth;
andrewboyson 26:0421132e6eaf 99 if (*pMonth > 11)
andrewboyson 26:0421132e6eaf 100 {
andrewboyson 26:0421132e6eaf 101 ++*pYear;
andrewboyson 26:0421132e6eaf 102 *pDayOfYear = 0;
andrewboyson 26:0421132e6eaf 103 *pMonth = 0;
andrewboyson 26:0421132e6eaf 104 }
andrewboyson 26:0421132e6eaf 105 *pDayOfMonth = 1;
andrewboyson 26:0421132e6eaf 106 }
andrewboyson 26:0421132e6eaf 107 }
andrewboyson 26:0421132e6eaf 108
andrewboyson 26:0421132e6eaf 109 if (*pHours < 0)
andrewboyson 26:0421132e6eaf 110 {
andrewboyson 26:0421132e6eaf 111 *pHours += 24;
andrewboyson 26:0421132e6eaf 112 --*pDayOfWeek;
andrewboyson 26:0421132e6eaf 113 if (*pDayOfWeek < 0) *pDayOfWeek = 6;
andrewboyson 26:0421132e6eaf 114 --*pDayOfYear;
andrewboyson 26:0421132e6eaf 115 --*pDayOfMonth;
andrewboyson 26:0421132e6eaf 116 if (*pDayOfMonth < 1)
andrewboyson 26:0421132e6eaf 117 {
andrewboyson 26:0421132e6eaf 118 --*pMonth;
andrewboyson 26:0421132e6eaf 119 if (*pMonth < 0)
andrewboyson 26:0421132e6eaf 120 {
andrewboyson 26:0421132e6eaf 121 --*pYear;
andrewboyson 26:0421132e6eaf 122 *pDayOfYear = isLeapYear(*pYear) ? 365 : 364;
andrewboyson 26:0421132e6eaf 123 *pMonth = 11;
andrewboyson 26:0421132e6eaf 124 }
andrewboyson 26:0421132e6eaf 125 *pDayOfMonth = monthLength(*pYear, *pMonth);
andrewboyson 26:0421132e6eaf 126 }
andrewboyson 26:0421132e6eaf 127 }
andrewboyson 26:0421132e6eaf 128 }
andrewboyson 26:0421132e6eaf 129 static void addYears(int* pYear, int* pDayOfWeek, int* pDaysLeft)
andrewboyson 26:0421132e6eaf 130 {
andrewboyson 26:0421132e6eaf 131 while(1)
andrewboyson 26:0421132e6eaf 132 {
andrewboyson 26:0421132e6eaf 133 //See if it is a leap year
andrewboyson 26:0421132e6eaf 134 int leapYear = isLeapYear(*pYear);
andrewboyson 26:0421132e6eaf 135
andrewboyson 26:0421132e6eaf 136 //Find the number of days in this year
andrewboyson 26:0421132e6eaf 137 int daysInYear = leapYear ? 366 : 365;
andrewboyson 26:0421132e6eaf 138
andrewboyson 26:0421132e6eaf 139 //Stop if this is the final year
andrewboyson 26:0421132e6eaf 140 if (*pDaysLeft < daysInYear) break;
andrewboyson 26:0421132e6eaf 141
andrewboyson 26:0421132e6eaf 142 //Calculate the current day of the week at the start of the year
andrewboyson 26:0421132e6eaf 143 *pDayOfWeek += leapYear ? 2 : 1;
andrewboyson 26:0421132e6eaf 144 if (*pDayOfWeek >= 7) *pDayOfWeek -= 7;
andrewboyson 26:0421132e6eaf 145
andrewboyson 26:0421132e6eaf 146 //Move on to the next year
andrewboyson 26:0421132e6eaf 147 *pDaysLeft -= daysInYear;
andrewboyson 26:0421132e6eaf 148 ++*pYear;
andrewboyson 26:0421132e6eaf 149 }
andrewboyson 26:0421132e6eaf 150 }
andrewboyson 26:0421132e6eaf 151 static void addMonths(int year, int* pMonth, int* pDaysLeft)
andrewboyson 26:0421132e6eaf 152 {
andrewboyson 26:0421132e6eaf 153 while(1)
andrewboyson 26:0421132e6eaf 154 {
andrewboyson 26:0421132e6eaf 155 int daysInMonth = monthLength(year, *pMonth);
andrewboyson 26:0421132e6eaf 156
andrewboyson 26:0421132e6eaf 157 //Stop if this is the last month
andrewboyson 26:0421132e6eaf 158 if (*pDaysLeft < daysInMonth) break;
andrewboyson 26:0421132e6eaf 159
andrewboyson 26:0421132e6eaf 160 //Move onto next month
andrewboyson 26:0421132e6eaf 161 *pDaysLeft -= daysInMonth;
andrewboyson 26:0421132e6eaf 162 ++*pMonth;
andrewboyson 26:0421132e6eaf 163 }
andrewboyson 26:0421132e6eaf 164 }
andrewboyson 57:4daf2e423b27 165 static void timeToTm(time64 t, struct tm* ptm, bool local)
andrewboyson 26:0421132e6eaf 166 {
andrewboyson 57:4daf2e423b27 167 //Extract the seconds, minutes, hours and days from the time64 t
andrewboyson 57:4daf2e423b27 168 lldiv_t divres;
andrewboyson 57:4daf2e423b27 169 divres = lldiv( t, 60); int seconds = divres.rem;
andrewboyson 57:4daf2e423b27 170 divres = lldiv(divres.quot, 60); int minutes = divres.rem;
andrewboyson 57:4daf2e423b27 171 divres = lldiv(divres.quot, 24); int hours = divres.rem;
andrewboyson 57:4daf2e423b27 172 int daysLeft = divres.quot;
andrewboyson 26:0421132e6eaf 173
andrewboyson 26:0421132e6eaf 174 //Add a year at a time while there is more than a year of days left
andrewboyson 26:0421132e6eaf 175 int year = 70; //Unix epoch is 1970
andrewboyson 26:0421132e6eaf 176 int dayOfWeek = 4; //1 Jan 1970 is a Thursday
andrewboyson 26:0421132e6eaf 177 addYears(&year, &dayOfWeek, &daysLeft);
andrewboyson 26:0421132e6eaf 178
andrewboyson 26:0421132e6eaf 179 //Days left contains the days left from the start (1 Jan) of the current year
andrewboyson 26:0421132e6eaf 180 int dayOfYear = daysLeft;
andrewboyson 26:0421132e6eaf 181 dayOfWeek += daysLeft;
andrewboyson 26:0421132e6eaf 182 dayOfWeek %= 7;
andrewboyson 26:0421132e6eaf 183
andrewboyson 26:0421132e6eaf 184 //Add a month at a time while there is more than a month of days left
andrewboyson 26:0421132e6eaf 185 int month = 0;
andrewboyson 26:0421132e6eaf 186 addMonths(year, &month, &daysLeft);
andrewboyson 26:0421132e6eaf 187
andrewboyson 26:0421132e6eaf 188 //Days left contains the days left from the start (1st) of the current month
andrewboyson 26:0421132e6eaf 189 int dayOfMonth = daysLeft + 1;
andrewboyson 26:0421132e6eaf 190
andrewboyson 26:0421132e6eaf 191 //Deal with local time offsets
andrewboyson 26:0421132e6eaf 192 int dst;
andrewboyson 26:0421132e6eaf 193 if (local)
andrewboyson 26:0421132e6eaf 194 {
andrewboyson 26:0421132e6eaf 195 //Work out if Daylight Saving Time applies
andrewboyson 26:0421132e6eaf 196 dst = isDst(year, month, dayOfMonth, dayOfWeek, hours);
andrewboyson 26:0421132e6eaf 197
andrewboyson 26:0421132e6eaf 198 //Adjust for the timezone
andrewboyson 26:0421132e6eaf 199 hours += dst ? DST_OFFSET : STD_OFFSET;
andrewboyson 26:0421132e6eaf 200 normalise(&hours, &dayOfWeek, &dayOfMonth, &month, &dayOfYear, &year);
andrewboyson 26:0421132e6eaf 201 }
andrewboyson 26:0421132e6eaf 202 else
andrewboyson 26:0421132e6eaf 203 {
andrewboyson 26:0421132e6eaf 204 dst = -1;
andrewboyson 26:0421132e6eaf 205 }
andrewboyson 26:0421132e6eaf 206
andrewboyson 26:0421132e6eaf 207 //Set up the broken time TM structure
andrewboyson 26:0421132e6eaf 208 ptm->tm_sec = seconds; // 00 --> 59
andrewboyson 26:0421132e6eaf 209 ptm->tm_min = minutes; // 00 --> 59
andrewboyson 26:0421132e6eaf 210 ptm->tm_hour = hours; // 00 --> 23
andrewboyson 26:0421132e6eaf 211 ptm->tm_mday = dayOfMonth; // 01 --> 31
andrewboyson 26:0421132e6eaf 212 ptm->tm_mon = month; // 00 --> 11
andrewboyson 26:0421132e6eaf 213 ptm->tm_year = year; // Years since 1900
andrewboyson 26:0421132e6eaf 214 ptm->tm_wday = dayOfWeek; // 0 --> 6 where 0 == Sunday
andrewboyson 26:0421132e6eaf 215 ptm->tm_yday = dayOfYear; // 0 --> 365
andrewboyson 26:0421132e6eaf 216 ptm->tm_isdst = dst; // +ve if DST, 0 if not DSTime, -ve if the information is not available. Note that 'true' evaluates to +1.
andrewboyson 26:0421132e6eaf 217 }
andrewboyson 37:330b844f54b6 218
andrewboyson 37:330b844f54b6 219
andrewboyson 57:4daf2e423b27 220 void TmUtcFromTimeT(time64 time, struct tm* ptm)
andrewboyson 26:0421132e6eaf 221 {
andrewboyson 26:0421132e6eaf 222 timeToTm(time, ptm, false);
andrewboyson 26:0421132e6eaf 223 }
andrewboyson 57:4daf2e423b27 224 void TmLocalFromTimeT(time64 time, struct tm* ptm)
andrewboyson 26:0421132e6eaf 225 {
andrewboyson 26:0421132e6eaf 226 timeToTm(time, ptm, true);
andrewboyson 26:0421132e6eaf 227 }
andrewboyson 57:4daf2e423b27 228 time64 TmUtcToTimeT(struct tm* ptm)
andrewboyson 26:0421132e6eaf 229 {
andrewboyson 57:4daf2e423b27 230 time64 days = 0;
andrewboyson 26:0421132e6eaf 231
andrewboyson 26:0421132e6eaf 232 for (int y = 70; y < ptm->tm_year; y++) days += isLeapYear(y) ? 366 : 365;
andrewboyson 26:0421132e6eaf 233
andrewboyson 26:0421132e6eaf 234 days += ptm->tm_yday;
andrewboyson 26:0421132e6eaf 235
andrewboyson 26:0421132e6eaf 236 return days * 86400 +
andrewboyson 26:0421132e6eaf 237 ptm->tm_hour * 3600 +
andrewboyson 26:0421132e6eaf 238 ptm->tm_min * 60 +
andrewboyson 26:0421132e6eaf 239 ptm->tm_sec;
andrewboyson 26:0421132e6eaf 240 }
andrewboyson 37:330b844f54b6 241
andrewboyson 37:330b844f54b6 242
andrewboyson 31:f6ff7fdb9c67 243 int TmSecondsBetween(struct tm* ptmLater, struct tm* ptmEarlier)
andrewboyson 26:0421132e6eaf 244 {
andrewboyson 26:0421132e6eaf 245 int days = 0;
andrewboyson 26:0421132e6eaf 246
andrewboyson 26:0421132e6eaf 247 if (ptmLater->tm_year > ptmEarlier->tm_year) for (int y = ptmEarlier->tm_year; y < ptmLater->tm_year; y++) days += isLeapYear(y) ? 366 : 365;
andrewboyson 26:0421132e6eaf 248 else for (int y = ptmEarlier->tm_year; y > ptmLater->tm_year; y--) days -= isLeapYear(y) ? 366 : 365;
andrewboyson 26:0421132e6eaf 249
andrewboyson 26:0421132e6eaf 250 days += ptmLater->tm_yday - ptmEarlier->tm_yday;
andrewboyson 26:0421132e6eaf 251
andrewboyson 26:0421132e6eaf 252 return days * 86400 +
andrewboyson 26:0421132e6eaf 253 (ptmLater->tm_hour - ptmEarlier->tm_hour) * 3600 +
andrewboyson 26:0421132e6eaf 254 (ptmLater->tm_min - ptmEarlier->tm_min ) * 60 +
andrewboyson 26:0421132e6eaf 255 (ptmLater->tm_sec - ptmEarlier->tm_sec );
andrewboyson 26:0421132e6eaf 256 }
andrewboyson 26:0421132e6eaf 257
andrewboyson 31:f6ff7fdb9c67 258 void TmUtcToLocal(struct tm* ptm)
andrewboyson 26:0421132e6eaf 259 {
andrewboyson 26:0421132e6eaf 260 //Establish DST
andrewboyson 26:0421132e6eaf 261 ptm->tm_isdst = isDst(ptm->tm_year, ptm->tm_mon, ptm->tm_mday, ptm->tm_wday, ptm->tm_hour);
andrewboyson 26:0421132e6eaf 262
andrewboyson 26:0421132e6eaf 263 //Adjust for the timezone
andrewboyson 26:0421132e6eaf 264 ptm->tm_hour += ptm->tm_isdst ? DST_OFFSET : STD_OFFSET;
andrewboyson 26:0421132e6eaf 265 normalise(&ptm->tm_hour, &ptm->tm_wday, &ptm->tm_mday, &ptm->tm_mon, &ptm->tm_yday, &ptm->tm_year);
andrewboyson 26:0421132e6eaf 266 }
andrewboyson 26:0421132e6eaf 267
andrewboyson 31:f6ff7fdb9c67 268 void TmFromAsciiDateTime(const char* pDate, const char* pTime, struct tm* ptm) // Convert compile time to system time
andrewboyson 26:0421132e6eaf 269 {
andrewboyson 26:0421132e6eaf 270 //__DATE__ The string constant contains eleven characters and looks like "Feb 12 1996". If the day of the month is less than 10, it is padded with a space on the left.
andrewboyson 26:0421132e6eaf 271 char month[5];
andrewboyson 26:0421132e6eaf 272 sscanf(pDate, "%s %d %d", month, &ptm->tm_mday, &ptm->tm_year); ptm->tm_year -= 1900;
andrewboyson 26:0421132e6eaf 273
andrewboyson 26:0421132e6eaf 274 // Find where month is in month_names. Deduce month value.
andrewboyson 26:0421132e6eaf 275 static const char month_names[] = "JanFebMarAprMayJunJulAugSepOctNovDec";
andrewboyson 26:0421132e6eaf 276 ptm->tm_mon = (strstr(month_names, month) - month_names) / 3;
andrewboyson 26:0421132e6eaf 277
andrewboyson 26:0421132e6eaf 278 //__TIME__ The string constant contains eight characters and looks like "23:59:01".
andrewboyson 26:0421132e6eaf 279 sscanf(pTime, "%2d %*c %2d %*c %2d", &ptm->tm_hour, &ptm->tm_min, &ptm->tm_sec);
andrewboyson 26:0421132e6eaf 280
andrewboyson 26:0421132e6eaf 281 //Fill the day of week and the day of year part of the tm structure
andrewboyson 26:0421132e6eaf 282 calculateDayOfYearAndWeek(ptm->tm_year, ptm->tm_mon, ptm->tm_mday, &ptm->tm_yday, &ptm->tm_wday);
andrewboyson 26:0421132e6eaf 283 }
andrewboyson 44:aa45226d118e 284 void TmFromInteger(int year, int month, int mday, int hour, int min, int sec, struct tm* ptm)
andrewboyson 44:aa45226d118e 285 {
andrewboyson 44:aa45226d118e 286 ptm->tm_year = year - 1900;
andrewboyson 44:aa45226d118e 287 ptm->tm_mon = month - 1;
andrewboyson 44:aa45226d118e 288 ptm->tm_mday = mday;
andrewboyson 44:aa45226d118e 289 calculateDayOfYearAndWeek(ptm->tm_year, ptm->tm_mon, ptm->tm_mday, &ptm->tm_yday, &ptm->tm_wday);
andrewboyson 44:aa45226d118e 290 ptm->tm_hour = hour;
andrewboyson 44:aa45226d118e 291 ptm->tm_min = min;
andrewboyson 44:aa45226d118e 292 ptm->tm_sec = sec;
andrewboyson 44:aa45226d118e 293 }
andrewboyson 37:330b844f54b6 294 void TmIncrement(struct tm* ptm)
andrewboyson 37:330b844f54b6 295 {
andrewboyson 37:330b844f54b6 296 ptm->tm_sec++;
andrewboyson 37:330b844f54b6 297 if (ptm->tm_sec > 59)
andrewboyson 37:330b844f54b6 298 {
andrewboyson 37:330b844f54b6 299 ptm->tm_sec = 0;
andrewboyson 37:330b844f54b6 300 ptm->tm_min++;
andrewboyson 37:330b844f54b6 301 }
andrewboyson 37:330b844f54b6 302 if (ptm->tm_min > 59)
andrewboyson 37:330b844f54b6 303 {
andrewboyson 37:330b844f54b6 304 ptm->tm_min = 0;
andrewboyson 37:330b844f54b6 305 ptm->tm_hour++;
andrewboyson 37:330b844f54b6 306 }
andrewboyson 37:330b844f54b6 307 if (ptm->tm_hour > 23)
andrewboyson 37:330b844f54b6 308 {
andrewboyson 37:330b844f54b6 309 ptm->tm_hour = 0;
andrewboyson 37:330b844f54b6 310 ptm->tm_wday++;
andrewboyson 37:330b844f54b6 311 if (ptm->tm_wday > 6) ptm->tm_wday = 0;
andrewboyson 37:330b844f54b6 312 ptm->tm_yday++;
andrewboyson 37:330b844f54b6 313 ptm->tm_mday++;
andrewboyson 37:330b844f54b6 314 if (ptm->tm_mday > monthLength(ptm->tm_year, ptm->tm_mon))
andrewboyson 37:330b844f54b6 315 {
andrewboyson 37:330b844f54b6 316 ptm->tm_mon++;
andrewboyson 37:330b844f54b6 317 if (ptm->tm_mon > 11)
andrewboyson 37:330b844f54b6 318 {
andrewboyson 37:330b844f54b6 319 ptm->tm_year++;
andrewboyson 37:330b844f54b6 320 ptm->tm_yday = 0;
andrewboyson 37:330b844f54b6 321 ptm->tm_mon = 0;
andrewboyson 37:330b844f54b6 322 }
andrewboyson 37:330b844f54b6 323 ptm->tm_mday = 1;
andrewboyson 37:330b844f54b6 324 }
andrewboyson 37:330b844f54b6 325 }
andrewboyson 37:330b844f54b6 326 }