Search code examples
iosios5calendarcore-foundationios5.1

CFCalendarGetOrdinalityOfUnit gives unexpected value in iOS 5.0 (9A334)


I am getting a strange problem in iOS 5.0 (9A334). The problem is I use CFCalendarGetOrdinalityOfUnit to get the day of week for a particular date.

CFCalendarRef theCalendar = CFCalendarCopyCurrent();
CFAbsoluteTime newTime = 0;
const char format[] = "yMdHms";
CFCalendarComposeAbsoluteTime (theCalendar,&newTime,format,theYear, theMonth,theDay, 0, 0, 0);
NSInteger lastDayOfWeekInPreviousMonth = 
CFCalendarGetOrdinalityOfUnit (calendar,kCFCalendarUnitDay,kCFCalendarUnitWeek,absTime );

It returns theExpectedValue - 1.
The values for theYear, theMonth, theDay i am sending today's values.
But in other versions of iOS (like 4.3 and 5.0.1) it returns the expected value correctly. Please provide me a solution. I am stuck here.


Solution

  • Got the answer - CFCalendarGetOrdinalityOfUnit for kCFCalendarUnitWeek might return different values for different versions of iOS. So we have to use NSDateFormatter on a particular date to get the weekDay.