如何使用 boost 将日期时间格式化为字符串?

发布于 2024-10-17 19:54:02 字数 226 浏览 4 评论 0原文

我想使用 boost 将日期/时间格式化为字符串。

从当前日期/时间开始:

ptime now = second_clock::universal_time();

并以包含以下格式的日期/时间的 wstring 结束:

%Y%m%d_%H%M%S

您能向我展示实现此目的的代码吗?谢谢。

I want to format a date/time to a string using boost.

Starting with the current date/time:

ptime now = second_clock::universal_time();

and ending up with a wstring containing the date/time in this format:

%Y%m%d_%H%M%S

Can you show me the code to achieve this? Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

风柔一江水 2024-10-24 19:54:02

无论它的价值如何,这里是我为此编写的函数:

#include "boost/date_time/posix_time/posix_time.hpp"
#include <iostream>
#include <sstream>

std::wstring FormatTime(boost::posix_time::ptime now)
{
  using namespace boost::posix_time;
  static std::locale loc(std::wcout.getloc(),
                         new wtime_facet(L"%Y%m%d_%H%M%S"));

  std::basic_stringstream<wchar_t> wss;
  wss.imbue(loc);
  wss << now;
  return wss.str();
}

int main() {
  using namespace boost::posix_time;
  ptime now = second_clock::universal_time();

  std::wstring ws(FormatTime(now));
  std::wcout << ws << std::endl;
  sleep(2);
  now = second_clock::universal_time();
  ws = FormatTime(now);
  std::wcout << ws << std::endl;

}

该程序的输出是:

20111130_142732
20111130_142734

我发现这些链接很有用:

For whatever it is worth, here is the function that I wrote to do this:

#include "boost/date_time/posix_time/posix_time.hpp"
#include <iostream>
#include <sstream>

std::wstring FormatTime(boost::posix_time::ptime now)
{
  using namespace boost::posix_time;
  static std::locale loc(std::wcout.getloc(),
                         new wtime_facet(L"%Y%m%d_%H%M%S"));

  std::basic_stringstream<wchar_t> wss;
  wss.imbue(loc);
  wss << now;
  return wss.str();
}

int main() {
  using namespace boost::posix_time;
  ptime now = second_clock::universal_time();

  std::wstring ws(FormatTime(now));
  std::wcout << ws << std::endl;
  sleep(2);
  now = second_clock::universal_time();
  ws = FormatTime(now);
  std::wcout << ws << std::endl;

}

The output of this program was:

20111130_142732
20111130_142734

I found these links useful:

愛上了 2024-10-24 19:54:02
// create your date
boost::gregorian::date d(2009, 1, 7); 

// create your formatting
boost::gregorian::date_facet *df = new boost::gregorian::date_facet("%Y%m%d_%H%M%S"); 

// set your formatting
ostringstream is;
is.imbue(std::locale(is.getloc(), df));
is << d << endl;

// get string
cout << "output :" << is.str() << endl;
// create your date
boost::gregorian::date d(2009, 1, 7); 

// create your formatting
boost::gregorian::date_facet *df = new boost::gregorian::date_facet("%Y%m%d_%H%M%S"); 

// set your formatting
ostringstream is;
is.imbue(std::locale(is.getloc(), df));
is << d << endl;

// get string
cout << "output :" << is.str() << endl;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文