Skip to content

Commit c447c85

Browse files
committed
init
1 parent e1aa6dc commit c447c85

8 files changed

+2033
-1
lines changed

DateTime.cpp

+206
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,206 @@
1+
//#include <avr/pgmspace.h>
2+
#include "DateTime.h"
3+
4+
#define SECONDS_PER_DAY 86400L
5+
#define SECONDS_FROM_1900_TO_2000 3155673600
6+
#define SECONDS_FROM_1970_TO_2000 946684800
7+
8+
//has to be const or compiler compaints
9+
const uint16_t daysInMonth [] PROGMEM = {31,28,31,30,31,30,31,31,30,31,30,31};
10+
11+
// number of days since 2000/01/01, valid for 2001..2099
12+
static uint16_t date2days(uint16_t y, uint16_t m, uint16_t d) {
13+
if (y >= 2000) {
14+
y -= 2000;
15+
}
16+
uint16_t days = d;
17+
for (uint16_t i = 1; i < m; ++i) {
18+
days += pgm_read_byte(daysInMonth + i - 1);
19+
}
20+
if (m > 2 && y % 4 == 0) {
21+
++days;
22+
}
23+
return days + 365 * y + (y + 3) / 4 - 1;
24+
}
25+
26+
static long time2long(uint16_t days, uint16_t h, uint16_t m, uint16_t s) {
27+
return ((days * 24L + h) * 60 + m) * 60 + s;
28+
}
29+
30+
////////////////////////////////////////////////////////////////////////////////
31+
// DateTime implementation - ignores time zones and DST changes
32+
// NOTE: also ignores leap seconds, see http://en.wikipedia.org/wiki/Leap_second
33+
34+
DateTime::DateTime(uint32_t t, unsigned long microsfraction)
35+
: microsfraction_(microsfraction) {
36+
// bring to 2000 timestamp from 1900
37+
t -= SECONDS_FROM_1900_TO_2000;
38+
39+
second_ = t % 60;
40+
t /= 60;
41+
minute_ = t % 60;
42+
t /= 60;
43+
hour_ = t % 24;
44+
uint16_t days = t / 24;
45+
uint16_t leap;
46+
for (year_ = 0; ; ++year_) {
47+
leap = year_ % 4 == 0;
48+
if (days < 365 + leap) {
49+
break;
50+
}
51+
days -= 365 + leap;
52+
}
53+
for (month_ = 1; ; ++month_) {
54+
uint16_t daysPerMonth = pgm_read_byte(daysInMonth + month_ - 1);
55+
if (leap && month_ == 2) {
56+
++daysPerMonth;
57+
}
58+
if (days < daysPerMonth) {
59+
break;
60+
}
61+
days -= daysPerMonth;
62+
}
63+
day_ = days + 1;
64+
}
65+
66+
void DateTime::time(uint32_t t) {
67+
// bring to 2000 timestamp from 1900
68+
t -= SECONDS_FROM_1900_TO_2000;
69+
70+
second_ = t % 60;
71+
t /= 60;
72+
minute_ = t % 60;
73+
t /= 60;
74+
hour_ = t % 24;
75+
uint16_t days = t / 24;
76+
uint16_t leap;
77+
for (year_ = 0; ; ++year_) {
78+
leap = year_ % 4 == 0;
79+
if (days < 365 + leap) {
80+
break;
81+
}
82+
days -= 365 + leap;
83+
}
84+
for (month_ = 1; ; ++month_) {
85+
uint16_t daysPerMonth = pgm_read_byte(daysInMonth + month_ - 1);
86+
if (leap && month_ == 2) {
87+
++daysPerMonth;
88+
}
89+
if (days < daysPerMonth) {
90+
break;
91+
}
92+
days -= daysPerMonth;
93+
}
94+
day_ = days + 1;
95+
}
96+
97+
DateTime::DateTime(
98+
uint16_t year, uint16_t month,
99+
uint16_t day, uint16_t hour,
100+
uint16_t minute, uint16_t second,
101+
unsigned long microsfraction)
102+
: year_( (year >= 2000) ? year - 2000 : year),
103+
month_(month),
104+
day_(day),
105+
hour_(hour),
106+
minute_(minute),
107+
second_(second),
108+
microsfraction_(microsfraction) {}
109+
110+
static uint16_t conv2d(const char *p) {
111+
uint16_t v = 0;
112+
if ('0' <= *p && *p <= '9') {
113+
v = *p - '0';
114+
}
115+
return 10 * v + *++p - '0';
116+
}
117+
118+
// A convenient constructor for using "the compiler's time":
119+
// DateTime now (__DATE__, __TIME__);
120+
// NOTE: using PSTR would further reduce the RAM footprint
121+
DateTime::DateTime(
122+
const char *date,
123+
const char *time,
124+
unsigned long microsfraction)
125+
: microsfraction_(microsfraction) {
126+
// sample input: date = "Dec 26 2009", time = "12:34:56"
127+
year_ = conv2d(date + 9);
128+
// Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
129+
switch (date[0]) {
130+
case 'J':
131+
month_ = date[1] == 'a' ? 1 : (month_ = date[2] == 'n' ? 6 : 7);
132+
break;
133+
case 'F':
134+
month_ = 2;
135+
break;
136+
case 'A':
137+
month_ = date[2] == 'r' ? 4 : 8;
138+
break;
139+
case 'M':
140+
month_ = date[2] == 'r' ? 3 : 5;
141+
break;
142+
case 'S':
143+
month_ = 9;
144+
break;
145+
case 'O':
146+
month_ = 10;
147+
break;
148+
case 'N':
149+
month_ = 11;
150+
break;
151+
case 'D':
152+
month_ = 12;
153+
break;
154+
default:
155+
break;
156+
}
157+
day_ = conv2d(date + 4);
158+
hour_ = conv2d(time);
159+
minute_ = conv2d(time + 3);
160+
second_ = conv2d(time + 6);
161+
162+
163+
/*uint16_t DateTime::dayOfWeek() const {
164+
uint16_t day = date2days(year_, month_, day_);
165+
166+
// Jan 1, 2000 is a Saturday, i.e. returns 6
167+
return (day + 6) % 7;
168+
169+
*/
170+
}
171+
172+
uint32_t DateTime::ntptime(void) const
173+
{
174+
uint32_t t;
175+
uint16_t days = date2days(year_, month_, day_);
176+
t = time2long(days, hour_, minute_, second_);
177+
t += SECONDS_FROM_1900_TO_2000;
178+
179+
return t;
180+
}
181+
182+
uint32_t DateTime::unixtime(void) const
183+
{
184+
uint32_t t;
185+
uint16_t days = date2days(year_, month_, day_);
186+
t = time2long(days, hour_, minute_, second_);
187+
t += SECONDS_FROM_1970_TO_2000;
188+
189+
return t;
190+
}
191+
192+
String DateTime::toStringTime(void) const
193+
{
194+
char chartime[9];
195+
sprintf(chartime, "%02d:%02d:%02d", hour(), minute(), second() );
196+
return chartime;
197+
}
198+
199+
200+
String DateTime::toStringDate(void) const
201+
{
202+
char charDate[11];
203+
sprintf(charDate, "%02d/%02d/%04d", day(), month(), year() );
204+
return charDate;
205+
}
206+

