/*
*******************************************************************************
- * Copyright (C) 2003-2008, International Business Machines Corporation and *
- * others. All Rights Reserved. *
+ * Copyright (C) 2003-2013, International Business Machines Corporation and
+ * others. All Rights Reserved.
*******************************************************************************
*
* File TAIWNCAL.CPP
}
#endif
-// default century
-const UDate TaiwanCalendar::fgSystemDefaultCentury = DBL_MIN;
-const int32_t TaiwanCalendar::fgSystemDefaultCenturyYear = -1;
-
-UDate TaiwanCalendar::fgSystemDefaultCenturyStart = DBL_MIN;
-int32_t TaiwanCalendar::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 TaiwanCalendar::haveDefaultCentury() const
{
return TRUE;
}
-UDate TaiwanCalendar::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;
+ TaiwanCalendar calendar(Locale("@calendar=roc"),status);
+ if (U_SUCCESS(status))
+ {
+ calendar.setTime(Calendar::getNow(), status);
+ calendar.add(UCAL_YEAR, -80, status);
-int32_t TaiwanCalendar::defaultCenturyStartYear() const
-{
- return internalGetDefaultCenturyStartYear();
+ gSystemDefaultCenturyStart = calendar.getTime(status);
+ gSystemDefaultCenturyStartYear = calendar.get(UCAL_YEAR, status);
+ }
+ // We have no recourse upon failure unless we want to propagate the failure
+ // out.
}
-UDate
-TaiwanCalendar::internalGetDefaultCenturyStart() const
-{
+UDate TaiwanCalendar::defaultCenturyStart() const {
// lazy-evaluate systemDefaultCenturyStart
- UBool needsUpdate;
- UMTX_CHECK(NULL, (fgSystemDefaultCenturyStart == fgSystemDefaultCentury), needsUpdate);
-
- if (needsUpdate) {
- initializeSystemDefaultCentury();
- }
-
- // use defaultCenturyStart unless it's the flag value;
- // then use systemDefaultCenturyStart
-
- return fgSystemDefaultCenturyStart;
+ umtx_initOnce(gSystemDefaultCenturyInit, &initializeSystemDefaultCentury);
+ return gSystemDefaultCenturyStart;
}
-int32_t
-TaiwanCalendar::internalGetDefaultCenturyStartYear() const
-{
+int32_t TaiwanCalendar::defaultCenturyStartYear() const {
// lazy-evaluate systemDefaultCenturyStartYear
- UBool needsUpdate;
- UMTX_CHECK(NULL, (fgSystemDefaultCenturyStart == fgSystemDefaultCentury), needsUpdate);
-
- if (needsUpdate) {
- initializeSystemDefaultCentury();
- }
-
- // use defaultCenturyStart unless it's the flag value;
- // then use systemDefaultCenturyStartYear
-
- return fgSystemDefaultCenturyStartYear;
+ umtx_initOnce(gSystemDefaultCenturyInit, &initializeSystemDefaultCentury);
+ return gSystemDefaultCenturyStartYear;
}
-void
-TaiwanCalendar::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;
- TaiwanCalendar calendar(Locale("@calendar=roc"),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);
- {
- umtx_lock(NULL);
- fgSystemDefaultCenturyStart = newStart;
- fgSystemDefaultCenturyStartYear = newYear;
- umtx_unlock(NULL);
- }
- }
- // We have no recourse upon failure unless we want to propagate the failure
- // out.
- }
-}
-
-
U_NAMESPACE_END
#endif