#include "list.h"
#include "lp.h"
+char *time_to_str(time_t t)
+{
+ char *str;
+ struct tm *tm;
+ size_t ret;
+
+ tm = localtime(&t);
+
+ if (!tm)
+ return NULL;
+
+ str = malloc(strlen("YYYY-MM-DDThh:mm:ss") + 1);
+ ret = strftime(str, strlen("YYYY-MM-DDThh:mm:ss") + 1, "%FT%T", tm);
+
+ if (ret)
+ return str;
+
+ free(str);
+ return NULL;
+}
+
struct distro_series *distro_series_new(const char *name,
const char *version,
const char *title,
const char *binary_package_version,
const char *distro_arch_series_link,
const char *self_link,
- int architecture_specific)
+ const char *status,
+ int architecture_specific,
+ time_t date_created)
{
struct bpph *h;
h->distro_arch_series_link = strdup(distro_arch_series_link);
h->self_link = strdup(self_link);
h->architecture_specific = architecture_specific;
- h->status = NULL;
- h->date_created.tm_isdst = -1;
+ h->status = strdup(status);
+ h->date_created = date_created;
return h;
}
#define URL_BASE_LP "https://api.launchpad.net/1.0"
-#define _XOPEN_SOURCE
#include <time.h>
struct daily_download_total {
char *status;
int architecture_specific;
- struct tm date_created;
+ time_t date_created;
};
struct distro_arch_series {
const char *binary_package_version,
const char *distro_arch_series_link,
const char *self_link,
- int architecture_specific);
+ const char *status,
+ int architecture_specific,
+ time_t date_created);
void bpph_list_free(struct bpph **list);
char *get_archive_url(const char *owner, const char *ppa);
+char *time_to_str(time_t t);
+
#endif
#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"));
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;
}
static json_object *bpph_to_json(struct bpph *bpph)
{
json_object *json;
- char *date;
json = json_object_new_object();
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,
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;
}
return NULL;
}
-char *get_bpph_list_cache_key(const char *archive_url)
+static char *get_bpph_list_cache_key(const char *archive_url)
{
char *key;
return key;
}
-struct bpph **get_bpph_list_from_cache(const char *key)
+static struct bpph **get_bpph_list_from_cache(const char *key)
{
char *content;
struct bpph **list;
return list;
}
-struct tm *get_last_creation_date(struct bpph **list)
+static char *get_last_creation_date(struct bpph **list)
{
time_t last, t;
struct bpph **cur;
if (list)
for (cur = list; *cur; cur++) {
- t = mktime(&(*cur)->date_created);
+ t = (*cur)->date_created;
if (t > last)
last = t;
}
- return localtime(&last);
+ if (last)
+ return time_to_str(last);
+ else
+ return NULL;
}
struct bpph **get_bpph_list(const char *archive_url, const char *pkg_status)
struct bpph **result = NULL;
struct json_object *o, *bpph_json, *o_next;
char *created_since_date;
- struct tm *tm;
int ok;
url = malloc(strlen(archive_url)
result = get_bpph_list_from_cache(key);
if (result) {
- tm = get_last_creation_date(result);
-
- created_since_date = malloc(200);
- strftime(created_since_date,
- 100,
- "%FT%T",
- tm);
+ created_since_date = get_last_creation_date(result);
- printf("Update package since: %s\n", created_since_date);
+ if (created_since_date) {
+ printf("Update package since: %s\n",
+ created_since_date);
- tmp = malloc(strlen(url)
- + strlen("&created_since_date=")
- + strlen(created_since_date)+1);
- strcpy(tmp, url);
- strcat(tmp, "&created_since_date=");
- strcat(tmp, created_since_date);
+ tmp = malloc(strlen(url)
+ + strlen("&created_since_date=")
+ + strlen(created_since_date)+1);
+ strcpy(tmp, url);
+ strcat(tmp, "&created_since_date=");
+ strcat(tmp, created_since_date);
- free(url);
- url = tmp;
+ free(url);
+ url = tmp;
- free(created_since_date);
+ free(created_since_date);
+ }
}
ok = 1;