Skip to content

Commit e5072e2

Browse files
committed
Add API's to convert epoch time to date/time component
1 parent 304c733 commit e5072e2

File tree

2 files changed

+67
-15
lines changed

2 files changed

+67
-15
lines changed

libraries/CurieTime/src/CurieTime.cpp

+52-9
Original file line numberDiff line numberDiff line change
@@ -32,50 +32,93 @@ unsigned long now()
3232
return *RTC_CCVR;
3333
}
3434

35-
struct tm* nowTm() {
36-
time_t t = now();
35+
struct tm* tTm(unsigned long t)
36+
{
37+
time_t time = t;
3738

38-
return gmtime(&t);
39+
return gmtime(&time);
3940
}
4041

4142
int year()
4243
{
43-
struct tm* tm = nowTm();
44+
unsigned long t = now();
45+
46+
return year(t);
47+
}
48+
49+
int year(unsigned long t)
50+
{
51+
struct tm* tm = tTm(t);
4452

4553
return (tm->tm_year + YEAR_OFFSET);
4654
}
4755

4856
int month()
4957
{
50-
struct tm* tm = nowTm();
58+
unsigned long t = now();
59+
60+
return month(t);
61+
}
62+
63+
int month(unsigned long t)
64+
{
65+
struct tm* tm = tTm(t);
5166

5267
return (tm->tm_mon + MONTH_OFFSET);
5368
}
5469

5570
int day()
5671
{
57-
struct tm* tm = nowTm();
72+
unsigned long t = now();
73+
74+
return day(t);
75+
}
76+
77+
int day(unsigned long t)
78+
{
79+
struct tm* tm = tTm(t);
5880

5981
return tm->tm_mday;
6082
}
6183

6284
int hour()
6385
{
64-
struct tm* tm = nowTm();
86+
unsigned long t = now();
87+
88+
return hour(t);
89+
}
90+
91+
int hour(unsigned long t)
92+
{
93+
struct tm* tm = tTm(t);
6594

6695
return tm->tm_hour;
6796
}
6897

6998
int minute()
7099
{
71-
struct tm* tm = nowTm();
100+
unsigned long t = now();
101+
102+
return minute(t);
103+
}
104+
105+
int minute(unsigned long t)
106+
{
107+
struct tm* tm = tTm(t);
72108

73109
return tm->tm_min;
74110
}
75111

76112
int second()
77113
{
78-
struct tm* tm = nowTm();
114+
unsigned long t = now();
115+
116+
return second(t);
117+
}
118+
119+
int second(unsigned long t)
120+
{
121+
struct tm* tm = tTm(t);
79122

80123
return tm->tm_sec;
81124
}

libraries/CurieTime/src/CurieTime.h

+15-6
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,23 @@
1313
#define RTC_RSTAT 0xb0000414 // Interrupt Raw Status Register
1414
#define RTC_EOI 0xb0000418 // End of Interrupt Register
1515

16+
// The following API is based on Paul Stoffregen's Arduino Time Library:
17+
// https://github.com/PaulStoffregen/Time
18+
1619
unsigned long now(); // current time as seconds since Jan 1 1970
1720

18-
int year(); // current year as an integer
19-
int month(); // current month as an integer (1 - 12)
20-
int day(); // current day as an integer (1 - 31)
21-
int hour(); // current hour as an integer (0 - 23)
22-
int minute(); // current minute as an integer (0 - 59)
23-
int second(); // current second as an integer (0 - 59)
21+
int year(); // current year as an integer
22+
int year(unsigned long t); // year of t as an integer
23+
int month(); // current month as an integer (1 - 12)
24+
int month(unsigned long t); // month of t as an integer (1 - 12)
25+
int day(); // current day as an integer (1 - 31)
26+
int day(unsigned long t); // day of t as an integer (1 - 31)
27+
int hour(); // current hour as an integer (0 - 23)
28+
int hour(unsigned long t); // hour of t as an integer (0 - 23)
29+
int minute(); // current minute as an integer (0 - 59)
30+
int minute(unsigned long t); // minute of t as an integer (0 - 59)
31+
int second(); // current second as an integer (0 - 59)
32+
int second(unsigned long t); // second of t as an integer (0 - 59)
2433

2534
void setTime(int hour, int minute, int second, int day, int month, int year); // set the current time
2635
void setTime(unsigned long t); // set the current time from seconds since Jan 1 1970

0 commit comments

Comments
 (0)