LCOV - code coverage report
Current view: top level - intl/icu/source/i18n - indiancal.cpp (source / functions) Hit Total Coverage
Test: output.info Lines: 0 158 0.0 %
Date: 2017-07-14 16:53:18 Functions: 0 25 0.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : // © 2016 and later: Unicode, Inc. and others.
       2             : // License & terms of use: http://www.unicode.org/copyright.html
       3             : /*
       4             :  * Copyright (C) 2003-2014, International Business Machines Corporation
       5             :  * and others. All Rights Reserved.
       6             :  ******************************************************************************
       7             :  *
       8             :  * File INDIANCAL.CPP
       9             :  *****************************************************************************
      10             :  */
      11             : 
      12             : #include "indiancal.h"
      13             : #include <stdlib.h>
      14             : #if !UCONFIG_NO_FORMATTING
      15             : 
      16             : #include "mutex.h"
      17             : #include <float.h>
      18             : #include "gregoimp.h" // Math
      19             : #include "astro.h" // CalendarAstronomer
      20             : #include "uhash.h"
      21             : 
      22             : // Debugging
      23             : #ifdef U_DEBUG_INDIANCAL
      24             : #include <stdio.h>
      25             : #include <stdarg.h>
      26             : 
      27             : #endif
      28             : 
      29             : U_NAMESPACE_BEGIN
      30             : 
      31             : // Implementation of the IndianCalendar class
      32             : 
      33             : //-------------------------------------------------------------------------
      34             : // Constructors...
      35             : //-------------------------------------------------------------------------
      36             : 
      37             : 
      38           0 : Calendar* IndianCalendar::clone() const {
      39           0 :   return new IndianCalendar(*this);
      40             : }
      41             : 
      42           0 : IndianCalendar::IndianCalendar(const Locale& aLocale, UErrorCode& success)
      43           0 :   :   Calendar(TimeZone::createDefault(), aLocale, success)
      44             : {
      45           0 :   setTimeInMillis(getNow(), success); // Call this again now that the vtable is set up properly.
      46           0 : }
      47             : 
      48           0 : IndianCalendar::IndianCalendar(const IndianCalendar& other) : Calendar(other) {
      49           0 : }
      50             : 
      51           0 : IndianCalendar::~IndianCalendar()
      52             : {
      53           0 : }
      54           0 : const char *IndianCalendar::getType() const { 
      55           0 :    return "indian";
      56             : }
      57             :   
      58             : static const int32_t LIMITS[UCAL_FIELD_COUNT][4] = {
      59             :     // Minimum  Greatest     Least   Maximum
      60             :     //           Minimum   Maximum
      61             :     {        0,        0,        0,        0}, // ERA
      62             :     { -5000000, -5000000,  5000000,  5000000}, // YEAR
      63             :     {        0,        0,       11,       11}, // MONTH
      64             :     {        1,        1,       52,       53}, // WEEK_OF_YEAR
      65             :     {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // WEEK_OF_MONTH
      66             :     {        1,        1,       30,       31}, // DAY_OF_MONTH
      67             :     {        1,        1,      365,      366}, // DAY_OF_YEAR
      68             :     {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // DAY_OF_WEEK
      69             :     {       -1,       -1,        5,        5}, // DAY_OF_WEEK_IN_MONTH
      70             :     {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // AM_PM
      71             :     {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // HOUR
      72             :     {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // HOUR_OF_DAY
      73             :     {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // MINUTE
      74             :     {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // SECOND
      75             :     {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // MILLISECOND
      76             :     {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // ZONE_OFFSET
      77             :     {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // DST_OFFSET
      78             :     { -5000000, -5000000,  5000000,  5000000}, // YEAR_WOY
      79             :     {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // DOW_LOCAL
      80             :     { -5000000, -5000000,  5000000,  5000000}, // EXTENDED_YEAR
      81             :     {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // JULIAN_DAY
      82             :     {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // MILLISECONDS_IN_DAY
      83             :     {/*N/A*/-1,/*N/A*/-1,/*N/A*/-1,/*N/A*/-1}, // IS_LEAP_MONTH
      84             : };
      85             : 
      86             : static const double JULIAN_EPOCH = 1721425.5;
      87             : static const int32_t INDIAN_ERA_START  = 78;
      88             : static const int32_t INDIAN_YEAR_START = 80;
      89             : 
      90           0 : int32_t IndianCalendar::handleGetLimit(UCalendarDateFields field, ELimitType limitType) const {
      91           0 :   return LIMITS[field][limitType];
      92             : }
      93             : 
      94             : /*
      95             :  * Determine whether the given gregorian year is a Leap year 
      96             :  */
      97           0 : static UBool isGregorianLeap(int32_t year)
      98             : {
      99           0 :     return ((year % 4) == 0) && (!(((year % 100) == 0) && ((year % 400) != 0))); 
     100             : }
     101             :   
     102             : //----------------------------------------------------------------------
     103             : // Calendar framework
     104             : //----------------------------------------------------------------------
     105             : 
     106             : /*
     107             :  * Return the length (in days) of the given month.
     108             :  *
     109             :  * @param eyear  The year in Saka Era
     110             :  * @param month  The month(0-based) in Indian calendar
     111             :  */
     112           0 : int32_t IndianCalendar::handleGetMonthLength(int32_t eyear, int32_t month) const {
     113           0 :    if (month < 0 || month > 11) {
     114           0 :       eyear += ClockMath::floorDivide(month, 12, month);
     115             :    }
     116             : 
     117           0 :    if (isGregorianLeap(eyear + INDIAN_ERA_START) && month == 0) {
     118           0 :        return 31;
     119             :    }
     120             : 
     121           0 :    if (month >= 1 && month <= 5) {
     122           0 :        return 31;
     123             :    }
     124             : 
     125           0 :    return 30;
     126             : }
     127             : 
     128             : /*
     129             :  * Return the number of days in the given Indian year
     130             :  *
     131             :  * @param eyear The year in Saka Era.
     132             :  */
     133           0 : int32_t IndianCalendar::handleGetYearLength(int32_t eyear) const {
     134           0 :     return isGregorianLeap(eyear + INDIAN_ERA_START) ? 366 : 365;
     135             : }
     136             : /*
     137             :  * Returns the Julian Day corresponding to gregorian date
     138             :  *
     139             :  * @param year The Gregorian year
     140             :  * @param month The month in Gregorian Year
     141             :  * @param date The date in Gregorian day in month
     142             :  */
     143           0 : static double gregorianToJD(int32_t year, int32_t month, int32_t date) {
     144           0 :    double julianDay = (JULIAN_EPOCH - 1) +
     145           0 :       (365 * (year - 1)) +
     146           0 :       uprv_floor((year - 1) / 4) +
     147           0 :       (-uprv_floor((year - 1) / 100)) +
     148           0 :       uprv_floor((year - 1) / 400) +
     149           0 :       uprv_floor((((367 * month) - 362) / 12) +
     150           0 :             ((month <= 2) ? 0 :
     151           0 :              (isGregorianLeap(year) ? -1 : -2)
     152           0 :             ) +
     153           0 :             date);
     154             : 
     155           0 :    return julianDay;
     156             : }
     157             : 
     158             : /*
     159             :  * Returns the Gregorian Date corresponding to a given Julian Day
     160             :  * @param jd The Julian Day
     161             :  */
     162           0 : static int32_t* jdToGregorian(double jd, int32_t gregorianDate[3]) {
     163             :    double wjd, depoch, quadricent, dqc, cent, dcent, quad, dquad, yindex, yearday, leapadj;
     164             :    int32_t year, month, day;
     165           0 :    wjd = uprv_floor(jd - 0.5) + 0.5;
     166           0 :    depoch = wjd - JULIAN_EPOCH;
     167           0 :    quadricent = uprv_floor(depoch / 146097);
     168           0 :    dqc = (int32_t)uprv_floor(depoch) % 146097;
     169           0 :    cent = uprv_floor(dqc / 36524);
     170           0 :    dcent = (int32_t)uprv_floor(dqc) % 36524;
     171           0 :    quad = uprv_floor(dcent / 1461);
     172           0 :    dquad = (int32_t)uprv_floor(dcent) % 1461;
     173           0 :    yindex = uprv_floor(dquad / 365);
     174           0 :    year = (int32_t)((quadricent * 400) + (cent * 100) + (quad * 4) + yindex);
     175           0 :    if (!((cent == 4) || (yindex == 4))) {
     176           0 :       year++;
     177             :    }
     178           0 :    yearday = wjd - gregorianToJD(year, 1, 1);
     179           0 :    leapadj = ((wjd < gregorianToJD(year, 3, 1)) ? 0
     180             :          :
     181           0 :          (isGregorianLeap(year) ? 1 : 2)
     182           0 :          );
     183           0 :    month = (int32_t)uprv_floor((((yearday + leapadj) * 12) + 373) / 367);
     184           0 :    day = (int32_t)(wjd - gregorianToJD(year, month, 1)) + 1;
     185             : 
     186           0 :    gregorianDate[0] = year;
     187           0 :    gregorianDate[1] = month;
     188           0 :    gregorianDate[2] = day;
     189             : 
     190           0 :    return gregorianDate;
     191             : }
     192             : 
     193             :    
     194             : //-------------------------------------------------------------------------
     195             : // Functions for converting from field values to milliseconds....
     196             : //-------------------------------------------------------------------------
     197           0 : static double IndianToJD(int32_t year, int32_t month, int32_t date) {
     198             :    int32_t leapMonth, gyear, m;
     199             :    double start, jd;
     200             : 
     201           0 :    gyear = year + INDIAN_ERA_START;
     202             : 
     203             : 
     204           0 :    if(isGregorianLeap(gyear)) {
     205           0 :       leapMonth = 31;
     206           0 :       start = gregorianToJD(gyear, 3, 21);
     207             :    } 
     208             :    else {
     209           0 :       leapMonth = 30;
     210           0 :       start = gregorianToJD(gyear, 3, 22);
     211             :    }
     212             : 
     213           0 :    if (month == 1) {
     214           0 :       jd = start + (date - 1);
     215             :    } else {
     216           0 :       jd = start + leapMonth;
     217           0 :       m = month - 2;
     218             : 
     219             :       //m = Math.min(m, 5);
     220           0 :       if (m > 5) {
     221           0 :           m = 5;
     222             :       }
     223             : 
     224           0 :       jd += m * 31;
     225             : 
     226           0 :       if (month >= 8) {
     227           0 :          m = month - 7;
     228           0 :          jd += m * 30;
     229             :       }
     230           0 :       jd += date - 1;
     231             :    }
     232             : 
     233           0 :    return jd;
     234             : }
     235             : 
     236             : /*
     237             :  * Return JD of start of given month/year of Indian Calendar
     238             :  * @param eyear The year in Indian Calendar measured from Saka Era (78 AD).
     239             :  * @param month The month in Indian calendar
     240             :  */
     241           0 : int32_t IndianCalendar::handleComputeMonthStart(int32_t eyear, int32_t month, UBool /* useMonth */ ) const {
     242             : 
     243             :    //month is 0 based; converting it to 1-based 
     244             :    int32_t imonth;
     245             : 
     246             :     // If the month is out of range, adjust it into range, and adjust the extended eyar accordingly
     247           0 :    if (month < 0 || month > 11) {
     248           0 :       eyear += (int32_t)ClockMath::floorDivide(month, 12, month);
     249             :    }
     250             : 
     251           0 :    if(month == 12){
     252           0 :        imonth = 1;
     253             :    } else {
     254           0 :        imonth = month + 1; 
     255             :    }
     256             :    
     257           0 :    double jd = IndianToJD(eyear ,imonth, 1);
     258             : 
     259           0 :    return (int32_t)jd;
     260             : }
     261             : 
     262             : //-------------------------------------------------------------------------
     263             : // Functions for converting from milliseconds to field values
     264             : //-------------------------------------------------------------------------
     265             : 
     266           0 : int32_t IndianCalendar::handleGetExtendedYear() {
     267             :     int32_t year;
     268             : 
     269           0 :     if (newerField(UCAL_EXTENDED_YEAR, UCAL_YEAR) == UCAL_EXTENDED_YEAR) {
     270           0 :         year = internalGet(UCAL_EXTENDED_YEAR, 1); // Default to year 1
     271             :     } else {
     272           0 :         year = internalGet(UCAL_YEAR, 1); // Default to year 1
     273             :     }
     274             : 
     275           0 :     return year;
     276             : }
     277             : 
     278             : /*
     279             :  * Override Calendar to compute several fields specific to the Indian
     280             :  * calendar system.  These are:
     281             :  *
     282             :  * <ul><li>ERA
     283             :  * <li>YEAR
     284             :  * <li>MONTH
     285             :  * <li>DAY_OF_MONTH
     286             :  * <li>EXTENDED_YEAR</ul>
     287             :  * 
     288             :  * The DAY_OF_WEEK and DOW_LOCAL fields are already set when this
     289             :  * method is called. The getGregorianXxx() methods return Gregorian
     290             :  * calendar equivalents for the given Julian day.
     291             :  */
     292           0 : void IndianCalendar::handleComputeFields(int32_t julianDay, UErrorCode&  /* status */) {
     293             :     double jdAtStartOfGregYear;
     294             :     int32_t leapMonth, IndianYear, yday, IndianMonth, IndianDayOfMonth, mday;
     295             :     int32_t gregorianYear;      // Stores gregorian date corresponding to Julian day;
     296             :     int32_t gd[3];
     297             : 
     298           0 :     gregorianYear = jdToGregorian(julianDay, gd)[0];          // Gregorian date for Julian day
     299           0 :     IndianYear = gregorianYear - INDIAN_ERA_START;            // Year in Saka era
     300           0 :     jdAtStartOfGregYear = gregorianToJD(gregorianYear, 1, 1); // JD at start of Gregorian year
     301           0 :     yday = (int32_t)(julianDay - jdAtStartOfGregYear);        // Day number in Gregorian year (starting from 0)
     302             : 
     303           0 :     if (yday < INDIAN_YEAR_START) {
     304             :         // Day is at the end of the preceding Saka year
     305           0 :         IndianYear -= 1;
     306           0 :         leapMonth = isGregorianLeap(gregorianYear - 1) ? 31 : 30; // Days in leapMonth this year, previous Gregorian year
     307           0 :         yday += leapMonth + (31 * 5) + (30 * 3) + 10;
     308             :     } else {
     309           0 :         leapMonth = isGregorianLeap(gregorianYear) ? 31 : 30; // Days in leapMonth this year
     310           0 :         yday -= INDIAN_YEAR_START;
     311             :     }
     312             : 
     313           0 :     if (yday < leapMonth) {
     314           0 :         IndianMonth = 0;
     315           0 :         IndianDayOfMonth = yday + 1;
     316             :     } else {
     317           0 :         mday = yday - leapMonth;
     318           0 :         if (mday < (31 * 5)) {
     319           0 :             IndianMonth = (int32_t)uprv_floor(mday / 31) + 1;
     320           0 :             IndianDayOfMonth = (mday % 31) + 1;
     321             :         } else {
     322           0 :             mday -= 31 * 5;
     323           0 :             IndianMonth = (int32_t)uprv_floor(mday / 30) + 6;
     324           0 :             IndianDayOfMonth = (mday % 30) + 1;
     325             :         }
     326             :    }
     327             : 
     328           0 :    internalSet(UCAL_ERA, 0);
     329           0 :    internalSet(UCAL_EXTENDED_YEAR, IndianYear);
     330           0 :    internalSet(UCAL_YEAR, IndianYear);
     331           0 :    internalSet(UCAL_MONTH, IndianMonth);
     332           0 :    internalSet(UCAL_DAY_OF_MONTH, IndianDayOfMonth);
     333           0 :    internalSet(UCAL_DAY_OF_YEAR, yday + 1); // yday is 0-based
     334           0 : }    
     335             : 
     336             : UBool
     337           0 : IndianCalendar::inDaylightTime(UErrorCode& status) const
     338             : {
     339             :     // copied from GregorianCalendar
     340           0 :     if (U_FAILURE(status) || !getTimeZone().useDaylightTime()) {
     341           0 :         return FALSE;
     342             :     }
     343             : 
     344             :     // Force an update of the state of the Calendar.
     345           0 :     ((IndianCalendar*)this)->complete(status); // cast away const
     346             : 
     347           0 :     return (UBool)(U_SUCCESS(status) ? (internalGet(UCAL_DST_OFFSET) != 0) : FALSE);
     348             : }
     349             : 
     350             : // default century
     351             : const UDate     IndianCalendar::fgSystemDefaultCentury          = DBL_MIN;
     352             : const int32_t   IndianCalendar::fgSystemDefaultCenturyYear      = -1;
     353             : 
     354             : UDate           IndianCalendar::fgSystemDefaultCenturyStart     = DBL_MIN;
     355             : int32_t         IndianCalendar::fgSystemDefaultCenturyStartYear = -1;
     356             : 
     357             : 
     358           0 : UBool IndianCalendar::haveDefaultCentury() const
     359             : {
     360           0 :     return TRUE;
     361             : }
     362             : 
     363           0 : UDate IndianCalendar::defaultCenturyStart() const
     364             : {
     365           0 :     return internalGetDefaultCenturyStart();
     366             : }
     367             : 
     368           0 : int32_t IndianCalendar::defaultCenturyStartYear() const
     369             : {
     370           0 :     return internalGetDefaultCenturyStartYear();
     371             : }
     372             : 
     373             : UDate
     374           0 : IndianCalendar::internalGetDefaultCenturyStart() const
     375             : {
     376             :     // lazy-evaluate systemDefaultCenturyStart
     377             :     UBool needsUpdate;
     378             :     { 
     379           0 :         Mutex m;
     380           0 :         needsUpdate = (fgSystemDefaultCenturyStart == fgSystemDefaultCentury);
     381             :     }
     382             : 
     383           0 :     if (needsUpdate) {
     384           0 :         initializeSystemDefaultCentury();
     385             :     }
     386             : 
     387             :     // use defaultCenturyStart unless it's the flag value;
     388             :     // then use systemDefaultCenturyStart
     389             : 
     390           0 :     return fgSystemDefaultCenturyStart;
     391             : }
     392             : 
     393             : int32_t
     394           0 : IndianCalendar::internalGetDefaultCenturyStartYear() const
     395             : {
     396             :     // lazy-evaluate systemDefaultCenturyStartYear
     397             :     UBool needsUpdate;
     398             :     { 
     399           0 :         Mutex m;
     400             : 
     401           0 :         needsUpdate = (fgSystemDefaultCenturyStart == fgSystemDefaultCentury);
     402             :     }
     403             : 
     404           0 :     if (needsUpdate) {
     405           0 :         initializeSystemDefaultCentury();
     406             :     }
     407             : 
     408             :     // use defaultCenturyStart unless it's the flag value;
     409             :     // then use systemDefaultCenturyStartYear
     410             : 
     411           0 :     return    fgSystemDefaultCenturyStartYear;
     412             : }
     413             : 
     414             : void
     415           0 : IndianCalendar::initializeSystemDefaultCentury()
     416             : {
     417             :     // initialize systemDefaultCentury and systemDefaultCenturyYear based
     418             :     // on the current time.  They'll be set to 80 years before
     419             :     // the current time.
     420             :     // No point in locking as it should be idempotent.
     421           0 :     if (fgSystemDefaultCenturyStart == fgSystemDefaultCentury) {
     422           0 :         UErrorCode status = U_ZERO_ERROR;
     423             : 
     424           0 :         IndianCalendar calendar(Locale("@calendar=Indian"),status);
     425           0 :         if (U_SUCCESS(status)) {
     426           0 :             calendar.setTime(Calendar::getNow(), status);
     427           0 :             calendar.add(UCAL_YEAR, -80, status);
     428             : 
     429           0 :             UDate    newStart = calendar.getTime(status);
     430           0 :             int32_t  newYear  = calendar.get(UCAL_YEAR, status);
     431             : 
     432             :             {
     433           0 :                 Mutex m;
     434             : 
     435           0 :                 fgSystemDefaultCenturyStart = newStart;
     436           0 :                 fgSystemDefaultCenturyStartYear = newYear;
     437             :             }
     438             :         }
     439             : 
     440             :         // We have no recourse upon failure unless we want to propagate the failure
     441             :         // out.
     442             :     }
     443           0 : }
     444             : 
     445           0 : UOBJECT_DEFINE_RTTI_IMPLEMENTATION(IndianCalendar)
     446             : 
     447             : U_NAMESPACE_END
     448             : 
     449             : #endif
     450             : 

Generated by: LCOV version 1.13