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,
56 calendar_error_func *error, void *aux)
59 if (y >= 0 && y < 100)
61 int epoch = settings_get_epoch ();
62 int century = epoch / 100 + (y < epoch % 100);
66 /* Normalize month. */
81 error (aux, _("Month %d is not in acceptable range of 0 to 13."), m);
89 error (aux, _("Day %d is not in acceptable range of 0 to 31."), d);
94 if (y < 1582 || (y == 1582 && (m < 10 || (m == 10 && d < 15))))
96 error (aux, _("Date %04d-%d-%d is before the earliest acceptable "
97 "date of 1582-10-15."), y, m, d);
101 /* Calculate offset. */
102 return raw_gregorian_to_offset (y, m, d);
105 /* Returns the number of days in the given YEAR from January 1 up
106 to (but not including) the first day of MONTH. */
108 cum_month_days (int year, int month)
110 static const int cum_month_days[12] =
115 31 + 28 + 31, /* Mar */
116 31 + 28 + 31 + 30, /* Apr */
117 31 + 28 + 31 + 30 + 31, /* May */
118 31 + 28 + 31 + 30 + 31 + 30, /* Jun */
119 31 + 28 + 31 + 30 + 31 + 30 + 31, /* Jul */
120 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31, /* Aug */
121 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30, /* Sep */
122 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31, /* Oct */
123 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30, /* Nov */
126 assert (month >= 1 && month <= 12);
127 return cum_month_days[month - 1] + (month >= 3 && is_leap_year (year));
130 /* Takes a count of days from 14 Oct 1582 and returns the
131 Gregorian calendar year it is in. Dates both before and after
132 the epoch are supported. */
134 calendar_offset_to_year (int ofs)
144 floor_divmod (d0, 365 * 400 + 100 - 3, &n400, &d1);
145 floor_divmod (d1, 365 * 100 + 25 - 1, &n100, &d2);
146 floor_divmod (d2, 365 * 4 + 1, &n4, &d3);
147 n1 = floor_div (d3, 365);
148 y = 400 * n400 + 100 * n100 + 4 * n4 + n1;
149 if (n100 != 4 && n1 != 4)
155 /* Takes a count of days from 14 Oct 1582 and translates it into
156 a Gregorian calendar date in (*Y,*M,*D). Also stores the
157 year-relative day number into *YD. Dates both before and
158 after the epoch are supported. */
160 calendar_offset_to_gregorian (int ofs, int *y, int *m, int *d, int *yd)
162 int year = *y = calendar_offset_to_year (ofs);
163 int january1 = raw_gregorian_to_offset (year, 1, 1);
164 int yday = *yd = ofs - january1 + 1;
165 int march1 = january1 + cum_month_days (year, 3);
166 int correction = ofs < march1 ? 0 : (is_leap_year (year) ? 1 : 2);
167 int month = *m = (12 * (yday - 1 + correction) + 373) / 367;
168 *d = yday - cum_month_days (year, month);
171 /* Takes a count of days from 14 Oct 1582 and returns the 1-based
172 year-relative day number, that is, the number of days from the
173 beginning of the year. */
175 calendar_offset_to_yday (int ofs)
177 int year = calendar_offset_to_year (ofs);
178 int january1 = raw_gregorian_to_offset (year, 1, 1);
179 int yday = ofs - january1 + 1;
183 /* Takes a count of days from 14 Oct 1582 and returns the
184 corresponding weekday 1...7, with 1=Sunday. */
186 calendar_offset_to_wday (int ofs)
188 int wday = (ofs - EPOCH + 1) % 7 + 1;
194 /* Takes a count of days from 14 Oct 1582 and returns the month
197 calendar_offset_to_month (int ofs)
200 calendar_offset_to_gregorian (ofs, &y, &m, &d, &yd);
204 /* Takes a count of days from 14 Oct 1582 and returns the
205 corresponding day of the month. */
207 calendar_offset_to_mday (int ofs)
210 calendar_offset_to_gregorian (ofs, &y, &m, &d, &yd);
214 /* Returns the number of days in the specified month. */
216 calendar_days_in_month (int y, int m)
218 static const int days_per_month[12]
219 = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
221 assert (m >= 1 && m <= 12);
222 return m == 2 && is_leap_year (y) ? 29 : days_per_month[m - 1];