*** /dev/null Sat Nov 23 01:24:58 2024
--- - Sat Nov 23 01:25:09 2024
***************
*** 0 ****
--- 1,138 ----
+ /*
+ ** OSSP tai - Time Handling
+ ** Copyright (c) 2002 Ralf S. Engelschall <rse@engelschall.com>
+ ** Copyright (c) 2002 The OSSP Project <http://www.ossp.org/>
+ ** Copyright (c) 2002 Cable & Wireless Deutschland <http://www.cw.com/de/>
+ **
+ ** This file is part of OSSP tai, a time handling library
+ ** which can be found at http://www.ossp.org/pkg/lib/tai/.
+ **
+ ** Permission to use, copy, modify, and distribute this software for
+ ** any purpose with or without fee is hereby granted, provided that
+ ** the above copyright notice and this permission notice appear in all
+ ** copies.
+ **
+ ** THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
+ ** WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+ ** MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ ** IN NO EVENT SHALL THE AUTHORS AND COPYRIGHT HOLDERS AND THEIR
+ ** CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
+ ** USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ ** ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ ** OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+ ** OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ ** SUCH DAMAGE.
+ **
+ ** tai_test.c: test suite
+ */
+
+ #include <stdio.h>
+ #include <stdlib.h>
+ #include <string.h>
+ #include <time.h>
+
+ #include "ts.h"
+ #include "tai.h"
+
+ TS_TEST(test_object)
+ {
+ tai_t *tm;
+ tai_rc_t rv;
+
+ ts_test_check(TS_CTX, "object creation");
+ if ((rv = tai_create(&tm)) != TAI_OK) {
+ ts_test_fail(TS_CTX, "tai_create: rv=%d", rv);
+ return;
+ }
+
+ ts_test_check(TS_CTX, "object destruction");
+ if ((rv = tai_destroy(tm)) != TAI_OK)
+ ts_test_fail(TS_CTX, "tai_destroy: rv=%d", rv);
+ }
+
+ TS_TEST(test_importexport)
+ {
+ tai_t *tm;
+ tai_rc_t rv;
+ time_t t;
+ time_t t2;
+
+ ts_test_check(TS_CTX, "time import");
+ tai_create(&tm);
+ if ((rv = tai_import(tm, TAI_TYPE_TIMET, 0)) != TAI_OK)
+ ts_test_fail(TS_CTX, "tai_import: rv=%d", rv);
+ if ((rv = tai_export(tm, TAI_TYPE_TIMET, &t)) != TAI_OK)
+ ts_test_fail(TS_CTX, "tai_export: rv=%d", rv);
+ if (t != 0)
+ ts_test_fail(TS_CTX, "unexpected time %d (!= 0)", t);
+ t = time(NULL);
+ if ((rv = tai_import(tm, TAI_TYPE_TIMET, t)) != TAI_OK)
+ ts_test_fail(TS_CTX, "tai_import: rv=%d", rv);
+ if ((rv = tai_export(tm, TAI_TYPE_TIMET, &t2)) != TAI_OK)
+ ts_test_fail(TS_CTX, "tai_export: rv=%d", rv);
+ if (t != t2)
+ ts_test_fail(TS_CTX, "unexpected time %d (!= %d)", t2, t);
+ tai_destroy(tm);
+ }
+
+ TS_TEST(test_parsing)
+ {
+ tai_t *tm;
+ tai_rc_t rv;
+ char out[32];
+ int i;
+ struct {
+ char *date;
+ char *fmt;
+ } table[] = {
+ { "2000-02-22", "%Y-%m-%d" },
+ { "22-02-2000", "%d-%m-%Y" },
+ { "22-Feb-2000", "%d-%b-%Y" },
+ { "29-Feb-2000", "%d-%b-%Y" },
+ { "31-Feb-2000", "%d-%b-%Y" },
+ { NULL, NULL }
+ };
+
+ ts_test_check(TS_CTX, "time parsing");
+ tai_create(&tm);
+ for (i = 0; table[i].date != NULL; i++) {
+ ts_test_log(TS_CTX, "date=%s, fmt=%s", table[i].date, table[i].fmt);
+ if ((rv = tai_parse(tm, table[i].date, strlen(table[i].date), table[i].fmt)) != TAI_OK)
+ ts_test_fail(TS_CTX, "#%d: tai_parse(\"%s\"): rv=%d", i, table[i].date, rv);
+ else {
+ if ((rv = tai_format(tm, out, sizeof(out), table[i].fmt)) != TAI_OK)
+ ts_test_fail(TS_CTX, "#%d: tai_format(\"%s\"): rv=%d", i, table[i].date, rv);
+ if (strcmp(table[i].date, out) != 0)
+ ts_test_fail(TS_CTX, "#%d: output \"%s\", expected \"%s\" (input)", i, out, table[i].date);
+ }
+ }
+ tai_destroy(tm);
+ }
+
+ TS_TEST(test_formatting)
+ {
+ tai_t *tm;
+
+ ts_test_check(TS_CTX, "time formatting");
+ tai_create(&tm);
+ /* FIXME */
+ tai_destroy(tm);
+ }
+
+ int main(int argc, char *argv[])
+ {
+ ts_suite_t *ts;
+ int n;
+
+ ts = ts_suite_new("OSSP tai (Time Handling)");
+ ts_suite_test(ts, test_object, "object handling");
+ ts_suite_test(ts, test_importexport, "time import/export");
+ ts_suite_test(ts, test_parsing, "time parsing");
+ ts_suite_test(ts, test_formatting, "time formatting");
+ n = ts_suite_run(ts);
+ ts_suite_free(ts);
+ return n;
+ }
+
|