X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Fdata%2Fcalendar.c;h=c3d9d8e6ea00728cca253c4be18dbc01527dc5f6;hb=992bcbd6001c1b36828de093c3b218f5bfd688b6;hp=0a6498dd3ad2def303ac79f26b2756cf9e280853;hpb=dcf9b154cbcaa35c3d8459a201b77eec8bcb30bd;p=pspp diff --git a/src/data/calendar.c b/src/data/calendar.c index 0a6498dd3a..c3d9d8e6ea 100644 --- a/src/data/calendar.c +++ b/src/data/calendar.c @@ -153,14 +153,15 @@ calendar_offset_to_year (int ofs) } /* Takes a count of days from 14 Oct 1582 and translates it into - a Gregorian calendar date in (*Y,*M,*D). Dates both before - and after the epoch are supported. */ + a Gregorian calendar date in (*Y,*M,*D). Also stores the + year-relative day number into *YD. Dates both before and + after the epoch are supported. */ void -calendar_offset_to_gregorian (int ofs, int *y, int *m, int *d) +calendar_offset_to_gregorian (int ofs, int *y, int *m, int *d, int *yd) { int year = *y = calendar_offset_to_year (ofs); int january1 = raw_gregorian_to_offset (year, 1, 1); - int yday = ofs - january1 + 1; + int yday = *yd = ofs - january1 + 1; int march1 = january1 + cum_month_days (year, 3); int correction = ofs < march1 ? 0 : (is_leap_year (year) ? 1 : 2); int month = *m = (12 * (yday - 1 + correction) + 373) / 367; @@ -195,8 +196,8 @@ calendar_offset_to_wday (int ofs) int calendar_offset_to_month (int ofs) { - int y, m, d; - calendar_offset_to_gregorian (ofs, &y, &m, &d); + int y, m, d, yd; + calendar_offset_to_gregorian (ofs, &y, &m, &d, &yd); return m; } @@ -205,7 +206,18 @@ calendar_offset_to_month (int ofs) int calendar_offset_to_mday (int ofs) { - int y, m, d; - calendar_offset_to_gregorian (ofs, &y, &m, &d); + int y, m, d, yd; + calendar_offset_to_gregorian (ofs, &y, &m, &d, &yd); return d; } + +/* Returns the number of days in the specified month. */ +int +calendar_days_in_month (int y, int m) +{ + static const int days_per_month[12] + = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; + + assert (m >= 1 && m <= 12); + return m == 2 && is_leap_year (y) ? 29 : days_per_month[m - 1]; +}