]> git.saurik.com Git - apple/icu.git/blobdiff - icuSources/i18n/indiancal.cpp
ICU-64243.0.1.tar.gz
[apple/icu.git] / icuSources / i18n / indiancal.cpp
index 6563a4f16a038bf7e0faddc6e1fc772a67dbb62b..ae1098f055814157452247f9ad44beceb264b4a9 100644 (file)
@@ -1,5 +1,7 @@
+// © 2016 and later: Unicode, Inc. and others.
+// License & terms of use: http://www.unicode.org/copyright.html
 /*
- * Copyright (C) 2003-2008, International Business Machines Corporation
+ * Copyright (C) 2003-2014, International Business Machines Corporation
  * and others. All Rights Reserved.
  ******************************************************************************
  *
@@ -16,7 +18,6 @@
 #include "gregoimp.h" // Math
 #include "astro.h" // CalendarAstronomer
 #include "uhash.h"
-#include "ucln_in.h"
 
 // Debugging
 #ifdef U_DEBUG_INDIANCAL
@@ -110,7 +111,7 @@ static UBool isGregorianLeap(int32_t year)
  */
 int32_t IndianCalendar::handleGetMonthLength(int32_t eyear, int32_t month) const {
    if (month < 0 || month > 11) {
-      eyear += Math::floorDivide(month, 12, month);
+      eyear += ClockMath::floorDivide(month, 12, month);
    }
 
    if (isGregorianLeap(eyear + INDIAN_ERA_START) && month == 0) {
@@ -237,14 +238,14 @@ static double IndianToJD(int32_t year, int32_t month, int32_t date) {
  * @param eyear The year in Indian Calendar measured from Saka Era (78 AD).
  * @param month The month in Indian calendar
  */
-int32_t IndianCalendar::handleComputeMonthStart(int32_t eyear, int32_t month, UBool useMonth) const {
+int32_t IndianCalendar::handleComputeMonthStart(int32_t eyear, int32_t month, UBool /* useMonth */ ) const {
 
    //month is 0 based; converting it to 1-based 
    int32_t imonth;
 
     // If the month is out of range, adjust it into range, and adjust the extended eyar accordingly
    if (month < 0 || month > 11) {
-      eyear += (int32_t)Math::floorDivide(month, 12, month);
+      eyear += (int32_t)ClockMath::floorDivide(month, 12, month);
    }
 
    if(month == 12){
@@ -288,7 +289,7 @@ int32_t IndianCalendar::handleGetExtendedYear() {
  * method is called. The getGregorianXxx() methods return Gregorian
  * calendar equivalents for the given Julian day.
  */
-void IndianCalendar::handleComputeFields(int32_t julianDay, UErrorCode& status) {
+void IndianCalendar::handleComputeFields(int32_t julianDay, UErrorCode&  /* status */) {
     double jdAtStartOfGregYear;
     int32_t leapMonth, IndianYear, yday, IndianMonth, IndianDayOfMonth, mday;
     int32_t gregorianYear;      // Stores gregorian date corresponding to Julian day;
@@ -346,12 +347,15 @@ IndianCalendar::inDaylightTime(UErrorCode& status) const
     return (UBool)(U_SUCCESS(status) ? (internalGet(UCAL_DST_OFFSET) != 0) : FALSE);
 }
 
-// default century
-const UDate     IndianCalendar::fgSystemDefaultCentury          = DBL_MIN;
-const int32_t   IndianCalendar::fgSystemDefaultCenturyYear      = -1;
 
-UDate           IndianCalendar::fgSystemDefaultCenturyStart     = DBL_MIN;
-int32_t         IndianCalendar::fgSystemDefaultCenturyStartYear = -1;
+/**
+ * The system maintains a static default century start date and Year.  They are
+ * initialized the first time they are used.  Once the system default century date
+ * and year are set, they do not change.
+ */
+static UDate           gSystemDefaultCenturyStart       = DBL_MIN;
+static int32_t         gSystemDefaultCenturyStartYear   = -1;
+static icu::UInitOnce  gSystemDefaultCenturyInit        = U_INITONCE_INITIALIZER;
 
 
 UBool IndianCalendar::haveDefaultCentury() const
@@ -359,87 +363,45 @@ UBool IndianCalendar::haveDefaultCentury() const
     return TRUE;
 }
 
-UDate IndianCalendar::defaultCenturyStart() const
+static void U_CALLCONV
+initializeSystemDefaultCentury()
 {
-    return internalGetDefaultCenturyStart();
-}
+    // initialize systemDefaultCentury and systemDefaultCenturyYear based
+    // on the current time.  They'll be set to 80 years before
+    // the current time.
+    UErrorCode status = U_ZERO_ERROR;
 
-int32_t IndianCalendar::defaultCenturyStartYear() const
-{
-    return internalGetDefaultCenturyStartYear();
-}
+    IndianCalendar calendar ( Locale ( "@calendar=Indian" ), status);
+    if ( U_SUCCESS ( status ) ) {
+        calendar.setTime ( Calendar::getNow(), status );
+        calendar.add ( UCAL_YEAR, -80, status );
 
-UDate
-IndianCalendar::internalGetDefaultCenturyStart() const
-{
-    // lazy-evaluate systemDefaultCenturyStart
-    UBool needsUpdate;
-    { 
-        Mutex m;
-        needsUpdate = (fgSystemDefaultCenturyStart == fgSystemDefaultCentury);
-    }
+        UDate    newStart = calendar.getTime ( status );
+        int32_t  newYear  = calendar.get ( UCAL_YEAR, status );
 
-    if (needsUpdate) {
-        initializeSystemDefaultCentury();
+        gSystemDefaultCenturyStart = newStart;
+        gSystemDefaultCenturyStartYear = newYear;
     }
+    // We have no recourse upon failure.
+}
 
-    // use defaultCenturyStart unless it's the flag value;
-    // then use systemDefaultCenturyStart
 
-    return fgSystemDefaultCenturyStart;
+UDate
+IndianCalendar::defaultCenturyStart() const
+{
+    // lazy-evaluate systemDefaultCenturyStart
+    umtx_initOnce(gSystemDefaultCenturyInit, &initializeSystemDefaultCentury);
+    return gSystemDefaultCenturyStart;
 }
 
 int32_t
-IndianCalendar::internalGetDefaultCenturyStartYear() const
+IndianCalendar::defaultCenturyStartYear() const
 {
     // lazy-evaluate systemDefaultCenturyStartYear
-    UBool needsUpdate;
-    { 
-        Mutex m;
-
-        needsUpdate = (fgSystemDefaultCenturyStart == fgSystemDefaultCentury);
-    }
-
-    if (needsUpdate) {
-        initializeSystemDefaultCentury();
-    }
-
-    // use defaultCenturyStart unless it's the flag value;
-    // then use systemDefaultCenturyStartYear
-
-    return    fgSystemDefaultCenturyStartYear;
+    umtx_initOnce(gSystemDefaultCenturyInit, &initializeSystemDefaultCentury);
+    return    gSystemDefaultCenturyStartYear;
 }
 
-void
-IndianCalendar::initializeSystemDefaultCentury()
-{
-    // initialize systemDefaultCentury and systemDefaultCenturyYear based
-    // on the current time.  They'll be set to 80 years before
-    // the current time.
-    // No point in locking as it should be idempotent.
-    if (fgSystemDefaultCenturyStart == fgSystemDefaultCentury) {
-        UErrorCode status = U_ZERO_ERROR;
-
-        IndianCalendar calendar(Locale("@calendar=Indian"),status);
-        if (U_SUCCESS(status)) {
-            calendar.setTime(Calendar::getNow(), status);
-            calendar.add(UCAL_YEAR, -80, status);
-
-            UDate    newStart = calendar.getTime(status);
-            int32_t  newYear  = calendar.get(UCAL_YEAR, status);
-
-            {
-                Mutex m;
-
-                fgSystemDefaultCenturyStart = newStart;
-                fgSystemDefaultCenturyStartYear = newYear;
-            }
-        }
-
-        // We have no recourse upon failure unless we want to propagate the failure
-        // out.
-    }
-}
 
 UOBJECT_DEFINE_RTTI_IMPLEMENTATION(IndianCalendar)