[ci] Restore macos builds
[survex.git] / src / date.c
blob4fd18cd5a1180c5f0b03a9176a2c20e009cf346c
1 /* date.c
2 * Routines for date handling
3 * Copyright (C) 2010,2015,2018 Olly Betts
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 #ifdef HAVE_CONFIG_H
21 # include <config.h>
22 #endif
24 #include "date.h"
26 #include "debug.h"
28 int
29 is_leap_year(int year)
31 return (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
34 unsigned int
35 last_day(int year, int month)
37 static const unsigned char lastday[13] = {
38 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
40 SVX_ASSERT(month >= 1 && month <= 12);
41 return (month == 2 && is_leap_year(year)) ? 29 : lastday[month];
44 int
45 days_since_1900(int y, int m, int d)
47 static const int m_to_d[12] = {
48 0 - (1900 * 365) - 461 + 365,
49 31 - (1900 * 365) - 461 + 365,
50 59 - (1900 * 365) - 461,
51 90 - (1900 * 365) - 461,
52 120 - (1900 * 365) - 461,
53 151 - (1900 * 365) - 461,
54 181 - (1900 * 365) - 461,
55 212 - (1900 * 365) - 461,
56 243 - (1900 * 365) - 461,
57 273 - (1900 * 365) - 461,
58 304 - (1900 * 365) - 461,
59 334 - (1900 * 365) - 461
61 if (m < 3)
62 --y;
63 return d + (y * 365) + m_to_d[m - 1] + (y / 4) - (y / 100) + (y / 400);
66 void
67 ymd_from_days_since_1900(int days, int * py, int * pm, int * pd)
69 int g, dg, c, dc, b, db, a, da, y, m;
70 days += 693901;
71 g = days / 146097;
72 dg = days % 146097;
73 c = (dg / 36524 + 1) * 3 / 4;
74 dc = dg - c * 36524;
75 b = dc / 1461;
76 db = dc % 1461;
77 a = (db / 365 + 1) * 3 / 4;
78 da = db - a * 365;
79 y = g * 400 + c * 100 + b * 4 + a;
80 m = (da * 5 + 308) / 153;
81 *py = y + m / 12;
82 *pm = m % 12 + 1;
83 *pd = da - (m + 2) * 153 / 5 + 123;
86 double
87 julian_date_from_days_since_1900(int days)
89 int g, dg, c, dc, b, db, a, da, y, m;
90 int days_in = days;
91 int dys;
92 double scale;
93 days += 693901;
94 g = days / 146097;
95 dg = days % 146097;
96 c = (dg / 36524 + 1) * 3 / 4;
97 dc = dg - c * 36524;
98 b = dc / 1461;
99 db = dc % 1461;
100 a = (db / 365 + 1) * 3 / 4;
101 da = db - a * 365;
102 y = g * 400 + c * 100 + b * 4 + a;
103 m = (da * 5 + 308) / 153;
104 y = y + m / 12;
105 /* dys is days since 1900 for the start of the year y. */
106 dys = (y - 1900) * 365 - 460;
107 dys += ((y - 1) / 4) - ((y - 1) / 100) + ((y - 1) / 400);
108 scale = (is_leap_year(y) ? 1 / 366.0 : 1 / 365.0);
109 return y + scale * (days_in - dys);