/src/tarantool/third_party/c-dt/dt_util.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright (c) 2012-2015 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 | | |
28 | | bool |
29 | 96.2k | dt_leap_year(int y) { |
30 | 96.2k | return ((y & 3) == 0 && (y % 100 != 0 || y % 400 == 0)); |
31 | 96.2k | } |
32 | | |
33 | | int |
34 | 48.9k | dt_days_in_year(int y) { |
35 | 48.9k | return dt_leap_year(y) ? 366 : 365; |
36 | 48.9k | } |
37 | | |
38 | | int |
39 | 157 | dt_days_in_quarter(int y, int q) { |
40 | 157 | static const int days_in_quarter[2][5] = { |
41 | 157 | { 0, 90, 91, 92, 92 }, |
42 | 157 | { 0, 91, 91, 92, 92 } |
43 | 157 | }; |
44 | 157 | if (q < 1 || q > 4) |
45 | 0 | return 0; |
46 | 157 | return days_in_quarter[dt_leap_year(y)][q]; |
47 | 157 | } |
48 | | |
49 | | int |
50 | 47.1k | dt_days_in_month(int y, int m) { |
51 | 47.1k | static const int days_in_month[2][13] = { |
52 | 47.1k | { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }, |
53 | 47.1k | { 0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 } |
54 | 47.1k | }; |
55 | 47.1k | if (m < 1 || m > 12) |
56 | 0 | return 0; |
57 | 47.1k | return days_in_month[dt_leap_year(y)][m]; |
58 | 47.1k | } |
59 | | |
60 | | int |
61 | 230 | dt_weeks_in_year(int year) { |
62 | 230 | unsigned int y, d; |
63 | 230 | if (year < 1) |
64 | 102 | year += 400 * (1 - year/400); |
65 | 230 | y = year - 1; |
66 | 230 | d = (y + y/4 - y/100 + y/400) % 7; /* [0=Mon, 6=Sun]*/ |
67 | 230 | return (d == 3 || (d == 2 && dt_leap_year(year))) ? 53 : 52; |
68 | 230 | } |
69 | | |