CFCalendarRef CFCalendarCreateWithIdentifier(CFAllocatorRef allocator, CFStringRef identifier) { if (allocator == NULL) allocator = __CFGetDefaultAllocator(); __CFGenericValidateType(allocator, CFAllocatorGetTypeID()); __CFGenericValidateType(identifier, CFStringGetTypeID()); // return NULL until Chinese calendar is available if (identifier != kCFGregorianCalendar && identifier != kCFBuddhistCalendar && identifier != kCFJapaneseCalendar && identifier != kCFIslamicCalendar && identifier != kCFIslamicCivilCalendar && identifier != kCFHebrewCalendar) { // if (identifier != kCFGregorianCalendar && identifier != kCFBuddhistCalendar && identifier != kCFJapaneseCalendar && identifier != kCFIslamicCalendar && identifier != kCFIslamicCivilCalendar && identifier != kCFHebrewCalendar && identifier != kCFChineseCalendar) { if (CFEqual(kCFGregorianCalendar, identifier)) identifier = kCFGregorianCalendar; else if (CFEqual(kCFBuddhistCalendar, identifier)) identifier = kCFBuddhistCalendar; else if (CFEqual(kCFJapaneseCalendar, identifier)) identifier = kCFJapaneseCalendar; else if (CFEqual(kCFIslamicCalendar, identifier)) identifier = kCFIslamicCalendar; else if (CFEqual(kCFIslamicCivilCalendar, identifier)) identifier = kCFIslamicCivilCalendar; else if (CFEqual(kCFHebrewCalendar, identifier)) identifier = kCFHebrewCalendar; // else if (CFEqual(kCFChineseCalendar, identifier)) identifier = kCFChineseCalendar; else return NULL; } struct __CFCalendar *calendar = NULL; uint32_t size = sizeof(struct __CFCalendar) - sizeof(CFRuntimeBase); calendar = (struct __CFCalendar *)_CFRuntimeCreateInstance(allocator, CFCalendarGetTypeID(), size, NULL); if (NULL == calendar) { return NULL; } calendar->_identifier = (CFStringRef)CFRetain(identifier); calendar->_locale = NULL; calendar->_localeID = CFLocaleGetIdentifier(CFLocaleGetSystem()); calendar->_tz = CFTimeZoneCopyDefault(); calendar->_cal = NULL; return (CFCalendarRef)calendar; }
CFLocaleRef CFLocaleCreate(CFAllocatorRef allocator, CFStringRef identifier) { if (allocator == NULL) allocator = __CFGetDefaultAllocator(); __CFGenericValidateType(allocator, CFAllocatorGetTypeID()); __CFGenericValidateType(identifier, CFStringGetTypeID()); CFStringRef localeIdentifier = NULL; if (identifier) { localeIdentifier = CFLocaleCreateCanonicalLocaleIdentifierFromString(allocator, identifier); } if (NULL == localeIdentifier) return NULL; CFStringRef old = localeIdentifier; localeIdentifier = (CFStringRef)CFStringCreateCopy(allocator, localeIdentifier); CFRelease(old); __CFLocaleLockGlobal(); // Look for cases where we can return a cached instance. // We only use cached objects if the allocator is the system // default allocator. if (!allocator) allocator = __CFGetDefaultAllocator(); Boolean canCache = (kCFAllocatorSystemDefault == allocator); if (canCache && __CFLocaleCache) { CFLocaleRef locale = (CFLocaleRef)CFDictionaryGetValue(__CFLocaleCache, localeIdentifier); if (locale) { CFRetain(locale); __CFLocaleUnlockGlobal(); CFRelease(localeIdentifier); return locale; } } struct __CFLocale *locale = NULL; uint32_t size = sizeof(struct __CFLocale) - sizeof(CFRuntimeBase); locale = (struct __CFLocale *)_CFRuntimeCreateInstance(allocator, CFLocaleGetTypeID(), size, NULL); if (NULL == locale) { return NULL; } __CFLocaleSetType(locale, __kCFLocaleOrdinary); locale->_identifier = localeIdentifier; locale->_cache = CFDictionaryCreateMutable(allocator, 0, NULL, &kCFTypeDictionaryValueCallBacks); locale->_overrides = NULL; locale->_prefs = NULL; CF_SPINLOCK_INIT_FOR_STRUCTS(locale->_lock); if (canCache) { if (NULL == __CFLocaleCache) { __CFLocaleCache = CFDictionaryCreateMutable(kCFAllocatorSystemDefault, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks); } CFDictionarySetValue(__CFLocaleCache, localeIdentifier, locale); } __CFLocaleUnlockGlobal(); return (CFLocaleRef)locale; }