user5416792
user5416792

Reputation:

How to convert boost::date_time::date::day_of_week() to string type?

Here's what i want to do.

ptime now = second_clock::local_time();
date today = now.date();
today.day_of_week();
string day = "Sat";
if(day == to_string(today.day_of_week()))
{
   //perform an action
}
else
{
   //perform another action
}

The code compiles but the program never executes the if block. How else can i convert day_of_week() to string?

Upvotes: 3

Views: 2942

Answers (1)

sehe
sehe

Reputation: 392911

I suggest boost::lexical_cast<> here:

std::string day = boost::lexical_cast<std::string>(today.day_of_week());

Or simply:

std::cout << today.day_of_week();

Live On C++ Shell

#include <boost/date_time/posix_time/posix_time_io.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/lexical_cast.hpp>

int main() {
    auto now = boost::posix_time::second_clock::local_time();
    auto today = now.date();

    std::string day = boost::lexical_cast<std::string>(today.day_of_week());
    std::cout << today.day_of_week();
}

Prints

Fri

Upvotes: 5

Related Questions