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

3 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
}
Run Code Online (Sandbox Code Playgroud)

代码可以编译,但程序从不执行 if 块。我还能如何将 day_of_week() 转换为字符串?

seh*_*ehe 5

我在这里建议boost::lexical_cast<>

std::string day = boost::lexical_cast<std::string>(today.day_of_week());
Run Code Online (Sandbox Code Playgroud)

或者简单地:

std::cout << today.day_of_week();
Run Code Online (Sandbox Code Playgroud)

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();
}
Run Code Online (Sandbox Code Playgroud)

印刷

Fri
Run Code Online (Sandbox Code Playgroud)