]> git.saurik.com Git - apple/icu.git/blame - icuSources/i18n/japancal.cpp
ICU-6.2.15.tar.gz
[apple/icu.git] / icuSources / i18n / japancal.cpp
CommitLineData
b75a7d8f
A
1/*
2*******************************************************************************
374ca955 3* Copyright (C) 2003-2004, International Business Machines Corporation and *
b75a7d8f
A
4* others. All Rights Reserved. *
5*******************************************************************************
6*
7* File JAPANCAL.CPP
8*
9* Modification History:
10* 05/16/2003 srl copied from buddhcal.cpp
11*
12*/
13
14#include "unicode/utypes.h"
15
16#if !UCONFIG_NO_FORMATTING
17
18#include "japancal.h"
19#include "unicode/gregocal.h"
374ca955
A
20#include "mutex.h"
21#include "uassert.h"
b75a7d8f
A
22
23//#define U_DEBUG_JCAL
24
25#ifdef U_DEBUG_JCAL
26#include <stdio.h>
27#endif
28
29U_NAMESPACE_BEGIN
30
374ca955 31UOBJECT_DEFINE_RTTI_IMPLEMENTATION(JapaneseCalendar)
b75a7d8f
A
32
33// Gregorian date of each emperor's ascension
34// Years are AD, months are 1-based.
35static const struct {
36 int16_t year;
37 int8_t month;
38 int8_t day;
39} kEraInfo[] = {
40 // Year Month Day
41 { 645, 6, 19 }, // Taika 0
42 { 650, 2, 15 }, // Hakuchi 1
43 { 672, 1, 1 }, // Hakuho 2
44 { 686, 7, 20 }, // Shucho 3
45 { 701, 3, 21 }, // Taiho 4
46 { 704, 5, 10 }, // Keiun 5
47 { 708, 1, 11 }, // Wado 6
48 { 715, 9, 2 }, // Reiki 7
49 { 717, 11, 17 }, // Yoro 8
50 { 724, 2, 4 }, // Jinki 9
51 { 729, 8, 5 }, // Tempyo 10
52 { 749, 4, 14 }, // Tempyo-kampo 11
53 { 749, 7, 2 }, // Tempyo-shoho 12
54 { 757, 8, 18 }, // Tempyo-hoji 13
55 { 765, 1, 7 }, // Tempho-jingo 14
56 { 767, 8, 16 }, // Jingo-keiun 15
57 { 770, 10, 1 }, // Hoki 16
58 { 781, 1, 1 }, // Ten-o 17
59 { 782, 8, 19 }, // Enryaku 18
60 { 806, 5, 18 }, // Daido 19
61 { 810, 9, 19 }, // Konin 20
62 { 824, 1, 5 }, // Tencho
63 { 834, 1, 3 }, // Showa
64 { 848, 6, 13 }, // Kajo
65 { 851, 4, 28 }, // Ninju
66 { 854, 11, 30 }, // Saiko
67 { 857, 2, 21 }, // Tennan
68 { 859, 4, 15 }, // Jogan
69 { 877, 4, 16 }, // Genkei
70 { 885, 2, 21 }, // Ninna
71 { 889, 4, 27 }, // Kampyo 30
72 { 898, 4, 26 }, // Shotai
73 { 901, 7, 15 }, // Engi
74 { 923, 4, 11 }, // Encho
75 { 931, 4, 26 }, // Shohei
76 { 938, 5, 22 }, // Tengyo
77 { 947, 4, 22 }, // Tenryaku
78 { 957, 10, 27 }, // Tentoku
79 { 961, 2, 16 }, // Owa
80 { 964, 7, 10 }, // Koho
81 { 968, 8, 13 }, // Anna 40
82 { 970, 3, 25 }, // Tenroku
83 { 973, 12, 20 }, // Ten-en
84 { 976, 7, 13 }, // Jogen
85 { 978, 11, 29 }, // Tengen
86 { 983, 4, 15 }, // Eikan
87 { 985, 4, 27 }, // Kanna
88 { 987, 4, 5 }, // Ei-en
89 { 989, 8, 8 }, // Eiso
90 { 990, 11, 7 }, // Shoryaku
91 { 995, 2, 22 }, // Chotoku 50
92 { 999, 1, 13 }, // Choho
93 { 1004, 7, 20 }, // Kanko
94 { 1012, 12, 25 }, // Chowa
95 { 1017, 4, 23 }, // Kannin
96 { 1021, 2, 2 }, // Jian
97 { 1024, 7, 13 }, // Manju
98 { 1028, 7, 25 }, // Chogen
99 { 1037, 4, 21 }, // Choryaku
100 { 1040, 11, 10 }, // Chokyu
101 { 1044, 11, 24 }, // Kantoku 60
102 { 1046, 4, 14 }, // Eisho
103 { 1053, 1, 11 }, // Tengi
104 { 1058, 8, 29 }, // Kohei
105 { 1065, 8, 2 }, // Jiryaku
106 { 1069, 4, 13 }, // Enkyu
107 { 1074, 8, 23 }, // Shoho
108 { 1077, 11, 17 }, // Shoryaku
109 { 1081, 2, 10 }, // Eiho
110 { 1084, 2, 7 }, // Otoku
111 { 1087, 4, 7 }, // Kanji 70
112 { 1094, 12, 15 }, // Kaho
113 { 1096, 12, 17 }, // Eicho
114 { 1097, 11, 21 }, // Shotoku
115 { 1099, 8, 28 }, // Kowa
116 { 1104, 2, 10 }, // Choji
117 { 1106, 4, 9 }, // Kasho
118 { 1108, 8, 3 }, // Tennin
119 { 1110, 7, 13 }, // Ten-ei
120 { 1113, 7, 13 }, // Eikyu
121 { 1118, 4, 3 }, // Gen-ei 80
122 { 1120, 4, 10 }, // Hoan
123 { 1124, 4, 3 }, // Tenji
124 { 1126, 1, 22 }, // Daiji
125 { 1131, 1, 29 }, // Tensho
126 { 1132, 8, 11 }, // Chosho
127 { 1135, 4, 27 }, // Hoen
128 { 1141, 7, 10 }, // Eiji
129 { 1142, 4, 28 }, // Koji
130 { 1144, 2, 23 }, // Tenyo
131 { 1145, 7, 22 }, // Kyuan 90
132 { 1151, 1, 26 }, // Ninpei
133 { 1154, 10, 28 }, // Kyuju
134 { 1156, 4, 27 }, // Hogen
135 { 1159, 4, 20 }, // Heiji
136 { 1160, 1, 10 }, // Eiryaku
137 { 1161, 9, 4 }, // Oho
138 { 1163, 3, 29 }, // Chokan
139 { 1165, 6, 5 }, // Eiman
140 { 1166, 8, 27 }, // Nin-an
141 { 1169, 4, 8 }, // Kao 100
142 { 1171, 4, 21 }, // Shoan
143 { 1175, 7, 28 }, // Angen
144 { 1177, 8, 4 }, // Jisho
145 { 1181, 7, 14 }, // Yowa
146 { 1182, 5, 27 }, // Juei
147 { 1184, 4, 16 }, // Genryuku
148 { 1185, 8, 14 }, // Bunji
149 { 1190, 4, 11 }, // Kenkyu
150 { 1199, 4, 27 }, // Shoji
151 { 1201, 2, 13 }, // Kennin 110
152 { 1204, 2, 20 }, // Genkyu
153 { 1206, 4, 27 }, // Ken-ei
154 { 1207, 10, 25 }, // Shogen
155 { 1211, 3, 9 }, // Kenryaku
156 { 1213, 12, 6 }, // Kenpo
157 { 1219, 4, 12 }, // Shokyu
158 { 1222, 4, 13 }, // Joo
159 { 1224, 11, 20 }, // Gennin
160 { 1225, 4, 20 }, // Karoku
161 { 1227, 12, 10 }, // Antei 120
162 { 1229, 3, 5 }, // Kanki
163 { 1232, 4, 2 }, // Joei
164 { 1233, 4, 15 }, // Tempuku
165 { 1234, 11, 5 }, // Bunryaku
166 { 1235, 9, 19 }, // Katei
167 { 1238, 11, 23 }, // Ryakunin
168 { 1239, 2, 7 }, // En-o
169 { 1240, 7, 16 }, // Ninji
170 { 1243, 2, 26 }, // Kangen
171 { 1247, 2, 28 }, // Hoji 130
172 { 1249, 3, 18 }, // Kencho
173 { 1256, 10, 5 }, // Kogen
174 { 1257, 3, 14 }, // Shoka
175 { 1259, 3, 26 }, // Shogen
176 { 1260, 4, 13 }, // Bun-o
177 { 1261, 2, 20 }, // Kocho
178 { 1264, 2, 28 }, // Bun-ei
179 { 1275, 4, 25 }, // Kenji
180 { 1278, 2, 29 }, // Koan
181 { 1288, 4, 28 }, // Shoo 140
182 { 1293, 8, 55 }, // Einin
183 { 1299, 4, 25 }, // Shoan
184 { 1302, 11, 21 }, // Kengen
185 { 1303, 8, 5 }, // Kagen
186 { 1306, 12, 14 }, // Tokuji
187 { 1308, 10, 9 }, // Enkei
188 { 1311, 4, 28 }, // Ocho
189 { 1312, 3, 20 }, // Showa
190 { 1317, 2, 3 }, // Bunpo
191 { 1319, 4, 28 }, // Geno 150
192 { 1321, 2, 23 }, // Genkyo
193 { 1324, 12, 9 }, // Shochu
194 { 1326, 4, 26 }, // Kareki
195 { 1329, 8, 29 }, // Gentoku
196 { 1331, 8, 9 }, // Genko
197 { 1334, 1, 29 }, // Kemmu
198 { 1336, 2, 29 }, // Engen
199 { 1340, 4, 28 }, // Kokoku
200 { 1346, 12, 8 }, // Shohei
201 { 1370, 7, 24 }, // Kentoku 160
202 { 1372, 4, 1 }, // Bunch\u0169
203 { 1375, 5, 27 }, // Tenju
374ca955 204 { 1379, 3, 22 }, // Koryaku
b75a7d8f
A
205 { 1381, 2, 10 }, // Kowa
206 { 1384, 4, 28 }, // Gench\u0169
207 { 1384, 2, 27 }, // Meitoku
b75a7d8f
A
208 { 1387, 8, 23 }, // Kakei
209 { 1389, 2, 9 }, // Koo
210 { 1390, 3, 26 }, // Meitoku
211 { 1394, 7, 5 }, // Oei 170
212 { 1428, 4, 27 }, // Shocho
213 { 1429, 9, 5 }, // Eikyo
214 { 1441, 2, 17 }, // Kakitsu
215 { 1444, 2, 5 }, // Bun-an
216 { 1449, 7, 28 }, // Hotoku
217 { 1452, 7, 25 }, // Kyotoku
218 { 1455, 7, 25 }, // Kosho
219 { 1457, 9, 28 }, // Choroku
220 { 1460, 12, 21 }, // Kansho
221 { 1466, 2, 28 }, // Bunsho 180
222 { 1467, 3, 3 }, // Onin
223 { 1469, 4, 28 }, // Bunmei
224 { 1487, 7, 29 }, // Chokyo
225 { 1489, 8, 21 }, // Entoku
226 { 1492, 7, 19 }, // Meio
227 { 1501, 2, 29 }, // Bunki
228 { 1504, 2, 30 }, // Eisho
229 { 1521, 8, 23 }, // Taiei
230 { 1528, 8, 20 }, // Kyoroku
231 { 1532, 7, 29 }, // Tenmon 190
232 { 1555, 10, 23 }, // Koji
233 { 1558, 2, 28 }, // Eiroku
234 { 1570, 4, 23 }, // Genki
235 { 1573, 7, 28 }, // Tensho
236 { 1592, 12, 8 }, // Bunroku
237 { 1596, 10, 27 }, // Keicho
238 { 1615, 7, 13 }, // Genwa
239 { 1624, 2, 30 }, // Kan-ei
240 { 1644, 12, 16 }, // Shoho
241 { 1648, 2, 15 }, // Keian 200
242 { 1652, 9, 18 }, // Shoo
243 { 1655, 4, 13 }, // Meiryaku
244 { 1658, 7, 23 }, // Manji
245 { 1661, 4, 25 }, // Kanbun
246 { 1673, 9, 21 }, // Enpo
247 { 1681, 9, 29 }, // Tenwa
248 { 1684, 2, 21 }, // Jokyo
249 { 1688, 9, 30 }, // Genroku
250 { 1704, 3, 13 }, // Hoei
251 { 1711, 4, 25 }, // Shotoku 210
252 { 1716, 6, 22 }, // Kyoho
253 { 1736, 4, 28 }, // Genbun
254 { 1741, 2, 27 }, // Kanpo
255 { 1744, 2, 21 }, // Enkyo
256 { 1748, 7, 12 }, // Kan-en
257 { 1751, 10, 27 }, // Horyaku
258 { 1764, 6, 2 }, // Meiwa
259 { 1772, 11, 16 }, // An-ei
260 { 1781, 4, 2 }, // Tenmei
261 { 1789, 1, 25 }, // Kansei 220
262 { 1801, 2, 5 }, // Kyowa
263 { 1804, 2, 11 }, // Bunka
264 { 1818, 4, 22 }, // Bunsei
265 { 1830, 12, 10 }, // Tenpo
266 { 1844, 12, 2 }, // Koka
267 { 1848, 2, 28 }, // Kaei
268 { 1854, 11, 27 }, // Ansei
269 { 1860, 3, 18 }, // Man-en
270 { 1861, 2, 19 }, // Bunkyu
271 { 1864, 2, 20 }, // Genji 230
272 { 1865, 4, 7 }, // Keio 231
273 { 1868, 9, 8 }, // Meiji 232
274 { 1912, 7, 30 }, // Taisho 233
275 { 1926, 12, 25 }, // Showa 234
276 { 1989, 1, 8 } // Heisei 235
277 };
278
279#define kEraCount (sizeof(kEraInfo)/sizeof(kEraInfo[0]))
280
281const uint32_t JapaneseCalendar::kCurrentEra = (kEraCount-1);
282
283JapaneseCalendar::JapaneseCalendar(const Locale& aLocale, UErrorCode& success)
284 : GregorianCalendar(aLocale, success)
285{
286 setTimeInMillis(getNow(), success); // Call this again now that the vtable is set up properly.
287}
288
289JapaneseCalendar::~JapaneseCalendar()
290{
291}
292
293JapaneseCalendar::JapaneseCalendar(const JapaneseCalendar& source)
294 : GregorianCalendar(source)
295{
296}
297
298JapaneseCalendar& JapaneseCalendar::operator= ( const JapaneseCalendar& right)
299{
300 GregorianCalendar::operator=(right);
301 return *this;
302}
303
304Calendar* JapaneseCalendar::clone(void) const
305{
306 return new JapaneseCalendar(*this);
307}
308
309const char *JapaneseCalendar::getType() const
310{
311 return "japanese";
312}
313
374ca955 314int32_t JapaneseCalendar::getDefaultMonthInYear()
b75a7d8f
A
315{
316 UErrorCode status = U_ZERO_ERROR;
317 int32_t era = internalGetEra();
374ca955
A
318 computeFields(status); // slow
319 int32_t year = getGregorianYear();
b75a7d8f
A
320 // TODO do we assume we can trust 'era'? What if it is denormalized?
321
322 int32_t month = GregorianCalendar::getDefaultMonthInYear();
323
324 // Find out if we are at the edge of an era
325
326 if(year == kEraInfo[era].year) {
327 // Yes, we're in the first year of this era.
328 return kEraInfo[era].month-1;
329 }
330
b75a7d8f
A
331 return month;
332}
333
374ca955 334int32_t JapaneseCalendar::getDefaultDayInMonth(int32_t month)
b75a7d8f
A
335{
336 UErrorCode status = U_ZERO_ERROR;
337 int32_t era = internalGetEra();
374ca955
A
338 computeFields(status); // slow
339 int32_t year = getGregorianYear();
b75a7d8f
A
340 int32_t day = GregorianCalendar::getDefaultDayInMonth(month);
341
342 if(year == kEraInfo[era].year) {
343 if(month == (kEraInfo[era].month-1)) {
344 return kEraInfo[era].day;
345 }
346 }
347
348 return day;
349}
350
351
352int32_t JapaneseCalendar::internalGetEra() const
353{
374ca955 354 return internalGet(UCAL_ERA, kCurrentEra);
b75a7d8f
A
355}
356
374ca955 357int32_t JapaneseCalendar::handleGetExtendedYear()
b75a7d8f 358{
374ca955
A
359 int32_t year;
360
361 if (newerField(UCAL_EXTENDED_YEAR, UCAL_YEAR) == UCAL_EXTENDED_YEAR &&
362 newerField(UCAL_EXTENDED_YEAR, UCAL_ERA) == UCAL_EXTENDED_YEAR) {
363 year = internalGet(UCAL_EXTENDED_YEAR, 1);
364 } else {
365 // Subtract one because year starts at 1
366 year = internalGet(UCAL_YEAR) + kEraInfo[internalGet(UCAL_ERA)].year - 1;
b75a7d8f 367 }
374ca955 368 return year;
b75a7d8f 369
b75a7d8f
A
370}
371
374ca955
A
372
373void JapaneseCalendar::handleComputeFields(int32_t julianDay, UErrorCode& status)
b75a7d8f 374{
374ca955
A
375 //Calendar::timeToFields(theTime, quick, status);
376 GregorianCalendar::handleComputeFields(julianDay, status);
377 int32_t year = internalGet(UCAL_EXTENDED_YEAR); // Gregorian year
b75a7d8f 378
b75a7d8f
A
379 int32_t low = 0;
380
381 // Short circuit for recent years. Most modern computations will
382 // occur in the current era and won't require the binary search.
383 // Note that if the year is == the current era year, then we use
384 // the binary search to handle the month/dom comparison.
385#ifdef U_DEBUG_JCAL
386 fprintf(stderr, "== %d:%d \n", era, year);
387#endif
388
389 if (year > kEraInfo[kCurrentEra].year) {
390 low = kCurrentEra;
391#ifdef U_DEBUG_JCAL
392 fprintf(stderr, " low=%d (special)\n", low);
393#endif
394 } else {
395 // Binary search
396 int32_t high = kEraCount;
397
398#ifdef U_DEBUG_JCAL
399 fprintf(stderr, " high=%d\n", high);
400#endif
401 while (low < high - 1) {
402 int32_t i = (low + high) / 2;
403 int32_t diff = year - kEraInfo[i].year;
404
405#ifdef U_DEBUG_JCAL
406 fprintf(stderr, " d=%d low=%d, high=%d. Considering %d:M%d D%d Y%d. { we are ?:M%d D%d Y%d }\n",
407 diff,low, high, i, kEraInfo[i].month-1, kEraInfo[i].day, kEraInfo[i].year, internalGet(UCAL_MONTH), internalGet(UCAL_DATE),year);
408#endif
409
410 // If years are the same, then compare the months, and if those
411 // are the same, compare days of month. In the ERAS array
412 // months are 1-based for easier maintenance.
413 if (diff == 0) {
414 diff = internalGet(UCAL_MONTH) - (kEraInfo[i].month - 1);
415#ifdef U_DEBUG_JCAL
416 fprintf(stderr, "diff now %d (M) = %d - %d - 1\n", diff, internalGet(UCAL_MONTH), kEraInfo[i].month);
417#endif
418 if (diff == 0) {
419 diff = internalGet(UCAL_DATE) - kEraInfo[i].day;
420#ifdef U_DEBUG_JCAL
421 fprintf(stderr, "diff now %d (D)\n", diff);
422#endif
423 }
424 }
425 if (diff >= 0) {
426 low = i;
427 } else {
428 high = i;
429 }
430#ifdef U_DEBUG_JCAL
431 fprintf(stderr, ". low=%d, high=%d, i=%d, diff=%d.. %d\n", low, high, i, diff, year);
432#endif
433
434 }
435 }
436
437#ifdef U_DEBUG_JCAL
438 fprintf(stderr, " low[era]=%d,.. %d\n", low, year);
439#endif
440 // Now we've found the last era that starts before this date, so
441 // adjust the year to count from the start of that era. Note that
442 // all dates before the first era will fall into the first era by
443 // the algorithm.
444
445 internalSet(UCAL_ERA, low);
446 internalSet(UCAL_YEAR, year - kEraInfo[low].year + 1);
447#ifdef U_DEBUG_JCAL
448 fprintf(stderr, " Set ERA=%d, year=%d\n", low, year-kEraInfo[low].year+1);
449#endif
450
451}
452
453/*
454 Disable pivoting
455*/
456UBool JapaneseCalendar::haveDefaultCentury() const
457{
458 return FALSE;
459}
460
461UDate JapaneseCalendar::defaultCenturyStart() const
462{
463 return 0;// WRONG
464}
465
466int32_t JapaneseCalendar::defaultCenturyStartYear() const
467{
468 return 0;
469}
470
374ca955
A
471static int32_t gJapanCalendarLimits[2][4] = {
472 // Minimum Greatest min Least max Greatest max
473 { 0, 0, JapaneseCalendar::kCurrentEra, JapaneseCalendar::kCurrentEra }, // ERA
474 { 1, 1, 0, 0 }, // YEAR least-max/greatest-max computed at runtime
475};
476
477static UBool gJapanYearLimitsKnown = FALSE;
478
479int32_t JapaneseCalendar::handleGetLimit(UCalendarDateFields field, ELimitType limitType) const
480{
481 switch(field) {
482 case UCAL_YEAR:
483 {
484 UBool needCalc = FALSE;
485 {
486 Mutex m;
487 needCalc = (gJapanYearLimitsKnown == FALSE);
488 }
489 if(needCalc) {
490 int32_t min = kEraInfo[1].year - kEraInfo[0].year;
491 int32_t max = min;
492 for (uint32_t i=2; i<kEraCount; i++) { // counting by year, not field (3's)
493 int32_t d = kEraInfo[i].year - kEraInfo[i-1].year;
494 U_ASSERT(d >= 0);
495 if (d < min) {
496 min = d;
497 }
498 if (d > max) {
499 max = d;
500 }
501 }
502 U_ASSERT(min >= 0 && max > min);
503 {
504 Mutex m;
505 if(gJapanYearLimitsKnown==FALSE) {
506 gJapanCalendarLimits[field][UCAL_LIMIT_LEAST_MAXIMUM] = ++min; // 1-based
507 gJapanCalendarLimits[field][UCAL_LIMIT_MAXIMUM] = ++max; // 1-based
508 gJapanYearLimitsKnown = TRUE;
509 }
510 }
511 }
512 return gJapanCalendarLimits[field][limitType];
513 }
514
515 case UCAL_ERA:
516 return gJapanCalendarLimits[field][limitType];
517
518 case UCAL_EXTENDED_YEAR: // extended year limits
519 switch(limitType) {
520 case UCAL_LIMIT_GREATEST_MINIMUM:
521 case UCAL_LIMIT_MINIMUM:
522 return kEraInfo[0].year; /* minimum is 1st era year */
523
524 case UCAL_LIMIT_LEAST_MAXIMUM:
525 case UCAL_LIMIT_MAXIMUM:
526 /* use Gregorian calendar max */
527 default:
528 return GregorianCalendar::handleGetLimit(field,limitType);
529 }
530 break;
531
532 default:
533 return GregorianCalendar::handleGetLimit(field,limitType);
534 }
535}
b75a7d8f
A
536
537
538U_NAMESPACE_END
539
540#endif