Logo Search packages:      
Sourcecode: icu version File versions  Download package

Calendar *U_EXPORT2 Calendar::createInstance ( TimeZone zoneToAdopt,
const Locale aLocale,
UErrorCode success 
) [static, inherited]

Creates a Calendar using the given timezone and given locale. The Calendar takes ownership of zoneToAdopt; the client must not delete it.

Parameters:
zoneToAdopt The given timezone to be adopted.
aLocale The given locale.
success Indicates the success/failure of Calendar creation. Filled in with U_ZERO_ERROR if created successfully, set to a failure result otherwise.
Returns:
A Calendar if created successfully. NULL otherwise. ICU 2.0

Definition at line 780 of file calendar.cpp.

References Calendar::adoptTimeZone(), UnicodeString::extract(), Calendar::getDynamicClassID(), UObject::getDynamicClassID(), Locale::getName(), Calendar::getNow(), UnicodeString::getStaticClassID(), Calendar::getType(), UnicodeString::length(), NULL, Calendar::setTimeInMillis(), Calendar::setWeekCountData(), U_FAILURE, U_INTERNAL_PROGRAM_ERROR, U_MISSING_RESOURCE_ERROR, and U_SUCCESS.

{
    if (U_FAILURE(success)) {
        return NULL;
    }

    Locale actualLoc;
    UObject* u = NULL;

#if !UCONFIG_NO_SERVICE
    if (isCalendarServiceUsed()) {
        u = getCalendarService(success)->get(aLocale, LocaleKey::KIND_ANY, &actualLoc, success);
    }
    else
#endif
    {
        u = createStandardCalendar(getCalendarTypeForLocale(aLocale.getName()), aLocale, success);
    }
    Calendar* c = NULL;

    if(U_FAILURE(success) || !u) {
        delete zone;
        if(U_SUCCESS(success)) { // Propagate some kind of err
            success = U_INTERNAL_PROGRAM_ERROR;
        }
        return NULL;
    }

#if !UCONFIG_NO_SERVICE
    if(u->getDynamicClassID() == UnicodeString::getStaticClassID()) {
        // It's a unicode string telling us what type of calendar to load ("gregorian", etc)
        const UnicodeString& str = *(UnicodeString*)u;

        // Create a Locale over this string
        Locale l("");
        LocaleUtility::initLocaleFromName(str, l);

#ifdef U_DEBUG_CALSVC
        fprintf(stderr, "Calendar::createInstance(%s), looking up [%s]\n", aLocale.getName(), l.getName());
#endif

        Locale actualLoc2;
        delete u;
        u = NULL;

        // Don't overwrite actualLoc, since the actual loc from this call
        // may be something like "@calendar=gregorian" -- TODO investigate
        // further...
        c = (Calendar*)getCalendarService(success)->get(l, LocaleKey::KIND_ANY, &actualLoc2, success);

        if(U_FAILURE(success) || !c) {
            delete zone;
            if(U_SUCCESS(success)) { 
                success = U_INTERNAL_PROGRAM_ERROR; // Propagate some err
            }
            return NULL;
        }

        if(c->getDynamicClassID() == UnicodeString::getStaticClassID()) {
            // recursed! Second lookup returned a UnicodeString. 
            // Perhaps DefaultCalendar{} was set to another locale.
#ifdef U_DEBUG_CALSVC
            char tmp[200];
            const UnicodeString& str = *(UnicodeString*)c;
            // Extract a char* out of it..
            int32_t len = str.length();
            int32_t actLen = sizeof(tmp)-1;
            if(len > actLen) {
                len = actLen;
            }
            str.extract(0,len,tmp);
            tmp[len]=0;

            fprintf(stderr, "err - recursed, 2nd lookup was unistring %s\n", tmp);
#endif
            success = U_MISSING_RESOURCE_ERROR;  // requested a calendar type which could NOT be found.
            delete c;
            delete zone;
            return NULL;
        }
#ifdef U_DEBUG_CALSVC
        fprintf(stderr, "%p: setting week count data to locale %s, actual locale %s\n", c, (const char*)aLocale.getName(), (const char *)actualLoc.getName());
#endif
        c->setWeekCountData(aLocale, c->getType(), success);  // set the correct locale (this was an indirected calendar)
    }
    else
#endif /* UCONFIG_NO_SERVICE */
    {
        // a calendar was returned - we assume the factory did the right thing.
        c = (Calendar*)u;
    }

    // Now, reset calendar to default state:
    c->adoptTimeZone(zone); //  Set the correct time zone
    c->setTimeInMillis(getNow(), success); // let the new calendar have the current time.

    return c;
}


Generated by  Doxygen 1.6.0   Back to index