1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 2006, 2007, 2008, 2010, 2011 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19 #include "data/calendar.h"
24 #include "data/settings.h"
25 #include "data/val-type.h"
28 #define _(msgid) gettext (msgid)
31 #define EPOCH (-577734)
33 /* Calculates and returns floor(a/b) for integer b > 0. */
35 floor_div (int a, int b)
38 return (a >= 0 ? a : a - b + 1) / b;
41 /* Calculates floor(a/b) and the corresponding remainder and
42 stores them into *Q and *R. */
44 floor_divmod (int a, int b, int *q, int *r)
46 *q = floor_div (a, b);
50 /* Returns true if Y is a leap year, false otherwise. */
54 return y % 4 == 0 && (y % 100 != 0 || y % 400 == 0);
58 raw_gregorian_to_offset (int y, int m, int d)
62 + floor_div (y - 1, 4)
63 - floor_div (y - 1, 100)
64 + floor_div (y - 1, 400)
65 + floor_div (367 * m - 362, 12)
66 + (m <= 2 ? 0 : (m >= 2 && is_leap_year (y) ? -1 : -2))
70 /* Returns the number of days from 14 Oct 1582 to (Y,M,D) in the
71 Gregorian calendar. Returns SYSMIS for dates before 14 Oct
74 calendar_gregorian_to_offset (int y, int m, int d,
75 const struct fmt_settings *settings,
79 if (y >= 0 && y < 100)
81 int epoch = fmt_settings_get_epoch (settings);
82 int century = epoch / 100 + (y < epoch % 100);
86 /* Normalize month. */
102 *errorp = xasprintf (_("Month %d is not in acceptable range of "
112 *errorp = xasprintf (_("Day %d is not in acceptable range of "
118 if (y < 1582 || (y == 1582 && (m < 10 || (m == 10 && d < 15))))
121 *errorp = xasprintf (_("Date %04d-%d-%d is before the earliest "
122 "acceptable date of 1582-10-15."), y, m, d);
126 /* Calculate offset. */
129 return raw_gregorian_to_offset (y, m, d);
132 /* Returns the number of days in the given YEAR from January 1 up
133 to (but not including) the first day of MONTH. */
135 cum_month_days (int year, int month)
137 static const int cum_month_days[12] =
142 31 + 28 + 31, /* Mar */
143 31 + 28 + 31 + 30, /* Apr */
144 31 + 28 + 31 + 30 + 31, /* May */
145 31 + 28 + 31 + 30 + 31 + 30, /* Jun */
146 31 + 28 + 31 + 30 + 31 + 30 + 31, /* Jul */
147 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31, /* Aug */
148 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30, /* Sep */
149 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31, /* Oct */
150 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30, /* Nov */
153 assert (month >= 1 && month <= 12);
154 return cum_month_days[month - 1] + (month >= 3 && is_leap_year (year));
157 /* Takes a count of days from 14 Oct 1582 and returns the
158 Gregorian calendar year it is in. Dates both before and after
159 the epoch are supported. */
161 calendar_offset_to_year (int ofs)
171 floor_divmod (d0, 365 * 400 + 100 - 3, &n400, &d1);
172 floor_divmod (d1, 365 * 100 + 25 - 1, &n100, &d2);
173 floor_divmod (d2, 365 * 4 + 1, &n4, &d3);
174 n1 = floor_div (d3, 365);
175 y = 400 * n400 + 100 * n100 + 4 * n4 + n1;
176 if (n100 != 4 && n1 != 4)
182 /* Takes a count of days from 14 Oct 1582 and translates it into
183 a Gregorian calendar date in (*Y,*M,*D). Also stores the
184 year-relative day number into *YD. Dates both before and
185 after the epoch are supported. */
187 calendar_offset_to_gregorian (int ofs, int *y, int *m, int *d, int *yd)
189 int year = *y = calendar_offset_to_year (ofs);
190 int january1 = raw_gregorian_to_offset (year, 1, 1);
191 int yday = *yd = ofs - january1 + 1;
192 int march1 = january1 + cum_month_days (year, 3);
193 int correction = ofs < march1 ? 0 : (is_leap_year (year) ? 1 : 2);
194 int month = *m = (12 * (yday - 1 + correction) + 373) / 367;
195 *d = yday - cum_month_days (year, month);
198 /* Takes a count of days from 14 Oct 1582 and returns the 1-based
199 year-relative day number, that is, the number of days from the
200 beginning of the year. */
202 calendar_offset_to_yday (int ofs)
204 int year = calendar_offset_to_year (ofs);
205 int january1 = raw_gregorian_to_offset (year, 1, 1);
206 int yday = ofs - january1 + 1;
210 /* Takes a count of days from 14 Oct 1582 and returns the
211 corresponding weekday 1...7, with 1=Sunday. */
213 calendar_offset_to_wday (int ofs)
215 int wday = (ofs - EPOCH + 1) % 7 + 1;
221 /* Takes a count of days from 14 Oct 1582 and returns the month
224 calendar_offset_to_month (int ofs)
227 calendar_offset_to_gregorian (ofs, &y, &m, &d, &yd);
231 /* Takes a count of days from 14 Oct 1582 and returns the
232 corresponding day of the month. */
234 calendar_offset_to_mday (int ofs)
237 calendar_offset_to_gregorian (ofs, &y, &m, &d, &yd);
241 /* Returns the number of days in the specified month. */
243 calendar_days_in_month (int y, int m)
245 static const int days_per_month[12]
246 = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
248 assert (m >= 1 && m <= 12);
249 return m == 2 && is_leap_year (y) ? 29 : days_per_month[m - 1];