-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaya.c
305 lines (261 loc) · 6.6 KB
/
maya.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
/*
* maya.c [email protected]
* マヤ暦算出プログラム 24 February 1991
* Copyright (c) 1991-1995,1996,1999, 2001, 2003, 2007 by Hiroshi Tsujimura
* All Rights Reserved.
*
* History :
* $Log: maya.c $
* Revision 1.63 2007/02/06 10:55:29 tsujimura543
* copyright 表記をアップデート
*
* Revision 1.62 2003/11/11 07:02:19 tsujimura543
* K&R 表記 → ANSI C 表記 に完全移行
*
* Revision 1.61 2003/03/11 02:22:57 tsujimura543
* for Tsuporone's Activity Memo
*
* Revision 1.60 2001/02/20 12:23:36 tsujimura543
* 他の派生バージョンと統合するため、いったん revision を固定
*
* Revision 1.51 1999/07/06 19:42:48 tsujimura543
* 開発環境を Win32 に移す
* -- HTML 出力対応作業や動作確認は今後もUNIX (peach.na.rim.or.jp) 上で行なう
*
* Revision 1.50 1996/08/19 12:01:10 tsujimura
* Withdrawal from `shizuka' (UNIX上での開発終了版)
*
* Revision 1.5 95/12/03 11:46:34 tsujimura
* コメントの整理・修正
*
* Revision 1.4 91/03/02 05:59:36 tsujimura
* test release
*
* Revision 1.3 91/03/02 05:59:36 tsujimura
* test version
*
* Revision 1.2 91/03/02 05:59:36 tsujimura
* changed the date of base for calculating Maya-calendar
*
* Revision 1.1 91/03/02 05:59:36 tsujimura
* Initial revision
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXKETA 20
#ifndef NUL
#define NUL '\0'
#endif
#ifndef lint
static char *rcs_id =
"$Header: C:/user/local/src/tamo/RCS/maya.c 1.63 2007/02/06 10:55:29 tsujimura543 Exp tsujimura543 $";
#endif
static char *tsolkin[] = {
"イミシ", "イク", "アクバル", "カン", "チッチャン", "キミ", "マニク",
"ラマト", "ムルク", "オク", "チュエン", "エブ", "ベン", "イシ", "メン",
"キブ", "カバン", "エツナブ", "カワク", "アハウ" };
static char *haabu[] = {
"ポップ", "ウオ", "シップ", "ソツ", "ツェク", "シュル", "ヤシキン",
"モル", "チェン", "ヤシ", "サク", "ケ", "マク", "カンキン", "ムアン",
"パシ", "カヤブ", "クムフ", "ワエィブ" };
char *
ten2twenty( long i )
{
static char buf[BUFSIZ];
char tmp[4];
long num[MAXKETA];
int j = 0;
while ( i >= 20 ) {
num[ j++ ] = i % 20;
i /= 20;
}
num[ j++ ] = i;
buf[ 0 ] = NUL;
while ( j-- ) {
sprintf( tmp, "%ld.", num[ j ] );
strcat( buf, tmp );
}
buf[ strlen( buf ) - 1 ] = NUL;
return ( buf );
}
long
twenty2ten( const char *s )
{
long i = 0, j = 0;
char tmp[4];
tmp[0] = NUL;
while ( *s ) {
if ( *s == '.' ) {
tmp[j] = NUL;
i = i * 20 + atol( tmp );
j = 0; tmp[0] = NUL; s++;
continue;
}
tmp[j++] = *s++;
}
tmp[j] = NUL;
i = i * 20 + atol( tmp );
return ( i );
}
char *
ten2twentyForCal( long i )
{
static char buf[BUFSIZ];
char tmp[5];
long num[MAXKETA];
long mod[2];
int j = 0;
mod[0] = 20; mod[1] = 18;
while ( ( j < 2 ) && ( i >= mod[j] ) ) {
num[j] = i % mod[j];
i /= mod[j]; j++;
}
while ( i >= 20 ) {
num[ j++ ] = i % 20;
i /= 20;
}
num[ j++ ] = i;
while ( j < 5 )
num[ j++ ] = 0;
buf[ 0 ] = NUL;
while ( j-- ) {
sprintf( tmp, "%ld.", num[ j ] );
strcat( buf, tmp );
}
buf[ strlen( buf ) - 1 ] = NUL;
return ( buf );
}
long
twenty2tenForCal( const char *s )
{
long l = 0;
int i = 0, j = 0;
long num[MAXKETA], mod[2];
char tmp[4];
mod[0] = 18; mod[1] = 20;
tmp[0] = NUL;
while ( *s ) {
if ( *s == '.' ) {
tmp[j] = NUL;
num[i++] = atol( tmp );
j = 0; tmp[0] = 0; s++;
continue;
}
tmp[j++] = *s++;
}
tmp[j] = NUL;
num[i++] = atol( tmp );
j = 0;
while ( j < i - 2 ) {
l = l * 20 + num[j++];
}
while ( j < i ) {
l = l * mod[2-(i-j)] + num[j++];
}
return ( l );
}
char *
mayaFromAbsolute( long date )
{
return ( ten2twentyForCal( date + 1137142L ) );
}
long
absoluteFromMaya( const char *s )
{
return ( twenty2tenForCal( s ) - 1137142L );
}
char *
tsolkinFromAbsolute( long date )
{
static char buf[16];
while ( date - 4 < 0 )
date += 13 * 20;
sprintf( buf, "%2ld%s", (date-4) % 13 + 1, tsolkin[(date+1) % 20] );
return ( buf );
}
char *
haabuFromAbsolute( long date )
{
static char buf[16];
while ( date + 150 < 0 )
date += 20 * 365;
date = ( date + 150 ) % 365;
sprintf( buf, "%2ld%s", date % 20, haabu[ date / 20 ] );
return ( buf );
}
char *
katunFromAbsolute( long date )
{
static char buf[80];
long ahau;
long baktun = 0, katun = 0, tun = 0, uinal = 0, kin = 0;
date += 1137142L;
if ( date >= 20 ) {
kin = date % 20;
date /= 20;
}
if ( date >= 18 ) {
uinal = date % 18;
date /= 18;
}
if ( date >= 20 ) {
tun = date % 20;
date /= 20;
}
if ( ( tun == 0 ) && ( uinal == 0 ) && ( kin == 0 ) )
date--;
if ( date >= 20 ) {
katun = date % 20;
date /= 20;
}
baktun = date;
ahau = ( baktun * 144000L + katun * 7200 + 1 ) % 13 + 1;
sprintf( buf, "%2ldアハウのカトゥン %2ldトゥン %2ldウィナル %2ldキン",
ahau, tun, uinal, kin );
return ( buf );
}
#ifdef TEST
int
main()
{
printf( "12345 -> %s -> %ld\n",
ten2twenty( 12345L ), twenty2ten( ten2twenty( 12345L ) ) );
printf( "7865782 -> %s -> %ld\n",
ten2twenty( 7865782 ), twenty2ten( ten2twenty( 7865782 ) ) );
printf( "10.4.3.6.9 -> %ld -> %s\n",
twenty2ten( "10.4.3.6.9" ),
ten2twenty( twenty2ten( "10.4.3.6.9" ) ) );
printf( "12345 -> %s -> %ld\n",
ten2twentyForCal( 12345L ),
twenty2tenForCal( ten2twentyForCal( 12345L ) ) );
printf( "7865782 -> %s -> %ld\n",
ten2twentyForCal( 7865782 ),
twenty2tenForCal( ten2twentyForCal( 7865782 ) ) );
printf( "244865782 -> %s -> %ld\n",
ten2twentyForCal( 244865782 ),
twenty2tenForCal( ten2twentyForCal( 244865782 ) ) );
printf( "10.4.3.6.9 -> %ld -> %s\n",
twenty2tenForCal( "10.4.3.6.9" ),
ten2twentyForCal( twenty2tenForCal( "10.4.3.6.9" ) ) );
printf( "5.9.4.8.9.2 -> %ld -> %s\n",
twenty2tenForCal( "5.9.4.8.9.2" ),
ten2twentyForCal( twenty2tenForCal( "5.9.4.8.9.2" ) ) );
printf( "12.15.7.8.9.6 -> %ld -> %s\n",
twenty2tenForCal( "12.15.7.8.9.6" ),
ten2twentyForCal( twenty2tenForCal( "12.15.7.8.9.6" ) ) );
printf( "12.18.17.11.14 -> %ld -> %s\n",
twenty2tenForCal( "12.18.17.11.14" ),
ten2twentyForCal( twenty2tenForCal( "12.18.17.11.14" ) ) );
printf( "8.14.3.1.12 -> %ld -> %s -> %s %s\n",
twenty2tenForCal( "8.14.3.1.12" ),
ten2twentyForCal( twenty2tenForCal( "8.14.3.1.12" ) ),
tsolkinFromAbsolute( absoluteFromMaya( "8.14.3.1.12" ) ),
haabuFromAbsolute( absoluteFromMaya( "8.14.3.1.12" ) ) );
printf( "9.14.13.4.17 -> %s %s\n",
tsolkinFromAbsolute( absoluteFromMaya( "9.14.13.4.17" ) ),
haabuFromAbsolute( absoluteFromMaya( "9.14.13.4.17" ) ) );
return ( 1 );
}
#endif