Andrew Boyson
/
iot
Backing up an unused program in case of future need
time.cpp@7:024ace6d943c, 2016-06-02 (annotated)
- Committer:
- andrewboyson
- Date:
- Thu Jun 02 09:26:28 2016 +0000
- Revision:
- 7:024ace6d943c
- Parent:
- 6:be97d38e0b01
- Child:
- 8:45a0205a298f
Added a routine to break time_t into a struct tm without using gmtime as that did not seem to work.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
andrewboyson | 0:09f915e6f9f6 | 1 | #include "mbed.h" |
andrewboyson | 0:09f915e6f9f6 | 2 | #define ONE_MILLION 1000000 |
andrewboyson | 0:09f915e6f9f6 | 3 | |
andrewboyson | 6:be97d38e0b01 | 4 | static Ticker ticker; |
andrewboyson | 6:be97d38e0b01 | 5 | static volatile uint64_t unixTime16ths; |
andrewboyson | 0:09f915e6f9f6 | 6 | static void tick(void) |
andrewboyson | 0:09f915e6f9f6 | 7 | { |
andrewboyson | 0:09f915e6f9f6 | 8 | unixTime16ths++; |
andrewboyson | 0:09f915e6f9f6 | 9 | } |
andrewboyson | 0:09f915e6f9f6 | 10 | void TimeSet(uint32_t t) |
andrewboyson | 0:09f915e6f9f6 | 11 | { |
andrewboyson | 0:09f915e6f9f6 | 12 | unixTime16ths = (uint64_t)t << 4; |
andrewboyson | 0:09f915e6f9f6 | 13 | } |
andrewboyson | 0:09f915e6f9f6 | 14 | uint32_t TimeGet() |
andrewboyson | 0:09f915e6f9f6 | 15 | { |
andrewboyson | 0:09f915e6f9f6 | 16 | return unixTime16ths >> 4; |
andrewboyson | 0:09f915e6f9f6 | 17 | } |
andrewboyson | 0:09f915e6f9f6 | 18 | void TimeSet16ths(uint64_t t) |
andrewboyson | 0:09f915e6f9f6 | 19 | { |
andrewboyson | 0:09f915e6f9f6 | 20 | unixTime16ths = t; |
andrewboyson | 0:09f915e6f9f6 | 21 | } |
andrewboyson | 0:09f915e6f9f6 | 22 | uint64_t TimeGet16ths() |
andrewboyson | 0:09f915e6f9f6 | 23 | { |
andrewboyson | 0:09f915e6f9f6 | 24 | return unixTime16ths; |
andrewboyson | 6:be97d38e0b01 | 25 | } |
andrewboyson | 7:024ace6d943c | 26 | void TimeToTm(uint32_t time, struct tm* ptm) |
andrewboyson | 7:024ace6d943c | 27 | { |
andrewboyson | 7:024ace6d943c | 28 | int seconds = time % 60; time /= 60; |
andrewboyson | 7:024ace6d943c | 29 | int minutes = time % 60; time /= 60; |
andrewboyson | 7:024ace6d943c | 30 | int hours = time % 24; time /= 24; |
andrewboyson | 7:024ace6d943c | 31 | int daysLeft = time; |
andrewboyson | 7:024ace6d943c | 32 | |
andrewboyson | 7:024ace6d943c | 33 | //Add a year at a time while there is more than a year of days left |
andrewboyson | 7:024ace6d943c | 34 | int year = 1970; //Unix epoch is 1970 |
andrewboyson | 7:024ace6d943c | 35 | int dayOfWeek = 4; //1 Jan 1970 is a Thursday |
andrewboyson | 7:024ace6d943c | 36 | int dayOfYear; |
andrewboyson | 7:024ace6d943c | 37 | int leapYear; |
andrewboyson | 7:024ace6d943c | 38 | while(1) |
andrewboyson | 7:024ace6d943c | 39 | { |
andrewboyson | 7:024ace6d943c | 40 | //See if it is a leap year |
andrewboyson | 7:024ace6d943c | 41 | int year4 = !(year % 4); |
andrewboyson | 7:024ace6d943c | 42 | int year100 = !(year % 100); |
andrewboyson | 7:024ace6d943c | 43 | int year400 = !(year % 400); |
andrewboyson | 7:024ace6d943c | 44 | leapYear = false; |
andrewboyson | 7:024ace6d943c | 45 | if (year4) leapYear = true; |
andrewboyson | 7:024ace6d943c | 46 | if (year100) leapYear = false; |
andrewboyson | 7:024ace6d943c | 47 | if (year400) leapYear = true; |
andrewboyson | 7:024ace6d943c | 48 | |
andrewboyson | 7:024ace6d943c | 49 | //Find the number of days in this year |
andrewboyson | 7:024ace6d943c | 50 | int daysInYear = leapYear ? 366 : 365; |
andrewboyson | 7:024ace6d943c | 51 | |
andrewboyson | 7:024ace6d943c | 52 | //Stop if this is the final year |
andrewboyson | 7:024ace6d943c | 53 | if (daysLeft < daysInYear) break; |
andrewboyson | 7:024ace6d943c | 54 | |
andrewboyson | 7:024ace6d943c | 55 | //Calculate the current day of the week at the start of the year |
andrewboyson | 7:024ace6d943c | 56 | dayOfWeek += leapYear ? 2 : 1; |
andrewboyson | 7:024ace6d943c | 57 | if (dayOfWeek >= 7) dayOfWeek -= 7; |
andrewboyson | 7:024ace6d943c | 58 | |
andrewboyson | 7:024ace6d943c | 59 | //Move on to the next year |
andrewboyson | 7:024ace6d943c | 60 | daysLeft -= daysInYear; |
andrewboyson | 7:024ace6d943c | 61 | year++; |
andrewboyson | 7:024ace6d943c | 62 | } |
andrewboyson | 7:024ace6d943c | 63 | |
andrewboyson | 7:024ace6d943c | 64 | dayOfYear = daysLeft; |
andrewboyson | 7:024ace6d943c | 65 | dayOfWeek += daysLeft; |
andrewboyson | 7:024ace6d943c | 66 | dayOfWeek %= 7; |
andrewboyson | 7:024ace6d943c | 67 | |
andrewboyson | 7:024ace6d943c | 68 | //Add a month at a time while there is more than a month of days left |
andrewboyson | 7:024ace6d943c | 69 | int month = 0; |
andrewboyson | 7:024ace6d943c | 70 | while(1) |
andrewboyson | 7:024ace6d943c | 71 | { |
andrewboyson | 7:024ace6d943c | 72 | int daysInMonth; |
andrewboyson | 7:024ace6d943c | 73 | switch (month + 1) |
andrewboyson | 7:024ace6d943c | 74 | { |
andrewboyson | 7:024ace6d943c | 75 | case 1: daysInMonth = 31; break; |
andrewboyson | 7:024ace6d943c | 76 | case 2: daysInMonth = leapYear ? 29 : 28; break; |
andrewboyson | 7:024ace6d943c | 77 | case 3: daysInMonth = 31; break; |
andrewboyson | 7:024ace6d943c | 78 | case 4: daysInMonth = 30; break; |
andrewboyson | 7:024ace6d943c | 79 | case 5: daysInMonth = 31; break; |
andrewboyson | 7:024ace6d943c | 80 | case 6: daysInMonth = 30; break; |
andrewboyson | 7:024ace6d943c | 81 | case 7: daysInMonth = 31; break; |
andrewboyson | 7:024ace6d943c | 82 | case 8: daysInMonth = 31; break; |
andrewboyson | 7:024ace6d943c | 83 | case 9: daysInMonth = 30; break; |
andrewboyson | 7:024ace6d943c | 84 | case 10: daysInMonth = 31; break; |
andrewboyson | 7:024ace6d943c | 85 | case 11: daysInMonth = 30; break; |
andrewboyson | 7:024ace6d943c | 86 | case 12: daysInMonth = 31; break; |
andrewboyson | 7:024ace6d943c | 87 | } |
andrewboyson | 7:024ace6d943c | 88 | |
andrewboyson | 7:024ace6d943c | 89 | //Stop if this is the last month |
andrewboyson | 7:024ace6d943c | 90 | if (daysLeft < daysInMonth) break; |
andrewboyson | 7:024ace6d943c | 91 | |
andrewboyson | 7:024ace6d943c | 92 | //Move onto next month |
andrewboyson | 7:024ace6d943c | 93 | daysLeft -= daysInMonth; |
andrewboyson | 7:024ace6d943c | 94 | month++; |
andrewboyson | 7:024ace6d943c | 95 | } |
andrewboyson | 7:024ace6d943c | 96 | |
andrewboyson | 7:024ace6d943c | 97 | ptm->tm_sec = seconds; |
andrewboyson | 7:024ace6d943c | 98 | ptm->tm_min = minutes; |
andrewboyson | 7:024ace6d943c | 99 | ptm->tm_hour = hours; |
andrewboyson | 7:024ace6d943c | 100 | ptm->tm_mday = daysLeft + 1; |
andrewboyson | 7:024ace6d943c | 101 | ptm->tm_mon = month; |
andrewboyson | 7:024ace6d943c | 102 | ptm->tm_year = year - 1900; |
andrewboyson | 7:024ace6d943c | 103 | ptm->tm_wday = dayOfWeek; |
andrewboyson | 7:024ace6d943c | 104 | ptm->tm_yday = dayOfYear; |
andrewboyson | 7:024ace6d943c | 105 | } |
andrewboyson | 7:024ace6d943c | 106 | void TimeGetTm(struct tm* ptm) |
andrewboyson | 7:024ace6d943c | 107 | { |
andrewboyson | 7:024ace6d943c | 108 | uint32_t time = TimeGet(); |
andrewboyson | 7:024ace6d943c | 109 | TimeToTm(time, ptm); |
andrewboyson | 7:024ace6d943c | 110 | } |
andrewboyson | 6:be97d38e0b01 | 111 | static Timer timer; |
andrewboyson | 6:be97d38e0b01 | 112 | int TimeScanUs = 0; |
andrewboyson | 6:be97d38e0b01 | 113 | int TimeMain() |
andrewboyson | 6:be97d38e0b01 | 114 | { |
andrewboyson | 6:be97d38e0b01 | 115 | int scanUs = timer.read_us(); |
andrewboyson | 6:be97d38e0b01 | 116 | timer.reset(); |
andrewboyson | 6:be97d38e0b01 | 117 | timer.start(); |
andrewboyson | 6:be97d38e0b01 | 118 | if (scanUs > TimeScanUs) TimeScanUs++; |
andrewboyson | 6:be97d38e0b01 | 119 | if (scanUs < TimeScanUs) TimeScanUs--; |
andrewboyson | 6:be97d38e0b01 | 120 | return 0; |
andrewboyson | 6:be97d38e0b01 | 121 | } |
andrewboyson | 6:be97d38e0b01 | 122 | int TimeInit() |
andrewboyson | 6:be97d38e0b01 | 123 | { |
andrewboyson | 6:be97d38e0b01 | 124 | ticker.attach_us(&tick, ONE_MILLION >> 4); |
andrewboyson | 6:be97d38e0b01 | 125 | timer.stop(); |
andrewboyson | 6:be97d38e0b01 | 126 | timer.reset(); |
andrewboyson | 6:be97d38e0b01 | 127 | return 0; |
andrewboyson | 6:be97d38e0b01 | 128 | } |