aboutsummaryrefslogtreecommitdiff
path: root/libc/test
diff options
context:
space:
mode:
authorAlistair Popple <alistair@popple.id.au>2014-08-14 18:47:49 +1000
committerBenjamin Herrenschmidt <benh@kernel.crashing.org>2014-08-15 13:03:08 +1000
commit39e53e483b186122db12712aca5dfb666e819729 (patch)
tree71d2402f8020b939c16151d5f29fd8823a825d3f /libc/test
parentc0515b6361b03d18cffcbc358d0f6109cd684031 (diff)
downloadskiboot-39e53e483b186122db12712aca5dfb666e819729.zip
skiboot-39e53e483b186122db12712aca5dfb666e819729.tar.gz
skiboot-39e53e483b186122db12712aca5dfb666e819729.tar.bz2
libc: Add mktime and gmtime_r
Both the FSP RTC and the upcoming IPMI RTC implementation need to manipulate time in various ways. Rather than re-implementing slightly different versions of the calculations twice lets implement some standard library functions (with tests) and use those. This patch adds mktime and gmtime_r to the libc. Signed-off-by: Alistair Popple <alistair@popple.id.au> Signed-off-by: Benjamin Herrenschmidt <benh@kernel.crashing.org>
Diffstat (limited to 'libc/test')
-rw-r--r--libc/test/Makefile.check22
-rw-r--r--libc/test/run-time.c82
2 files changed, 104 insertions, 0 deletions
diff --git a/libc/test/Makefile.check b/libc/test/Makefile.check
new file mode 100644
index 0000000..188b6b1
--- /dev/null
+++ b/libc/test/Makefile.check
@@ -0,0 +1,22 @@
+# -*-Makefile-*-
+LIBC_TEST := libc/test/run-time
+
+check: $(LIBC_TEST:%=%-check)
+
+$(LIBC_TEST:%=%-check) : %-check: %
+ $(VALGRIND) $<
+
+$(LIBC_TEST) : % : %.c
+ $(HOSTCC) $(HOSTCFLAGS) -O0 -g -I include -I . -I libfdt -I libc/include -o $@ $<
+
+$(LIBC_TEST): % : %.d
+
+libc/test/%.d: libc/test/%.c
+ $(HOSTCC) $(HOSTCFLAGS) -I include -I . -I libfdt -I libc/include -M $< > $@
+
+-include libc/test/*.d
+
+clean: libc-test-clean
+
+libc-test-clean:
+ $(RM) -f libc/test/*.[od] $(LIBC_TEST)
diff --git a/libc/test/run-time.c b/libc/test/run-time.c
new file mode 100644
index 0000000..95662f4
--- /dev/null
+++ b/libc/test/run-time.c
@@ -0,0 +1,82 @@
+#include "/usr/include/assert.h"
+#include <stdio.h>
+#include <time.h>
+
+#include "../time.c"
+
+#define MKTIME_TEST(Y,M,D,h,m,s,t) \
+ tm.tm_year = Y; \
+ tm.tm_mon = M; \
+ tm.tm_mday = D; \
+ tm.tm_hour = h; \
+ tm.tm_min = m; \
+ tm.tm_sec = s; \
+ assert(mktime(&tm) == t); \
+ assert(tm.tm_year == Y); \
+ assert(tm.tm_mon == M); \
+ assert(tm.tm_mday == D); \
+ assert(tm.tm_hour == h); \
+ assert(tm.tm_min == m); \
+ assert(tm.tm_sec == s)
+
+#define GMTIME_TEST(Y,M,D,h,m,s,tv) \
+ t = tv; \
+ gmtime_r(&t, &tm); \
+ assert(tm.tm_year == Y); \
+ assert(tm.tm_mon == M); \
+ assert(tm.tm_mday == D); \
+ assert(tm.tm_hour == h); \
+ assert(tm.tm_min == m); \
+ assert(tm.tm_sec == s)
+
+#define TIME_TEST(Y,M,D,h,m,s,tv) \
+ MKTIME_TEST(Y,M,D,h,m,s,tv); \
+ GMTIME_TEST(Y,M,D,h,m,s,tv)
+
+int main(void)
+{
+ struct tm tm;
+ time_t t = 0;
+ uint32_t time;
+
+ TIME_TEST(1970, 0, 1, 0, 0, 0, 0);
+ TIME_TEST(1971, 0, 1, 0, 0, 0, 365*SECS_PER_DAY);
+ TIME_TEST(1972, 0, 1, 0, 0, 0, 2*365*SECS_PER_DAY);
+ TIME_TEST(1972, 11, 31, 0, 0, 0, 3*365*SECS_PER_DAY);
+ TIME_TEST(1973, 0, 1, 0, 0, 0, (3*365+1)*SECS_PER_DAY);
+ TIME_TEST(2000, 11, 31, 0, 0, 0, 978220800);
+ TIME_TEST(2001, 0, 1, 0, 0, 0, 978307200);
+ TIME_TEST(2003, 11, 31, 0, 0, 0, 1072828800);
+ TIME_TEST(2004, 0, 1, 0, 0, 0, 1072828800+SECS_PER_DAY);
+ TIME_TEST(2004, 11, 29, 0, 0, 0, 1072828800+364*SECS_PER_DAY);
+ TIME_TEST(2004, 11, 30, 0, 0, 0, 1072828800+365*SECS_PER_DAY);
+ TIME_TEST(2004, 11, 31, 0, 0, 0, 1072828800+366*SECS_PER_DAY);
+ TIME_TEST(2004, 11, 31, 23, 59, 59, 1072828800+367*SECS_PER_DAY-1);
+ TIME_TEST(2100, 11, 31, 0, 0, 0, 4133894400);
+ TIME_TEST(2101, 0, 1, 0, 0, 0, 4133980800);
+
+ /* Test the normalisation functionality of mktime */
+ tm.tm_year = 2000;
+ tm.tm_mon = 1;
+ tm.tm_mday = 10;
+ tm.tm_hour = 5;
+ tm.tm_min = 32;
+ tm.tm_sec = 105;
+ mktime(&tm);
+ assert(tm.tm_year == 2000);
+ assert(tm.tm_mon == 1);
+ assert(tm.tm_mday == 10);
+ assert(tm.tm_hour == 5);
+ assert(tm.tm_min == 33);
+ assert(tm.tm_sec == 45);
+ tm.tm_sec += 366*24*60*60;
+ mktime(&tm);
+ assert(tm.tm_year == 2001);
+ assert(tm.tm_mon == 1);
+ assert(tm.tm_mday == 10);
+ assert(tm.tm_hour == 5);
+ assert(tm.tm_min == 33);
+ assert(tm.tm_sec == 45);
+
+ return 0;
+}