Skip to content

Commit 0e58789

Browse files
committed
use mktime to compute unix timestamp
1 parent 2a37d6c commit 0e58789

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed

src/TimeUtils.h

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,27 @@ class Time {
102102
*/
103103
unsigned long getUNIXTimestamp() {
104104
// 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;
105+
struct tm t =
106+
{
107+
0 /* tm_sec */,
108+
0 /* tm_min */,
109+
0 /* tm_hour */,
110+
0 /* tm_mday */,
111+
0 /* tm_mon */,
112+
0 /* tm_year */,
113+
0 /* tm_wday */,
114+
0 /* tm_yday */,
115+
0 /* tm_isdst */
116+
};
117+
t.tm_mon = month-1;
118+
t.tm_mday = day;
119+
t.tm_year = year - 1900;
120+
t.tm_hour = hour;
121+
t.tm_min = minute;
122+
t.tm_sec = second;
123+
t.tm_isdst = -1;
124+
125+
return mktime(&t);
107126
}
108127

109128
/**

0 commit comments

Comments
 (0)