/
SetTimeHelper.cpp
93 lines (74 loc) · 2.03 KB
/
SetTimeHelper.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/*
* SetTimeHelper.cpp
*
* Created on: Nov 28, 2014
* Author: Konstantin Gredeskoul
* Code: https://github.com/kigster
*
* (c) 2014 All rights reserved, MIT License.
*/
#include "SetTimeHelper.h"
#include "Wallock.h"
#if ENABLE_SET_TIME
#include <Time.h>
#include <Arduino.h>
static const char *monthNamesList[12] = {
"Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
};
SetTimeHelper::SetTimeHelper() {
for (int i = 0; i < 12; i ++) { monthNames[i] = (char *)monthNamesList[i]; }
breakTime(now(), currentTime);
retrieveCompileTime();
}
bool SetTimeHelper::changeTimeToCompileTimeIfNeeded() {
time_t tt_now = makeTime(currentTime);
time_t tt_compiled = makeTime(compiledAt);
if (tt_now < tt_compiled) {
setTimeToCompileTime();
return true;
}
return false;
}
bool SetTimeHelper::setTimeToCompileTime() {
// get the date and time the compiler was run
if (retrieveCompileTime()) {
return setTimeTo(compiledAt, millis() + COMPILE_TIME_AND_BOOTLOADER_DELAY);
}
return false;
}
bool SetTimeHelper::setTimeTo(tmElements_t tm, time_t offset) {
time_t timeToSet = makeTime(tm);
setTime(timeToSet);
#if TEENSYDUINO
Teensy3Clock.set(timeToSet);
return true;
#else
return (bool) RTC.write(tm);
#endif
}
// private
bool SetTimeHelper::retrieveCompileTime() {
int Hour, Min, Sec;
if (sscanf(__TIME__, "%d:%d:%d", &Hour, &Min, &Sec) != 3)
return false;
compiledAt.Hour = Hour;
compiledAt.Minute = Min;
compiledAt.Second = Sec;
char Month[12];
int Day, Year;
uint8_t monthIndex;
if (sscanf(__DATE__, "%s %d %d", Month, &Day, &Year) != 3)
return false;
for (monthIndex = 0; monthIndex < 12; monthIndex++) {
if (strcmp(Month, monthNames[monthIndex]) == 0)
break;
}
if (monthIndex >= 12)
return false;
compiledAt.Day = Day;
compiledAt.Month = monthIndex + 1;
compiledAt.Year = CalendarYrToTm(Year);
return true;
}
#endif