-
Notifications
You must be signed in to change notification settings - Fork 4.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[iOS][non-icu] HybridGlobalization implement japanese calendar data #92471
Changes from 8 commits
19dc7e8
2c4e707
f932247
7011aa9
4e3a1d9
8ba1be5
d5d95b8
5a7675d
b04b27e
33f6a7e
7fa2128
b17c33a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -50,6 +50,35 @@ | |
return calendarIdentifier; | ||
} | ||
|
||
/* | ||
Function: | ||
GetCalendarId | ||
|
||
Gets the associated CalendarId for the calendar name. | ||
*/ | ||
static CalendarId GetCalendarId(const char* calendarName) | ||
{ | ||
if (strcasecmp(calendarName, GREGORIAN_NAME) == 0) | ||
return GREGORIAN; | ||
else if (strcasecmp(calendarName, JAPANESE_NAME) == 0) | ||
return JAPAN; | ||
else if (strcasecmp(calendarName, BUDDHIST_NAME) == 0) | ||
return THAI; | ||
else if (strcasecmp(calendarName, HEBREW_NAME) == 0) | ||
return HEBREW; | ||
else if (strcasecmp(calendarName, DANGI_NAME) == 0) | ||
return KOREA; | ||
mdh1418 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
else if (strcasecmp(calendarName, PERSIAN_NAME) == 0) | ||
return PERSIAN; | ||
else if (strcasecmp(calendarName, ISLAMIC_NAME) == 0) | ||
return HIJRI; | ||
else if (strcasecmp(calendarName, ISLAMIC_UMALQURA_NAME) == 0) | ||
return UMALQURA; | ||
else if (strcasecmp(calendarName, ROC_NAME) == 0) | ||
return TAIWAN; | ||
else | ||
return UNINITIALIZED_VALUE; | ||
} | ||
/* | ||
Function: | ||
GlobalizationNative_GetCalendarInfoNative | ||
|
@@ -79,7 +108,7 @@ | |
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:calendarIdentifier]; | ||
|
||
if (dataType == CalendarData_NativeName) | ||
return calendar ? strdup([[calendar calendarIdentifier] UTF8String]) : NULL; | ||
return strdup([[currentLocale localizedStringForCalendarIdentifier:calendarIdentifier] UTF8String]); | ||
|
||
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init]; | ||
dateFormat.locale = currentLocale; | ||
|
@@ -141,4 +170,128 @@ | |
return arrayToString ? strdup([arrayToString UTF8String]) : NULL; | ||
} | ||
} | ||
|
||
/* | ||
Function: | ||
GetLatestJapaneseEraNative | ||
|
||
Gets the latest era in the Japanese calendar. | ||
*/ | ||
int32_t GlobalizationNative_GetLatestJapaneseEraNative(void) | ||
{ | ||
@autoreleasepool | ||
{ | ||
// Create an NSCalendar with the Japanese calendar identifier | ||
NSCalendar *japaneseCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierJapanese]; | ||
// Get the latest era | ||
NSDateComponents *latestEraComponents = [japaneseCalendar components:NSCalendarUnitEra fromDate:[NSDate date]]; | ||
// Extract the era component | ||
NSInteger latestEra = [latestEraComponents era]; | ||
return (int32_t)latestEra; | ||
} | ||
} | ||
|
||
/* | ||
Function: | ||
GetJapaneseEraStartDateNative | ||
|
||
Gets the starting Gregorian date of the specified Japanese Era. | ||
*/ | ||
int32_t GlobalizationNative_GetJapaneseEraStartDateNative(int32_t era, int32_t* startYear, int32_t* startMonth, int32_t* startDay) | ||
{ | ||
@autoreleasepool | ||
{ | ||
NSCalendar *japaneseCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierJapanese]; | ||
NSDateComponents *startDateComponents = [[NSDateComponents alloc] init]; | ||
startDateComponents.era = era; | ||
// set the date to Jan 1, 1 | ||
startDateComponents.month = 1; | ||
startDateComponents.day = 1; | ||
startDateComponents.year = 1; | ||
NSDate *date = [japaneseCalendar dateFromComponents:startDateComponents]; | ||
int32_t currentEra; | ||
|
||
for (int month = 0; month <= 12; month++) | ||
{ | ||
NSDateComponents *eraComponents = [japaneseCalendar components:NSCalendarUnitEra fromDate:date]; | ||
currentEra = [eraComponents era]; | ||
if (currentEra == era) | ||
mdh1418 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
for (int day = 0; day < 31; day++) | ||
{ | ||
// subtract 1 day at a time until we get out of the specified Era | ||
ilonatommy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
startDateComponents.day = startDateComponents.day - 1; | ||
date = [japaneseCalendar dateFromComponents:startDateComponents]; | ||
eraComponents = [japaneseCalendar components:NSCalendarUnitEra fromDate:date]; | ||
currentEra = [eraComponents era]; | ||
if (currentEra != era) | ||
{ | ||
// add back 1 day to get back into the specified Era | ||
startDateComponents.day = startDateComponents.day + 1; | ||
date = [japaneseCalendar dateFromComponents:startDateComponents]; | ||
NSCalendar *gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian]; | ||
NSDateComponents *components = [gregorianCalendar components:NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear fromDate:date]; | ||
*startYear = [components year]; | ||
*startMonth = [components month]; | ||
*startDay = [components day]; | ||
return 1; | ||
} | ||
} | ||
} | ||
// add 1 month at a time until we get into the specified Era | ||
startDateComponents.month = startDateComponents.month + 1; | ||
date = [japaneseCalendar dateFromComponents:startDateComponents]; | ||
eraComponents = [japaneseCalendar components:NSCalendarUnitEra fromDate:date]; | ||
currentEra = [eraComponents era]; | ||
} | ||
|
||
return 0; | ||
} | ||
} | ||
|
||
/* | ||
Function: | ||
GetCalendarsNative | ||
|
||
Returns the list of CalendarIds that are available for the specified locale. | ||
*/ | ||
int32_t GlobalizationNative_GetCalendarsNative(const char* localeName, CalendarId* calendars, int32_t calendarsCapacity) | ||
{ | ||
@autoreleasepool | ||
{ | ||
NSArray *calendarIdentifiers = @[ | ||
NSCalendarIdentifierGregorian, | ||
NSCalendarIdentifierBuddhist, | ||
NSCalendarIdentifierChinese, | ||
NSCalendarIdentifierCoptic, | ||
NSCalendarIdentifierEthiopicAmeteMihret, | ||
NSCalendarIdentifierEthiopicAmeteAlem, | ||
NSCalendarIdentifierHebrew, | ||
NSCalendarIdentifierISO8601, | ||
NSCalendarIdentifierIndian, | ||
NSCalendarIdentifierIslamicCivil, | ||
NSCalendarIdentifierIslamicTabular, | ||
NSCalendarIdentifierIslamicUmmAlQura, | ||
NSCalendarIdentifierIslamic, | ||
NSCalendarIdentifierJapanese, | ||
NSCalendarIdentifierPersian, | ||
NSCalendarIdentifierRepublicOfChina, | ||
mdh1418 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
]; | ||
mdh1418 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
NSString *locName = [NSString stringWithFormat:@"%s", localeName]; | ||
NSLocale *currentLocale = [[NSLocale alloc] initWithLocaleIdentifier:locName]; | ||
NSString *defaultCalendarIdentifier = [currentLocale calendarIdentifier]; | ||
int32_t calendarCount = MIN(calendarIdentifiers.count, calendarsCapacity); | ||
int32_t calendarIndex = 0; | ||
calendars[calendarIndex++] = GetCalendarId([defaultCalendarIdentifier UTF8String]); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Theres a possibility of this being There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice catch, I will update it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Updated the part related to |
||
for (int i = 0; i < calendarCount; i++) | ||
{ | ||
NSString *calendarIdentifier = calendarIdentifiers[i]; | ||
if (calendarIdentifier == defaultCalendarIdentifier) | ||
continue; | ||
calendars[calendarIndex++] = GetCalendarId([calendarIdentifier UTF8String]); | ||
} | ||
return calendarCount; | ||
} | ||
} | ||
#endif |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we still use
out abbrevEnglishEraNames!
directly instead like in the non-apple block? So making this else block the same as the non-apple blockThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated it.