当前位置: 首页 > 知识库问答 >
问题:

boost日期时间--输出格式

屠和洽
2023-03-14
#include <boost/date_time.hpp>

#include <iostream>
#include <locale>
#include <sstream>

int main()
{
  boost::local_time::local_time_facet* facet = new boost::local_time::local_time_facet("%e %b %Y %T %q");
  std::ostringstream date_osstr;
  date_osstr.imbue(std::locale(date_osstr.getloc(), facet));
  const boost::posix_time::ptime& now = boost::posix_time::second_clock::local_time();
  date_osstr << now;

  std::cout << date_osstr.str() << '\n';
}

2014年4月28日12:40:04

为什么?我做错了什么?我该怎么修好它?

共有1个答案

彭鸿畅
2023-03-14

您需要实际使用local_date_time才能从local_time_facet:

#include <boost/date_time.hpp>
#include <boost/date_time/local_time/local_time.hpp>

#include <iostream>
#include <locale>
#include <sstream>

using namespace boost;
static local_time::time_zone_ptr const utc_time_zone(new local_time::posix_time_zone("GMT"));

int main()
{
    local_time::local_time_facet* facet = new local_time::local_time_facet("%e %b %Y %T %q");

    posix_time::ptime my_ptime = posix_time::second_clock::universal_time();
    local_time::local_date_time now(my_ptime, utc_time_zone);

    std::ostringstream date_osstr;
    date_osstr.imbue(std::locale(date_osstr.getloc(), facet));
    date_osstr << now;

    std::cout << date_osstr.str() << '\n';
}

印刷品

28 Apr 2014 12:59:01 +0000

在Coliru上看现场直播

 类似资料:
  • 下表显示可用于为图表中的日期时间字段创建用户定义的数据格式的说明符。 说明符 描述 D 将日显示为不带前导零的数字(1-31)。 DD 将日显示为带前导零的数字(01-31)。 M 将月份显示为不带前导零的数字(1-12)。 MM 将月份显示为带前导零的数字(01-12)。 MMM 将月份显示为缩写形式(Jan-Dec)。 MMMM 将月份显示为完整月份名(January-December)。 Y

  • 下表显示可用于为图表中的日期时间字段创建用户定义的数据格式的说明符。 说明符 描述 D 将日显示为不带前导零的数字(1-31)。 DD 将日显示为带前导零的数字(01-31)。 M 将月份显示为不带前导零的数字(1-12)。 MM 将月份显示为带前导零的数字(01-12)。 MMM 将月份显示为缩写形式(Jan-Dec)。 MMMM 将月份显示为完整月份名(January-December)。 Y

  • 下表显示可用于为图表中的日期时间字段创建用户定义的数据格式的说明符。 说明符 描述 D 将日显示为不带前导零的数字(1-31)。 DD 将日显示为带前导零的数字(01-31)。 M 将月份显示为不带前导零的数字(1-12)。 MM 将月份显示为带前导零的数字(01-12)。 MMM 将月份显示为缩写形式(Jan-Dec)。 MMMM 将月份显示为完整月份名(January-December)。 Y

  • >美国东部时间2014年11月12日06:53:47

  • 问题内容: 我需要改变从一个日期/时间 2014年8月20日15:30:00 看起来像 二○一四年八月二十○日下午3:30 可以使用javascript的Date对象完成此操作吗? 问题答案: 是的,您可以使用本地javascript Date() 对象 及其方法。 例如,您可以创建一个类似以下的函数: 并显示上午/下午和正确的时间。 请记住使用 getFullYear() 方法,而不要使用get

  • 主要内容:日期格式化符号,Python处理,Pandas处理当进行数据分析时,我们会遇到很多带有日期、时间格式的数据集,在处理这些数据集时,可能会遇到日期格式不统一的问题,此时就需要对日期时间做统一的格式化处理。比如“Wednesday, June 6, 2020”可以写成“6/6/20”,或者写成“06-06-2020。 日期格式化符号 在对时间进行格式化处理时,它们都有固定的表示格式,比如小时的格式化符号为 ,分钟简写为 ,秒简写为 。下表对常用的日期