X-Git-Url: https://git.wpitchoune.net/gitweb/?a=blobdiff_plain;f=src%2Flp_json.c;h=76a97d652b18fc46640c33feab83417bbca4657e;hb=12b0e2c57c55b4d3086ef920deda5400016339f2;hp=c9379c5a707ef023b089da1485aea3161d8ce742;hpb=8fbc54a7960312d1626f4032d5fef5bf8a37db70;p=ppastats.git diff --git a/src/lp_json.c b/src/lp_json.c index c9379c5..76a97d6 100644 --- a/src/lp_json.c +++ b/src/lp_json.c @@ -25,6 +25,7 @@ #include "lp_json.h" #include "lp_ws.h" +#include static time_t json_to_time(json_object *json) { @@ -36,6 +37,8 @@ static time_t json_to_time(json_object *json) if (!str) return -1; + + memset(&tm, 0, sizeof(struct tm)); tm.tm_isdst = -1; ret = strptime(str, "%FT%T", &tm); @@ -49,7 +52,7 @@ static json_object *time_to_json(time_t t) { char *str; - str = time_to_str(t); + str = time_to_ISO8601_time(&t); if (str) return json_object_new_string(str); @@ -252,6 +255,7 @@ json_object_to_daily_download_total(const char *d, json_object *o_c) result = malloc(sizeof(struct daily_download_total)); result->count = json_object_get_int(o_c); + memset(&result->date, 0, sizeof(struct tm)); strptime(d, "%FT%T%z", &result->date); return result; @@ -292,3 +296,41 @@ json_object_to_daily_download_totals(json_object *o) return result; } + +struct json_object *date_to_json(struct tm *tm) +{ + json_object *json; + + json = json_object_new_array(); + json_object_array_add(json, json_object_new_int(tm->tm_year+1900)); + json_object_array_add(json, json_object_new_int(tm->tm_mon+1)); + json_object_array_add(json, json_object_new_int(tm->tm_mday)); + + return json; +} + +json_object *ddts_to_json(struct daily_download_total **ddts) +{ + json_object *json_ddt, *json_ddts; + struct daily_download_total *ddt; + + json_ddts = json_object_new_array(); + + while (ddts && *ddts) { + ddt = *ddts; + + json_ddt = json_object_new_object(); + json_object_object_add(json_ddt, + "value", + json_object_new_int(ddt->count)); + json_object_object_add(json_ddt, + "time", + date_to_json(&ddt->date)); + + json_object_array_add(json_ddts, json_ddt); + + ddts++; + } + + return json_ddts; +}