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