From e3e7618f6dfc0ae997042cb22d9eee1dd8e19678 Mon Sep 17 00:00:00 2001 From: Erich Eckner Date: Tue, 30 Oct 2018 10:39:55 +0100 Subject: input_gadgets.c: parse date & time of fh temperature instead of copying fields --- input_gadgets.c | 41 +++++++++++++++++++---------------------- 1 file changed, 19 insertions(+), 22 deletions(-) (limited to 'input_gadgets.c') diff --git a/input_gadgets.c b/input_gadgets.c index df0abe1..292a112 100644 --- a/input_gadgets.c +++ b/input_gadgets.c @@ -12,7 +12,7 @@ #include "input_gadgets.h" -#define fh_temperature_regex "\\s*\n\\s*Wetterdaten vom ([0-9]{2})\\.([0-9]{2})\\.([0-9]{4}) um ([0-9:]{5}) MEZ(.|\n)*>Temperatur\\s*\n\\s*]*>([^<]*)" +#define fh_temperature_regex "\\s*\n\\s*Wetterdaten vom ([0-9]{2}\\.[0-9]{2}\\.[0-9]{4} um [0-9]{2}:[0-9]{2} MEZ)(.|\n)*>Temperatur\\s*\n\\s*]*>([^<]*)" #define wetter_warnung_filename_regex "^2\\.49\\.0\\.1\\.276\\.0\\.DWD\\.PVW\\.([0-9]+)\\." #define warnings_future_timespan (60*60*10) @@ -153,15 +153,13 @@ char *gadgets_retrieve_current_temperature(char *output, int max_len) } regex_t re; - regmatch_t rm[7]; - if (regcomp(&re, fh_temperature_regex, REG_EXTENDED|REG_NEWLINE) != 0) - { + regmatch_t rm[4]; + if (regcomp(&re, fh_temperature_regex, REG_EXTENDED|REG_NEWLINE) != 0) { fprintf(stderr, "Failed to compile regex '%s'\n", fh_temperature_regex); free(chunk.memory); return NULL; } - if (ret_val = regexec(&re, chunk.memory, 7, rm, 0)) - { + if (ret_val = regexec(&re, chunk.memory, 4, rm, 0)) { char *reg_err; reg_err = malloc(1024); regerror(ret_val, &re, reg_err, 1024); @@ -171,25 +169,24 @@ char *gadgets_retrieve_current_temperature(char *output, int max_len) return NULL; } regfree(&re); - char* ende; + char* ende = output; if (max_len > 0) { - int i = snprintf(output, max_len, "%.*s (%.*s %.*s-%.*s-%.*s)", - (int)(rm[6].rm_eo - rm[6].rm_so), (char*)(chunk.memory + rm[6].rm_so), - (int)(rm[4].rm_eo - rm[4].rm_so), (char*)(chunk.memory + rm[4].rm_so), - (int)(rm[3].rm_eo - rm[3].rm_so), (char*)(chunk.memory + rm[3].rm_so), - (int)(rm[2].rm_eo - rm[2].rm_so), (char*)(chunk.memory + rm[2].rm_so), - (int)(rm[1].rm_eo - rm[1].rm_so), (char*)(chunk.memory + rm[1].rm_so) + struct tm time_struct; + if (strptime((char*)(chunk.memory + rm[1].rm_so),"%d.%m.%Y um %H:%M MEZ",&time_struct) == NULL) { + fprintf(stderr, "Failed to parse time '%.*s'\n", (int)(rm[1].rm_eo - rm[1].rm_so), (char*)(chunk.memory + rm[1].rm_so)); + free(chunk.memory); + return NULL; + } + time_struct.tm_isdst = 0; // the fh gives CET all the time + mktime(&time_struct); + ende += snprintf(output, max_len, "%.*s", + (int)(rm[3].rm_eo - rm[3].rm_so), (char*)(chunk.memory + rm[3].rm_so) // temperature ); - ende = output + i; - for (; i>=0; i--) - if (output[i] == '(') - break; - for (; i>=0; i--) - if (output[i] == '.') - output[i] = ','; + for (char *i = output; i < ende; i++) + if (*i == '.') + *i = ','; + ende += strftime(ende, output + max_len - ende, " (%m-%d %H:%M)", &time_struct); } - else - ende = output; free(chunk.memory); return ende; } -- cgit v1.2.3-70-g09d2