So I have some date class and I wanna make a function that will return how many weeks into the year a certain date is but like after thinking about it for a bit I just did which I think technically works?
int week_of_year(const Date& d)
{
int week = (int(d.month()) - 1) * 5 + ceil(d.day() / 7.0); //50
return week;
}
So I just multiply 5 by a month below the current month and then add the day divided by a week and round it up and this seems to work but it seems like I just took the easy way and also not really the smartest way so does anyone else have any idea for how I could do something like this?
Also I looked up a solution for this because it's from some book's exercise and I found this:
Date day_one(const Date& d)
{
int year = d.year();
const int earliest_first_day = 26;
if (d.month() == Month::dec && d.day() >= earliest_first_day) {
if (int(day_of_week(d)) <= (d.day() - earliest_first_day))
++year;
}
Date d1 {year, Month::jan, 1};
while (day_of_week(d1) != Day::sun)
d1.add_day(-1);
return d1;
}
// Ex 11
int week_of_year(const Date& d)
{
Date iter{day_one(d)};
if (iter == d)
return 1;
int week = 0;
while (iter < d) {
iter.add_day(7);
++week;
}
return week;
}
But I'm not even sure what's going on here to be honest.. Edit: Date class
enum class Day
{
sun=0, mon, tues, wed, thurs, fri, sat
};
enum class Month {
jan = 1, feb, mar, apr, may, jun, jul, aug, sep, oct, nov, dec
};
class Date {
int y;
Month m;
int d;
public:
Date(int y, Month m, int d);
void add_day(int n);
int year() const { return y; }
Month month() const { return m; }
int day() const { return d; }
};