Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Maybe you should look at the <a href="http://www.boost.org/doc/libs/1_39_0/doc/html/date_time/gregorian.html" rel="nofollow">boost::date_time::gregorian</a>. Using it you can write a function like that:</p> <pre><code>#include &lt;boost/date_time/gregorian/gregorian.hpp&gt; // 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(&amp;tmp)); return ret; } </code></pre> <p>Unfortunately their format of date is different from yours - they numerate days of week starting from Sunday, i.e. <code>Sunday = 0, Monday = 1, ..., Saturday = 6</code>. If it doesn't satisfy your needs, you can use this slightly changed function:</p> <pre><code>#include &lt;boost/date_time/gregorian/gregorian.hpp&gt; // 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(&amp;tmp)); return ret; } </code></pre> <p><strong>EDIT:</strong></p> <p>After thinking a little I found a way to implement the same function without using boost. Here is the code:</p> <p><strong>WARNING: the code below is broken, do not use it!</strong></p> <pre><code>// 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(&amp;timeinfo, 0, sizeof(tm)); timeinfo.tm_year = year - 1900; timeinfo.tm_mon = 0; timeinfo.tm_mday = 1; time_t * ret = new time_t(mktime(&amp;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; } </code></pre> <p><strong>EDIT2:</strong></p> <p>Yep, code in <strong>EDIT</strong> is completely broken because I didn't take enough time to understand how week numbers are assigned.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload