some cleanup
[ppastats.git] / src / lp_ws.c
index 0dce140..64db1d7 100644 (file)
@@ -120,7 +120,7 @@ static char *get_last_creation_date(struct bpph **list)
                }
 
        if (last)
-               return time_to_str(&last);
+               return time_to_ISO8601_time(&last);
        else
                return NULL;
 }
@@ -309,17 +309,6 @@ const struct distro_series *get_distro_series(const char *url)
        return distro;
 }
 
-char *date_to_str(struct tm tm)
-{
-       char *str;
-
-       str = malloc(4 + 1 + 2 + 1 + 2 + 1);
-
-       strftime(str, 11, "%Y-%m-%d", &tm);
-
-       return str;
-}
-
 /*
   Convert ddts older than 4 weeks to the same JSON representation than
   the LP one.  Newer ddts are not stored in the cache because the data
@@ -348,7 +337,7 @@ static json_object *ddts_to_json_for_cache(struct daily_download_total **ddts)
                d = difftime(tv->tv_sec, t);
 
                if (d > 4 * 7 * 24 * 60 * 60) { /* older than 4 weeks */
-                       date = date_to_str(ddt->date);
+                       date = tm_to_ISO8601_date(&ddt->date);
                        json_object_object_add(j_ddts,
                                               date,
                                               json_object_new_int(ddt->count));
@@ -363,25 +352,13 @@ static json_object *ddts_to_json_for_cache(struct daily_download_total **ddts)
        return j_ddts;
 }
 
-static char *time_t_to_str(time_t t)
-{
-       struct tm *tm;
-       char *str;
-
-       tm = gmtime(&t);
-
-       str = date_to_str(*tm);
-
-       return str;
-}
-
 char *create_ddts_query(const char *binary_url, time_t st)
 {
        char *q;
        char *sdate;
 
        if (st) {
-               sdate = time_t_to_str(st);
+               sdate = time_to_ISO8601_date(&st);
 
                q = malloc(strlen(binary_url)
                           + strlen(QUERY_GET_DAILY_DOWNLOAD_TOTALS)
@@ -416,13 +393,16 @@ struct daily_download_total **get_daily_download_totals(const char *binary_url)
        key = get_ddts_list_cache_key(binary_url);
 
        content = fcache_get(key);
-       if (content)
+       if (content) {
                json = json_tokener_parse(content);
-       else
+               free(content);
+       } else {
                json = NULL;
+       }
 
        if (json) {
                cached_ddts = json_object_to_daily_download_totals(json);
+               json_object_put(json);
                last_t = ddts_get_last_date(cached_ddts);
        } else {
                last_t = 0;
@@ -449,8 +429,9 @@ struct daily_download_total **get_daily_download_totals(const char *binary_url)
        }
 
        free(key);
-       free(cached_ddts);
-       free(retrieved_ddts);
+       if (ddts != cached_ddts)
+               daily_download_total_list_free(cached_ddts);
+       daily_download_total_list_free(retrieved_ddts);
 
        return ddts;
 }