Maybe you should look at the boost::date_time::gregorian. Using it you can write a function like that:
#include <boost/date_time/gregorian/gregorian.hpp>
// Get the date for a given year, week and weekday(0-6)
time_t *GetDateFromWeekNumber(int year, int week, int dayOfWeek)
{
using namespace boost::gregorian;
date d(year, Jan, 1);
int curWeekDay = d.day_of_week();
d += date_duration((week - 1) * 7) + date_duration(dayOfWeek - curWeekDay);
tm tmp = to_tm(d);
time_t * ret = new time_t(mktime(&tmp));
return ret;
}
Unfortunately their format of date is different from yours - they numerate days of week starting from Sunday, i.e. Sunday = 0, Monday = 1, ..., Saturday = 6
. If it doesn't satisfy your needs, you can use this slightly changed function:
#include <boost/date_time/gregorian/gregorian.hpp>
// Get the date for a given year, week and weekday(1-7)
time_t *GetDateFromWeekNumber(int year, int week, int dayOfWeek)
{
using namespace boost::gregorian;
date d(year, Jan, 1);
if(dayOfWeek == 7) {
dayOfWeek = 0;
week++;
}
int curWeekDay = d.day_of_week();
d += date_duration((week - 1) * 7) + date_duration(dayOfWeek - curWeekDay);
tm tmp = to_tm(d);
time_t * ret = new time_t(mktime(&tmp));
return ret;
}
EDIT:
After thinking a little I found a way to implement the same function without using boost. Here is the code:
WARNING: the code below is broken, do not use it!
// Get the date for a given year, week and weekday(1-7)
time_t *GetDateFromWeekNumber(int year, int week, int dayOfWeek)
{
const time_t SEC_PER_DAY = 60*60*24;
if(week_day == 7) {
week_day = 0;
week++;
}
struct tm timeinfo;
memset(&timeinfo, 0, sizeof(tm));
timeinfo.tm_year = year - 1900;
timeinfo.tm_mon = 0;
timeinfo.tm_mday = 1;
time_t * ret = new time_t(mktime(&timeinfo)); // set all the other fields
int cur_week_day = timeinfo.tm_wday;
*ret += sec_per_day * ((week_day - cur_week_day) + (week - 1) * 7);
return ret;
}
EDIT2:
Yep, code in EDIT is completely broken because I didn't take enough time to understand how week numbers are assigned.