Skip to content

Commit 66dd34a

Browse files
committed
use mktime to compute unix timestamp
1 parent 2a37d6c commit 66dd34a

File tree

1 file changed

+22
-6
lines changed

1 file changed

+22
-6
lines changed

src/TimeUtils.h

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -91,19 +91,35 @@ class Time {
9191
* @return The time in UNIX timestamp format.
9292
*/
9393
String getUNIXTimestampString() {
94-
// Simple conversion to UNIX timestamp, not accounting for leap years or time zones
95-
long timestamp = second + minute*60 + hour*3600 + day*86400 + (month-1)*2629743 + (year-1970)*31556926;
96-
return String(timestamp);
94+
return String(getUNIXTimestamp());
9795
}
9896

9997
/**
10098
* Returns the time in UNIX timestamp format.
10199
* @return The time in UNIX timestamp format.
102100
*/
103101
unsigned long getUNIXTimestamp() {
104-
// Simple conversion to UNIX timestamp, not accounting for leap years or time zones
105-
unsigned long timestamp = second + minute*60 + hour*3600 + day*86400 + (month-1)*2629743 + (year-1970)*31556926;
106-
return timestamp;
102+
struct tm t =
103+
{
104+
0 /* tm_sec */,
105+
0 /* tm_min */,
106+
0 /* tm_hour */,
107+
0 /* tm_mday */,
108+
0 /* tm_mon */,
109+
0 /* tm_year */,
110+
0 /* tm_wday */,
111+
0 /* tm_yday */,
112+
0 /* tm_isdst */
113+
};
114+
t.tm_mon = month - 1;
115+
t.tm_mday = day;
116+
t.tm_year = year - 1900;
117+
t.tm_hour = hour;
118+
t.tm_min = minute;
119+
t.tm_sec = second;
120+
t.tm_isdst = -1;
121+
122+
return mktime(&t);
107123
}
108124

109125
/**

0 commit comments

Comments
 (0)