如何将boost :: date_time :: date :: day_of_week()转换为字符串类型?

时间:2015-10-16 08:55:03

标签: c++ boost

这就是我想要做的事。

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
}

代码编译但程序从不执行if块。我怎么能将day_of_week()转换为字符串?

1 个答案:

答案 0 :(得分:3)

我建议boost::lexical_cast<>

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

或者简单地说:

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();
}

打印

Fri