eb9e67250a733394739f997ab1c7b504627b1b50
[pspp] / src / data / calendar.c
1 #include <config.h>
2 #include "calendar.h"
3 #include <assert.h>
4 #include <stdbool.h>
5 #include <data/settings.h>
6 #include <data/val-type.h>
7
8 #include "gettext.h"
9 #define _(msgid) gettext (msgid)
10
11 /* 14 Oct 1582. */
12 #define EPOCH (-577734)
13
14 /* Calculates and returns floor(a/b) for integer b > 0. */
15 static int
16 floor_div (int a, int b)
17 {
18   assert (b > 0);
19   return (a >= 0 ? a : a - b + 1) / b;
20 }
21
22 /* Calculates floor(a/b) and the corresponding remainder and
23    stores them into *Q and *R. */
24 static void
25 floor_divmod (int a, int b, int *q, int *r)
26 {
27   *q = floor_div (a, b);
28   *r = a - b * *q;
29 }
30
31 /* Returns true if Y is a leap year, false otherwise. */
32 static bool
33 is_leap_year (int y)
34 {
35   return y % 4 == 0 && (y % 100 != 0 || y % 400 == 0);
36 }
37
38 static int
39 raw_gregorian_to_offset (int y, int m, int d)
40 {
41   return (EPOCH - 1
42           + 365 * (y - 1)
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))
48           + d);
49 }
50
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
53    1582. */
54 double
55 calendar_gregorian_to_offset (int y, int m, int d,
56                               calendar_error_func *error, void *aux)
57 {
58   /* Normalize year. */
59   if (y >= 0 && y < 100)
60     {
61       int epoch = settings_get_epoch ();
62       int century = epoch / 100 + (y < epoch % 100);
63       y += century * 100;
64     }
65
66   /* Normalize month. */
67   if (m < 1 || m > 12)
68     {
69       if (m == 0)
70         {
71           y--;
72           m = 12;
73         }
74       else if (m == 13)
75         {
76           y++;
77           m = 1;
78         }
79       else
80         {
81           error (aux, _("Month %d is not in acceptable range of 0 to 13."), m);
82           return SYSMIS;
83         }
84     }
85
86   /* Normalize day. */
87   if (d < 0 || d > 31)
88     {
89       error (aux, _("Day %d is not in acceptable range of 0 to 31."), d);
90       return SYSMIS;
91     }
92
93   /* Validate date. */
94   if (y < 1582 || (y == 1582 && (m < 10 || (m == 10 && d < 15))))
95     {
96       error (aux, _("Date %04d-%d-%d is before the earliest acceptable "
97                     "date of 1582-10-15."), y, m, d);
98       return SYSMIS;
99     }
100
101   /* Calculate offset. */
102   return raw_gregorian_to_offset (y, m, d);
103 }
104
105 /* Returns the number of days in the given YEAR from January 1 up
106    to (but not including) the first day of MONTH. */
107 static int
108 cum_month_days (int year, int month)
109 {
110   static const int cum_month_days[12] =
111     {
112       0,
113       31, /* Jan */
114       31 + 28, /* Feb */
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 */
124     };
125
126   assert (month >= 1 && month <= 12);
127   return cum_month_days[month - 1] + (month >= 3 && is_leap_year (year));
128 }
129
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. */
133 int
134 calendar_offset_to_year (int ofs)
135 {
136   int d0;
137   int n400, d1;
138   int n100, d2;
139   int n4, d3;
140   int n1;
141   int y;
142
143   d0 = ofs - EPOCH;
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)
150     y++;
151
152   return y;
153 }
154
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. */
159 void
160 calendar_offset_to_gregorian (int ofs, int *y, int *m, int *d, int *yd)
161 {
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);
169 }
170
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. */
174 int
175 calendar_offset_to_yday (int ofs)
176 {
177   int year = calendar_offset_to_year (ofs);
178   int january1 = raw_gregorian_to_offset (year, 1, 1);
179   int yday = ofs - january1 + 1;
180   return yday;
181 }
182
183 /* Takes a count of days from 14 Oct 1582 and returns the
184    corresponding weekday 1...7, with 1=Sunday. */
185 int
186 calendar_offset_to_wday (int ofs)
187 {
188   int wday = (ofs - EPOCH + 1) % 7 + 1;
189   if (wday <= 0)
190     wday += 7;
191   return wday;
192 }
193
194 /* Takes a count of days from 14 Oct 1582 and returns the month
195    it is in. */
196 int
197 calendar_offset_to_month (int ofs)
198 {
199   int y, m, d, yd;
200   calendar_offset_to_gregorian (ofs, &y, &m, &d, &yd);
201   return m;
202 }
203
204 /* Takes a count of days from 14 Oct 1582 and returns the
205    corresponding day of the month. */
206 int
207 calendar_offset_to_mday (int ofs)
208 {
209   int y, m, d, yd;
210   calendar_offset_to_gregorian (ofs, &y, &m, &d, &yd);
211   return d;
212 }
213
214 /* Returns the number of days in the specified month. */
215 int
216 calendar_days_in_month (int y, int m)
217 {
218   static const int days_per_month[12]
219     = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
220
221   assert (m >= 1 && m <= 12);
222   return m == 2 && is_leap_year (y) ? 29 : days_per_month[m - 1];
223 }