2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 2019-2020 The DragonFly Project. All rights reserved.
6 * This code is derived from software contributed to The DragonFly Project
7 * by Aaron LI <aly@aaronly.me>
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
19 * 3. Neither the name of The DragonFly Project nor the names of its
20 * contributors may be used to endorse or promote products derived
21 * from this software without specific, prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
29 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
31 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
32 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
33 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * Calendrical Calculations, The Ultimate Edition (4th Edition)
38 * by Edward M. Reingold and Nachum Dershowitz
39 * 2018, Cambridge University Press
43 * Rata Die (R.D.), i.e., fixed date, is 1 at midnight (00:00) local time
44 * on January 1, AD 1 in the proleptic Gregorian calendar.
50 #include "gregorian.h"
54 * Fixed date of the start of the (proleptic) Gregorian calendar.
55 * Ref: Sec.(2.2), Eq.(2.3)
57 static const int epoch = 1;
60 * Return true if $year is a leap year on the Gregorian calendar,
61 * otherwise return false.
62 * Ref: Sec.(2.2), Eq.(2.16)
65 gregorian_leap_year(int year)
67 int r4 = mod(year, 4);
68 int r400 = mod(year, 400);
69 return (r4 == 0 && (r400 != 100 && r400 != 200 && r400 != 300));
73 * Calculate the fixed date (RD) equivalent to the Gregorian date $date.
74 * Ref: Sec.(2.2), Eq.(2.17)
77 fixed_from_gregorian(const struct date *date)
79 int rd = ((epoch - 1) + 365 * (date->year - 1) +
80 div_floor(date->year - 1, 4) -
81 div_floor(date->year - 1, 100) +
82 div_floor(date->year - 1, 400) +
83 div_floor(date->month * 367 - 362, 12));
84 /* correct for the assumption that February always has 30 days */
86 return rd + date->day;
87 else if (gregorian_leap_year(date->year))
88 return rd + date->day - 1;
90 return rd + date->day - 2;
94 * Calculate the fixed date of January 1 in year $year.
95 * Ref: Sec.(2.2), Eq.(2.18)
98 gregorian_new_year(int year)
100 struct date date = { year, 1, 1 };
101 return fixed_from_gregorian(&date);
105 * Calculate the Gregorian year corresponding to the fixed date $rd.
106 * Ref: Sec.(2.2), Eq.(2.21)
109 gregorian_year_from_fixed(int rd)
112 int d1 = mod(d0, 146097);
113 int d2 = mod(d1, 36524);
114 int d3 = mod(d2, 1461);
116 int n400 = div_floor(d0, 146097);
117 int n100 = div_floor(d1, 36524);
118 int n4 = div_floor(d2, 1461);
119 int n1 = div_floor(d3, 365);
121 int year = 400 * n400 + 100 * n100 + 4 * n4 + n1;
122 if (n100 == 4 || n1 == 4)
129 * Number of days from Gregorian date $date1 until $date2.
130 * Ref: Sec.(2.2), Eq.(2.24)
133 gregorian_date_difference(const struct date *date1,
134 const struct date *date2)
136 return fixed_from_gregorian(date2) - fixed_from_gregorian(date1);
140 * Calculate the Gregorian date (year, month, day) corresponding to the
142 * Ref: Sec.(2.2), Eq.(2.23)
145 gregorian_from_fixed(int rd, struct date *date)
147 int correction, pdays;
149 date->year = gregorian_year_from_fixed(rd);
151 struct date d = { date->year, 3, 1 };
152 if (rd < fixed_from_gregorian(&d))
154 else if (gregorian_leap_year(date->year))
160 pdays = rd - fixed_from_gregorian(&d);
161 date->month = div_floor(12 * (pdays + correction) + 373, 367);
163 d.month = date->month;
164 date->day = rd - fixed_from_gregorian(&d) + 1;