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

标签 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 block 。我还能如何将 day_of_week() 转换为字符串?

最佳答案

我建议 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

关于c++ - 如何将 boost::date_time::date::day_of_week() 转换为字符串类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33166259/

相关文章:

C++ Xerces-c - 如何判断节点是否为占位符

c++ - 为什么 fstream::open() 需要 C 风格的字符串?

c++ - 正则表达式很奇怪

c++ - 将 `boost::tuple` 转换为 `boost::fusion::tuple`

c++ - std::future 在 Boost UDP 套接字异步接收操作中不起作用

c++ - 在排序数组中查找旋转点

c++ - TBB:可能获得线程ID?

c++ - boost::variate_generator 语法错误

c++ - 如何永久安装 Visual Studio 2015 的 GLUT?

c++ - boost 日志记录,按命名范围过滤