1 /*** dtest.c -- like test(1) but for dates
2 *
3 * Copyright (C) 2011-2016 Sebastian Freundt
4 *
5 * Author: Sebastian Freundt <freundt@ga-group.nl>
6 *
7 * This file is part of dateutils.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 *
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 *
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 *
20 * 3. Neither the name of the author nor the names of any contributors
21 * may be used to endorse or promote products derived from this
22 * software without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR
25 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
26 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
27 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
31 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
32 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
33 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
34 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 *
36 **/
37
38 #if defined HAVE_CONFIG_H
39 # include "config.h"
40 #endif /* HAVE_CONFIG_H */
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <stdint.h>
44 #include <string.h>
45 #include <sys/time.h>
46 #include <time.h>
47
48 #include "dt-core.h"
49 #include "dt-io.h"
50 #include "dt-locale.h"
51
52 const char *prog = "dtest";
53
54
55 #include "dtest.yucc"
56
57 int
main(int argc,char * argv[])58 main(int argc, char *argv[])
59 {
60 yuck_t argi[1U];
61 char **ifmt;
62 size_t nifmt;
63 struct dt_dt_s d1, d2;
64 zif_t fromz = NULL;
65 int res = 0;
66
67 if (yuck_parse(argi, argc, argv)) {
68 res = 2;
69 goto out;
70 }
71
72
73 if (argi->nargs != 1U + !argi->isvalid_flag) {
74 yuck_auto_help(argi);
75 res = 2;
76 goto out;
77 }
78 if (argi->from_locale_arg) {
79 setilocale(argi->from_locale_arg);
80 }
81 if (argi->from_zone_arg) {
82 fromz = dt_io_zone(argi->from_zone_arg);
83 }
84 if (argi->base_arg) {
85 struct dt_dt_s base = dt_strpdt(argi->base_arg, NULL, NULL);
86 dt_set_base(base);
87 }
88
89 ifmt = argi->input_format_args;
90 nifmt = argi->input_format_nargs;
91
92 if (argi->isvalid_flag) {
93 /* check that one date */
94 res = dt_unk_p(dt_io_strpdt(*argi->args, ifmt, nifmt, fromz));
95 goto out;
96 }
97 if (dt_unk_p(d1 = dt_io_strpdt(argi->args[0U], ifmt, nifmt, fromz))) {
98 if (!argi->quiet_flag) {
99 dt_io_warn_strpdt(argi->args[0U]);
100 }
101 res = 2;
102 goto out;
103 }
104 if (dt_unk_p(d2 = dt_io_strpdt(argi->args[1U], ifmt, nifmt, fromz))) {
105 if (!argi->quiet_flag) {
106 dt_io_warn_strpdt(argi->args[1U]);
107 }
108 res = 2;
109 goto out;
110 }
111
112 /* just do the comparison */
113 if ((res = dt_dtcmp(d1, d2)) == -2) {
114 /* uncomparable */
115 res = 3;
116 } else if (argi->cmp_flag) {
117 switch (res) {
118 case 0:
119 res = 0;
120 break;
121 case -1:
122 res = 2;
123 break;
124 case 1:
125 res = 1;
126 break;
127 default:
128 res = 3;
129 break;
130 }
131 } else if (argi->eq_flag) {
132 res = res == 0 ? 0 : 1;
133 } else if (argi->ne_flag) {
134 res = res != 0 ? 0 : 1;
135 } else if (argi->lt_flag || argi->ot_flag) {
136 res = res == -1 ? 0 : 1;
137 } else if (argi->le_flag) {
138 res = res == -1 || res == 0 ? 0 : 1;
139 } else if (argi->gt_flag || argi->nt_flag) {
140 res = res == 1 ? 0 : 1;
141 } else if (argi->ge_flag) {
142 res = res == 1 || res == 0 ? 0 : 1;
143 }
144 out:
145 if (fromz != NULL) {
146 zif_close(fromz);
147 }
148 if (argi->from_locale_arg) {
149 setilocale(NULL);
150 }
151
152 yuck_free(argi);
153 return res;
154 }
155
156 /* dtest.c ends here */
157