3 ms·
Let's be a little more DRYer: days_in_year = IsLeapYear(year) ? 366 : 365; while (days > days_in_year) { days -= days_in_year; year +=
by judofyr 18y ago
Let's be a little more DRYer:
days_in_year = IsLeapYear(year) ? 366 : 365;
while (days > days_in_year) {
days -= days_in_year;
year += 1;
}
- blablebla 18y agoHow about a little more CORRECTer? #define diy(y) (IsLeapYear(y) ? 366 : 355) while (days > diy(year)) { days -= diy(year); year += 1; }
- ars 18y agoFor the confused: the year changes each time, so you need to recheck IsLeapYear each time. But I wouldn't do it with a define but rather: while (days > (days_in_year = IsLeapYear(year) ? 366 : 365)) { days -= days_in_year; year += 1; }
- tlrobinson 18y agoIncorrect, since you don't update days_in_year on each loop iteration. If you really want... while (days > (days_in_year = IsLeapYear(year) ? 366 : 365)) { days -= days_in_year; year += 1; }