better code
[ppastats.git] / src / lp_json.c
index e217180..6f251f3 100644 (file)
 #include "lp_json.h"
 #include "lp_ws.h"
 
+static time_t json_to_time(json_object *json)
+{
+       const char *str;
+       struct tm tm;
+       char *ret;
+
+       str = json_object_get_string(json);
+       if (!str)
+               return -1;
+
+       tm.tm_isdst = -1;
+       ret = strptime(str, "%FT%T", &tm);
+
+       if (ret)
+               return mktime(&tm);
+       else
+               return -1;
+}
+
+static json_object *time_to_json(time_t t)
+{
+       char *str;
+
+       str = time_to_str(t);
+
+       if (str)
+               return json_object_new_string(str);
+       else
+               return NULL;
+}
+
 static struct bpph *json_to_bpph(json_object *o)
 {
        const char *binary_package_name;
        const char *binary_package_version;
        const char *distro_arch_series_link;
        const char *self_link;
-       int architecture_specific;
-       const char *date_created;
+       int arch_specific;
        struct bpph *bpph;
        const char *status;
+       time_t date_created;
 
        binary_package_name = json_object_get_string
                (json_object_object_get(o, "binary_package_name"));
@@ -49,26 +80,20 @@ static struct bpph *json_to_bpph(json_object *o)
        self_link = json_object_get_string
                (json_object_object_get(o, "self_link"));
 
-       if (json_object_get_boolean
-           (json_object_object_get(o, "architecture_specific")))
-               architecture_specific = 1;
-       else
-               architecture_specific = 0;
+       arch_specific = json_object_get_boolean
+               (json_object_object_get(o, "architecture_specific"));
+
+       date_created = json_to_time(json_object_object_get(o, "date_created"));
+
+       status = json_object_get_string(json_object_object_get(o, "status"));
 
        bpph =  bpph_new(binary_package_name,
                         binary_package_version,
                         distro_arch_series_link,
                         self_link,
-                        architecture_specific);
-
-       date_created = json_object_get_string
-               (json_object_object_get(o, "date_created"));
-       if (date_created)
-               strptime(date_created, "%FT%T", &bpph->date_created);
-
-       status = json_object_get_string(json_object_object_get(o, "status"));
-       if (status)
-               bpph->status = strdup(status);
+                        status,
+                        arch_specific,
+                        date_created);
 
        return bpph;
 }
@@ -76,7 +101,6 @@ static struct bpph *json_to_bpph(json_object *o)
 static json_object *bpph_to_json(struct bpph *bpph)
 {
        json_object *json;
-       char *date;
 
        json = json_object_new_object();
 
@@ -96,9 +120,7 @@ static json_object *bpph_to_json(struct bpph *bpph)
                 json_object_new_string(bpph->distro_arch_series_link));
 
        json_object_object_add
-               (json,
-                "self_link",
-                json_object_new_string(bpph->self_link));
+               (json, "self_link", json_object_new_string(bpph->self_link));
 
        json_object_object_add
                (json,
@@ -108,15 +130,8 @@ static json_object *bpph_to_json(struct bpph *bpph)
        json_object_object_add
                (json, "status", json_object_new_string(bpph->status));
 
-       date = malloc(strlen("YY-MM-DDThh:mm:ss+xxx") + 1);
-       strftime(date,
-                strlen("YY-MM-DDThh:mm:ss+xxx") + 1,
-                "%FT%T",
-                &bpph->date_created);
-
        json_object_object_add
-               (json, "date_created", json_object_new_string(date));
-       free(date);
+               (json, "date_created", time_to_json(bpph->date_created));
 
        return json;
 }