0

I'm currently toying around with boost::date_time. While doing so, I came upon the days_until_weekday (documentation link) function which appears highly useful to me. Unfortunately, I get a compile time error from the following snippet

date f(date d){
    return next_weekday(d,  boost::date_time::weekdays::Friday);
}

reading

> In file included from
> /usr/include/boost/date_time/gregorian/gregorian_types.hpp:25:0,
>                  from /usr/include/boost/date_time/posix_time/posix_time_config.hpp:18,
>                  from /usr/include/boost/date_time/posix_time/posix_time_system.hpp:13,
>                  from /usr/include/boost/date_time/posix_time/ptime.hpp:12,
>                  from /usr/include/boost/date_time/posix_time/posix_time.hpp:15,
>                  from prog.cpp:3: /usr/include/boost/date_time/date_generators.hpp: In instantiation of
> 'typename date_type::duration_type
> boost::date_time::days_until_weekday(const date_type&, const
> weekday_type&) [with date_type = boost::gregorian::date; weekday_type
> = boost::date_time::weekdays; typename date_type::duration_type = boost::gregorian::date_duration]':
> /usr/include/boost/date_time/date_generators.hpp:488:34:   required
> from 'date_type boost::date_time::next_weekday(const date_type&, const
> weekday_type&) [with date_type = boost::gregorian::date; weekday_type
> = boost::date_time::weekdays]' prog.cpp:11:67:   required from here /usr/include/boost/date_time/date_generators.hpp:452:37: error:
> request for member 'as_number' in 'wd', which is of non-class type
> 'const boost::date_time::weekdays'
>      duration_type dd(wd.as_number() - d.day_of_week().as_number());

Go here for a paste of my code.

As the snippet causing the error is so short, I'm really out of ideas to fix this.

By the way, I'm on boost 1.60.0 using clang 3.7.0.

niklasfi
  • 15,245
  • 7
  • 40
  • 54
  • As long as you're kicking the tires of date/time libs, here is one based on the C++11 chrono: https://github.com/HowardHinnant/date The syntax for this same operation is `return d + (date::fri - date::weekday{d});`. Previous Friday would look like `return d - (date::weekday{d} - date::fri);`. – Howard Hinnant Apr 21 '16 at 22:48

1 Answers1

2

You need to convert the date_time enum to an object that matches the interface expected by the weekday_type passed to the function. Use the greg_weekday function to do this for you, eg:

    return next_weekday(d, boost::gregorian::greg_weekday(boost::date_time::Friday));

That compiles for me under VS2015 and boost 1.53.

A link to the documentation for this function: greg_weekday

Scott
  • 178
  • 1
  • 9