From: Jean-Philippe Orsini Date: Sun, 16 Feb 2014 10:08:18 +0000 (+0000) Subject: merged plib X-Git-Tag: v1.3.0~44 X-Git-Url: https://git.wpitchoune.net/gitweb/?p=ppastats.git;a=commitdiff_plain;h=12b0e2c57c55b4d3086ef920deda5400016339f2 merged plib --- diff --git a/src/lp_json.c b/src/lp_json.c index 0643a1b..76a97d6 100644 --- a/src/lp_json.c +++ b/src/lp_json.c @@ -52,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); diff --git a/src/lp_ws.c b/src/lp_ws.c index 0dce140..74bcde5 100644 --- a/src/lp_ws.c +++ b/src/lp_ws.c @@ -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) diff --git a/src/plog.c b/src/plog.c index be6d09e..4cc64a5 100644 --- a/src/plog.c +++ b/src/plog.c @@ -84,7 +84,7 @@ static void vlogf(int lvl, const char *fmt, va_list ap) lvl_str = "[??]"; } - t = get_current_time_str(); + t = get_current_ISO8601_time(); if (!t) return ; diff --git a/src/ptime.c b/src/ptime.c index 3eb966b..2c06c7c 100644 --- a/src/ptime.c +++ b/src/ptime.c @@ -1,40 +1,74 @@ /* - * Copyright (C) 2010-2014 jeanfi@gmail.com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License as - * published by the Free Software Foundation; either version 2 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA - * 02110-1301 USA + Copyright (C) 2010-2014 jeanfi@gmail.com + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License as + published by the Free Software Foundation; either version 2 of the + License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + 02110-1301 USA */ #include #include #include -static int ISO8601_TIME_LENGTH = 219; /* YYYY-MM-DDThh:mm:ss */ +const int P_TIME_VER = 2; + +static const int ISO8601_TIME_LENGTH = 19; /* YYYY-MM-DDThh:mm:ss */ +static const int ISO8601_DATE_LENGTH = 10; /* YYYY-MM-DD */ -char *time_to_str(time_t *t) +char *time_to_ISO8601_time(time_t *t) { struct tm lt; - char *str; memset(<, 0, sizeof(struct tm)); - if (!localtime_r(t, <)) + if (!gmtime_r(t, <)) return NULL; - str = malloc(ISO8601_TIME_LENGTH); + return tm_to_ISO8601_time(<); +} + +char *time_to_ISO8601_date(time_t *t) +{ + struct tm lt; + + memset(<, 0, sizeof(struct tm)); + if (!gmtime_r(t, <)) + return NULL; + + return tm_to_ISO8601_date(<); +} + +char *tm_to_ISO8601_date(struct tm *tm) +{ + char *str; + + str = malloc(ISO8601_DATE_LENGTH + 1); + + if (strftime(str, ISO8601_DATE_LENGTH + 1, "%F", tm)) { + return str; + } else { + free(str); + return NULL; + } +} + +char *tm_to_ISO8601_time(struct tm *tm) +{ + char *str; + + str = malloc(ISO8601_TIME_LENGTH + 1); - if (strftime(str, ISO8601_TIME_LENGTH, "%FT%T", <)) { + if (strftime(str, ISO8601_TIME_LENGTH + 1, "%FT%T", tm)) { return str; } else { free(str); @@ -42,10 +76,10 @@ char *time_to_str(time_t *t) } } -char *get_current_time_str() +char *get_current_ISO8601_time() { time_t t; t = time(NULL); - return time_to_str(&t); + return time_to_ISO8601_time(&t); } diff --git a/src/ptime.h b/src/ptime.h index d560ff3..eac7342 100644 --- a/src/ptime.h +++ b/src/ptime.h @@ -19,11 +19,16 @@ #ifndef _P_TIME_H #define _P_TIME_H -#define P_TIME_VER 1 - #include -char *get_current_time_str(); -char *time_to_str(time_t *t); +extern const int P_TIME_VER; + +char *get_current_ISO8601_time(); + +char *time_to_ISO8601_time(time_t *); +char *time_to_ISO8601_date(time_t *); + +char *tm_to_ISO8601_date(struct tm *); +char *tm_to_ISO8601_time(struct tm *); #endif diff --git a/tests/Makefile.am b/tests/Makefile.am index c3afa6a..cb62afe 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -4,11 +4,13 @@ check-local: checkpatch.pl EXTRA_DIST = \ checkpatch.pl \ - test_strrep.c + test_strrep.c \ + test_ptime.c -TESTS = test-strrep +TESTS = test-strrep \ + test-ptime -check_PROGRAMS = test-strrep +check_PROGRAMS = test-strrep test-ptime test_strrep_SOURCES = \ test_strrep.c \ @@ -16,3 +18,9 @@ test_strrep_SOURCES = \ $(top_builddir)/src/pstr.c test_strrep_CFLAGS = -I$(top_srcdir)/src +test_ptime_SOURCES = \ + test_ptime.c \ + $(top_builddir)/src/ptime.h \ + $(top_builddir)/src/ptime.c +test_ptime_CFLAGS = -I$(top_srcdir)/src + diff --git a/tests/Makefile.in b/tests/Makefile.in index 1361fd4..e870bed 100644 --- a/tests/Makefile.in +++ b/tests/Makefile.in @@ -77,8 +77,8 @@ PRE_UNINSTALL = : POST_UNINSTALL = : build_triplet = @build@ host_triplet = @host@ -TESTS = test-strrep$(EXEEXT) -check_PROGRAMS = test-strrep$(EXEEXT) +TESTS = test-strrep$(EXEEXT) test-ptime$(EXEEXT) +check_PROGRAMS = test-strrep$(EXEEXT) test-ptime$(EXEEXT) subdir = tests DIST_COMMON = $(srcdir)/Makefile.in $(srcdir)/Makefile.am \ $(top_srcdir)/depcomp $(top_srcdir)/test-driver @@ -95,6 +95,12 @@ mkinstalldirs = $(install_sh) -d CONFIG_HEADER = $(top_builddir)/config.h CONFIG_CLEAN_FILES = CONFIG_CLEAN_VPATH_FILES = +am_test_ptime_OBJECTS = test_ptime-test_ptime.$(OBJEXT) \ + test_ptime-ptime.$(OBJEXT) +test_ptime_OBJECTS = $(am_test_ptime_OBJECTS) +test_ptime_LDADD = $(LDADD) +test_ptime_LINK = $(CCLD) $(test_ptime_CFLAGS) $(CFLAGS) $(AM_LDFLAGS) \ + $(LDFLAGS) -o $@ am_test_strrep_OBJECTS = test_strrep-test_strrep.$(OBJEXT) \ test_strrep-pstr.$(OBJEXT) test_strrep_OBJECTS = $(am_test_strrep_OBJECTS) @@ -133,8 +139,8 @@ AM_V_CCLD = $(am__v_CCLD_@AM_V@) am__v_CCLD_ = $(am__v_CCLD_@AM_DEFAULT_V@) am__v_CCLD_0 = @echo " CCLD " $@; am__v_CCLD_1 = -SOURCES = $(test_strrep_SOURCES) -DIST_SOURCES = $(test_strrep_SOURCES) +SOURCES = $(test_ptime_SOURCES) $(test_strrep_SOURCES) +DIST_SOURCES = $(test_ptime_SOURCES) $(test_strrep_SOURCES) am__can_run_installinfo = \ case $$AM_UPDATE_INFO_DIR in \ n|no|NO) false;; \ @@ -488,7 +494,8 @@ top_builddir = @top_builddir@ top_srcdir = @top_srcdir@ EXTRA_DIST = \ checkpatch.pl \ - test_strrep.c + test_strrep.c \ + test_ptime.c test_strrep_SOURCES = \ test_strrep.c \ @@ -496,6 +503,12 @@ test_strrep_SOURCES = \ $(top_builddir)/src/pstr.c test_strrep_CFLAGS = -I$(top_srcdir)/src +test_ptime_SOURCES = \ + test_ptime.c \ + $(top_builddir)/src/ptime.h \ + $(top_builddir)/src/ptime.c + +test_ptime_CFLAGS = -I$(top_srcdir)/src all: all-am .SUFFIXES: @@ -534,6 +547,10 @@ $(am__aclocal_m4_deps): clean-checkPROGRAMS: -test -z "$(check_PROGRAMS)" || rm -f $(check_PROGRAMS) +test-ptime$(EXEEXT): $(test_ptime_OBJECTS) $(test_ptime_DEPENDENCIES) $(EXTRA_test_ptime_DEPENDENCIES) + @rm -f test-ptime$(EXEEXT) + $(AM_V_CCLD)$(test_ptime_LINK) $(test_ptime_OBJECTS) $(test_ptime_LDADD) $(LIBS) + test-strrep$(EXEEXT): $(test_strrep_OBJECTS) $(test_strrep_DEPENDENCIES) $(EXTRA_test_strrep_DEPENDENCIES) @rm -f test-strrep$(EXEEXT) $(AM_V_CCLD)$(test_strrep_LINK) $(test_strrep_OBJECTS) $(test_strrep_LDADD) $(LIBS) @@ -544,6 +561,8 @@ mostlyclean-compile: distclean-compile: -rm -f *.tab.c +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/test_ptime-ptime.Po@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/test_ptime-test_ptime.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/test_strrep-pstr.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/test_strrep-test_strrep.Po@am__quote@ @@ -561,6 +580,34 @@ distclean-compile: @AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@ @am__fastdepCC_FALSE@ $(AM_V_CC@am__nodep@)$(COMPILE) -c `$(CYGPATH_W) '$<'` +test_ptime-test_ptime.o: test_ptime.c +@am__fastdepCC_TRUE@ $(AM_V_CC)$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(test_ptime_CFLAGS) $(CFLAGS) -MT test_ptime-test_ptime.o -MD -MP -MF $(DEPDIR)/test_ptime-test_ptime.Tpo -c -o test_ptime-test_ptime.o `test -f 'test_ptime.c' || echo '$(srcdir)/'`test_ptime.c +@am__fastdepCC_TRUE@ $(AM_V_at)$(am__mv) $(DEPDIR)/test_ptime-test_ptime.Tpo $(DEPDIR)/test_ptime-test_ptime.Po +@AMDEP_TRUE@@am__fastdepCC_FALSE@ $(AM_V_CC)source='test_ptime.c' object='test_ptime-test_ptime.o' libtool=no @AMDEPBACKSLASH@ +@AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@ +@am__fastdepCC_FALSE@ $(AM_V_CC@am__nodep@)$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(test_ptime_CFLAGS) $(CFLAGS) -c -o test_ptime-test_ptime.o `test -f 'test_ptime.c' || echo '$(srcdir)/'`test_ptime.c + +test_ptime-test_ptime.obj: test_ptime.c +@am__fastdepCC_TRUE@ $(AM_V_CC)$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(test_ptime_CFLAGS) $(CFLAGS) -MT test_ptime-test_ptime.obj -MD -MP -MF $(DEPDIR)/test_ptime-test_ptime.Tpo -c -o test_ptime-test_ptime.obj `if test -f 'test_ptime.c'; then $(CYGPATH_W) 'test_ptime.c'; else $(CYGPATH_W) '$(srcdir)/test_ptime.c'; fi` +@am__fastdepCC_TRUE@ $(AM_V_at)$(am__mv) $(DEPDIR)/test_ptime-test_ptime.Tpo $(DEPDIR)/test_ptime-test_ptime.Po +@AMDEP_TRUE@@am__fastdepCC_FALSE@ $(AM_V_CC)source='test_ptime.c' object='test_ptime-test_ptime.obj' libtool=no @AMDEPBACKSLASH@ +@AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@ +@am__fastdepCC_FALSE@ $(AM_V_CC@am__nodep@)$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(test_ptime_CFLAGS) $(CFLAGS) -c -o test_ptime-test_ptime.obj `if test -f 'test_ptime.c'; then $(CYGPATH_W) 'test_ptime.c'; else $(CYGPATH_W) '$(srcdir)/test_ptime.c'; fi` + +test_ptime-ptime.o: $(top_builddir)/src/ptime.c +@am__fastdepCC_TRUE@ $(AM_V_CC)$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(test_ptime_CFLAGS) $(CFLAGS) -MT test_ptime-ptime.o -MD -MP -MF $(DEPDIR)/test_ptime-ptime.Tpo -c -o test_ptime-ptime.o `test -f '$(top_builddir)/src/ptime.c' || echo '$(srcdir)/'`$(top_builddir)/src/ptime.c +@am__fastdepCC_TRUE@ $(AM_V_at)$(am__mv) $(DEPDIR)/test_ptime-ptime.Tpo $(DEPDIR)/test_ptime-ptime.Po +@AMDEP_TRUE@@am__fastdepCC_FALSE@ $(AM_V_CC)source='$(top_builddir)/src/ptime.c' object='test_ptime-ptime.o' libtool=no @AMDEPBACKSLASH@ +@AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@ +@am__fastdepCC_FALSE@ $(AM_V_CC@am__nodep@)$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(test_ptime_CFLAGS) $(CFLAGS) -c -o test_ptime-ptime.o `test -f '$(top_builddir)/src/ptime.c' || echo '$(srcdir)/'`$(top_builddir)/src/ptime.c + +test_ptime-ptime.obj: $(top_builddir)/src/ptime.c +@am__fastdepCC_TRUE@ $(AM_V_CC)$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(test_ptime_CFLAGS) $(CFLAGS) -MT test_ptime-ptime.obj -MD -MP -MF $(DEPDIR)/test_ptime-ptime.Tpo -c -o test_ptime-ptime.obj `if test -f '$(top_builddir)/src/ptime.c'; then $(CYGPATH_W) '$(top_builddir)/src/ptime.c'; else $(CYGPATH_W) '$(srcdir)/$(top_builddir)/src/ptime.c'; fi` +@am__fastdepCC_TRUE@ $(AM_V_at)$(am__mv) $(DEPDIR)/test_ptime-ptime.Tpo $(DEPDIR)/test_ptime-ptime.Po +@AMDEP_TRUE@@am__fastdepCC_FALSE@ $(AM_V_CC)source='$(top_builddir)/src/ptime.c' object='test_ptime-ptime.obj' libtool=no @AMDEPBACKSLASH@ +@AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@ +@am__fastdepCC_FALSE@ $(AM_V_CC@am__nodep@)$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(test_ptime_CFLAGS) $(CFLAGS) -c -o test_ptime-ptime.obj `if test -f '$(top_builddir)/src/ptime.c'; then $(CYGPATH_W) '$(top_builddir)/src/ptime.c'; else $(CYGPATH_W) '$(srcdir)/$(top_builddir)/src/ptime.c'; fi` + test_strrep-test_strrep.o: test_strrep.c @am__fastdepCC_TRUE@ $(AM_V_CC)$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(test_strrep_CFLAGS) $(CFLAGS) -MT test_strrep-test_strrep.o -MD -MP -MF $(DEPDIR)/test_strrep-test_strrep.Tpo -c -o test_strrep-test_strrep.o `test -f 'test_strrep.c' || echo '$(srcdir)/'`test_strrep.c @am__fastdepCC_TRUE@ $(AM_V_at)$(am__mv) $(DEPDIR)/test_strrep-test_strrep.Tpo $(DEPDIR)/test_strrep-test_strrep.Po @@ -789,6 +836,13 @@ test-strrep.log: test-strrep$(EXEEXT) --log-file $$b.log --trs-file $$b.trs \ $(am__common_driver_flags) $(AM_LOG_DRIVER_FLAGS) $(LOG_DRIVER_FLAGS) -- $(LOG_COMPILE) \ "$$tst" $(AM_TESTS_FD_REDIRECT) +test-ptime.log: test-ptime$(EXEEXT) + @p='test-ptime$(EXEEXT)'; \ + b='test-ptime'; \ + $(am__check_pre) $(LOG_DRIVER) --test-name "$$f" \ + --log-file $$b.log --trs-file $$b.trs \ + $(am__common_driver_flags) $(AM_LOG_DRIVER_FLAGS) $(LOG_DRIVER_FLAGS) -- $(LOG_COMPILE) \ + "$$tst" $(AM_TESTS_FD_REDIRECT) .test.log: @p='$<'; \ $(am__set_b); \ diff --git a/tests/test_ptime.c b/tests/test_ptime.c new file mode 100644 index 0000000..f3fb0ca --- /dev/null +++ b/tests/test_ptime.c @@ -0,0 +1,108 @@ +/* + Copyright (C) 2010-2011 jeanfi@gmail.com + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + 02110-1301 USA +*/ + +#include +#include +#include + +#include + +int test_time_to_ISO8601_time(time_t t, const char *ref) +{ + char *result; + int failure; + + result = time_to_ISO8601_time(&t); + + failure = strcmp(result, ref); + + if (failure) + fprintf(stderr, + "test_time_to_ISO8601_time(%ld)=%s instead of %s.\n", + t, + result, + ref); + + free(result); + + return failure; +} + +int test_time_to_ISO8601_date(time_t t, const char *ref) +{ + char *result; + int failure; + + result = time_to_ISO8601_date(&t); + + failure = strcmp(result, ref); + + if (failure) + fprintf(stderr, + "test_date_to_ISO8601_time(%ld)=%s instead of %s.\n", + t, + result, + ref); + + free(result); + + return failure; +} + +static int tests_time_to_ISO8601_time() +{ + int failures; + + failures = 0; + + failures += test_time_to_ISO8601_date(0, "1970-01-01T00:00:00"); + failures += test_time_to_ISO8601_date(83, "1970-01-01T00:01:23"); + failures += test_time_to_ISO8601_date(1392542321, + "2014-02-16T09:18:41"); + + return failures; +} + +static int tests_time_to_ISO8601_date() +{ + int failures; + + failures = 0; + + failures += test_time_to_ISO8601_time(0, "1970-01-01"); + failures += test_time_to_ISO8601_time(83, "1970-01-01"); + failures += test_time_to_ISO8601_time(1392542321, "2014-02-16"); + + return failures; +} + +int main(int argc, char **argv) +{ + int failures; + + failures = 0; + + failures += tests_time_to_ISO8601_time(); + failures += tests_time_to_ISO8601_date(); + + if (failures) + exit(EXIT_FAILURE); + else + exit(EXIT_SUCCESS); +}