-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimecalc.cpp
68 lines (57 loc) · 1.55 KB
/
timecalc.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
#include "timecalc.h"
/**
* taken from https://github.com/PaulStoffregen/Time
*/
#include <cstdio>
#include "Arduino.h"
void breakTime(unsigned long timeInput, tmElements_t &tm){
// break the given time_t into time components
// this is a more compact version of the C library localtime function
uint8_t year;
uint8_t month, monthLength;
uint32_t time;
unsigned long days;
time = (uint32_t)timeInput;
tm.Second = time % 60;
time /= 60; // now it is minutes
tm.Minute = time % 60;
time /= 60; // now it is hours
tm.Hour = time % 24;
time /= 24; // now it is days
tm.Wday = ((time + 4) % 7) + 1; // Sunday is day 1
year = 0;
days = 0;
while((unsigned)(days += (LEAP_YEAR(year) ? 366 : 365)) <= time) {
year++;
}
tm.Year = year; // year is offset from 1970
days -= LEAP_YEAR(year) ? 366 : 365;
time -= days; // now it is days in this year, starting at 0
days=0;
month=0;
monthLength=0;
for (month=0; month<12; month++) {
if (month==1) { // february
if (LEAP_YEAR(year)) {
monthLength=29;
} else {
monthLength=28;
}
} else {
monthLength = monthDays[month];
}
if (time >= monthLength) {
time -= monthLength;
} else {
break;
}
}
tm.Month = month + 1; // jan is month 1
tm.Day = time + 1; // day of month
}
int formattedDate(char* buf, unsigned long ts)
{
tmElements_t tm;
breakTime(ts, tm);
return std::sprintf(buf, "%02u.%02u.%04u %02u:%02u:%02u", tm.Day, tm.Month, 1970+tm.Year, tm.Hour, tm.Minute, tm.Second);
}