{
char *str;
- str = time_to_str(&t);
+ str = time_to_ISO8601_time(&t);
if (str)
return json_object_new_string(str);
}
if (last)
- return time_to_str(&last);
+ return time_to_ISO8601_time(&last);
else
return NULL;
}
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
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));
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)
lvl_str = "[??]";
}
- t = get_current_time_str();
+ t = get_current_ISO8601_time();
if (!t)
return ;
/*
- * 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 <stdlib.h>
#include <string.h>
#include <ptime.h>
-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);
}
}
-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);
}
#ifndef _P_TIME_H
#define _P_TIME_H
-#define P_TIME_VER 1
-
#include <time.h>
-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
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 \
$(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
+
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
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)
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;; \
top_srcdir = @top_srcdir@
EXTRA_DIST = \
checkpatch.pl \
- test_strrep.c
+ test_strrep.c \
+ test_ptime.c
test_strrep_SOURCES = \
test_strrep.c \
$(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:
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)
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@
@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
--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); \
--- /dev/null
+/*
+ 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 <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+
+#include <ptime.h>
+
+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);
+}