DateTime.h

+114
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
#ifndef DATETIME_H_
2+
#define DATETIME_H_
3+
4+
#include "Arduino.h"
5+
6+
class DateTime {
7+
public:
8+
DateTime(uint32_t t = 0, unsigned long microsfraction = 0);
9+
DateTime(uint16_t year, uint16_t month, uint16_t day,
10+
uint16_t hour = 0, uint16_t minute = 0, uint16_t second = 0,
11+
unsigned long microsfraction = 0);
12+
DateTime(const char *date, const char *time, unsigned long microsfraction = 0);
13+
14+
void time(uint32_t t);
15+
void microsfraction(unsigned long microsfraction) {
16+
microsfraction_ = microsfraction;
17+
};
18+
19+
uint16_t year() const { return 2000 + year_; }
20+
uint16_t month() const { return month_; }
21+
uint16_t day() const { return day_; }
22+
uint16_t hour() const { return hour_; }
23+
uint16_t minute() const { return minute_; }
24+
uint16_t second() const { return second_; }
25+
unsigned long microsfraction() const { return microsfraction_; }
26+
//uint16_t dayOfWeek(); //const;// { return dayOfWeek_;}
27+
28+
// 32-bit times as seconds since 1/1/2000
29+
long secondstime() const;
30+
// 32-bit times as seconds since 1/1/1900
31+
uint32_t ntptime(void) const;
32+
// 32-bit times as seconds since 1/1/1970
33+
uint32_t unixtime(void) const;
34+
35+
String toStringTime(void) const;
36+
String toStringDate(void) const;
37+
38+
void print(void) {
39+
40+
long diff2 = microsfraction();
41+
Serial.print(F("UNIX: "));
42+
Serial.print(unixtime());
43+
Serial.print(F(","));
44+
if (diff2 < 10)
45+
Serial.print("000000");
46+
else if (diff2 < 100)
47+
Serial.print("00000");
48+
else if (diff2 < 1000)
49+
Serial.print("0000");
50+
else if (diff2 < 10000)
51+
Serial.print("000");
52+
else if (diff2 < 100000)
53+
Serial.print("00");
54+
else if (diff2 < 1000000)
55+
Serial.print("0");
56+
57+
Serial.println(diff2);
58+
59+
Serial.print(F("NTP: "));
60+
Serial.print(ntptime());
61+
Serial.print(F(","));
62+
if (diff2 < 10)
63+
Serial.print("000000");
64+
else if (diff2 < 100)
65+
Serial.print("00000");
66+
else if (diff2 < 1000)
67+
Serial.print("0000");
68+
else if (diff2 < 10000)
69+
Serial.print("000");
70+
else if (diff2 < 100000)
71+
Serial.print("00");
72+
else if (diff2 < 1000000)
73+
Serial.print("0");
74+
75+
Serial.println(diff2);
76+
77+
Serial.print(F("DATE: "));
78+
Serial.print(day());
79+
Serial.print(F("."));
80+
Serial.print(month());
81+
Serial.print(F("."));
82+
Serial.println(year());
83+
84+
Serial.print(F("TIME: "));
85+
Serial.print(hour());
86+
Serial.print(F(":"));
87+
Serial.print(minute());
88+
Serial.print(F(":"));
89+
Serial.print(second());
90+
Serial.print(F(","));
91+
if (diff2 < 10)
92+
Serial.print("000000");
93+
else if (diff2 < 100)
94+
Serial.print("00000");
95+
else if (diff2 < 1000)
96+
Serial.print("0000");
97+
else if (diff2 < 10000)
98+
Serial.print("000");
99+
else if (diff2 < 100000)
100+
Serial.print("00");
101+
else if (diff2 < 1000000)
102+
Serial.print("0");
103+
104+
Serial.println(diff2);
105+
106+
Serial.println();
107+
};
108+
109+
protected:
110+
uint16_t year_, month_, day_, hour_, minute_, second_;
111+
unsigned long microsfraction_;
112+
};
113+
114+
#endif // DATETIME_H_

0 commit comments

Comments
 (0)