src/dt_core.c

filter: Statementstmt 86.7% 92 / 106 Branchbran 65.7% 75 / 114 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 75.9% 167 / 220 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« src/dt_length.c ↑ Summary src/moment_parse.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 <assert.h>
27 #include "dt_core.h"
28  
29 #define LEAP_YEAR(y) \
30 (((y) & 3) == 0 && ((y) % 100 != 0 || (y) % 400 == 0))
31  
32 #define DAYS_IN_YEAR(y) \
33 (LEAP_YEAR(y) ? 366 : 365)
34  
35 static const int days_preceding_month[2][13] = {
36 { 0, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 },
37 { 0, 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335 }
38 };
39  
40 static const int days_preceding_quarter[2][5] = {
41 { 0, 0, 90, 181, 273 },
42 { 0, 0, 91, 182, 274 }
43 };
44  
45 dt_t
46 85818 dt_from_rdn(int n) {
47 85818 return n + DT_EPOCH_OFFSET;
48 }
49  
50 dt_t
51 8462 dt_from_yd(int y, int d) {
52 8462 y--;
53 8462 ▶ if (y < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (y < 0) { 0 8462 8462
54 0 const int n400 = 1 - y/400;
55 0 y += n400 * 400;
56 0 d -= n400 * 146097;
57 }
58 8462 return 365 * y + y/4 - y/100 + y/400 + d + DT_EPOCH_OFFSET;
59 }
60  
61 dt_t
62 7960 dt_from_ymd(int y, int m, int d) {
63 7960 ▶ if (m < 1 || m > 12) {
Branch: 2 branches
BranchTrueFalseTotal
if (m < 1 || m > 12) { 7924 36 7960
if (m < 1 || m > 12) { 288 7636 7924
64 324 y += m / 12;
65 324 m %= 12;
66 324 ▶ if (m < 1)
Branch: 1 branch
BranchTrueFalseTotal
if (m < 1) 58 266 324
67 58 y--, m += 12;
68 }
69 7960 ▶ assert(m >= 1);
Branch: 1 branch
BranchTrueFalseTotal
assert(m >= 1); 0 7960 7960
70 7960 ▶ assert(m <= 12);
Branch: 1 branch
BranchTrueFalseTotal
assert(m <= 12); 0 7960 7960
71 7960 ▶ return dt_from_yd(y, days_preceding_month[LEAP_YEAR(y)][m] + d);
Branch: 3 branches
BranchTrueFalseTotal
return dt_from_yd(y, days_preceding_month[LEAP_YEAR(y)][m] + d); 6369 1591 7960
return dt_from_yd(y, days_preceding_month[LEAP_YEAR(y)][m] + d); 103 6266 6369
return dt_from_yd(y, days_preceding_month[LEAP_YEAR(y)][m] + d); 87 16 103
72 }
73  
74 dt_t
75 111 dt_from_yqd(int y, int q, int d) {
76 111 ▶ if (q < 1 || q > 4) {
Branch: 2 branches
BranchTrueFalseTotal
if (q < 1 || q > 4) { 111 0 111
if (q < 1 || q > 4) { 3 108 111
77 3 y += q / 4;
78 3 q %= 4;
79 3 ▶ if (q < 1)
Branch: 1 branch
BranchTrueFalseTotal
if (q < 1) 0 3 3
80 0 y--, q += 4;
81 }
82 111 ▶ assert(q >= 1);
Branch: 1 branch
BranchTrueFalseTotal
assert(q >= 1); 0 111 111
83 111 ▶ assert(q <= 4);
Branch: 1 branch
BranchTrueFalseTotal
assert(q <= 4); 0 111 111
84 111 ▶ return dt_from_yd(y, days_preceding_quarter[LEAP_YEAR(y)][q] + d);
Branch: 3 branches
BranchTrueFalseTotal
return dt_from_yd(y, days_preceding_quarter[LEAP_YEAR(y)][q] + d); 101 10 111
return dt_from_yd(y, days_preceding_quarter[LEAP_YEAR(y)][q] + d); 0 101 101
return dt_from_yd(y, days_preceding_quarter[LEAP_YEAR(y)][q] + d); 0 0 0
85 }
86  
87 dt_t
88 9 dt_from_ywd(int y, int w, int d) {
89 dt_t dt;
90  
91 9 dt = dt_from_yd(y, 4);
92 9 dt -= dt_dow(dt);
93 9 dt += w * 7 + d - 7;
94 9 return dt;
95 }
96  
97  
98 #ifndef DT_NO_SHORTCUTS
99 static const dt_t DT1901 = 693961 + DT_EPOCH_OFFSET; /* 1901-01-01 */
100 static const dt_t DT2099 = 766644 + DT_EPOCH_OFFSET; /* 2099-12-31 */
101 #endif
102  
103 void
104 73426 dt_to_yd(dt_t d, int *yp, int *dp) {
105 int y, n100, n1;
106  
107 73426 y = 0;
108 #ifndef DT_NO_SHORTCUTS
109 /* Shortcut dates between the years 1901-2099 inclusive */
110 73426 ▶ if (d >= DT1901 && d <= DT2099) {
Branch: 2 branches
BranchTrueFalseTotal
if (d >= DT1901 && d <= DT2099) { 71770 1656 73426
if (d >= DT1901 && d <= DT2099) { 70295 1475 71770
111 70295 d -= DT1901 - 1;
112 70295 y += (4 * d - 1) / 1461;
113 70295 d -= (1461 * y) / 4;
114 70295 y += 1901;
115 }
116 else
117 #endif
118 {
119 3131 d -= DT_EPOCH_OFFSET;
120 3131 ▶ if (d < 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (d < 1) { 0 3131 3131
121 0 const int n400 = 1 - d/146097;
122 0 y -= n400 * 400;
123 0 d += n400 * 146097;
124 }
125 3131 d--;
126 3131 y += 400 * (d / 146097);
127 3131 d %= 146097;
128  
129 3131 n100 = d / 36524;
130 3131 y += 100 * n100;
131 3131 d %= 36524;
132  
133 3131 y += 4 * (d / 1461);
134 3131 d %= 1461;
135  
136 3131 n1 = d / 365;
137 3131 y += n1;
138 3131 d %= 365;
139  
140 3131 ▶ if (n100 == 4 || n1 == 4)
Branch: 2 branches
BranchTrueFalseTotal
if (n100 == 4 || n1 == 4) 3131 0 3131
if (n100 == 4 || n1 == 4) 0 3131 3131
141 0 d = 366;
142 else
143 3131 y++, d++;
144 }
145 73426 ▶ if (yp) *yp = y;
Branch: 1 branch
BranchTrueFalseTotal
if (yp) *yp = y; 73013 413 73426
146 73426 ▶ if (dp) *dp = (int)d;
Branch: 1 branch
BranchTrueFalseTotal
if (dp) *dp = (int)d; 69274 4152 73426
147 73426 }
148  
149 void
150 68592 dt_to_ymd(dt_t dt, int *yp, int *mp, int *dp) {
151 int y, doy, m, l;
152  
153 68592 dt_to_yd(dt, &y, &doy);
154 68592 ▶ l = LEAP_YEAR(y);
Branch: 3 branches
BranchTrueFalseTotal
l = LEAP_YEAR(y); 63971 4621 68592
l = LEAP_YEAR(y); 556 63415 63971
l = LEAP_YEAR(y); 504 52 556
155 68592 ▶ m = doy < 32 ? 1 : 1 + (5 * (doy - 59 - l) + 303) / 153;
Branch: 1 branch
BranchTrueFalseTotal
m = doy < 32 ? 1 : 1 + (5 * (doy - 59 - l) + 303) / 153; 61089 7503 68592
156  
157 68592 ▶ assert(m >= 1);
Branch: 1 branch
BranchTrueFalseTotal
assert(m >= 1); 0 68592 68592
158 68592 ▶ assert(m <= 12);
Branch: 1 branch
BranchTrueFalseTotal
assert(m <= 12); 0 68592 68592
159  
160 68592 ▶ if (yp) *yp = y;
Branch: 1 branch
BranchTrueFalseTotal
if (yp) *yp = y; 37909 30683 68592
161 68592 ▶ if (mp) *mp = m;
Branch: 1 branch
BranchTrueFalseTotal
if (mp) *mp = m; 67621 971 68592
162 68592 ▶ if (dp) *dp = doy - days_preceding_month[l][m];
Branch: 1 branch
BranchTrueFalseTotal
if (dp) *dp = doy - days_preceding_month[l][m]; 32863 35729 68592
163 68592 }
164  
165 void
166 221 dt_to_yqd(dt_t dt, int *yp, int *qp, int *dp) {
167 int y, doy, q, l;
168  
169 221 dt_to_yd(dt, &y, &doy);
170 221 ▶ l = LEAP_YEAR(y);
Branch: 3 branches
BranchTrueFalseTotal
l = LEAP_YEAR(y); 203 18 221
l = LEAP_YEAR(y); 0 203 203
l = LEAP_YEAR(y); 0 0 0
171 221 ▶ q = doy < 91 ? 1 : 1 + (5 * (doy - 59 - l) + 303) / 459;
Branch: 1 branch
BranchTrueFalseTotal
q = doy < 91 ? 1 : 1 + (5 * (doy - 59 - l) + 303) / 459; 211 10 221
172  
173 221 ▶ assert(q >= 1);
Branch: 1 branch
BranchTrueFalseTotal
assert(q >= 1); 0 221 221
174 221 ▶ assert(q <= 4);
Branch: 1 branch
BranchTrueFalseTotal
assert(q <= 4); 0 221 221
175  
176 221 ▶ if (yp) *yp = y;
Branch: 1 branch
BranchTrueFalseTotal
if (yp) *yp = y; 119 102 221
177 221 ▶ if (qp) *qp = q;
Branch: 1 branch
BranchTrueFalseTotal
if (qp) *qp = q; 126 95 221
178 221 ▶ if (dp) *dp = doy - days_preceding_quarter[l][q];
Branch: 1 branch
BranchTrueFalseTotal
if (dp) *dp = doy - days_preceding_quarter[l][q]; 95 126 221
179 221 }
180  
181 void
182 48 dt_to_ywd(dt_t dt, int *yp, int *wp, int *dp) {
183 int y, doy, dow;
184  
185 48 dt_to_yd(dt, &y, &doy);
186 48 dow = dt_dow(dt);
187 48 doy = doy + 4 - dow;
188 48 ▶ if (doy < 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (doy < 1) { 0 48 48
189 0 y--;
190 0 ▶ doy += DAYS_IN_YEAR(y);
Branch: 3 branches
BranchTrueFalseTotal
doy += DAYS_IN_YEAR(y); 0 0 0
doy += DAYS_IN_YEAR(y); 0 0 0
doy += DAYS_IN_YEAR(y); 0 0 0
191 }
192 48 ▶ else if (doy > 365) {
Branch: 1 branch
BranchTrueFalseTotal
else if (doy > 365) { 0 48 48
193 0 ▶ const int diy = DAYS_IN_YEAR(y);
Branch: 3 branches
BranchTrueFalseTotal
const int diy = DAYS_IN_YEAR(y); 0 0 0
const int diy = DAYS_IN_YEAR(y); 0 0 0
const int diy = DAYS_IN_YEAR(y); 0 0 0
194 0 ▶ if (doy > diy) {
Branch: 1 branch
BranchTrueFalseTotal
if (doy > diy) { 0 0 0
195 0 doy -= diy;
196 0 y++;
197 }
198 }
199 48 ▶ if (yp) *yp = y;
Branch: 1 branch
BranchTrueFalseTotal
if (yp) *yp = y; 28 20 48
200 48 ▶ if (wp) *wp = (doy + 6) / 7;
Branch: 1 branch
BranchTrueFalseTotal
if (wp) *wp = (doy + 6) / 7; 20 28 48
201 48 ▶ if (dp) *dp = dow;
Branch: 1 branch
BranchTrueFalseTotal
if (dp) *dp = dow; 0 48 48
202 48 }
203  
204 int
205 8896 dt_rdn(dt_t dt) {
206 8896 return dt - DT_EPOCH_OFFSET;
207 }
208  
209 dt_dow_t
210 11929 dt_dow(dt_t dt) {
211 11929 int dow = (dt - DT_EPOCH_OFFSET) % 7;
212 11929 ▶ if (dow < 1)
Branch: 1 branch
BranchTrueFalseTotal
if (dow < 1) 1825 10104 11929
213 1825 dow += 7;
214 11929 ▶ assert(dow >= 1);
Branch: 1 branch
BranchTrueFalseTotal
assert(dow >= 1); 0 11929 11929
215 11929 ▶ assert(dow <= 7);
Branch: 1 branch
BranchTrueFalseTotal
assert(dow <= 7); 0 11929 11929
216 11929 return dow;
217 }
218  
« src/dt_length.c ↑ Summary src/moment_parse.c »