5 #include <data/settings.h>
6 #include <data/val-type.h>
9 #define _(msgid) gettext (msgid)
12 #define EPOCH (-577734)
14 /* Calculates and returns floor(a/b) for integer b > 0. */
16 floor_div (int a, int b)
19 return (a >= 0 ? a : a - b + 1) / b;
22 /* Calculates floor(a/b) and the corresponding remainder and
23 stores them into *Q and *R. */
25 floor_divmod (int a, int b, int *q, int *r)
27 *q = floor_div (a, b);
31 /* Returns true if Y is a leap year, false otherwise. */
35 return y % 4 == 0 && (y % 100 != 0 || y % 400 == 0);
39 raw_gregorian_to_offset (int y, int m, int d)
43 + floor_div (y - 1, 4)
44 - floor_div (y - 1, 100)
45 + floor_div (y - 1, 400)
46 + floor_div (367 * m - 362, 12)
47 + (m <= 2 ? 0 : (m >= 2 && is_leap_year (y) ? -1 : -2))
51 /* Returns the number of days from 14 Oct 1582 to (Y,M,D) in the
52 Gregorian calendar. Returns SYSMIS for dates before 14 Oct
55 calendar_gregorian_to_offset (int y, int m, int d, char **errorp)
58 if (y >= 0 && y < 100)
60 int epoch = settings_get_epoch ();
61 int century = epoch / 100 + (y < epoch % 100);
65 /* Normalize month. */
81 *errorp = xasprintf (_("Month %d is not in acceptable range of "
91 *errorp = xasprintf (_("Day %d is not in acceptable range of "
97 if (y < 1582 || (y == 1582 && (m < 10 || (m == 10 && d < 15))))
100 *errorp = xasprintf (_("Date %04d-%d-%d is before the earliest "
101 "acceptable date of 1582-10-15."), y, m, d);
105 /* Calculate offset. */
108 return raw_gregorian_to_offset (y, m, d);
111 /* Returns the number of days in the given YEAR from January 1 up
112 to (but not including) the first day of MONTH. */
114 cum_month_days (int year, int month)
116 static const int cum_month_days[12] =
121 31 + 28 + 31, /* Mar */
122 31 + 28 + 31 + 30, /* Apr */
123 31 + 28 + 31 + 30 + 31, /* May */
124 31 + 28 + 31 + 30 + 31 + 30, /* Jun */
125 31 + 28 + 31 + 30 + 31 + 30 + 31, /* Jul */
126 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31, /* Aug */
127 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30, /* Sep */
128 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31, /* Oct */
129 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30, /* Nov */
132 assert (month >= 1 && month <= 12);
133 return cum_month_days[month - 1] + (month >= 3 && is_leap_year (year));
136 /* Takes a count of days from 14 Oct 1582 and returns the
137 Gregorian calendar year it is in. Dates both before and after
138 the epoch are supported. */
140 calendar_offset_to_year (int ofs)
150 floor_divmod (d0, 365 * 400 + 100 - 3, &n400, &d1);
151 floor_divmod (d1, 365 * 100 + 25 - 1, &n100, &d2);
152 floor_divmod (d2, 365 * 4 + 1, &n4, &d3);
153 n1 = floor_div (d3, 365);
154 y = 400 * n400 + 100 * n100 + 4 * n4 + n1;
155 if (n100 != 4 && n1 != 4)
161 /* Takes a count of days from 14 Oct 1582 and translates it into
162 a Gregorian calendar date in (*Y,*M,*D). Also stores the
163 year-relative day number into *YD. Dates both before and
164 after the epoch are supported. */
166 calendar_offset_to_gregorian (int ofs, int *y, int *m, int *d, int *yd)
168 int year = *y = calendar_offset_to_year (ofs);
169 int january1 = raw_gregorian_to_offset (year, 1, 1);
170 int yday = *yd = ofs - january1 + 1;
171 int march1 = january1 + cum_month_days (year, 3);
172 int correction = ofs < march1 ? 0 : (is_leap_year (year) ? 1 : 2);
173 int month = *m = (12 * (yday - 1 + correction) + 373) / 367;
174 *d = yday - cum_month_days (year, month);
177 /* Takes a count of days from 14 Oct 1582 and returns the 1-based
178 year-relative day number, that is, the number of days from the
179 beginning of the year. */
181 calendar_offset_to_yday (int ofs)
183 int year = calendar_offset_to_year (ofs);
184 int january1 = raw_gregorian_to_offset (year, 1, 1);
185 int yday = ofs - january1 + 1;
189 /* Takes a count of days from 14 Oct 1582 and returns the
190 corresponding weekday 1...7, with 1=Sunday. */
192 calendar_offset_to_wday (int ofs)
194 int wday = (ofs - EPOCH + 1) % 7 + 1;
200 /* Takes a count of days from 14 Oct 1582 and returns the month
203 calendar_offset_to_month (int ofs)
206 calendar_offset_to_gregorian (ofs, &y, &m, &d, &yd);
210 /* Takes a count of days from 14 Oct 1582 and returns the
211 corresponding day of the month. */
213 calendar_offset_to_mday (int ofs)
216 calendar_offset_to_gregorian (ofs, &y, &m, &d, &yd);
220 /* Returns the number of days in the specified month. */
222 calendar_days_in_month (int y, int m)
224 static const int days_per_month[12]
225 = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
227 assert (m >= 1 && m <= 12);
228 return m == 2 && is_leap_year (y) ? 29 : days_per_month[m - 1];