1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
4 *******************************************************************************
5 * Copyright (C) 2003-2009,2012,2016 International Business Machines Corporation and
6 * others. All Rights Reserved.
7 *******************************************************************************
11 * Modification History:
12 * 05/16/2003 srl copied from buddhcal.cpp
16 #include "unicode/utypes.h"
18 #if !UCONFIG_NO_FORMATTING
22 #include "unicode/gregocal.h"
26 //#define U_DEBUG_JCAL
34 UOBJECT_DEFINE_RTTI_IMPLEMENTATION(JapaneseCalendar
)
36 // Gregorian date of each emperor's ascension
37 // Years are AD, months are 1-based.
44 { 645, 6, 19 }, // Taika 0
45 { 650, 2, 15 }, // Hakuchi 1
46 { 672, 1, 1 }, // Hakuho 2
47 { 686, 7, 20 }, // Shucho 3
48 { 701, 3, 21 }, // Taiho 4
49 { 704, 5, 10 }, // Keiun 5
50 { 708, 1, 11 }, // Wado 6
51 { 715, 9, 2 }, // Reiki 7
52 { 717, 11, 17 }, // Yoro 8
53 { 724, 2, 4 }, // Jinki 9
54 { 729, 8, 5 }, // Tempyo 10
55 { 749, 4, 14 }, // Tempyo-kampo 11
56 { 749, 7, 2 }, // Tempyo-shoho 12
57 { 757, 8, 18 }, // Tempyo-hoji 13
58 { 765, 1, 7 }, // Tempho-jingo 14
59 { 767, 8, 16 }, // Jingo-keiun 15
60 { 770, 10, 1 }, // Hoki 16
61 { 781, 1, 1 }, // Ten-o 17
62 { 782, 8, 19 }, // Enryaku 18
63 { 806, 5, 18 }, // Daido 19
64 { 810, 9, 19 }, // Konin 20
65 { 824, 1, 5 }, // Tencho
66 { 834, 1, 3 }, // Showa
67 { 848, 6, 13 }, // Kajo
68 { 851, 4, 28 }, // Ninju
69 { 854, 11, 30 }, // Saiko
70 { 857, 2, 21 }, // Tennan
71 { 859, 4, 15 }, // Jogan
72 { 877, 4, 16 }, // Genkei
73 { 885, 2, 21 }, // Ninna
74 { 889, 4, 27 }, // Kampyo 30
75 { 898, 4, 26 }, // Shotai
76 { 901, 7, 15 }, // Engi
77 { 923, 4, 11 }, // Encho
78 { 931, 4, 26 }, // Shohei
79 { 938, 5, 22 }, // Tengyo
80 { 947, 4, 22 }, // Tenryaku
81 { 957, 10, 27 }, // Tentoku
82 { 961, 2, 16 }, // Owa
83 { 964, 7, 10 }, // Koho
84 { 968, 8, 13 }, // Anna 40
85 { 970, 3, 25 }, // Tenroku
86 { 973, 12, 20 }, // Ten-en
87 { 976, 7, 13 }, // Jogen
88 { 978, 11, 29 }, // Tengen
89 { 983, 4, 15 }, // Eikan
90 { 985, 4, 27 }, // Kanna
91 { 987, 4, 5 }, // Ei-en
92 { 989, 8, 8 }, // Eiso
93 { 990, 11, 7 }, // Shoryaku
94 { 995, 2, 22 }, // Chotoku 50
95 { 999, 1, 13 }, // Choho
96 { 1004, 7, 20 }, // Kanko
97 { 1012, 12, 25 }, // Chowa
98 { 1017, 4, 23 }, // Kannin
99 { 1021, 2, 2 }, // Jian
100 { 1024, 7, 13 }, // Manju
101 { 1028, 7, 25 }, // Chogen
102 { 1037, 4, 21 }, // Choryaku
103 { 1040, 11, 10 }, // Chokyu
104 { 1044, 11, 24 }, // Kantoku 60
105 { 1046, 4, 14 }, // Eisho
106 { 1053, 1, 11 }, // Tengi
107 { 1058, 8, 29 }, // Kohei
108 { 1065, 8, 2 }, // Jiryaku
109 { 1069, 4, 13 }, // Enkyu
110 { 1074, 8, 23 }, // Shoho
111 { 1077, 11, 17 }, // Shoryaku
112 { 1081, 2, 10 }, // Eiho
113 { 1084, 2, 7 }, // Otoku
114 { 1087, 4, 7 }, // Kanji 70
115 { 1094, 12, 15 }, // Kaho
116 { 1096, 12, 17 }, // Eicho
117 { 1097, 11, 21 }, // Shotoku
118 { 1099, 8, 28 }, // Kowa
119 { 1104, 2, 10 }, // Choji
120 { 1106, 4, 9 }, // Kasho
121 { 1108, 8, 3 }, // Tennin
122 { 1110, 7, 13 }, // Ten-ei
123 { 1113, 7, 13 }, // Eikyu
124 { 1118, 4, 3 }, // Gen-ei 80
125 { 1120, 4, 10 }, // Hoan
126 { 1124, 4, 3 }, // Tenji
127 { 1126, 1, 22 }, // Daiji
128 { 1131, 1, 29 }, // Tensho
129 { 1132, 8, 11 }, // Chosho
130 { 1135, 4, 27 }, // Hoen
131 { 1141, 7, 10 }, // Eiji
132 { 1142, 4, 28 }, // Koji
133 { 1144, 2, 23 }, // Tenyo
134 { 1145, 7, 22 }, // Kyuan 90
135 { 1151, 1, 26 }, // Ninpei
136 { 1154, 10, 28 }, // Kyuju
137 { 1156, 4, 27 }, // Hogen
138 { 1159, 4, 20 }, // Heiji
139 { 1160, 1, 10 }, // Eiryaku
140 { 1161, 9, 4 }, // Oho
141 { 1163, 3, 29 }, // Chokan
142 { 1165, 6, 5 }, // Eiman
143 { 1166, 8, 27 }, // Nin-an
144 { 1169, 4, 8 }, // Kao 100
145 { 1171, 4, 21 }, // Shoan
146 { 1175, 7, 28 }, // Angen
147 { 1177, 8, 4 }, // Jisho
148 { 1181, 7, 14 }, // Yowa
149 { 1182, 5, 27 }, // Juei
150 { 1184, 4, 16 }, // Genryuku
151 { 1185, 8, 14 }, // Bunji
152 { 1190, 4, 11 }, // Kenkyu
153 { 1199, 4, 27 }, // Shoji
154 { 1201, 2, 13 }, // Kennin 110
155 { 1204, 2, 20 }, // Genkyu
156 { 1206, 4, 27 }, // Ken-ei
157 { 1207, 10, 25 }, // Shogen
158 { 1211, 3, 9 }, // Kenryaku
159 { 1213, 12, 6 }, // Kenpo
160 { 1219, 4, 12 }, // Shokyu
161 { 1222, 4, 13 }, // Joo
162 { 1224, 11, 20 }, // Gennin
163 { 1225, 4, 20 }, // Karoku
164 { 1227, 12, 10 }, // Antei 120
165 { 1229, 3, 5 }, // Kanki
166 { 1232, 4, 2 }, // Joei
167 { 1233, 4, 15 }, // Tempuku
168 { 1234, 11, 5 }, // Bunryaku
169 { 1235, 9, 19 }, // Katei
170 { 1238, 11, 23 }, // Ryakunin
171 { 1239, 2, 7 }, // En-o
172 { 1240, 7, 16 }, // Ninji
173 { 1243, 2, 26 }, // Kangen
174 { 1247, 2, 28 }, // Hoji 130
175 { 1249, 3, 18 }, // Kencho
176 { 1256, 10, 5 }, // Kogen
177 { 1257, 3, 14 }, // Shoka
178 { 1259, 3, 26 }, // Shogen
179 { 1260, 4, 13 }, // Bun-o
180 { 1261, 2, 20 }, // Kocho
181 { 1264, 2, 28 }, // Bun-ei
182 { 1275, 4, 25 }, // Kenji
183 { 1278, 2, 29 }, // Koan
184 { 1288, 4, 28 }, // Shoo 140
185 { 1293, 8, 55 }, // Einin
186 { 1299, 4, 25 }, // Shoan
187 { 1302, 11, 21 }, // Kengen
188 { 1303, 8, 5 }, // Kagen
189 { 1306, 12, 14 }, // Tokuji
190 { 1308, 10, 9 }, // Enkei
191 { 1311, 4, 28 }, // Ocho
192 { 1312, 3, 20 }, // Showa
193 { 1317, 2, 3 }, // Bunpo
194 { 1319, 4, 28 }, // Geno 150
195 { 1321, 2, 23 }, // Genkyo
196 { 1324, 12, 9 }, // Shochu
197 { 1326, 4, 26 }, // Kareki
198 { 1329, 8, 29 }, // Gentoku
199 { 1331, 8, 9 }, // Genko
200 { 1334, 1, 29 }, // Kemmu
201 { 1336, 2, 29 }, // Engen
202 { 1340, 4, 28 }, // Kokoku
203 { 1346, 12, 8 }, // Shohei
204 { 1370, 7, 24 }, // Kentoku 160
205 { 1372, 4, 1 }, // Bunch\u0169
206 { 1375, 5, 27 }, // Tenju
207 { 1379, 3, 22 }, // Koryaku
208 { 1381, 2, 10 }, // Kowa
209 { 1384, 4, 28 }, // Gench\u0169
210 { 1384, 2, 27 }, // Meitoku
211 { 1387, 8, 23 }, // Kakei
212 { 1389, 2, 9 }, // Koo
213 { 1390, 3, 26 }, // Meitoku
214 { 1394, 7, 5 }, // Oei 170
215 { 1428, 4, 27 }, // Shocho
216 { 1429, 9, 5 }, // Eikyo
217 { 1441, 2, 17 }, // Kakitsu
218 { 1444, 2, 5 }, // Bun-an
219 { 1449, 7, 28 }, // Hotoku
220 { 1452, 7, 25 }, // Kyotoku
221 { 1455, 7, 25 }, // Kosho
222 { 1457, 9, 28 }, // Choroku
223 { 1460, 12, 21 }, // Kansho
224 { 1466, 2, 28 }, // Bunsho 180
225 { 1467, 3, 3 }, // Onin
226 { 1469, 4, 28 }, // Bunmei
227 { 1487, 7, 29 }, // Chokyo
228 { 1489, 8, 21 }, // Entoku
229 { 1492, 7, 19 }, // Meio
230 { 1501, 2, 29 }, // Bunki
231 { 1504, 2, 30 }, // Eisho
232 { 1521, 8, 23 }, // Taiei
233 { 1528, 8, 20 }, // Kyoroku
234 { 1532, 7, 29 }, // Tenmon 190
235 { 1555, 10, 23 }, // Koji
236 { 1558, 2, 28 }, // Eiroku
237 { 1570, 4, 23 }, // Genki
238 { 1573, 7, 28 }, // Tensho
239 { 1592, 12, 8 }, // Bunroku
240 { 1596, 10, 27 }, // Keicho
241 { 1615, 7, 13 }, // Genwa
242 { 1624, 2, 30 }, // Kan-ei
243 { 1644, 12, 16 }, // Shoho
244 { 1648, 2, 15 }, // Keian 200
245 { 1652, 9, 18 }, // Shoo
246 { 1655, 4, 13 }, // Meiryaku
247 { 1658, 7, 23 }, // Manji
248 { 1661, 4, 25 }, // Kanbun
249 { 1673, 9, 21 }, // Enpo
250 { 1681, 9, 29 }, // Tenwa
251 { 1684, 2, 21 }, // Jokyo
252 { 1688, 9, 30 }, // Genroku
253 { 1704, 3, 13 }, // Hoei
254 { 1711, 4, 25 }, // Shotoku 210
255 { 1716, 6, 22 }, // Kyoho
256 { 1736, 4, 28 }, // Genbun
257 { 1741, 2, 27 }, // Kanpo
258 { 1744, 2, 21 }, // Enkyo
259 { 1748, 7, 12 }, // Kan-en
260 { 1751, 10, 27 }, // Horyaku
261 { 1764, 6, 2 }, // Meiwa
262 { 1772, 11, 16 }, // An-ei
263 { 1781, 4, 2 }, // Tenmei
264 { 1789, 1, 25 }, // Kansei 220
265 { 1801, 2, 5 }, // Kyowa
266 { 1804, 2, 11 }, // Bunka
267 { 1818, 4, 22 }, // Bunsei
268 { 1830, 12, 10 }, // Tenpo
269 { 1844, 12, 2 }, // Koka
270 { 1848, 2, 28 }, // Kaei
271 { 1854, 11, 27 }, // Ansei
272 { 1860, 3, 18 }, // Man-en
273 { 1861, 2, 19 }, // Bunkyu
274 { 1864, 2, 20 }, // Genji 230
275 { 1865, 4, 7 }, // Keio 231
276 { 1868, 9, 8 }, // Meiji 232
277 { 1912, 7, 30 }, // Taisho 233
278 { 1926, 12, 25 }, // Showa 234
279 { 1989, 1, 8 } // Heisei 235
282 #define kEraCount UPRV_LENGTHOF(kEraInfo)
285 * The current era, for reference.
287 static const int32_t kCurrentEra
= (kEraCount
-1); // int32_t to match the calendar field type
289 static const int32_t kGregorianEpoch
= 1970; // used as the default value of EXTENDED_YEAR
291 /* Some platforms don't like to export constants, like old Palm OS and some z/OS configurations. */
292 uint32_t JapaneseCalendar::getCurrentEra() {
296 JapaneseCalendar::JapaneseCalendar(const Locale
& aLocale
, UErrorCode
& success
)
297 : GregorianCalendar(aLocale
, success
)
299 setTimeInMillis(getNow(), success
); // Call this again now that the vtable is set up properly.
302 JapaneseCalendar::~JapaneseCalendar()
306 JapaneseCalendar::JapaneseCalendar(const JapaneseCalendar
& source
)
307 : GregorianCalendar(source
)
311 JapaneseCalendar
& JapaneseCalendar::operator= ( const JapaneseCalendar
& right
)
313 GregorianCalendar::operator=(right
);
317 Calendar
* JapaneseCalendar::clone(void) const
319 return new JapaneseCalendar(*this);
322 const char *JapaneseCalendar::getType() const
327 int32_t JapaneseCalendar::getDefaultMonthInYear(int32_t eyear
)
329 int32_t era
= internalGetEra();
330 // TODO do we assume we can trust 'era'? What if it is denormalized?
334 // Find out if we are at the edge of an era
336 if(eyear
== kEraInfo
[era
].year
) {
337 // Yes, we're in the first year of this era.
338 return kEraInfo
[era
].month
-1;
344 int32_t JapaneseCalendar::getDefaultDayInMonth(int32_t eyear
, int32_t month
)
346 int32_t era
= internalGetEra();
349 if(eyear
== kEraInfo
[era
].year
) {
350 if(month
== (kEraInfo
[era
].month
-1)) {
351 return kEraInfo
[era
].day
;
359 int32_t JapaneseCalendar::internalGetEra() const
361 return internalGet(UCAL_ERA
, kCurrentEra
);
364 int32_t JapaneseCalendar::handleGetExtendedYear()
366 // EXTENDED_YEAR in JapaneseCalendar is a Gregorian year
367 // The default value of EXTENDED_YEAR is 1970 (Showa 45)
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
);
374 // Subtract one because year starts at 1
375 year
= internalGet(UCAL_YEAR
) + kEraInfo
[internalGetEra()].year
- 1;
381 void JapaneseCalendar::handleComputeFields(int32_t julianDay
, UErrorCode
& status
)
383 //Calendar::timeToFields(theTime, quick, status);
384 GregorianCalendar::handleComputeFields(julianDay
, status
);
385 int32_t year
= internalGet(UCAL_EXTENDED_YEAR
); // Gregorian year
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.
394 fprintf(stderr
, "== %d \n", year
);
397 if (year
> kEraInfo
[kCurrentEra
].year
) {
400 fprintf(stderr
, " low=%d (special)\n", low
);
404 int32_t high
= kEraCount
;
407 fprintf(stderr
, " high=%d\n", high
);
409 while (low
< high
- 1) {
410 int32_t i
= (low
+ high
) / 2;
411 int32_t diff
= year
- kEraInfo
[i
].year
;
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
);
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.
422 diff
= internalGet(UCAL_MONTH
) - (kEraInfo
[i
].month
- 1);
424 fprintf(stderr
, "diff now %d (M) = %d - %d - 1\n", diff
, internalGet(UCAL_MONTH
), kEraInfo
[i
].month
);
427 diff
= internalGet(UCAL_DATE
) - kEraInfo
[i
].day
;
429 fprintf(stderr
, "diff now %d (D)\n", diff
);
439 fprintf(stderr
, ". low=%d, high=%d, i=%d, diff=%d.. %d\n", low
, high
, i
, diff
, year
);
446 fprintf(stderr
, " low[era]=%d,.. %d\n", low
, year
);
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
453 internalSet(UCAL_ERA
, low
);
454 internalSet(UCAL_YEAR
, year
- kEraInfo
[low
].year
+ 1);
456 fprintf(stderr
, " Set ERA=%d, year=%d\n", low
, year
-kEraInfo
[low
].year
+1);
464 UBool
JapaneseCalendar::haveDefaultCentury() const
469 UDate
JapaneseCalendar::defaultCenturyStart() const
474 int32_t JapaneseCalendar::defaultCenturyStartYear() const
479 int32_t JapaneseCalendar::handleGetLimit(UCalendarDateFields field
, ELimitType limitType
) const
483 if (limitType
== UCAL_LIMIT_MINIMUM
|| limitType
== UCAL_LIMIT_GREATEST_MINIMUM
) {
490 case UCAL_LIMIT_MINIMUM
:
491 case UCAL_LIMIT_GREATEST_MINIMUM
:
493 case UCAL_LIMIT_LEAST_MAXIMUM
:
495 case UCAL_LIMIT_COUNT
: //added to avoid warning
496 case UCAL_LIMIT_MAXIMUM
:
497 return GregorianCalendar::handleGetLimit(UCAL_YEAR
, UCAL_LIMIT_MAXIMUM
) - kEraInfo
[kCurrentEra
].year
;
499 return 1; // Error condition, invalid limitType
503 return GregorianCalendar::handleGetLimit(field
,limitType
);
507 int32_t JapaneseCalendar::getActualMaximum(UCalendarDateFields field
, UErrorCode
& status
) const {
508 if (field
== UCAL_YEAR
) {
509 int32_t era
= get(UCAL_ERA
, status
);
510 if (U_FAILURE(status
)) {
511 return 0; // error case... any value
513 if (era
== kCurrentEra
) {
514 // TODO: Investigate what value should be used here - revisit after 4.0.
515 return handleGetLimit(UCAL_YEAR
, UCAL_LIMIT_MAXIMUM
);
517 int32_t nextEraYear
= kEraInfo
[era
+ 1].year
;
518 int32_t nextEraMonth
= kEraInfo
[era
+ 1].month
;
519 int32_t nextEraDate
= kEraInfo
[era
+ 1].day
;
521 int32_t maxYear
= nextEraYear
- kEraInfo
[era
].year
+ 1; // 1-base
522 if (nextEraMonth
== 1 && nextEraDate
== 1) {
523 // Subtract 1, because the next era starts at Jan 1
529 return GregorianCalendar::getActualMaximum(field
, status
);