-
Notifications
You must be signed in to change notification settings - Fork 0
/
timeStuff.ino
204 lines (173 loc) · 7.12 KB
/
timeStuff.ino
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/*
***************************************************************************
** Program : timeStuff, part of DSMRloggerAPI
** Version : v2.0.1
**
** Copyright (c) 2020 Willem Aandewiel
**
** TERMS OF USE: MIT License. See bottom of file.
***************************************************************************
*/
//static time_t ntpTimeSav;
//===========================================================================================
String buildDateTimeString(const char* timeStamp, int len)
{
String tmpTS = String(timeStamp);
String DateTime = "";
if (len < 12) return String(timeStamp);
DateTime = "20" + tmpTS.substring(0, 2); // YY
DateTime += "-" + tmpTS.substring(2, 4); // MM
DateTime += "-" + tmpTS.substring(4, 6); // DD
DateTime += " " + tmpTS.substring(6, 8); // HH
DateTime += ":" + tmpTS.substring(8, 10); // MM
DateTime += ":" + tmpTS.substring(10, 12); // SS
return DateTime;
} // buildDateTimeString()
//===========================================================================================
void epochToTimestamp(time_t t, char *ts, int8_t len)
{
if (len < 12) {
strcpy(ts, "Error");
return;
}
//------------yy mm dd hh mm ss
sprintf(ts, "%02d%02d%02d%02d%02d%02d", year(t)-2000, month(t), day(t)
, hour(t), minute(t), second(t));
//DebugTf("epochToTimestamp() => [%s]\r\n", ts);
} // epochToTimestamp()
//===========================================================================================
int8_t SecondFromTimestamp(const char *timeStamp)
{
char aSS[4] = "";
// 0123456789ab
// YYMMDDHHmmss SS = 4-5
strCopy(aSS, 4, timeStamp, 10, 11);
return String(aSS).toInt();
} // SecondFromTimestamp()
//===========================================================================================
int8_t MinuteFromTimestamp(const char *timeStamp)
{
char aMM[4] = "";
// 0123456789ab
// YYMMDDHHmmss MM = 8-9
strCopy(aMM, 4, timeStamp, 8, 9);
return String(aMM).toInt();
} // MinuteFromTimestamp()
//===========================================================================================
int8_t HourFromTimestamp(const char *timeStamp)
{
char aHH[4] = "";
//DebugTf("timeStamp[%s] => \r\n", timeStamp); // YYMMDDHHmmss HH = 5-6
strCopy(aHH, 4, timeStamp, 6, 7);
//Debugf("aHH[%s], nHH[%02d]\r\n", aHH, String(aHH).toInt());
return String(aHH).toInt();
} // HourFromTimestamp()
//===========================================================================================
int8_t DayFromTimestamp(const char *timeStamp)
{
char aDD[4] = "";
// 0123456789ab
// YYMMDDHHmmss DD = 4-5
strCopy(aDD, 4, timeStamp, 4, 5);
return String(aDD).toInt();
} // DayFromTimestamp()
//===========================================================================================
int8_t MonthFromTimestamp(const char *timeStamp)
{
char aMM[4] = "";
// 0123456789ab
// YYMMDDHHmmss MM = 2-3
strCopy(aMM, 4, timeStamp, 2, 3);
return String(aMM).toInt();
} // MonthFromTimestamp()
//===========================================================================================
int8_t YearFromTimestamp(const char *timeStamp)
{
char aYY[4] = "";
// 0123456789ab
// YYMMDDHHmmss YY = 0-1
strCopy(aYY, 4, timeStamp, 0, 1);
return String(aYY).toInt();
} // YearFromTimestamp()
//===========================================================================================
int32_t HoursKeyTimestamp(const char *timeStamp)
{
char aHK[10] = "";
// 0123456789ab
// YYMMDDHHmmss YY = 0-1
strCopy(aHK, 4, timeStamp, 0, 7);
//return timeStamp.substring(0, 8).toInt();
return String(aHK).toInt();
} // HourFromTimestamp()
//===========================================================================================
// calculate epoch from timeStamp
// if syncTime is true, set system time to calculated epoch-time
time_t epoch(const char *timeStamp, int8_t len, bool syncTime)
{
char fullTimeStamp[16] = "";
strConcat(fullTimeStamp, 15, timeStamp);
if (Verbose2) DebugTf("epoch(%s) strlen([%d])\r\n", fullTimeStamp, strlen(fullTimeStamp));
switch(strlen(fullTimeStamp)) {
case 4: //--- timeStamp is YYMM
strConcat(fullTimeStamp, 15, "01010101X");
break;
case 6: //--- timeStamp is YYMMDD
strConcat(fullTimeStamp, 15, "010101X");
break;
case 8: //--- timeStamp is YYMMDDHH
strConcat(fullTimeStamp, 15, "0101X");
break;
case 10: //--- timeStamp is YYMMDDHHMM
strConcat(fullTimeStamp, 15, "01X");
break;
case 12: //--- timeStamp is YYMMDDHHMMSS
strConcat(fullTimeStamp, 15, "X");
break;
//default: return now();
}
if (strlen(fullTimeStamp) < 13) return now();
if (Verbose2) DebugTf("DateTime: [%02d]-[%02d]-[%02d] [%02d]:[%02d]:[%02d]\r\n"
,DayFromTimestamp(timeStamp)
,MonthFromTimestamp(timeStamp)
,YearFromTimestamp(timeStamp)
,HourFromTimestamp(timeStamp)
,MinuteFromTimestamp(timeStamp)
,0
);
time_t nT;
time_t savEpoch = now();
setTime(HourFromTimestamp(fullTimeStamp)
,MinuteFromTimestamp(fullTimeStamp)
,SecondFromTimestamp(fullTimeStamp)
,DayFromTimestamp(fullTimeStamp)
,MonthFromTimestamp(fullTimeStamp)
,YearFromTimestamp(fullTimeStamp));
nT = now();
if (!syncTime)
{
setTime(savEpoch);
}
return nT;
} // epoch()
/***************************************************************************
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to permit
* persons to whom the Software is furnished to do so, subject to the
* following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
* OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
* THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
***************************************************************************/