0

Lets say I have: Week = 13 Year = 2016

Is there anything in boost or standard library to get the month number (or name) from these two inputs.

I understand a week could overlap across more than one month so any other suggestions would be helpful.

Thanks!

bolov
  • 72,283
  • 15
  • 145
  • 224
user2930006
  • 223
  • 4
  • 13
  • maybe I could get last day of the week and then get the month using day, week and year? – user2930006 Mar 10 '16 at 11:19
  • I realize that the easy way is just to ask. But you should really just read the documentation: http://www.boost.org/doc/libs/1_60_0/doc/html/date_time.html , search for some examples. (btw, in case I was too subtle: that was my way of telling you to **rtfm**) – bolov Mar 10 '16 at 11:22
  • @bolov Thanks. I have referred to boost::date_time but so far have been unable to figure out a way. I cant create a boost::gregorian::date object with the inputs that I have. Checked other functions and algorithms but nothing seems suitable. If you could give any hint, I will appreciate it. – user2930006 Mar 10 '16 at 12:37
  • I don't use boost::date_time, but first google search gave me this: `date weekstart(2002,Feb,1); date weekend = weekstart + week(1);`. From here you can easily adapt to get what you need: (start from 2016 Jan 1, get the day so you can see when the first week of the year starts, add week(13) and voila you get the first day in week 13 of 2016. Again, never used it so can't help you more, but it seems to me you have all you need. If you still have problems, edit this question including your attempted code and where exactly you got stuck. – bolov Mar 10 '16 at 12:57
  • Thank you @bolov. Got it to work! – user2930006 Mar 10 '16 at 14:27
  • You are welcome. I've rolled back, because here we have a very clear distinction between questions and answers. If you want yo share your solution it's perfectly ok to post an answer to your question. You show that you found a solution by accepting an answer. – bolov Mar 10 '16 at 14:31

2 Answers2

2

If you are willing to use a free, open-source library that is not boost and are using C++11 or later, check out:

https://github.com/HowardHinnant/date

Example code:

#include "date.h"
#include "iso_week.h"
#include <iostream>

int
main()
{
    using namespace iso_week::literals;
    auto ymd = date::year_month_day{2016_y/13_w/mon};
    std::cout << ymd << '\n';
}

This outputs:

2016-03-28

There are year(), month() and day() getters on the ymd object.

There is full documentation at the above link. "date.h" and "iso_week.h" are header only, so there is no need to link to any other source.

These computations strictly follow the rules for the ISO week-based year outlined here. The first week of the year begins on the Monday following the last Thursday of December of the preceding year. This means that sometimes an ISO-year for a date is different than the Gregorian year. For example 2016_y/jan/1 == 2015_y/53_w/fri, and in this pseudo-code 2016_y and 2015_y have different types (date::year and iso_week::year) so they can not be accidentally confused. The C++ type system will find accidental ambiguities at compile-time.

It is just as easy to go the other direction:

#include "date.h"
#include "iso_week.h"
#include <iostream>

int
main()
{
    using namespace date::literals;
    auto iso = iso_week::year_weeknum_weekday{2016_y/mar/28};
    std::cout << iso << '\n';
}

which outputs:

2016-W13-Mon

In C++14, if your inputs are compile-time constants, then the result can be constexpr (a compile-time computation). (requires more constexpr muscle than VS-2015 currently supports)

Howard Hinnant
  • 206,506
  • 52
  • 449
  • 577
1
#include <boost/date_time/gregorian/gregorian.hpp>

using namespace boost::gregorian;

// Initialize variables with some values
int week_nmb = 13, year = 2017;

date d = date(year, Jan, 1) + weeks(week_nmb);

int month = d.month();
marc
  • 1,207
  • 11
  • 16