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
Calendar.date(from:)
incorrectly sets UCAL_DAY_OF_MONTH
#532
Comments
In terms of fixing this, I would suggest looking at the incoming Shouldn't the code only be setting the |
Indeed, this works as expected: for (int32_t week = 1; week <= 5; week++) {
ucal_clear(ucalendar);
ucal_set(ucalendar, UCAL_YEAR, 2024);
ucal_set(ucalendar, UCAL_MONTH, 0);
ucal_set(ucalendar, UCAL_WEEK_OF_MONTH, week);
UDate udate = ucal_getMillis(ucalendar, &error);
NSDate *d = [NSDate dateWithTimeIntervalSince1970:udate / 1000];
printf("%s\n", d.debugDescription.UTF8String);
} Foundation already conditionally resets fields on the |
I was trying to iterate through all the weeks in a month, like so:
However, this printed some unexpected results:
After some digging into the implementation, I discovered that the
date(from:)
method is incorrectly setting theUCAL_DAY_OF_MONTH
field on the clearedUCalendar
, which is messing up the calculation.I verified this by testing the logic myself:
When the
UCAL_DAY_OF_MONTH
field is set, this prints:When the line is commented out, it prints:
Setting the
UCAL_DAY_OF_MONTH
field on theUCalendar
causes operations around finding the weekOfMonth and weekOfYear to fail and return the first week of the month/year, instead of the desired week.The text was updated successfully, but these errors were encountered: