src/dt_arithmetic.c

filter: Statementstmt 11.2% 13 / 116 Branchbran 5.3% 5 / 94 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 8.5% 18 / 210 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« blib/lib/Time/Moment.pm ↑ Summary src/dt_valid.c »
1 /*
2 * Copyright (c) 2012-2014 Christian Hansen <chansen@cpan.org>
3 * <https://github.com/chansen/c-dt>
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright notice, this
10 * list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
19 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26 #include "dt_core.h"
27 #include "dt_util.h"
28 #include "dt_arithmetic.h"
29  
30 dt_t
31 0 dt_add_years(dt_t dt, int delta, dt_adjust_t adjust) {
32 int y, d;
33  
34 0 dt_to_yd(dt, &y, &d);
35 0 ▶ if (adjust == DT_EXCESS || d < 365)
Branch: 2 branches
BranchTrueFalseTotal
if (adjust == DT_EXCESS || d < 365) 0 0 0
if (adjust == DT_EXCESS || d < 365) 0 0 0
36 0 return dt_from_yd(y + delta, d);
37 else {
38 0 int ry = y + delta;
39 int diy;
40  
41 0 diy = dt_days_in_year(ry);
42 0 ▶ if (d > diy || (adjust == DT_SNAP && d == dt_days_in_year(y)))
Branch: 3 branches
BranchTrueFalseTotal
if (d > diy || (adjust == DT_SNAP && d == dt_days_in_year(y))) 0 0 0
if (d > diy || (adjust == DT_SNAP && d == dt_days_in_year(y))) 0 0 0
if (d > diy || (adjust == DT_SNAP && d == dt_days_in_year(y))) 0 0 0
43 0 d = diy;
44 0 return dt_from_yd(ry, d);
45 }
46 }
47  
48 dt_t
49 0 dt_add_quarters(dt_t dt, int delta, dt_adjust_t adjust) {
50 int y, q, d;
51  
52 0 dt_to_yqd(dt, &y, &q, &d);
53 0 ▶ if (adjust == DT_EXCESS || d < 90)
Branch: 2 branches
BranchTrueFalseTotal
if (adjust == DT_EXCESS || d < 90) 0 0 0
if (adjust == DT_EXCESS || d < 90) 0 0 0
54 0 return dt_from_yqd(y, q + delta, d);
55 else {
56 0 int ry = y;
57 0 int rq = q + delta;
58 int diq;
59  
60 0 ▶ if (rq < 1 || rq > 4) {
Branch: 2 branches
BranchTrueFalseTotal
if (rq < 1 || rq > 4) { 0 0 0
if (rq < 1 || rq > 4) { 0 0 0
61 0 ry += rq / 4;
62 0 rq %= 4;
63 0 ▶ if (rq < 1)
Branch: 1 branch
BranchTrueFalseTotal
if (rq < 1) 0 0 0
64 0 ry--, rq += 4;
65 }
66  
67 0 diq = dt_days_in_quarter(ry, rq);
68 0 ▶ if (d > diq || (adjust == DT_SNAP && d == dt_days_in_quarter(y, q)))
Branch: 3 branches
BranchTrueFalseTotal
if (d > diq || (adjust == DT_SNAP && d == dt_days_in_quarter(y, q))) 0 0 0
if (d > diq || (adjust == DT_SNAP && d == dt_days_in_quarter(y, q))) 0 0 0
if (d > diq || (adjust == DT_SNAP && d == dt_days_in_quarter(y, q))) 0 0 0
69 0 d = diq;
70 0 return dt_from_yqd(ry, rq, d);
71 }
72 }
73  
74 dt_t
75 80 dt_add_months(dt_t dt, int delta, dt_adjust_t adjust) {
76 int y, m, d;
77  
78 80 dt_to_ymd(dt, &y, &m, &d);
79 80 ▶ if (adjust == DT_EXCESS || d < 28)
Branch: 2 branches
BranchTrueFalseTotal
if (adjust == DT_EXCESS || d < 28) 80 0 80
if (adjust == DT_EXCESS || d < 28) 80 0 80
80 80 return dt_from_ymd(y, m + delta, d);
81 else {
82 0 int ry = y;
83 0 int rm = m + delta;
84 int dim;
85  
86 0 ▶ if (rm < 1 || rm > 12) {
Branch: 2 branches
BranchTrueFalseTotal
if (rm < 1 || rm > 12) { 0 0 0
if (rm < 1 || rm > 12) { 0 0 0
87 0 ry += rm / 12;
88 0 rm %= 12;
89 0 ▶ if (rm < 1)
Branch: 1 branch
BranchTrueFalseTotal
if (rm < 1) 0 0 0
90 0 ry--, rm += 12;
91 }
92  
93 0 dim = dt_days_in_month(ry, rm);
94 0 ▶ if (d > dim || (adjust == DT_SNAP && d == dt_days_in_month(y, m)))
Branch: 3 branches
BranchTrueFalseTotal
if (d > dim || (adjust == DT_SNAP && d == dt_days_in_month(y, m))) 0 0 0
if (d > dim || (adjust == DT_SNAP && d == dt_days_in_month(y, m))) 0 0 0
if (d > dim || (adjust == DT_SNAP && d == dt_days_in_month(y, m))) 0 0 0
95 0 d = dim;
96 0 return dt_from_ymd(ry, rm, d);
97 }
98 }
99  
100 void
101 0 dt_delta_yd(dt_t dt1, dt_t dt2, int *yp, int *dp) {
102 int y1, y2, d1, d2, ny, nd;
103  
104 0 dt_to_yd(dt1, &y1, &d1);
105 0 dt_to_yd(dt2, &y2, &d2);
106  
107 0 ny = y2 - y1;
108 0 nd = d2 - d1;
109  
110 0 ▶ if (ny > 0 && nd < 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (ny > 0 && nd < 0) { 0 0 0
if (ny > 0 && nd < 0) { 0 0 0
111 0 ny--;
112 0 nd = dt2 - dt_add_years(dt1, ny, DT_LIMIT);
113 }
114 0 ▶ else if (ny < 0 && nd > 0) {
Branch: 2 branches
BranchTrueFalseTotal
else if (ny < 0 && nd > 0) { 0 0 0
else if (ny < 0 && nd > 0) { 0 0 0
115 0 ny++;
116 0 nd -= dt_days_in_year(y2);
117 }
118
119 0 ▶ if (yp) *yp = ny;
Branch: 1 branch
BranchTrueFalseTotal
if (yp) *yp = ny; 0 0 0
120 0 ▶ if (dp) *dp = nd;
Branch: 1 branch
BranchTrueFalseTotal
if (dp) *dp = nd; 0 0 0
121 0 }
122  
123 void
124 0 dt_delta_yqd(dt_t dt1, dt_t dt2, int *yp, int *qp, int *dp) {
125 int y1, y2, q1, q2, d1, d2, ny, nq, nd;
126  
127 0 dt_to_yqd(dt1, &y1, &q1, &d1);
128 0 dt_to_yqd(dt2, &y2, &q2, &d2);
129  
130 0 nq = 4 * (y2 - y1) + q2 - q1;
131 0 nd = d2 - d1;
132  
133 0 ▶ if (nq > 0 && nd < 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (nq > 0 && nd < 0) { 0 0 0
if (nq > 0 && nd < 0) { 0 0 0
134 0 nq--;
135 0 nd = dt2 - dt_add_quarters(dt1, nq, DT_LIMIT);
136 }
137 0 ▶ else if (nq < 0 && nd > 0) {
Branch: 2 branches
BranchTrueFalseTotal
else if (nq < 0 && nd > 0) { 0 0 0
else if (nq < 0 && nd > 0) { 0 0 0
138 0 nq++;
139 0 nd -= dt_days_in_quarter(y2, q2);
140 }
141
142 0 ny = nq / 4;
143 0 nq = nq - ny * 4;
144
145 0 ▶ if (qp) *yp = ny;
Branch: 1 branch
BranchTrueFalseTotal
if (qp) *yp = ny; 0 0 0
146 0 ▶ if (qp) *qp = nq;
Branch: 1 branch
BranchTrueFalseTotal
if (qp) *qp = nq; 0 0 0
147 0 ▶ if (dp) *dp = nd;
Branch: 1 branch
BranchTrueFalseTotal
if (dp) *dp = nd; 0 0 0
148 0 }
149  
150 void
151 0 dt_delta_ymd(dt_t dt1, dt_t dt2, int *yp, int *mp, int *dp) {
152 int y1, y2, m1, m2, d1, d2, ny, nm, nd;
153  
154 0 dt_to_ymd(dt1, &y1, &m1, &d1);
155 0 dt_to_ymd(dt2, &y2, &m2, &d2);
156  
157 0 nm = 12 * (y2 - y1) + m2 - m1;
158 0 nd = d2 - d1;
159  
160 0 ▶ if (nm > 0 && nd < 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (nm > 0 && nd < 0) { 0 0 0
if (nm > 0 && nd < 0) { 0 0 0
161 0 nm--;
162 0 nd = dt2 - dt_add_months(dt1, nm, DT_LIMIT);
163 }
164 0 ▶ else if (nm < 0 && nd > 0) {
Branch: 2 branches
BranchTrueFalseTotal
else if (nm < 0 && nd > 0) { 0 0 0
else if (nm < 0 && nd > 0) { 0 0 0
165 0 nm++;
166 0 nd -= dt_days_in_month(y2, m2);
167 }
168  
169 0 ny = nm / 12;
170 0 nm = nm - ny * 12;
171  
172 0 ▶ if (yp) *yp = ny;
Branch: 1 branch
BranchTrueFalseTotal
if (yp) *yp = ny; 0 0 0
173 0 ▶ if (mp) *mp = nm;
Branch: 1 branch
BranchTrueFalseTotal
if (mp) *mp = nm; 0 0 0
174 0 ▶ if (dp) *dp = nd;
Branch: 1 branch
BranchTrueFalseTotal
if (dp) *dp = nd; 0 0 0
175 0 }
176  
177 int
178 0 dt_delta_years(dt_t dt1, dt_t dt2, bool complete) {
179 int y1, y2, d1, d2, ny;
180  
181 0 dt_to_yd(dt1, &y1, &d1);
182 0 dt_to_yd(dt2, &y2, &d2);
183  
184 0 ny = y2 - y1;
185 0 ▶ if (complete) {
Branch: 1 branch
BranchTrueFalseTotal
if (complete) { 0 0 0
186 0 ▶ if (dt1 > dt2)
Branch: 1 branch
BranchTrueFalseTotal
if (dt1 > dt2) 0 0 0
187 0 ny += (d2 > d1);
188 else
189 0 ny -= (d1 > d2);
190 }
191 0 return ny;
192 }
193  
194 int
195 0 dt_delta_quarters(dt_t dt1, dt_t dt2, bool complete) {
196 int y1, y2, q1, q2, d1, d2, nq;
197  
198 0 dt_to_yqd(dt1, &y1, &q1, &d1);
199 0 dt_to_yqd(dt2, &y2, &q2, &d2);
200  
201 0 nq = 4 * (y2 - y1) + q2 - q1;
202 0 ▶ if (complete) {
Branch: 1 branch
BranchTrueFalseTotal
if (complete) { 0 0 0
203 0 ▶ if (dt1 > dt2)
Branch: 1 branch
BranchTrueFalseTotal
if (dt1 > dt2) 0 0 0
204 0 nq += (d2 > d1);
205 else
206 0 nq -= (d1 > d2);
207 }
208 0 return nq;
209 }
210  
211 int
212 86 dt_delta_months(dt_t dt1, dt_t dt2, bool complete) {
213 int y1, y2, m1, m2, d1, d2, nm;
214  
215 86 dt_to_ymd(dt1, &y1, &m1, &d1);
216 86 dt_to_ymd(dt2, &y2, &m2, &d2);
217  
218 86 nm = 12 * (y2 - y1) + m2 - m1;
219 86 ▶ if (complete) {
Branch: 1 branch
BranchTrueFalseTotal
if (complete) { 86 0 86
220 86 ▶ if (dt1 > dt2)
Branch: 1 branch
BranchTrueFalseTotal
if (dt1 > dt2) 40 46 86
221 40 nm += (d2 > d1);
222 else
223 46 nm -= (d1 > d2);
224 }
225 86 return nm;
226 }
227  
228 int
229 0 dt_delta_weeks(dt_t dt1, dt_t dt2) {
230 0 return (dt2 - dt1) / 7;
231 }
232  
« blib/lib/Time/Moment.pm ↑ Summary src/dt_valid.c »