ruby-calendrical 2.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/LICENSE +26 -0
- data/README +72 -0
- data/README.en +76 -0
- data/example/cal.rb +39 -0
- data/ext/calendrical/MANIFEST +10 -0
- data/ext/calendrical/calendrical.c +1197 -0
- data/ext/calendrical/calendrical.h +99 -0
- data/ext/calendrical/calendrical2.c +802 -0
- data/ext/calendrical/calendrical2.h +72 -0
- data/ext/calendrical/calendricalmodule.c +1796 -0
- data/ext/calendrical/depend +4 -0
- data/ext/calendrical/extconf.rb +2 -0
- data/ext/calendrical/qref.c +20655 -0
- data/ext/calendrical/qref.h +13 -0
- metadata +58 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: f3d3f00ad1d48c523bcaafdff4d5d89486fcd1ca
|
4
|
+
data.tar.gz: e981441fa663ea07949b5d02fc1f94f65b5a9e8f
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: c74dd37be5b81672fee5fd87ea27d389315ee7106a44105989e8e1a54210824c808e5b0cb37f3afbb6abf78b07b349d6b37921443785264e1f715810fd8b6661
|
7
|
+
data.tar.gz: b8b3ea4415a6c29f4e8b64e7b4ea65037c7f37fb13f0e05287be8ffce1b5273ee158554ed40685dff0b32c0eb7244eb4fcc6d872e1ac59e833311a7b753650a1
|
data/LICENSE
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
Copyright (c) 1997-2014, Tadayoshi Funaba
|
2
|
+
All rights reserved.
|
3
|
+
|
4
|
+
Redistribution and use in source and binary forms, with or without
|
5
|
+
modification, are permitted provided that the following conditions are
|
6
|
+
met:
|
7
|
+
|
8
|
+
1. Redistributions of source code must retain the above copyright
|
9
|
+
notice, this list of conditions and the following disclaimer.
|
10
|
+
|
11
|
+
2. Redistributions in binary form must reproduce the above copyright
|
12
|
+
notice, this list of conditions and the following disclaimer in the
|
13
|
+
documentation and/or other materials provided with the
|
14
|
+
distribution.
|
15
|
+
|
16
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
17
|
+
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
18
|
+
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
19
|
+
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
20
|
+
HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
21
|
+
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
22
|
+
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
23
|
+
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
24
|
+
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
25
|
+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
26
|
+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
data/README
ADDED
@@ -0,0 +1,72 @@
|
|
1
|
+
"Calendrical" モジュール
|
2
|
+
========================
|
3
|
+
|
4
|
+
これは様々な暦についての計算をあつかうモジュールです。暦の計算は
|
5
|
+
"Calendrical Calculations"、および "Calendrical Calculations, II:
|
6
|
+
Three Historical Calendars" にもとづいています。
|
7
|
+
|
8
|
+
また、上記のものに加え、いくつかの暦のためにコードを追加しています。
|
9
|
+
|
10
|
+
このモジュールはつぎの暦をあつかいます:
|
11
|
+
|
12
|
+
グレゴリオ暦 (いわゆる新暦)、暦週 ("ISO")、ユリウス暦 (欧州旧暦)、
|
13
|
+
イスラム暦、ユダヤ暦、マヤ暦、フランス革命暦、古ヒンドゥ、アケリス
|
14
|
+
の世界暦、コプト暦、エチオピア暦、ジャラリ暦、バハイ暦、インド国民
|
15
|
+
暦、改訂ベンガル暦、新シク暦 (Nanakshahi)、いわゆる旧暦
|
16
|
+
|
17
|
+
このモジュールは、以前にただ "Calendar" モジュール群として扱っていたも
|
18
|
+
のから分離して "Calendrical" モジュールとしたものです。後方互換性のため
|
19
|
+
に lib/calendar.rb を用意しています。
|
20
|
+
|
21
|
+
注意: マヤ長期暦の対照は GMT (584,283) です。ジャラリ暦はまだ完全ではあ
|
22
|
+
りません。インド国民暦、改訂ベンガル暦、新シク暦 (Nanakshahi) は試験実
|
23
|
+
装です。旧暦の年は西暦をつかいます。このモジュールは、ユリウス日、およ
|
24
|
+
び修正ユリウス日を「年代学的」にあつかいます。
|
25
|
+
|
26
|
+
|
27
|
+
インストール
|
28
|
+
------------
|
29
|
+
|
30
|
+
スクリプト "install.rb" をつかいます。
|
31
|
+
|
32
|
+
$ ruby install.rb config
|
33
|
+
$ ruby install.rb setup
|
34
|
+
($ su)
|
35
|
+
# ruby install.rb install
|
36
|
+
|
37
|
+
くわしくは、"ruby install.rb --help" としてください。
|
38
|
+
|
39
|
+
|
40
|
+
謝辞
|
41
|
+
----
|
42
|
+
|
43
|
+
さまざまな暦計算のためのコードを書かれた Edward M. Reingold、
|
44
|
+
Nachum Dershowitz、Stewart M. Clamen 氏らに感謝します。
|
45
|
+
|
46
|
+
|
47
|
+
参考文献
|
48
|
+
--------
|
49
|
+
|
50
|
+
Nachum Dershowitz and Edward M. Reingold, "Calendrical Calculations"
|
51
|
+
Software---Practice & Experience, vol. 20, no. 9 (Sept. 1990),
|
52
|
+
pp. 899--928.
|
53
|
+
|
54
|
+
Edward M. Reingold, Nachum Dershowitz and Stewart M. Clamen,
|
55
|
+
"Calendrical Calculations, II: Three Historical Calendars"
|
56
|
+
Software---Practice & Experience, vol. 23, no. 4 (April. 1993),
|
57
|
+
pp. 383--404.
|
58
|
+
|
59
|
+
暦計算研究会編「新こよみ便利帳」恒星社厚生閣 1991
|
60
|
+
|
61
|
+
野島寿三郎編「日本暦西暦月日対照表」日外アソシエーツ 1987,1996
|
62
|
+
|
63
|
+
暦の会編「暦の百科事典」(新旧暦月日対照表:西沢利男)新人物往来社 1986,1989
|
64
|
+
|
65
|
+
内田正男編著「日本暦日原典第四版」雄山閣出版 1975,1992
|
66
|
+
|
67
|
+
日外アソシエーツ編集部編「21世紀暦」日外アソシエーツ 2000
|
68
|
+
|
69
|
+
|
70
|
+
ふなばただよし
|
71
|
+
mailto:tadf@funaba.org
|
72
|
+
http://www.funaba.org/
|
data/README.en
ADDED
@@ -0,0 +1,76 @@
|
|
1
|
+
A "Calendrical" module
|
2
|
+
======================
|
3
|
+
|
4
|
+
This module handles calculations about various calendars. It is based
|
5
|
+
on "Calendrical Calculations" and "Calendrical Calculations, II: Three
|
6
|
+
Historical Calendars".
|
7
|
+
|
8
|
+
In addition to the above, I also wrote code for a few calendars.
|
9
|
+
|
10
|
+
This module supports the following calendars:
|
11
|
+
|
12
|
+
Gregorian (New Style), Week Date ("ISO"), Julian (Old Style),
|
13
|
+
Islamic (Moslem), Hebrew (Jewish), Mayan, French Revolutionary,
|
14
|
+
Old Hindu, Achelis', Coptic, Ethiopian, Jalali, Bahá'í, Indian
|
15
|
+
National (experimental), Revised Bengali (experimental),
|
16
|
+
Nanakshahi (experimental) and Kyūreki
|
17
|
+
|
18
|
+
This "Calendrical" module was derived from old "Calendar" modules.
|
19
|
+
lib/calendar.rb is avaiable for backward compatibility.
|
20
|
+
|
21
|
+
NOTE: The correlation of Mayan Long Count is GMT (584,283). Jalali is
|
22
|
+
not complete yet. Indian National, Revised Bengali and Nanakshahi are
|
23
|
+
now experimental. This module represents Japanese traditional
|
24
|
+
calendar ("Kyūreki") in Gregorian year. This module handles Julian
|
25
|
+
day number and Modified Julian daynumber as "chronological" ones.
|
26
|
+
|
27
|
+
|
28
|
+
Install
|
29
|
+
-------
|
30
|
+
|
31
|
+
Use the script "install.rb".
|
32
|
+
|
33
|
+
$ ruby install.rb config
|
34
|
+
$ ruby install.rb setup
|
35
|
+
($ su)
|
36
|
+
# ruby install.rb install
|
37
|
+
|
38
|
+
For more information, type "ruby install.rb --help".
|
39
|
+
|
40
|
+
|
41
|
+
Acknowledgments
|
42
|
+
---------------
|
43
|
+
|
44
|
+
I thank Edward M. Reingold, Nachum Dershowitz and Stewart M. Clamen
|
45
|
+
who wrote various calendrical code.
|
46
|
+
|
47
|
+
|
48
|
+
References
|
49
|
+
----------
|
50
|
+
|
51
|
+
Nachum Dershowitz and Edward M. Reingold, "Calendrical Calculations"
|
52
|
+
Software---Practice & Experience, vol. 20, no. 9 (Sept. 1990),
|
53
|
+
pp. 899--928.
|
54
|
+
|
55
|
+
Edward M. Reingold, Nachum Dershowitz and Stewart M. Clamen,
|
56
|
+
"Calendrical Calculations, II: Three Historical Calendars"
|
57
|
+
Software---Practice & Experience, vol. 23, no. 4 (April. 1993),
|
58
|
+
pp. 383--404.
|
59
|
+
|
60
|
+
Koyomi-keisan Kenkyū-kai, Shin Koyomi Benrichō, Kōseisha Kōseikaku,
|
61
|
+
1991
|
62
|
+
|
63
|
+
Jusaburō Nojima, Nihon-reki Sei-reki Tsuki-hi Taishō-hyō, Nichigai
|
64
|
+
Associates, 1987,1996
|
65
|
+
|
66
|
+
Koyomi no Kai, Koyomi no Hyakka-jiten, Shin Jinbutsu Ōraisha,
|
67
|
+
1986,1989
|
68
|
+
|
69
|
+
Masao Uchida, Nihon Rekijitsu Genten 4th ed, Yūzan Kaku, 1975,1992
|
70
|
+
|
71
|
+
Nichigai Associates, 21 Seiki-reki, Nichigai Associates 2000
|
72
|
+
|
73
|
+
|
74
|
+
Tadayoshi Funaba
|
75
|
+
mailto:tadf@funaba.org
|
76
|
+
http://www.funaba.org/
|
data/example/cal.rb
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
#! /usr/local/bin/ruby
|
2
|
+
|
3
|
+
require 'calendrical'
|
4
|
+
include Calendrical
|
5
|
+
|
6
|
+
def cal(m, y)
|
7
|
+
printf(" %s %d\n", MONTH_NAMES[m], y)
|
8
|
+
printf(" S M Tu W Th F S\n")
|
9
|
+
fst = gregorian_to_rd(y, m, 1)
|
10
|
+
print(' ' * rd_to_day_of_week(fst))
|
11
|
+
days = gregorian_last_day_of_month(y, m)
|
12
|
+
for i in 1..days
|
13
|
+
printf('%2d', i)
|
14
|
+
if rd_to_day_of_week(fst + i) != 0
|
15
|
+
print(' ')
|
16
|
+
else
|
17
|
+
print("\n")
|
18
|
+
end
|
19
|
+
end
|
20
|
+
if ((rd_to_day_of_week(fst) + days) / 7) < 5
|
21
|
+
print("\n")
|
22
|
+
end
|
23
|
+
print("\n")
|
24
|
+
end
|
25
|
+
|
26
|
+
def main()
|
27
|
+
if $*.length > 2
|
28
|
+
printf($stderr, "usage: cal [ month [year] ]\n")
|
29
|
+
exit(1)
|
30
|
+
end
|
31
|
+
now = Time.now
|
32
|
+
m = now.mon
|
33
|
+
y = now.year
|
34
|
+
m = $*[0].to_i if $*.length >= 1
|
35
|
+
y = $*[1].to_i if $*.length >= 2
|
36
|
+
cal(m, y)
|
37
|
+
end
|
38
|
+
|
39
|
+
main()
|
@@ -0,0 +1,1197 @@
|
|
1
|
+
/*
|
2
|
+
calendrical.c: Translated by Tadayoshi Funaba 1996-2001,2014
|
3
|
+
|
4
|
+
The original Common Lisp code is from ``Calendrical
|
5
|
+
Calculations'' by Nachum Dershowitz and Edward
|
6
|
+
M. Reingold, Software---Practice & Experience,
|
7
|
+
vol. 20, no. 9 (September, 1990), pp. 899--928 and
|
8
|
+
from ``Calendrical Calculations, II: Three Historical
|
9
|
+
Calendars'' by Edward M. Reingold, Nachum Dershowitz,
|
10
|
+
and Stewart M. Clamen, Software---Practice & Experience,
|
11
|
+
vol. 23, no. 4 (April, 1993), pp. 383--404.
|
12
|
+
|
13
|
+
This code is in the public domain, but any use of it
|
14
|
+
should publically acknowledge its source.
|
15
|
+
|
16
|
+
$Id: calendrical.c,v 1.9 2014-04-08 19:27:31+09 tadf Exp $
|
17
|
+
*/
|
18
|
+
|
19
|
+
#define MODULE
|
20
|
+
|
21
|
+
#ifndef NULL
|
22
|
+
#define NULL 0
|
23
|
+
#endif
|
24
|
+
|
25
|
+
#include <stdlib.h>
|
26
|
+
#include <math.h>
|
27
|
+
#include "calendrical.h"
|
28
|
+
|
29
|
+
static div_t
|
30
|
+
divmod(int m, int n)
|
31
|
+
{
|
32
|
+
div_t d;
|
33
|
+
|
34
|
+
d = div(m, n);
|
35
|
+
if ((m < 0) != (n < 0) && d.rem != 0) {
|
36
|
+
d.quot -= 1;
|
37
|
+
d.rem += n;
|
38
|
+
}
|
39
|
+
return d;
|
40
|
+
}
|
41
|
+
|
42
|
+
#undef quotient
|
43
|
+
#define quotient(m, n) i_quotient(m, n)
|
44
|
+
static int
|
45
|
+
i_quotient(int m, int n)
|
46
|
+
{
|
47
|
+
return divmod(m, n).quot;
|
48
|
+
}
|
49
|
+
#undef mod
|
50
|
+
#define mod(m, n) i_mod(m, n)
|
51
|
+
static int
|
52
|
+
i_mod(int m, int n)
|
53
|
+
{
|
54
|
+
return divmod(m, n).rem;
|
55
|
+
}
|
56
|
+
#undef oddp
|
57
|
+
#define oddp(n) ((n) % 2)
|
58
|
+
|
59
|
+
static int
|
60
|
+
approximate(int x, int y)
|
61
|
+
{
|
62
|
+
return quotient(x, (x < 0) ? (y - 1) : y);
|
63
|
+
}
|
64
|
+
|
65
|
+
int
|
66
|
+
gregorian_leap_year(int year)
|
67
|
+
{
|
68
|
+
return
|
69
|
+
mod(year, 4) == 0 &&
|
70
|
+
!(mod(year, 400) == 100 ||
|
71
|
+
mod(year, 400) == 200 ||
|
72
|
+
mod(year, 400) == 300);
|
73
|
+
}
|
74
|
+
|
75
|
+
int
|
76
|
+
gregorian_last_day_of_month(int year, int month)
|
77
|
+
{
|
78
|
+
if (month == 2 && gregorian_leap_year(year))
|
79
|
+
return 29;
|
80
|
+
switch (month) {
|
81
|
+
case 1: return 31;
|
82
|
+
case 2: return 28;
|
83
|
+
case 3: return 31;
|
84
|
+
case 4: return 30;
|
85
|
+
case 5: return 31;
|
86
|
+
case 6: return 30;
|
87
|
+
case 7: return 31;
|
88
|
+
case 8: return 31;
|
89
|
+
case 9: return 30;
|
90
|
+
case 10: return 31;
|
91
|
+
case 11: return 30;
|
92
|
+
case 12: return 31;
|
93
|
+
default: return 0;
|
94
|
+
}
|
95
|
+
}
|
96
|
+
|
97
|
+
int
|
98
|
+
gregorian_to_rd(int year, int month, int day)
|
99
|
+
{
|
100
|
+
int sumres;
|
101
|
+
|
102
|
+
{
|
103
|
+
int temp, m;
|
104
|
+
for (temp = 0, m = 1;
|
105
|
+
(m < month);
|
106
|
+
temp = temp + gregorian_last_day_of_month(year, m), m++)
|
107
|
+
;
|
108
|
+
sumres = temp;
|
109
|
+
}
|
110
|
+
return day
|
111
|
+
+ sumres
|
112
|
+
+ (365 * (year - 1))
|
113
|
+
+ quotient(year - 1, 4)
|
114
|
+
- quotient(year - 1, 100)
|
115
|
+
+ quotient(year - 1, 400);
|
116
|
+
}
|
117
|
+
|
118
|
+
void
|
119
|
+
rd_to_gregorian(int date, int *ryear, int *rmonth, int *rday)
|
120
|
+
{
|
121
|
+
int approx, month, day, year;
|
122
|
+
int sumres1, sumres2;
|
123
|
+
|
124
|
+
#ifndef PAPER
|
125
|
+
approx = approximate(date, 366);
|
126
|
+
#else
|
127
|
+
approx = quotient(date, 366);
|
128
|
+
#endif
|
129
|
+
{
|
130
|
+
int temp, y;
|
131
|
+
for (temp = 0, y = approx;
|
132
|
+
(date >= gregorian_to_rd(1 + y, 1, 1));
|
133
|
+
temp = temp + 1, y++)
|
134
|
+
;
|
135
|
+
sumres1 = temp;
|
136
|
+
}
|
137
|
+
year = approx
|
138
|
+
+ sumres1;
|
139
|
+
{
|
140
|
+
int temp, m;
|
141
|
+
for (temp = 0, m = 1;
|
142
|
+
(date > gregorian_to_rd
|
143
|
+
(year, m, gregorian_last_day_of_month(year, m)));
|
144
|
+
temp = temp + 1, m++)
|
145
|
+
;
|
146
|
+
sumres2 = temp;
|
147
|
+
}
|
148
|
+
month = 1
|
149
|
+
+ sumres2;
|
150
|
+
day = date
|
151
|
+
- (gregorian_to_rd(year, month, 1) - 1);
|
152
|
+
if (rmonth)
|
153
|
+
*rmonth = month;
|
154
|
+
if (rday)
|
155
|
+
*rday = day;
|
156
|
+
if (ryear)
|
157
|
+
*ryear = year;
|
158
|
+
}
|
159
|
+
|
160
|
+
int
|
161
|
+
kday_on_or_before(int date, int k)
|
162
|
+
{
|
163
|
+
return date - mod(date - k, 7);
|
164
|
+
}
|
165
|
+
|
166
|
+
int
|
167
|
+
iso_to_rd(int year, int week, int day)
|
168
|
+
{
|
169
|
+
return kday_on_or_before(gregorian_to_rd(year, 1, 4), 1)
|
170
|
+
+ (7 * (week - 1))
|
171
|
+
+ (day - 1);
|
172
|
+
}
|
173
|
+
|
174
|
+
void
|
175
|
+
rd_to_iso(int date, int *ryear, int *rweek, int *rday)
|
176
|
+
{
|
177
|
+
int approx, week, day, year;
|
178
|
+
|
179
|
+
rd_to_gregorian(date - 3, &approx, NULL, NULL);
|
180
|
+
year = (date >= iso_to_rd(1 + approx, 1, 1)) ?
|
181
|
+
1 + approx : approx;
|
182
|
+
week = 1 + quotient(date - iso_to_rd(year, 1, 1), 7);
|
183
|
+
day = (mod(date, 7) == 0) ?
|
184
|
+
7 : mod(date, 7);
|
185
|
+
if (rweek)
|
186
|
+
*rweek = week;
|
187
|
+
if (rday)
|
188
|
+
*rday = day;
|
189
|
+
if (ryear)
|
190
|
+
*ryear = year;
|
191
|
+
}
|
192
|
+
|
193
|
+
int
|
194
|
+
julian_leap_year(int year)
|
195
|
+
{
|
196
|
+
#if !defined(PAPER) && defined(OMIT_LEAP4)
|
197
|
+
return year != 4 && mod(year, 4) == 0;
|
198
|
+
#else
|
199
|
+
return mod(year, 4) == 0;
|
200
|
+
#endif
|
201
|
+
}
|
202
|
+
|
203
|
+
int
|
204
|
+
julian_last_day_of_month(int year, int month)
|
205
|
+
{
|
206
|
+
if (month == 2 && julian_leap_year(year))
|
207
|
+
return 29;
|
208
|
+
switch (month) {
|
209
|
+
case 1: return 31;
|
210
|
+
case 2: return 28;
|
211
|
+
case 3: return 31;
|
212
|
+
case 4: return 30;
|
213
|
+
case 5: return 31;
|
214
|
+
case 6: return 30;
|
215
|
+
case 7: return 31;
|
216
|
+
case 8: return 31;
|
217
|
+
case 9: return 30;
|
218
|
+
case 10: return 31;
|
219
|
+
case 11: return 30;
|
220
|
+
case 12: return 31;
|
221
|
+
default: return 0;
|
222
|
+
}
|
223
|
+
}
|
224
|
+
|
225
|
+
int
|
226
|
+
julian_to_rd(int year, int month, int day)
|
227
|
+
{
|
228
|
+
int sumres;
|
229
|
+
|
230
|
+
{
|
231
|
+
int temp, m;
|
232
|
+
for (temp = 0, m = 1;
|
233
|
+
(m < month);
|
234
|
+
temp = temp + julian_last_day_of_month(year, m), m++)
|
235
|
+
;
|
236
|
+
sumres = temp;
|
237
|
+
}
|
238
|
+
return day
|
239
|
+
+ sumres
|
240
|
+
+ (365 * (year - 1))
|
241
|
+
+ quotient(year - 1, 4)
|
242
|
+
#if !defined(PAPER) && defined(OMIT_LEAP4)
|
243
|
+
- ((year > 4) ? 2 : 1);
|
244
|
+
#else
|
245
|
+
- 2;
|
246
|
+
#endif
|
247
|
+
}
|
248
|
+
|
249
|
+
void
|
250
|
+
rd_to_julian(int date, int *ryear, int *rmonth, int *rday)
|
251
|
+
{
|
252
|
+
int approx, month, day, year;
|
253
|
+
int sumres1, sumres2;
|
254
|
+
|
255
|
+
#if !defined(PAPER) && defined(OMIT_LEAP4)
|
256
|
+
approx = approximate(date + 1, 366);
|
257
|
+
#elif !defined(PAPER)
|
258
|
+
approx = approximate(date + 2, 366);
|
259
|
+
#else
|
260
|
+
approx = quotient(date + 2, 366);
|
261
|
+
#endif
|
262
|
+
|
263
|
+
{
|
264
|
+
int temp, y;
|
265
|
+
for (temp = 0, y = approx;
|
266
|
+
(date >= julian_to_rd(1 + y, 1, 1));
|
267
|
+
temp = temp + 1, y++)
|
268
|
+
;
|
269
|
+
sumres1 = temp;
|
270
|
+
}
|
271
|
+
year = approx
|
272
|
+
+ sumres1;
|
273
|
+
{
|
274
|
+
int temp, m;
|
275
|
+
for (temp = 0, m = 1;
|
276
|
+
(date > julian_to_rd
|
277
|
+
(year, m, julian_last_day_of_month(year, m)));
|
278
|
+
temp = temp + 1, m++)
|
279
|
+
;
|
280
|
+
sumres2 = temp;
|
281
|
+
}
|
282
|
+
month = 1
|
283
|
+
+ sumres2;
|
284
|
+
day = date - (julian_to_rd(year, month, 1) - 1);
|
285
|
+
if (rmonth)
|
286
|
+
*rmonth = month;
|
287
|
+
if (rday)
|
288
|
+
*rday = day;
|
289
|
+
if (ryear)
|
290
|
+
*ryear = year;
|
291
|
+
}
|
292
|
+
|
293
|
+
int
|
294
|
+
islamic_leap_year(int year)
|
295
|
+
{
|
296
|
+
return mod(14 + (11 * year), 30) < 11;
|
297
|
+
}
|
298
|
+
|
299
|
+
int
|
300
|
+
islamic_last_day_of_month(int year, int month)
|
301
|
+
{
|
302
|
+
return (oddp (month) ||
|
303
|
+
((month == 12) && islamic_leap_year(year))) ?
|
304
|
+
30 : 29;
|
305
|
+
}
|
306
|
+
|
307
|
+
int
|
308
|
+
islamic_to_rd(int year, int month, int day)
|
309
|
+
{
|
310
|
+
return day
|
311
|
+
+ (29 * (month - 1))
|
312
|
+
+ quotient(month, 2)
|
313
|
+
+ ((year - 1) * 354)
|
314
|
+
+ quotient(3 + (11 * year), 30)
|
315
|
+
+ 227014;
|
316
|
+
}
|
317
|
+
|
318
|
+
void
|
319
|
+
rd_to_islamic(int date, int *ryear, int *rmonth, int *rday)
|
320
|
+
{
|
321
|
+
int approx, month, day, year;
|
322
|
+
int sumres1, sumres2;
|
323
|
+
|
324
|
+
#ifdef PAPER
|
325
|
+
if (date <= 227014) {
|
326
|
+
if (rmonth)
|
327
|
+
*rmonth = 0;
|
328
|
+
if (rday)
|
329
|
+
*rday = 0;
|
330
|
+
if (ryear)
|
331
|
+
*ryear = 0;
|
332
|
+
return;
|
333
|
+
}
|
334
|
+
#endif
|
335
|
+
#ifndef PAPER
|
336
|
+
approx = approximate(date - 227014, 355);
|
337
|
+
#else
|
338
|
+
approx = quotient(date - 227014, 355);
|
339
|
+
#endif
|
340
|
+
{
|
341
|
+
int temp, y;
|
342
|
+
for (temp = 0, y = approx;
|
343
|
+
(date >= islamic_to_rd(1 + y, 1, 1));
|
344
|
+
temp = temp + 1, y++)
|
345
|
+
;
|
346
|
+
sumres1 = temp;
|
347
|
+
}
|
348
|
+
year = approx
|
349
|
+
+ sumres1;
|
350
|
+
{
|
351
|
+
int temp, m;
|
352
|
+
for (temp = 0, m = 1;
|
353
|
+
(date > islamic_to_rd
|
354
|
+
(year, m, islamic_last_day_of_month(year, m)));
|
355
|
+
temp = temp + 1, m++)
|
356
|
+
;
|
357
|
+
sumres2 = temp;
|
358
|
+
}
|
359
|
+
month = 1
|
360
|
+
+ sumres2;
|
361
|
+
day = date
|
362
|
+
- (islamic_to_rd(year, month, 1) - 1);
|
363
|
+
if (rmonth)
|
364
|
+
*rmonth = month;
|
365
|
+
if (rday)
|
366
|
+
*rday = day;
|
367
|
+
if (ryear)
|
368
|
+
*ryear = year;
|
369
|
+
}
|
370
|
+
|
371
|
+
int
|
372
|
+
hebrew_leap_year(int year)
|
373
|
+
{
|
374
|
+
return mod(1 + (7 * year), 19) < 7;
|
375
|
+
}
|
376
|
+
|
377
|
+
int
|
378
|
+
hebrew_last_month_of_year(int year)
|
379
|
+
{
|
380
|
+
return (hebrew_leap_year(year)) ?
|
381
|
+
13 : 12;
|
382
|
+
}
|
383
|
+
|
384
|
+
int
|
385
|
+
hebrew_last_day_of_month(int year, int month)
|
386
|
+
{
|
387
|
+
return
|
388
|
+
((month == 2 ||
|
389
|
+
month == 4 ||
|
390
|
+
month == 6 ||
|
391
|
+
month == 10 ||
|
392
|
+
month == 13) ||
|
393
|
+
(month == 12 && !hebrew_leap_year(year)) ||
|
394
|
+
(month == 8 && !long_heshvan(year)) ||
|
395
|
+
(month == 9 && short_kislev(year))) ?
|
396
|
+
29 : 30;
|
397
|
+
}
|
398
|
+
|
399
|
+
int
|
400
|
+
hebrew_calendar_elapsed_days(int year)
|
401
|
+
{
|
402
|
+
int months_elapsed, parts_elapsed, hours_elapsed,
|
403
|
+
day, parts, alternative_day;
|
404
|
+
|
405
|
+
months_elapsed = (235 * quotient(year - 1, 19))
|
406
|
+
+ (12 * mod(year - 1, 19))
|
407
|
+
+ quotient(1 + (7 * mod (year - 1, 19)), 19);
|
408
|
+
parts_elapsed = 204
|
409
|
+
+ (793 * mod(months_elapsed, 1080));
|
410
|
+
hours_elapsed = 5
|
411
|
+
+ (12 * months_elapsed)
|
412
|
+
+ (793 * quotient(months_elapsed, 1080))
|
413
|
+
+ quotient(parts_elapsed, 1080);
|
414
|
+
day = 1
|
415
|
+
+ (29 * months_elapsed)
|
416
|
+
+ quotient(hours_elapsed, 24);
|
417
|
+
parts = (1080 * mod(hours_elapsed, 24))
|
418
|
+
+ mod(parts_elapsed, 1080);
|
419
|
+
alternative_day =
|
420
|
+
((parts >= 19440) ||
|
421
|
+
((mod(day, 7) == 2) && (parts >= 9924)
|
422
|
+
&& !hebrew_leap_year(year)) ||
|
423
|
+
((mod(day, 7) == 1) && (parts >= 16789)
|
424
|
+
&& hebrew_leap_year(year - 1))) ?
|
425
|
+
1 + day : day;
|
426
|
+
return ((mod (alternative_day, 7) == 0) ||
|
427
|
+
(mod(alternative_day, 7) == 3) ||
|
428
|
+
(mod(alternative_day, 7) == 5)) ?
|
429
|
+
1 + alternative_day : alternative_day;
|
430
|
+
}
|
431
|
+
|
432
|
+
int
|
433
|
+
hebrew_days_in_year(int year)
|
434
|
+
{
|
435
|
+
return
|
436
|
+
hebrew_calendar_elapsed_days(1 + year)
|
437
|
+
- hebrew_calendar_elapsed_days(year);
|
438
|
+
}
|
439
|
+
|
440
|
+
int
|
441
|
+
long_heshvan(int year)
|
442
|
+
{
|
443
|
+
return mod(hebrew_days_in_year(year), 10) == 5;
|
444
|
+
}
|
445
|
+
|
446
|
+
int
|
447
|
+
short_kislev(int year)
|
448
|
+
{
|
449
|
+
return mod(hebrew_days_in_year (year), 10) == 3;
|
450
|
+
}
|
451
|
+
|
452
|
+
int
|
453
|
+
hebrew_to_rd(int year, int month, int day)
|
454
|
+
{
|
455
|
+
if (month < 7) {
|
456
|
+
int sumres1, sumres2;
|
457
|
+
|
458
|
+
{
|
459
|
+
int temp, m;
|
460
|
+
for (temp = 0, m = 7;
|
461
|
+
(m <= hebrew_last_month_of_year(year));
|
462
|
+
temp = temp + hebrew_last_day_of_month(year, m), m++)
|
463
|
+
;
|
464
|
+
sumres1 = temp;
|
465
|
+
}
|
466
|
+
{
|
467
|
+
int temp, m;
|
468
|
+
for (temp = 0, m = 1;
|
469
|
+
(m < month);
|
470
|
+
temp = temp + hebrew_last_day_of_month(year, m), m++)
|
471
|
+
;
|
472
|
+
sumres2 = temp;
|
473
|
+
}
|
474
|
+
return day
|
475
|
+
+ sumres1
|
476
|
+
+ sumres2
|
477
|
+
+ hebrew_calendar_elapsed_days(year)
|
478
|
+
- 1373429;
|
479
|
+
} else {
|
480
|
+
int sumres;
|
481
|
+
|
482
|
+
{
|
483
|
+
int temp, m;
|
484
|
+
for (temp = 0, m = 7;
|
485
|
+
(m < month);
|
486
|
+
temp = temp + hebrew_last_day_of_month(year, m), m++)
|
487
|
+
;
|
488
|
+
sumres = temp;
|
489
|
+
}
|
490
|
+
return day
|
491
|
+
+ sumres
|
492
|
+
+ hebrew_calendar_elapsed_days(year)
|
493
|
+
- 1373429;
|
494
|
+
}
|
495
|
+
}
|
496
|
+
|
497
|
+
void
|
498
|
+
rd_to_hebrew(int date, int *ryear, int *rmonth, int *rday)
|
499
|
+
{
|
500
|
+
int approx, year, start, month, day;
|
501
|
+
int sumres1, sumres2;
|
502
|
+
|
503
|
+
#ifndef PAPER
|
504
|
+
approx = approximate(date + 1373429, 366);
|
505
|
+
#else
|
506
|
+
approx = quotient(date + 1373429, 366);
|
507
|
+
#endif
|
508
|
+
{
|
509
|
+
int temp, y;
|
510
|
+
for (temp = 0, y = approx;
|
511
|
+
(date >= hebrew_to_rd(1 + y, 7, 1));
|
512
|
+
temp = temp + 1, y++)
|
513
|
+
;
|
514
|
+
sumres1 = temp;
|
515
|
+
}
|
516
|
+
year = approx
|
517
|
+
+ sumres1;
|
518
|
+
start = (date < hebrew_to_rd(year, 1, 1)) ?
|
519
|
+
7 : 1;
|
520
|
+
{
|
521
|
+
int temp, m;
|
522
|
+
for (temp = 0, m = start;
|
523
|
+
(date > hebrew_to_rd
|
524
|
+
(year, m, hebrew_last_day_of_month(year, m)));
|
525
|
+
temp = temp + 1, m++)
|
526
|
+
;
|
527
|
+
sumres2 = temp;
|
528
|
+
}
|
529
|
+
month = start
|
530
|
+
+ sumres2;
|
531
|
+
day = date
|
532
|
+
- (hebrew_to_rd(year, month, 1) - 1);
|
533
|
+
if (rmonth)
|
534
|
+
*rmonth = month;
|
535
|
+
if (rday)
|
536
|
+
*rday = day;
|
537
|
+
if (ryear)
|
538
|
+
*ryear = year;
|
539
|
+
}
|
540
|
+
|
541
|
+
#if !defined(PLUTO) && !defined(MODULE)
|
542
|
+
int
|
543
|
+
independence_day(int year)
|
544
|
+
{
|
545
|
+
return gregorian_to_rd(year, 7, 4);
|
546
|
+
}
|
547
|
+
#endif
|
548
|
+
|
549
|
+
#ifndef PLUTO
|
550
|
+
int
|
551
|
+
nth_kday(int year, int month, int n, int k)
|
552
|
+
{
|
553
|
+
if (n > 0)
|
554
|
+
return
|
555
|
+
kday_on_or_before
|
556
|
+
(gregorian_to_rd(year, month, 7), k)
|
557
|
+
+ (7 * (n - 1));
|
558
|
+
return
|
559
|
+
kday_on_or_before
|
560
|
+
(gregorian_to_rd
|
561
|
+
(year, month,
|
562
|
+
gregorian_last_day_of_month(year, month)),
|
563
|
+
k)
|
564
|
+
+ (7 * (1 + n));
|
565
|
+
}
|
566
|
+
#endif
|
567
|
+
|
568
|
+
#if !defined(PLUTO) && !defined(MODULE)
|
569
|
+
int
|
570
|
+
labor_day(int year)
|
571
|
+
{
|
572
|
+
return Nth_Kday(1, 1, 9, year);
|
573
|
+
}
|
574
|
+
|
575
|
+
int
|
576
|
+
memorial_day(int year)
|
577
|
+
{
|
578
|
+
return Nth_Kday(-1, 1, 5, year);
|
579
|
+
}
|
580
|
+
|
581
|
+
int
|
582
|
+
daylight_savings_start(int year)
|
583
|
+
{
|
584
|
+
return Nth_Kday(1, 0, 4, year);
|
585
|
+
}
|
586
|
+
|
587
|
+
int
|
588
|
+
daylight_savings_end(int year)
|
589
|
+
{
|
590
|
+
return Nth_Kday(-1, 0, 10, year);
|
591
|
+
}
|
592
|
+
|
593
|
+
int
|
594
|
+
christmas(int year)
|
595
|
+
{
|
596
|
+
return gregorian_to_rd(year, 12, 25);
|
597
|
+
}
|
598
|
+
|
599
|
+
int
|
600
|
+
advent(int year)
|
601
|
+
{
|
602
|
+
return kday_on_or_before(gregorian_to_rd(year, 12, 3), 0);
|
603
|
+
}
|
604
|
+
|
605
|
+
int
|
606
|
+
epiphany(int year)
|
607
|
+
{
|
608
|
+
return 12 + christmas(year);
|
609
|
+
}
|
610
|
+
|
611
|
+
int
|
612
|
+
eastern_orthodox_christmas(int year)
|
613
|
+
{
|
614
|
+
int jan1, dec31, y, c1, c2;
|
615
|
+
|
616
|
+
jan1 = gregorian_to_rd(year, 1, 1);
|
617
|
+
dec31 = gregorian_to_rd(year, 12, 31);
|
618
|
+
rd_to_julian(jan1, &y, NULL, NULL);
|
619
|
+
c1 = julian_to_rd(y, 12, 25);
|
620
|
+
c2 = julian_to_rd(1 + y, 12, 25);
|
621
|
+
if (jan1 <= c1 && c1 <= dec31)
|
622
|
+
return c1;
|
623
|
+
else if (jan1 <= c2 && c2 <= dec31)
|
624
|
+
return c2;
|
625
|
+
return 0;
|
626
|
+
}
|
627
|
+
#endif /* not PLUTO and not MODULE */
|
628
|
+
|
629
|
+
int
|
630
|
+
nicaean_rule_easter(int year)
|
631
|
+
{
|
632
|
+
int shifted_epact, paschal_moon;
|
633
|
+
|
634
|
+
shifted_epact = mod(14 + (11 * mod(year, 19)), 30);
|
635
|
+
paschal_moon = julian_to_rd(year, 4, 19)
|
636
|
+
- shifted_epact;
|
637
|
+
return kday_on_or_before(paschal_moon + 7, 0);
|
638
|
+
}
|
639
|
+
|
640
|
+
int
|
641
|
+
easter(int year)
|
642
|
+
{
|
643
|
+
int century, shifted_epact, adjusted_epact, paschal_moon;
|
644
|
+
|
645
|
+
century = 1 + quotient(year, 100);
|
646
|
+
shifted_epact = mod(14 + (11 * mod(year, 19))
|
647
|
+
- quotient(3 * century, 4)
|
648
|
+
+ quotient(5 + (8 * century), 25)
|
649
|
+
+ (30 * century),
|
650
|
+
30);
|
651
|
+
adjusted_epact = ((shifted_epact == 0)
|
652
|
+
|| ((shifted_epact == 1) && (10 < mod(year, 19)))) ?
|
653
|
+
1 + shifted_epact : shifted_epact;
|
654
|
+
paschal_moon = gregorian_to_rd(year, 4, 19)
|
655
|
+
- adjusted_epact;
|
656
|
+
return kday_on_or_before(paschal_moon + 7, 0);
|
657
|
+
}
|
658
|
+
|
659
|
+
#if !defined(PLUTO) && !defined(MODULE)
|
660
|
+
int
|
661
|
+
pentecost(int year)
|
662
|
+
{
|
663
|
+
return 49 + easter(year);
|
664
|
+
}
|
665
|
+
|
666
|
+
void
|
667
|
+
islamic_date(int year, int month, int day, int date[3])
|
668
|
+
{
|
669
|
+
int jan1, dec31, y, date1, date2, date3;
|
670
|
+
|
671
|
+
jan1 = gregorian_to_rd(year, 1, 1);
|
672
|
+
dec31 = gregorian_to_rd(year, 12, 31);
|
673
|
+
rd_to_islamic(jan1, &y, NULL, NULL);
|
674
|
+
date1 = islamic_to_rd(y, month, day);
|
675
|
+
date2 = islamic_to_rd(1 + y, month, day);
|
676
|
+
date3 = islamic_to_rd(2 + y, month, day);
|
677
|
+
date[0] = (jan1 <= date1 && date1 <= dec31) ?
|
678
|
+
date1 : 0;
|
679
|
+
date[1] = (jan1 <= date2 && date2 <= dec31) ?
|
680
|
+
date2 : 0;
|
681
|
+
date[2] = (jan1 <= date3 && date3 <= dec31) ?
|
682
|
+
date3 : 0;
|
683
|
+
}
|
684
|
+
|
685
|
+
void
|
686
|
+
mulad_al_nabi(int year, int date[3])
|
687
|
+
{
|
688
|
+
islamic_date(year, 3, 12, date);
|
689
|
+
}
|
690
|
+
|
691
|
+
int
|
692
|
+
yom_kippur(int year)
|
693
|
+
{
|
694
|
+
return hebrew_to_rd(year + 3761, 7, 10);
|
695
|
+
}
|
696
|
+
|
697
|
+
int
|
698
|
+
passover(int year)
|
699
|
+
{
|
700
|
+
return hebrew_to_rd(year + 3760, 1, 15);
|
701
|
+
}
|
702
|
+
|
703
|
+
int
|
704
|
+
purim(int year)
|
705
|
+
{
|
706
|
+
return
|
707
|
+
hebrew_to_rd
|
708
|
+
(year + 3760,
|
709
|
+
hebrew_last_month_of_year(year + 3760), 14);
|
710
|
+
}
|
711
|
+
|
712
|
+
int
|
713
|
+
ta_anit_esther(int year)
|
714
|
+
{
|
715
|
+
int purim_date;
|
716
|
+
|
717
|
+
purim_date = purim(year);
|
718
|
+
return (mod(purim_date, 7) == 0) ?
|
719
|
+
purim_date - 3 : purim_date - 1;
|
720
|
+
}
|
721
|
+
|
722
|
+
int
|
723
|
+
tisha_b_av(int year)
|
724
|
+
{
|
725
|
+
int ninth_of_av;
|
726
|
+
|
727
|
+
ninth_of_av = hebrew_to_rd(year + 3760, 5, 9);
|
728
|
+
return (mod(ninth_of_av, 7) == 6) ?
|
729
|
+
1 + ninth_of_av : ninth_of_av;
|
730
|
+
}
|
731
|
+
|
732
|
+
int
|
733
|
+
hebrew_birthday(int birth_year, int birth_month, int birth_day, int year)
|
734
|
+
{
|
735
|
+
return
|
736
|
+
(birth_month == hebrew_last_month_of_year(birth_year)) ?
|
737
|
+
hebrew_to_rd(year, hebrew_last_month_of_year(year), birth_day) :
|
738
|
+
hebrew_to_rd(year, birth_month, birth_day);
|
739
|
+
}
|
740
|
+
|
741
|
+
int
|
742
|
+
yahrzeit(int death_year, int death_month, int death_day, int year)
|
743
|
+
{
|
744
|
+
if (death_month == 8 &&
|
745
|
+
death_day == 30 &&
|
746
|
+
!long_heshvan(1 + death_year))
|
747
|
+
return hebrew_to_rd(year, 9, 1) - 1;
|
748
|
+
if (death_month == 9 &&
|
749
|
+
death_day == 30 &&
|
750
|
+
!short_kislev(1 + death_year))
|
751
|
+
return hebrew_to_rd(year, 10, 1) - 1;
|
752
|
+
if (death_month == 13)
|
753
|
+
return hebrew_to_rd
|
754
|
+
(year, hebrew_last_month_of_year(year), death_day);
|
755
|
+
if (death_day == 30 &&
|
756
|
+
death_month == 12 &&
|
757
|
+
!hebrew_leap_year(year))
|
758
|
+
return hebrew_to_rd(year, 11, 30);
|
759
|
+
return hebrew_to_rd(year, death_month, death_day);
|
760
|
+
}
|
761
|
+
#endif /* not PLUTO and not MODULE */
|
762
|
+
|
763
|
+
#ifndef SPINDEN_CORRELATION
|
764
|
+
#ifndef PAPER
|
765
|
+
/* GMT - Sept 6, 3114 BCE (Julian) */
|
766
|
+
#define mayan_days_before_absolute_zero 1137142
|
767
|
+
#else
|
768
|
+
/* GMT - Sept 8, 3114 BCE (Julian) */
|
769
|
+
#define mayan_days_before_absolute_zero 1137140
|
770
|
+
#endif
|
771
|
+
#else
|
772
|
+
/* SPINDEN - Nov 11, 3374 BCE (Julian) */
|
773
|
+
#define mayan_days_before_absolute_zero 1232041
|
774
|
+
#endif
|
775
|
+
|
776
|
+
int
|
777
|
+
mayan_long_count_to_rd(int baktun, int katun, int tun, int uinal, int kin)
|
778
|
+
{
|
779
|
+
return baktun * 144000
|
780
|
+
+ katun * 7200
|
781
|
+
+ tun * 360
|
782
|
+
+ uinal * 20
|
783
|
+
+ kin
|
784
|
+
- mayan_days_before_absolute_zero;
|
785
|
+
}
|
786
|
+
|
787
|
+
void
|
788
|
+
rd_to_mayan_long_count
|
789
|
+
(int date, int *rbaktun, int *rkatun, int *rtun, int *ruinal, int *rkin)
|
790
|
+
{
|
791
|
+
int long_count,
|
792
|
+
baktun, day_of_baktun,
|
793
|
+
katun, day_of_katun,
|
794
|
+
tun, day_of_tun,
|
795
|
+
uinal, kin;
|
796
|
+
|
797
|
+
long_count = date + mayan_days_before_absolute_zero;
|
798
|
+
baktun = quotient(long_count, 144000);
|
799
|
+
day_of_baktun = mod(long_count, 144000);
|
800
|
+
katun = quotient(day_of_baktun, 7200);
|
801
|
+
day_of_katun = mod(day_of_baktun, 7200);
|
802
|
+
tun = quotient(day_of_katun, 360);
|
803
|
+
day_of_tun = mod(day_of_katun, 360);
|
804
|
+
uinal = quotient(day_of_tun, 20);
|
805
|
+
kin = mod(day_of_tun, 20);
|
806
|
+
if (rbaktun)
|
807
|
+
*rbaktun = baktun;
|
808
|
+
if (rkatun)
|
809
|
+
*rkatun = katun;
|
810
|
+
if (rtun)
|
811
|
+
*rtun = tun;
|
812
|
+
if (ruinal)
|
813
|
+
*ruinal = uinal;
|
814
|
+
if (rkin)
|
815
|
+
*rkin = kin;
|
816
|
+
}
|
817
|
+
|
818
|
+
#define mayan_haab_at_epoch_day 8
|
819
|
+
#define mayan_haab_at_epoch_month 18
|
820
|
+
|
821
|
+
void
|
822
|
+
rd_to_mayan_haab(int date, int *rmonth, int *rday)
|
823
|
+
{
|
824
|
+
int long_count, day_of_haab, day, month;
|
825
|
+
|
826
|
+
long_count = date + mayan_days_before_absolute_zero;
|
827
|
+
day_of_haab = mod
|
828
|
+
(long_count
|
829
|
+
+ mayan_haab_at_epoch_day
|
830
|
+
+ (20 * (mayan_haab_at_epoch_month - 1)),
|
831
|
+
365);
|
832
|
+
day = mod(day_of_haab, 20);
|
833
|
+
month = 1 + quotient(day_of_haab, 20);
|
834
|
+
if (rday)
|
835
|
+
*rday = day;
|
836
|
+
if (rmonth)
|
837
|
+
*rmonth = month;
|
838
|
+
}
|
839
|
+
|
840
|
+
int
|
841
|
+
mayan_haab_difference(int month1, int day1, int month2, int day2)
|
842
|
+
{
|
843
|
+
return mod((20 * (month2 - month1))
|
844
|
+
+ (day2 - day1),
|
845
|
+
365);
|
846
|
+
}
|
847
|
+
|
848
|
+
int
|
849
|
+
mayan_haab_on_or_before(int haab_month, int haab_day, int date)
|
850
|
+
{
|
851
|
+
int zero_day, zero_month;
|
852
|
+
|
853
|
+
rd_to_mayan_haab(0, &zero_month, &zero_day);
|
854
|
+
return date
|
855
|
+
- mod(date
|
856
|
+
- mayan_haab_difference
|
857
|
+
(zero_month, zero_day, haab_month, haab_day),
|
858
|
+
365);
|
859
|
+
}
|
860
|
+
|
861
|
+
static int
|
862
|
+
adjusted_mod(int m, int n)
|
863
|
+
{
|
864
|
+
return 1 + mod(m - 1, n);
|
865
|
+
}
|
866
|
+
|
867
|
+
#define mayan_tzolkin_at_epoch_number 4
|
868
|
+
#define mayan_tzolkin_at_epoch_name 20
|
869
|
+
|
870
|
+
void
|
871
|
+
rd_to_mayan_tzolkin(int date, int *rname, int *rnumber)
|
872
|
+
{
|
873
|
+
int long_count, number, name;
|
874
|
+
|
875
|
+
long_count = date + mayan_days_before_absolute_zero;
|
876
|
+
number = adjusted_mod(long_count + mayan_tzolkin_at_epoch_number,
|
877
|
+
13);
|
878
|
+
name = adjusted_mod(long_count + mayan_tzolkin_at_epoch_name,
|
879
|
+
20);
|
880
|
+
if (rnumber)
|
881
|
+
*rnumber = number;
|
882
|
+
if (rname)
|
883
|
+
*rname = name;
|
884
|
+
}
|
885
|
+
|
886
|
+
int
|
887
|
+
mayan_tzolkin_difference(int name1, int number1, int name2, int number2)
|
888
|
+
{
|
889
|
+
int number_difference, name_difference;
|
890
|
+
|
891
|
+
number_difference = number2 - number1;
|
892
|
+
name_difference = name2 - name1;
|
893
|
+
return mod(number_difference
|
894
|
+
+ (13 * mod(3 * (number_difference - name_difference),
|
895
|
+
20)),
|
896
|
+
260);
|
897
|
+
}
|
898
|
+
|
899
|
+
int
|
900
|
+
mayan_tzolkin_on_or_before(int name, int number, int date)
|
901
|
+
{
|
902
|
+
int zero_number, zero_name;
|
903
|
+
|
904
|
+
rd_to_mayan_tzolkin(0, &zero_name, &zero_number);
|
905
|
+
return date
|
906
|
+
- mod(date
|
907
|
+
- mayan_tzolkin_difference
|
908
|
+
(zero_name, zero_number, name, number),
|
909
|
+
260);
|
910
|
+
}
|
911
|
+
|
912
|
+
int
|
913
|
+
mayan_haab_tzolkin_on_or_before
|
914
|
+
(int month, int day, int name, int number, int date)
|
915
|
+
{
|
916
|
+
int zero_day, zero_month, zero_number, zero_name,
|
917
|
+
haab_difference, tzolkin_difference, difference;
|
918
|
+
|
919
|
+
rd_to_mayan_haab(0, &zero_month, &zero_day);
|
920
|
+
haab_difference = mayan_haab_difference(zero_month, zero_day, month, day);
|
921
|
+
rd_to_mayan_tzolkin(0, &zero_name, &zero_number);
|
922
|
+
tzolkin_difference = mayan_tzolkin_difference(zero_name, zero_number, name, number);
|
923
|
+
difference = tzolkin_difference - haab_difference;
|
924
|
+
if (mod(difference, 5) == 0)
|
925
|
+
return date
|
926
|
+
- mod(date
|
927
|
+
- (haab_difference + (365 * difference)),
|
928
|
+
18980);
|
929
|
+
return 0;
|
930
|
+
}
|
931
|
+
|
932
|
+
int
|
933
|
+
french_leap_year(int year)
|
934
|
+
{
|
935
|
+
return
|
936
|
+
(year == 3 ||
|
937
|
+
year == 7 ||
|
938
|
+
year == 11) ||
|
939
|
+
(year == 15 ||
|
940
|
+
year == 20) ||
|
941
|
+
#ifndef PAPER
|
942
|
+
(((year > 20) || (year < 0)) &&
|
943
|
+
#else
|
944
|
+
(year > 20 &&
|
945
|
+
#endif
|
946
|
+
(0 == mod(year, 4)) &&
|
947
|
+
!((mod(year, 400) == 100) ||
|
948
|
+
(mod(year, 400) == 200) ||
|
949
|
+
(mod(year, 400) == 300)) &&
|
950
|
+
!(0 == mod(year, 4000)));
|
951
|
+
}
|
952
|
+
|
953
|
+
int
|
954
|
+
french_last_day_of_month(int year, int month)
|
955
|
+
{
|
956
|
+
if (month < 13)
|
957
|
+
return 30;
|
958
|
+
else if (french_leap_year(year))
|
959
|
+
return 6;
|
960
|
+
return 5;
|
961
|
+
}
|
962
|
+
|
963
|
+
int
|
964
|
+
french_to_rd(int year, int month, int day)
|
965
|
+
{
|
966
|
+
return 654414
|
967
|
+
+ (365 * (year - 1))
|
968
|
+
#ifndef PAPER
|
969
|
+
+ (((year < 20)) && (year > 0) ?
|
970
|
+
#else
|
971
|
+
+ ((year < 20) ?
|
972
|
+
#endif
|
973
|
+
(quotient(year, 4)) :
|
974
|
+
(quotient(year - 1, 4)
|
975
|
+
- quotient(year - 1, 100)
|
976
|
+
+ quotient(year - 1, 400)
|
977
|
+
- quotient(year - 1, 4000)))
|
978
|
+
+ (30 * (month - 1))
|
979
|
+
+ day;
|
980
|
+
}
|
981
|
+
|
982
|
+
void
|
983
|
+
rd_to_french(int date, int *ryear, int *rmonth, int *rday)
|
984
|
+
{
|
985
|
+
int approx, year, month, day;
|
986
|
+
int sumres1, sumres2;
|
987
|
+
|
988
|
+
#ifdef PAPER
|
989
|
+
if (date < 654415) {
|
990
|
+
if (rmonth)
|
991
|
+
*rmonth = 0;
|
992
|
+
if (rday)
|
993
|
+
*rday = 0;
|
994
|
+
if (ryear)
|
995
|
+
*ryear = 0;
|
996
|
+
return;
|
997
|
+
}
|
998
|
+
#endif
|
999
|
+
#ifndef PAPER
|
1000
|
+
approx = approximate(date - 654414, 366);
|
1001
|
+
#else
|
1002
|
+
approx = quotient(date - 654414, 366);
|
1003
|
+
#endif
|
1004
|
+
{
|
1005
|
+
int temp, y;
|
1006
|
+
for (temp = 0, y = approx;
|
1007
|
+
(date >= french_to_rd(1 + y, 1, 1));
|
1008
|
+
temp = temp + 1, y++)
|
1009
|
+
;
|
1010
|
+
sumres1 = temp;
|
1011
|
+
}
|
1012
|
+
year = approx
|
1013
|
+
+ sumres1;
|
1014
|
+
{
|
1015
|
+
int temp, m;
|
1016
|
+
for (temp = 0, m = 1;
|
1017
|
+
(date > french_to_rd
|
1018
|
+
(year, m, french_last_day_of_month(year, m)));
|
1019
|
+
temp = temp + 1, m++)
|
1020
|
+
;
|
1021
|
+
sumres2 = temp;
|
1022
|
+
}
|
1023
|
+
month = 1
|
1024
|
+
+ sumres2;
|
1025
|
+
day = date - (french_to_rd(year, month, 1) - 1);
|
1026
|
+
if (rmonth)
|
1027
|
+
*rmonth = month;
|
1028
|
+
if (rday)
|
1029
|
+
*rday = day;
|
1030
|
+
if (ryear)
|
1031
|
+
*ryear = year;
|
1032
|
+
}
|
1033
|
+
|
1034
|
+
#undef quotient
|
1035
|
+
#define quotient(m, n) (floor(((double)(m)) / ((double)(n))))
|
1036
|
+
#undef mod
|
1037
|
+
#define mod(m, n) f_mod(m, n)
|
1038
|
+
static double
|
1039
|
+
f_mod(double m, double n)
|
1040
|
+
{
|
1041
|
+
double x;
|
1042
|
+
|
1043
|
+
x = fmod(m, n);
|
1044
|
+
if ((n < 0) ? (x > 0) : (x < 0))
|
1045
|
+
x += n;
|
1046
|
+
return x;
|
1047
|
+
}
|
1048
|
+
#undef oddp
|
1049
|
+
#define oddp(n) (((int)(n)) % 2)
|
1050
|
+
|
1051
|
+
#define solar_sidereal_year (365 + 279457.0 / 1080000)
|
1052
|
+
#define solar_month (solar_sidereal_year / 12)
|
1053
|
+
#define lunar_sidereal_month (27 + 4644439.0 / 14438334)
|
1054
|
+
#define lunar_synodic_month (29 + 7087771.0 / 13358334)
|
1055
|
+
|
1056
|
+
double
|
1057
|
+
solar_longitude(double days)
|
1058
|
+
{
|
1059
|
+
return mod(days / solar_sidereal_year, 1) * 360;
|
1060
|
+
}
|
1061
|
+
|
1062
|
+
double
|
1063
|
+
zodiac(double days)
|
1064
|
+
{
|
1065
|
+
return 1 + quotient(solar_longitude(days), 30);
|
1066
|
+
}
|
1067
|
+
|
1068
|
+
void
|
1069
|
+
rd_to_old_hindu_solar(int date, int *ryear, int *rmonth, int *rday)
|
1070
|
+
{
|
1071
|
+
double hdate;
|
1072
|
+
int year, month, day;
|
1073
|
+
|
1074
|
+
hdate = date + 1132959 + 1.0 / 4;
|
1075
|
+
year = quotient(hdate, solar_sidereal_year);
|
1076
|
+
month = zodiac(hdate);
|
1077
|
+
day = 1 + floor(mod(hdate, solar_month));
|
1078
|
+
if (rmonth)
|
1079
|
+
*rmonth = month;
|
1080
|
+
if (rday)
|
1081
|
+
*rday = day;
|
1082
|
+
if (ryear)
|
1083
|
+
*ryear = year;
|
1084
|
+
}
|
1085
|
+
|
1086
|
+
int
|
1087
|
+
old_hindu_solar_to_rd(int year, int month, int day)
|
1088
|
+
{
|
1089
|
+
return floor((year * solar_sidereal_year)
|
1090
|
+
+ ((month - 1) * solar_month)
|
1091
|
+
+ day - 1.0 / 4
|
1092
|
+
- 1132959);
|
1093
|
+
}
|
1094
|
+
|
1095
|
+
double
|
1096
|
+
lunar_longitude(double days)
|
1097
|
+
{
|
1098
|
+
return mod(days / lunar_sidereal_month, 1) * 360;
|
1099
|
+
}
|
1100
|
+
|
1101
|
+
double
|
1102
|
+
lunar_phase(double days)
|
1103
|
+
{
|
1104
|
+
return 1
|
1105
|
+
+ quotient
|
1106
|
+
(mod(lunar_longitude(days) - solar_longitude(days),
|
1107
|
+
360),
|
1108
|
+
12);
|
1109
|
+
}
|
1110
|
+
|
1111
|
+
double
|
1112
|
+
new_moon(double days)
|
1113
|
+
{
|
1114
|
+
return days - mod(days, lunar_synodic_month);
|
1115
|
+
}
|
1116
|
+
|
1117
|
+
void
|
1118
|
+
rd_to_old_hindu_lunar
|
1119
|
+
(int date, int *ryear, int *rmonth, int *rleapmonth, int *rday)
|
1120
|
+
{
|
1121
|
+
double hdate, sunrise, last_new_moon, next_new_moon, next_month;
|
1122
|
+
int day, month, leapmonth, year;
|
1123
|
+
|
1124
|
+
hdate = date + 1132959;
|
1125
|
+
sunrise = hdate + 1.0 / 4;
|
1126
|
+
last_new_moon = new_moon(sunrise);
|
1127
|
+
next_new_moon = last_new_moon + lunar_synodic_month;
|
1128
|
+
day = lunar_phase(sunrise);
|
1129
|
+
month = adjusted_mod(1 + zodiac (last_new_moon), 12);
|
1130
|
+
leapmonth = zodiac(last_new_moon) == zodiac (next_new_moon);
|
1131
|
+
next_month = next_new_moon + (leapmonth ? lunar_synodic_month : 0);
|
1132
|
+
year = quotient(next_month, solar_sidereal_year);
|
1133
|
+
if (rmonth)
|
1134
|
+
*rmonth = month;
|
1135
|
+
if (rleapmonth)
|
1136
|
+
*rleapmonth = leapmonth;
|
1137
|
+
if (rday)
|
1138
|
+
*rday = day;
|
1139
|
+
if (ryear)
|
1140
|
+
*ryear = year;
|
1141
|
+
}
|
1142
|
+
|
1143
|
+
int
|
1144
|
+
old_hindu_lunar_precedes
|
1145
|
+
(int year1, int month1, int leap1, int day1,
|
1146
|
+
int year2, int month2, int leap2, int day2)
|
1147
|
+
{
|
1148
|
+
return ((year1 < year2) ||
|
1149
|
+
((year1 == year2) &&
|
1150
|
+
((month1 < month2) ||
|
1151
|
+
((month1 == month2) &&
|
1152
|
+
((leap1 && !leap2) ||
|
1153
|
+
((leap1 == leap2) &&
|
1154
|
+
(day1 < day2)))))));
|
1155
|
+
}
|
1156
|
+
|
1157
|
+
int
|
1158
|
+
old_hindu_lunar_to_rd(int year, int month, int leapmonth, int day)
|
1159
|
+
{
|
1160
|
+
int years, months, approx, try,
|
1161
|
+
month1, leapmonth1, day1, year1;
|
1162
|
+
int sumres;
|
1163
|
+
|
1164
|
+
years = year;
|
1165
|
+
months = month - 2;
|
1166
|
+
approx = floor(years * solar_sidereal_year)
|
1167
|
+
+ floor(months * lunar_synodic_month)
|
1168
|
+
- 1132959;
|
1169
|
+
{
|
1170
|
+
int temp, i;
|
1171
|
+
for (temp = 0, i = approx;
|
1172
|
+
(rd_to_old_hindu_lunar
|
1173
|
+
(i, &year1, &month1, &leapmonth1, &day1),
|
1174
|
+
old_hindu_lunar_precedes
|
1175
|
+
(year1, month1, leapmonth1, day1,
|
1176
|
+
year, month, leapmonth, day));
|
1177
|
+
temp = temp + 1, i++)
|
1178
|
+
;
|
1179
|
+
sumres = temp;
|
1180
|
+
}
|
1181
|
+
try = approx
|
1182
|
+
+ sumres;
|
1183
|
+
rd_to_old_hindu_lunar(try, &year, &month1, &leapmonth1, &day1);
|
1184
|
+
if (month1 == month &&
|
1185
|
+
leapmonth1 == leapmonth &&
|
1186
|
+
day1 == day &&
|
1187
|
+
year1 == year)
|
1188
|
+
return try;
|
1189
|
+
return 0;
|
1190
|
+
}
|
1191
|
+
|
1192
|
+
|
1193
|
+
/*
|
1194
|
+
Local Variables:
|
1195
|
+
c-basic-offset: 2
|
1196
|
+
End:
|
1197
|
+
*/
|