0

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; }
    
};
yaelreshet11
  • 131
  • 5
  • What is `Date`? How is it defined? – kiner_shah Nov 04 '21 at 06:21
  • It's just a class with a couple member functions and private members int d(day), Month m(Month is an enum class which just contains jan, feb, etc) and int y(year) – yaelreshet11 Nov 04 '21 at 06:26
  • Can you please edit the question and include its definition? – kiner_shah Nov 04 '21 at 06:28
  • 1
    Done I just feel like my way was too easy and didn't require much thinking but can't really think of a better way and don't understand the second way I posted about in this post. day_of_week simply returns whatever day of the week it is btw so like for day_of_week(Date(2021,Month::nov, 4) it'll return Day::thurs – yaelreshet11 Nov 04 '21 at 06:40
  • 1
    Yes, this is not simple. Doesn't your book mention some algorithm or resources to refer to solve this exercise? You can however look at some of these related posts: https://stackoverflow.com/q/274861 https://stackoverflow.com/q/348880 – kiner_shah Nov 04 '21 at 06:48

1 Answers1

0

One way to accomplish this is to use mktime.

    tm tms;
    memset(&tms, '\0', sizeof(tms));
    tms.tm_year = d.year() - 1900;
    tms.tm_mon = d.month() - 1;
    tms.tm_mday = d.day();
    tms.tm_sec = 1;
    tms.tm_isdst = -1;
    if (mktime(&tms) == -1) throw some_error;
    auto days_into_year = tms.tm_yday + 1;
    return days_into_year / 7 + !!(days_into_year % 7);

You'll have to adjust the code according to what week the first partial week of the year actually means (i.e., week 0 or week 1).

jxh
  • 69,070
  • 8
  • 110
  • 193