Skip to content
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

Fix Timezone issues on InfluxDB #2283

Merged
merged 8 commits into from
Apr 2, 2023
Merged
Changes from 3 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
31 changes: 17 additions & 14 deletions code/components/jomjol_influxdb/interface_influxdb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,8 @@ void InfluxDB_V2_Publish(std::string _key, std::string _content, std::string _ti

LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "InfluxDB_V2_Publish - Key: " + _key + ", Content: " + _content + ", Timestamp: " + _timestamp);

// Format: #define PREVALUE_TIME_FORMAT_OUTPUT "%Y-%m-%dT%H:%M:%S%z"

char nowTimestamp[21];
std::string payload;
char nowTimestamp[21];

if (_timestamp.length() > 0)
{
Expand All @@ -60,17 +58,19 @@ void InfluxDB_V2_Publish(std::string _key, std::string _content, std::string _ti
struct tm * ptm;
ptm = gmtime ( &t );
time_t utc = mktime(ptm);
utc = 2*t - utc; // Take care of timezone (looks difficult, but is easy: t = t + (t - utc), weil t-utc = timezone)

sprintf(nowTimestamp,"%ld000000000", (long) utc); // UTC
LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "time conversion utc before: " + std::to_string(utc) + " t: " + std::to_string(t));

// utc = 2*t - utc; // Take care of timezone (looks difficult, but is easy: t = t + (t - utc), weil t-utc = timezone)
// LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "time conversion utc after: " + std::to_string(utc));

sprintf(nowTimestamp,"%ld000000000", (long) t); // UTC

payload = _influxDB_V2_Measurement + " " + _key + "=" + _content + " " + nowTimestamp;
// payload = _influxDB_V2_Measurement + " " + _key + "=774 " + nowTimestamp;
}
else
{
payload = _influxDB_V2_Measurement + " " + _key + "=" + _content;
// payload = _influxDB_V2_Measurement + " " + _key + "=774";
}

payload.shrink_to_fit();
Expand Down Expand Up @@ -157,10 +157,10 @@ void InfluxDBPublish(std::string _key, std::string _content, std::string _timest
http_config.auth_type = HTTP_AUTH_TYPE_BASIC;
}

LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "InfluxDBPublish - Key: " + _key + ", Content: " + _content + ", Timestamp: " + _timestamp);

char nowTimestamp[21];
std::string payload;
char nowTimestamp[21];

LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "InfluxDBPublish - Key: " + _key + ", Content: " + _content + ", Timestamp: " + _timestamp);

if (_timestamp.length() > 0)
{
Expand All @@ -171,16 +171,19 @@ void InfluxDBPublish(std::string _key, std::string _content, std::string _timest
struct tm * ptm;
ptm = gmtime ( &t );
time_t utc = mktime(ptm);
utc = 2*t - utc; // Take care of timezone (looks difficult, but is easy: t = t + (t - utc), weil t-utc = timezone)

sprintf(nowTimestamp,"%ld000000000", (long) utc); // UTC
LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "time conversion utc before: " + std::to_string(utc) + " t: " + std::to_string(t));

// utc = 2*t - utc; // Take care of timezone (looks difficult, but is easy: t = t + (t - utc), weil t-utc = timezone)
// LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "time conversion utc after: " + std::to_string(utc));

sprintf(nowTimestamp,"%ld000000000", (long) t); // UTC

payload = _influxDBMeasurement + " " + _key + "=" + _content + " " + nowTimestamp;
// payload = _influxDBMeasurement + " " + _key + "=774 " + nowTimestamp;
}
else
{
payload = _influxDBMeasurement + " " + _key + "=" + _content;
payload = _influxDB_V2_Measurement + " " + _key + "=" + _content;
}

payload.shrink_to_fit();
Expand Down