1 /*
2 *******************************************************************************
3 * Copyright (C) 2003-2007, International Business Machines Corporation and *
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"
20 #include "umutex.h"
21 #include "uassert.h"
22
23 //#define U_DEBUG_JCAL
24
25 #ifdef U_DEBUG_JCAL
26 #include <stdio.h>
27 #endif
28
29 U_NAMESPACE_BEGIN
30
31 UOBJECT_DEFINE_RTTI_IMPLEMENTATION(JapaneseCalendar)
32
33 // Gregorian date of each emperor's ascension
34 // Years are AD, months are 1-based.
35 static 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
204 { 1379, 3, 22 }, // Koryaku
205 { 1381, 2, 10 }, // Kowa
206 { 1384, 4, 28 }, // Gench\u0169
207 { 1384, 2, 27 }, // Meitoku
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
281 static const uint32_t kCurrentEra = (kEraCount-1);
282
283 static const int32_t kGregorianEpoch = 1970; // used as the default value of EXTENDED_YEAR
284
285 /* Some platforms don't like to export constants, like old Palm OS and some z/OS configurations. */
getCurrentEra()286 uint32_t JapaneseCalendar::getCurrentEra() {
287 return kCurrentEra;
288 }
289
JapaneseCalendar(const Locale & aLocale,UErrorCode & success)290 JapaneseCalendar::JapaneseCalendar(const Locale& aLocale, UErrorCode& success)
291 : GregorianCalendar(aLocale, success)
292 {
293 setTimeInMillis(getNow(), success); // Call this again now that the vtable is set up properly.
294 }
295
~JapaneseCalendar()296 JapaneseCalendar::~JapaneseCalendar()
297 {
298 }
299
JapaneseCalendar(const JapaneseCalendar & source)300 JapaneseCalendar::JapaneseCalendar(const JapaneseCalendar& source)
301 : GregorianCalendar(source)
302 {
303 }
304
operator =(const JapaneseCalendar & right)305 JapaneseCalendar& JapaneseCalendar::operator= ( const JapaneseCalendar& right)
306 {
307 GregorianCalendar::operator=(right);
308 return *this;
309 }
310
clone(void) const311 Calendar* JapaneseCalendar::clone(void) const
312 {
313 return new JapaneseCalendar(*this);
314 }
315
getType() const316 const char *JapaneseCalendar::getType() const
317 {
318 return "japanese";
319 }
320
getDefaultMonthInYear()321 int32_t JapaneseCalendar::getDefaultMonthInYear()
322 {
323 UErrorCode status = U_ZERO_ERROR;
324 int32_t era = internalGetEra();
325 computeFields(status); // slow
326 int32_t year = getGregorianYear();
327 // TODO do we assume we can trust 'era'? What if it is denormalized?
328
329 int32_t month = GregorianCalendar::getDefaultMonthInYear();
330
331 // Find out if we are at the edge of an era
332
333 if(year == kEraInfo[era].year) {
334 // Yes, we're in the first year of this era.
335 return kEraInfo[era].month-1;
336 }
337
338 return month;
339 }
340
getDefaultDayInMonth(int32_t month)341 int32_t JapaneseCalendar::getDefaultDayInMonth(int32_t month)
342 {
343 UErrorCode status = U_ZERO_ERROR;
344 int32_t era = internalGetEra();
345 computeFields(status); // slow
346 int32_t year = getGregorianYear();
347 int32_t day = GregorianCalendar::getDefaultDayInMonth(month);
348
349 if(year == kEraInfo[era].year) {
350 if(month == (kEraInfo[era].month-1)) {
351 return kEraInfo[era].day;
352 }
353 }
354
355 return day;
356 }
357
358
internalGetEra() const359 int32_t JapaneseCalendar::internalGetEra() const
360 {
361 return internalGet(UCAL_ERA, kCurrentEra);
362 }
363
handleGetExtendedYear()364 int32_t JapaneseCalendar::handleGetExtendedYear()
365 {
366 // EXTENDED_YEAR in JapaneseCalendar is a Gregorian year
367 // The default value of EXTENDED_YEAR is 1970 (Showa 45)
368 int32_t year;
369
370 if (newerField(UCAL_EXTENDED_YEAR, UCAL_YEAR) == UCAL_EXTENDED_YEAR &&
371 newerField(UCAL_EXTENDED_YEAR, UCAL_ERA) == UCAL_EXTENDED_YEAR) {
372 year = internalGet(UCAL_EXTENDED_YEAR, kGregorianEpoch);
373 } else {
374 // Subtract one because year starts at 1
375 year = internalGet(UCAL_YEAR) + kEraInfo[internalGetEra()].year - 1;
376 }
377 return year;
378 }
379
380
handleComputeFields(int32_t julianDay,UErrorCode & status)381 void JapaneseCalendar::handleComputeFields(int32_t julianDay, UErrorCode& status)
382 {
383 //Calendar::timeToFields(theTime, quick, status);
384 GregorianCalendar::handleComputeFields(julianDay, status);
385 int32_t year = internalGet(UCAL_EXTENDED_YEAR); // Gregorian year
386
387 int32_t low = 0;
388
389 // Short circuit for recent years. Most modern computations will
390 // occur in the current era and won't require the binary search.
391 // Note that if the year is == the current era year, then we use
392 // the binary search to handle the month/dom comparison.
393 #ifdef U_DEBUG_JCAL
394 fprintf(stderr, "== %d \n", year);
395 #endif
396
397 if (year > kEraInfo[kCurrentEra].year) {
398 low = kCurrentEra;
399 #ifdef U_DEBUG_JCAL
400 fprintf(stderr, " low=%d (special)\n", low);
401 #endif
402 } else {
403 // Binary search
404 int32_t high = kEraCount;
405
406 #ifdef U_DEBUG_JCAL
407 fprintf(stderr, " high=%d\n", high);
408 #endif
409 while (low < high - 1) {
410 int32_t i = (low + high) / 2;
411 int32_t diff = year - kEraInfo[i].year;
412
413 #ifdef U_DEBUG_JCAL
414 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",
415 diff,low, high, i, kEraInfo[i].month-1, kEraInfo[i].day, kEraInfo[i].year, internalGet(UCAL_MONTH), internalGet(UCAL_DATE),year);
416 #endif
417
418 // If years are the same, then compare the months, and if those
419 // are the same, compare days of month. In the ERAS array
420 // months are 1-based for easier maintenance.
421 if (diff == 0) {
422 diff = internalGet(UCAL_MONTH) - (kEraInfo[i].month - 1);
423 #ifdef U_DEBUG_JCAL
424 fprintf(stderr, "diff now %d (M) = %d - %d - 1\n", diff, internalGet(UCAL_MONTH), kEraInfo[i].month);
425 #endif
426 if (diff == 0) {
427 diff = internalGet(UCAL_DATE) - kEraInfo[i].day;
428 #ifdef U_DEBUG_JCAL
429 fprintf(stderr, "diff now %d (D)\n", diff);
430 #endif
431 }
432 }
433 if (diff >= 0) {
434 low = i;
435 } else {
436 high = i;
437 }
438 #ifdef U_DEBUG_JCAL
439 fprintf(stderr, ". low=%d, high=%d, i=%d, diff=%d.. %d\n", low, high, i, diff, year);
440 #endif
441
442 }
443 }
444
445 #ifdef U_DEBUG_JCAL
446 fprintf(stderr, " low[era]=%d,.. %d\n", low, year);
447 #endif
448 // Now we've found the last era that starts before this date, so
449 // adjust the year to count from the start of that era. Note that
450 // all dates before the first era will fall into the first era by
451 // the algorithm.
452
453 internalSet(UCAL_ERA, low);
454 internalSet(UCAL_YEAR, year - kEraInfo[low].year + 1);
455 #ifdef U_DEBUG_JCAL
456 fprintf(stderr, " Set ERA=%d, year=%d\n", low, year-kEraInfo[low].year+1);
457 #endif
458
459 }
460
461 /*
462 Disable pivoting
463 */
haveDefaultCentury() const464 UBool JapaneseCalendar::haveDefaultCentury() const
465 {
466 return FALSE;
467 }
468
defaultCenturyStart() const469 UDate JapaneseCalendar::defaultCenturyStart() const
470 {
471 return 0;// WRONG
472 }
473
defaultCenturyStartYear() const474 int32_t JapaneseCalendar::defaultCenturyStartYear() const
475 {
476 return 0;
477 }
478
479 static int32_t gJapanCalendarLimits[2][4] = {
480 // Minimum Greatest min Least max Greatest max
481 { 0, 0, kCurrentEra, kCurrentEra }, // ERA
482 { 1, 1, 0, 0 }, // YEAR least-max/greatest-max computed at runtime
483 };
484
485 static UBool gJapanYearLimitsKnown = FALSE;
486
handleGetLimit(UCalendarDateFields field,ELimitType limitType) const487 int32_t JapaneseCalendar::handleGetLimit(UCalendarDateFields field, ELimitType limitType) const
488 {
489 switch(field) {
490 case UCAL_YEAR:
491 {
492 UBool needCalc;
493 UMTX_CHECK(NULL, (gJapanYearLimitsKnown == FALSE), needCalc);
494 if(needCalc) {
495 int32_t min = kEraInfo[1].year - kEraInfo[0].year;
496 int32_t max = min;
497 for (uint32_t i=2; i<kEraCount; i++) { // counting by year, not field (3's)
498 int32_t d = kEraInfo[i].year - kEraInfo[i-1].year;
499 U_ASSERT(d >= 0);
500 if (d < min) {
501 min = d;
502 }
503 if (d > max) {
504 max = d;
505 }
506 }
507 U_ASSERT(min >= 0 && max > min);
508
509 umtx_lock(NULL);
510 if(gJapanYearLimitsKnown==FALSE) {
511 gJapanCalendarLimits[field][UCAL_LIMIT_LEAST_MAXIMUM] = ++min; // 1-based
512 gJapanCalendarLimits[field][UCAL_LIMIT_MAXIMUM] = ++max; // 1-based
513 gJapanYearLimitsKnown = TRUE;
514 }
515 umtx_unlock(NULL);
516 }
517 return gJapanCalendarLimits[field][limitType];
518 }
519
520 case UCAL_ERA:
521 return gJapanCalendarLimits[field][limitType];
522
523 case UCAL_EXTENDED_YEAR: // extended year limits
524 switch(limitType) {
525 case UCAL_LIMIT_GREATEST_MINIMUM:
526 case UCAL_LIMIT_MINIMUM:
527 return kEraInfo[0].year; /* minimum is 1st era year */
528
529 case UCAL_LIMIT_LEAST_MAXIMUM:
530 case UCAL_LIMIT_MAXIMUM:
531 /* use Gregorian calendar max */
532 default:
533 return GregorianCalendar::handleGetLimit(field,limitType);
534 }
535 break;
536
537 default:
538 return GregorianCalendar::handleGetLimit(field,limitType);
539 }
540 }
541
542
543 U_NAMESPACE_END
544
545 #endif
546