Home | History | Annotate | Download | only in i18n
      1 /*
      2 *******************************************************************************
      3 * Copyright (C) 2003-2009, 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 /**
    282  * The current era, for reference.
    283  */
    284 static const int32_t kCurrentEra = (kEraCount-1);  // int32_t to match the calendar field type
    285 
    286 static const int32_t kGregorianEpoch = 1970;    // used as the default value of EXTENDED_YEAR
    287 
    288 /* Some platforms don't like to export constants, like old Palm OS and some z/OS configurations. */
    289 uint32_t JapaneseCalendar::getCurrentEra() {
    290     return kCurrentEra;
    291 }
    292 
    293 JapaneseCalendar::JapaneseCalendar(const Locale& aLocale, UErrorCode& success)
    294 :   GregorianCalendar(aLocale, success)
    295 {
    296     setTimeInMillis(getNow(), success); // Call this again now that the vtable is set up properly.
    297 }
    298 
    299 JapaneseCalendar::~JapaneseCalendar()
    300 {
    301 }
    302 
    303 JapaneseCalendar::JapaneseCalendar(const JapaneseCalendar& source)
    304 : GregorianCalendar(source)
    305 {
    306 }
    307 
    308 JapaneseCalendar& JapaneseCalendar::operator= ( const JapaneseCalendar& right)
    309 {
    310     GregorianCalendar::operator=(right);
    311     return *this;
    312 }
    313 
    314 Calendar* JapaneseCalendar::clone(void) const
    315 {
    316     return new JapaneseCalendar(*this);
    317 }
    318 
    319 const char *JapaneseCalendar::getType() const
    320 {
    321     return "japanese";
    322 }
    323 
    324 int32_t JapaneseCalendar::getDefaultMonthInYear(int32_t eyear)
    325 {
    326     int32_t era = internalGetEra();
    327     // TODO do we assume we can trust 'era'?  What if it is denormalized?
    328 
    329     int32_t month = 0;
    330 
    331     // Find out if we are at the edge of an era
    332 
    333     if(eyear == 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 
    341 int32_t JapaneseCalendar::getDefaultDayInMonth(int32_t eyear, int32_t month)
    342 {
    343     int32_t era = internalGetEra();
    344     int32_t day = 1;
    345 
    346     if(eyear == kEraInfo[era].year) {
    347         if(month == (kEraInfo[era].month-1)) {
    348             return kEraInfo[era].day;
    349         }
    350     }
    351 
    352     return day;
    353 }
    354 
    355 
    356 int32_t JapaneseCalendar::internalGetEra() const
    357 {
    358     return internalGet(UCAL_ERA, kCurrentEra);
    359 }
    360 
    361 int32_t JapaneseCalendar::handleGetExtendedYear()
    362 {
    363     // EXTENDED_YEAR in JapaneseCalendar is a Gregorian year
    364     // The default value of EXTENDED_YEAR is 1970 (Showa 45)
    365     int32_t year;
    366 
    367     if (newerField(UCAL_EXTENDED_YEAR, UCAL_YEAR) == UCAL_EXTENDED_YEAR &&
    368         newerField(UCAL_EXTENDED_YEAR, UCAL_ERA) == UCAL_EXTENDED_YEAR) {
    369             year = internalGet(UCAL_EXTENDED_YEAR, kGregorianEpoch);
    370         } else {
    371             // Subtract one because year starts at 1
    372             year = internalGet(UCAL_YEAR) + kEraInfo[internalGetEra()].year - 1;
    373         }
    374         return year;
    375 }
    376 
    377 
    378 void JapaneseCalendar::handleComputeFields(int32_t julianDay, UErrorCode& status)
    379 {
    380     //Calendar::timeToFields(theTime, quick, status);
    381     GregorianCalendar::handleComputeFields(julianDay, status);
    382     int32_t year = internalGet(UCAL_EXTENDED_YEAR); // Gregorian year
    383 
    384     int32_t low = 0;
    385 
    386     // Short circuit for recent years.  Most modern computations will
    387     // occur in the current era and won't require the binary search.
    388     // Note that if the year is == the current era year, then we use
    389     // the binary search to handle the month/dom comparison.
    390 #ifdef U_DEBUG_JCAL
    391     fprintf(stderr, "==  %d \n", year);
    392 #endif
    393 
    394     if (year > kEraInfo[kCurrentEra].year) {
    395         low = kCurrentEra;
    396 #ifdef U_DEBUG_JCAL
    397         fprintf(stderr, " low=%d (special)\n", low);
    398 #endif
    399     } else {
    400         // Binary search
    401         int32_t high = kEraCount;
    402 
    403 #ifdef U_DEBUG_JCAL
    404         fprintf(stderr, " high=%d\n", high);
    405 #endif
    406         while (low < high - 1) {
    407             int32_t i = (low + high) / 2;
    408             int32_t diff = year - kEraInfo[i].year;
    409 
    410 #ifdef U_DEBUG_JCAL
    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",
    412                 diff,low, high, i, kEraInfo[i].month-1, kEraInfo[i].day,  kEraInfo[i].year, internalGet(UCAL_MONTH), internalGet(UCAL_DATE),year);
    413 #endif
    414 
    415             // If years are the same, then compare the months, and if those
    416             // are the same, compare days of month.  In the ERAS array
    417             // months are 1-based for easier maintenance.
    418             if (diff == 0) {
    419                 diff = internalGet(UCAL_MONTH) - (kEraInfo[i].month - 1);
    420 #ifdef U_DEBUG_JCAL
    421                 fprintf(stderr, "diff now %d (M)  = %d - %d - 1\n", diff, internalGet(UCAL_MONTH), kEraInfo[i].month);
    422 #endif
    423                 if (diff == 0) {
    424                     diff = internalGet(UCAL_DATE) - kEraInfo[i].day;
    425 #ifdef U_DEBUG_JCAL
    426                     fprintf(stderr, "diff now %d (D)\n", diff);
    427 #endif
    428                 }
    429             }
    430             if (diff >= 0) {
    431                 low = i;
    432             } else {
    433                 high = i;
    434             }
    435 #ifdef U_DEBUG_JCAL
    436             fprintf(stderr, ". low=%d, high=%d, i=%d, diff=%d.. %d\n", low, high, i, diff, year);
    437 #endif
    438 
    439         }
    440     }
    441 
    442 #ifdef U_DEBUG_JCAL
    443     fprintf(stderr, "  low[era]=%d,.. %d\n", low, year);
    444 #endif
    445     // Now we've found the last era that starts before this date, so
    446     // adjust the year to count from the start of that era.  Note that
    447     // all dates before the first era will fall into the first era by
    448     // the algorithm.
    449 
    450     internalSet(UCAL_ERA, low);
    451     internalSet(UCAL_YEAR, year - kEraInfo[low].year + 1);
    452 #ifdef U_DEBUG_JCAL
    453     fprintf(stderr, "  Set ERA=%d, year=%d\n", low, year-kEraInfo[low].year+1);
    454 #endif
    455 
    456 }
    457 
    458 /*
    459 Disable pivoting
    460 */
    461 UBool JapaneseCalendar::haveDefaultCentury() const
    462 {
    463     return FALSE;
    464 }
    465 
    466 UDate JapaneseCalendar::defaultCenturyStart() const
    467 {
    468     return 0;// WRONG
    469 }
    470 
    471 int32_t JapaneseCalendar::defaultCenturyStartYear() const
    472 {
    473     return 0;
    474 }
    475 
    476 int32_t JapaneseCalendar::handleGetLimit(UCalendarDateFields field, ELimitType limitType) const
    477 {
    478     switch(field) {
    479     case UCAL_ERA:
    480         if (limitType == UCAL_LIMIT_MINIMUM || limitType == UCAL_LIMIT_GREATEST_MINIMUM) {
    481             return 1;
    482         }
    483         return kCurrentEra;
    484     case UCAL_YEAR:
    485         {
    486             switch (limitType) {
    487             case UCAL_LIMIT_MINIMUM:
    488             case UCAL_LIMIT_GREATEST_MINIMUM:
    489                 return 1;
    490             case UCAL_LIMIT_LEAST_MAXIMUM:
    491                 return 1;
    492             case  UCAL_LIMIT_COUNT: //added to avoid warning
    493             case UCAL_LIMIT_MAXIMUM:
    494                 return GregorianCalendar::handleGetLimit(UCAL_YEAR, UCAL_LIMIT_MAXIMUM) - kEraInfo[kCurrentEra].year;
    495             default:
    496                 return 1;    // Error condition, invalid limitType
    497             }
    498         }
    499     default:
    500         return GregorianCalendar::handleGetLimit(field,limitType);
    501     }
    502 }
    503 
    504 int32_t JapaneseCalendar::getActualMaximum(UCalendarDateFields field, UErrorCode& status) const {
    505     if (field == UCAL_YEAR) {
    506         int32_t era = get(UCAL_ERA, status);
    507         if (U_FAILURE(status)) {
    508             return 0; // error case... any value
    509         }
    510         if (era == kCurrentEra) {
    511             // TODO: Investigate what value should be used here - revisit after 4.0.
    512             return handleGetLimit(UCAL_YEAR, UCAL_LIMIT_MAXIMUM);
    513         } else {
    514             int32_t nextEraYear = kEraInfo[era + 1].year;
    515             int32_t nextEraMonth = kEraInfo[era + 1].month;
    516             int32_t nextEraDate = kEraInfo[era + 1].day;
    517 
    518             int32_t maxYear = nextEraYear - kEraInfo[era].year + 1; // 1-base
    519             if (nextEraMonth == 1 && nextEraDate == 1) {
    520                 // Subtract 1, because the next era starts at Jan 1
    521                 maxYear--;
    522             }
    523             return maxYear;
    524         }
    525     }
    526     return GregorianCalendar::getActualMaximum(field, status);
    527 }
    528 
    529 U_NAMESPACE_END
    530 
    531 #endif
    532