1 /*
2 * Copyright (c) 2017 Daichi GOTO
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28 #include "command.h"
29
30 static void check_t(const char *, const char *, const char *);
31
32 int
main(int argc,char * argv[])33 main(int argc, char *argv[])
34 {
35 getcmdargs(argc, argv, "f:pshvD",
36 CMDARGS_R_NONE|CMDARGS_A_NEED|CMDARGS_F_NONE);
37
38 if (2 != A_ARGC)
39 usage();
40
41 const char *in_fmt = "%Y%m%d";
42 if (FLAG_f)
43 in_fmt = FLAG_f_ARG;
44
45 const char *date_B = A_ARGV[1];
46 const char *date_A = A_ARGV[2];
47
48 time_t tval_A, tval_B;
49 struct tm lt_A, lt_B;
50 const char *t;
51
52 (void)setlocale(LC_TIME, "");
53 time(&tval_A);
54 lt_A = *localtime(&tval_A);
55 strptime("000000", "%H%M%S", <_A);
56
57 tval_B = tval_A;
58 lt_B = lt_A;
59
60 t = strptime(date_A, in_fmt, <_A);
61 check_t(t, date_A, in_fmt);
62 tval_A = mktime(<_A);
63
64 t = strptime(date_B, in_fmt, <_B);
65 check_t(t, date_B, in_fmt);
66 tval_B = mktime(<_B);
67
68 long sec = tval_A - tval_B;
69
70 if (FLAG_p && sec >= 0)
71 putchar('+');
72 if (FLAG_s)
73 /* output in seconds */
74 printf("%ld\n", sec);
75 else
76 /* output in days */
77 printf("%ld\n", sec / 60 / 60 / 24);
78
79 exit(EX_OK);
80 }
81
82 static void
check_t(const char * t,const char * date,const char * fmt)83 check_t(const char *t, const char *date, const char *fmt)
84 {
85 /*
86 * Copied from date.c of FreeBSD 11.0-RELEASE and modified.
87 */
88 if (t == NULL) {
89 fprintf(stderr, "Failed conversion of ``%s''"
90 " using format ``%s''\n", date, fmt);
91 warnx("illegal time format");
92 usage();
93 }
94 else if ('\0' != *t)
95 fprintf(stderr, "Warning: Ignoring %ld extraneous"
96 " characters in date string (%s)\n",
97 (long) strlen(t), t);
98 /*-
99 * Copyright (c) 1985, 1987, 1988, 1993
100 * The Regents of the University of California. All rights reserved.
101 *
102 * Redistribution and use in source and binary forms, with or without
103 * modification, are permitted provided that the following conditions
104 * are met:
105 * 1. Redistributions of source code must retain the above copyright
106 * notice, this list of conditions and the following disclaimer.
107 * 2. Redistributions in binary form must reproduce the above copyright
108 * notice, this list of conditions and the following disclaimer in the
109 * documentation and/or other materials provided with the distribution.
110 * 4. Neither the name of the University nor the names of its contributors
111 * may be used to endorse or promote products derived from this software
112 * without specific prior written permission.
113 *
114 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
115 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
116 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
117 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
118 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
119 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
120 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
121 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
122 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
123 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
124 * SUCH DAMAGE.
125 */
126 }
127