Skip to content

use mktime to compute unix timestamp #30

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 23 additions & 6 deletions src/TimeUtils.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <Arduino.h>
#include <time.h>

/**
* @class Time
Expand Down Expand Up @@ -91,19 +92,35 @@ class Time {
* @return The time in UNIX timestamp format.
*/
String getUNIXTimestampString() {
// Simple conversion to UNIX timestamp, not accounting for leap years or time zones
long timestamp = second + minute*60 + hour*3600 + day*86400 + (month-1)*2629743 + (year-1970)*31556926;
return String(timestamp);
return String(getUNIXTimestamp());
}

/**
* Returns the time in UNIX timestamp format.
* @return The time in UNIX timestamp format.
*/
unsigned long getUNIXTimestamp() {
// Simple conversion to UNIX timestamp, not accounting for leap years or time zones
unsigned long timestamp = second + minute*60 + hour*3600 + day*86400 + (month-1)*2629743 + (year-1970)*31556926;
return timestamp;
struct tm t =
{
0 /* tm_sec */,
0 /* tm_min */,
0 /* tm_hour */,
0 /* tm_mday */,
0 /* tm_mon */,
0 /* tm_year */,
0 /* tm_wday */,
0 /* tm_yday */,
0 /* tm_isdst */
};
t.tm_mon = month - 1;
t.tm_mday = day;
t.tm_year = year - 1900;
t.tm_hour = hour;
t.tm_min = minute;
t.tm_sec = second;
t.tm_isdst = -1;

return mktime(&t);
}

/**
Expand Down
Loading