fix: timezone issue

This commit is contained in:
Tobias Reisinger 2020-05-07 01:41:16 +02:00
parent ad7a6028b3
commit 27cec1d8cc
10 changed files with 27 additions and 28 deletions

View file

@ -3,7 +3,7 @@ name = new emgauwa device
discovery-port = 4421
relay-count = 10
database = controller_db.lmdb
log-level = info
log-level = debug
[relay-0]
driver = piface

View file

@ -15,12 +15,14 @@
void
handler_loop(controller_t *controller)
{
time_t timestamp = time(NULL);
struct tm *time_struct = localtime(&timestamp);
LOG_DEBUG("===== IDLE LOOP START =====\n");
for(uint_fast8_t i = 0; i < controller->relay_count; ++i)
{
relay_t *relay = controller->relays[i];
int is_active = 0;
if(relay_is_active(relay, time(NULL)))
if(relay_is_active(relay, time_struct))
{
LOG_DEBUG("relay %d is active\n", i);
is_active = 1;

View file

@ -1,12 +0,0 @@
#include <time.h>
#include <helpers.h>
int
helper_get_weekday(const time_t timestamp_now)
{
struct tm *now = localtime(&timestamp_now);
int wday_sun_sat = now->tm_wday;
int wday_mon_sun = (wday_sun_sat + 6) % 7;
return wday_mon_sun;
}

11
helpers/get_weekday.c Normal file
View file

@ -0,0 +1,11 @@
#include <time.h>
#include <helpers.h>
int
helper_get_weekday(const struct tm *time_struct)
{
int wday_sun_sat = time_struct->tm_wday;
int wday_mon_sun = (wday_sun_sat + 6) % 7;
return wday_mon_sun;
}

View file

@ -1,6 +1,7 @@
#ifndef CONTROLLER_HELPERS_H
#define CONTROLLER_HELPERS_H
#include <time.h>
#include <confini.h>
#include <config.h>
@ -32,6 +33,6 @@ void
helper_parse_cli(int argc, const char **argv, config_t *config);
int
helper_get_weekday(const time_t timestamp_now);
helper_get_weekday(const struct tm *time_struct);
#endif /* CONTROLLER_HELPERS_H */

View file

@ -14,6 +14,6 @@ period_t*
period_create(uint16_t start, uint16_t end);
int
period_includes_time(period_t *period, uint16_t timestamp);
period_includes_time(period_t *period, struct tm *time_struct);
#endif /* CONTROLLER_PERIOD_H */

View file

@ -51,7 +51,7 @@ int
relay_save(relay_t *relay, MDB_env *mdb_env);
int
relay_is_active(relay_t *relay, time_t timestamp_now);
relay_is_active(relay_t *relay, struct tm *time_struct);
void
relay_free(relay_t *relay);

View file

@ -46,8 +46,7 @@ logger_log(FILE *stream, log_level_t level, const char *filename, int line, cons
}
char timestamp_str[32];
time_t rawtime;
time(&rawtime);
time_t rawtime = time(NULL);
strftime(timestamp_str, 32, "%Y-%m-%d %H:%M:%S", localtime(&rawtime));
fprintf(stream, "%s %s:%d:%s " COLOR_NONE, timestamp_str, filename, line, func);

View file

@ -15,11 +15,14 @@ period_create(uint16_t start, uint16_t end)
}
int
period_includes_time(period_t *period, uint16_t timestamp)
period_includes_time(period_t *period, struct tm *time_struct)
{
uint16_t start = period->start;
uint16_t end = period->end;
time_t timestamp = time_struct->tm_hour * 60;
timestamp += time_struct->tm_min;
// "normal" timespan
if(start < end)
{

View file

@ -34,22 +34,17 @@ relay_set_name(relay_t *relay, const char *name)
}
int
relay_is_active(relay_t *relay, time_t timestamp_now)
relay_is_active(relay_t *relay, struct tm *time_struct)
{
schedule_t *schedule = relay->schedules[helper_get_weekday(timestamp_now)];
schedule_t *schedule = relay->schedules[helper_get_weekday(time_struct)];
if(schedule->length == 0)
{
return 0;
}
// we don't need days. reduce to hours, minutes and seconds
timestamp_now %= SECONDS_PER_DAY;
// finally remove seconds
timestamp_now /= SECONDS_PER_MINUTE;
for(uint16_t i = 0; i < schedule->length; ++i)
{
if(period_includes_time(schedule->periods[i], timestamp_now))
if(period_includes_time(schedule->periods[i], time_struct))
{
return 1;
}