Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/126.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;转换日期格式_C++_Date_Format_Data Conversion - Fatal编程技术网

C++ C++;转换日期格式

C++ C++;转换日期格式,c++,date,format,data-conversion,C++,Date,Format,Data Conversion,我想将int日期转换为: 20111201 到字符串: 2011年12月1日 是否有一个快速的日期格式转换转换成C++(或者可能是BASH系统命令,我可以执行)来做这个操作,或者是我在几个月内一直在做一个开关吗?< P>你可以使用StrpTimes将你的字符串转换成StRetrm,然后使用StrfTimes来重新格式化它: #include <ctime> #include <iostream> #include <sstream> int main() {

我想将
int
日期转换为:

20111201

字符串

2011年12月1日


是否有一个快速的日期格式转换转换成C++(或者可能是BASH系统命令,我可以执行)来做这个操作,或者是我在几个月内一直在做一个开关吗?

< P>你可以使用StrpTimes将你的字符串转换成StRetrm,然后使用StrfTimes来重新格式化它:

#include <ctime>
#include <iostream>
#include <sstream>

int main()
{
  std::ostringstream date1;
  date1 << 20111201;

  struct tm tm;
  strptime(date1.str().c_str(), "%Y%m%d", &tm);

  char date2[10];
  strftime(date2, sizeof(date2), "%d%b%Y", &tm);

  std::cout << date1.str() << " -> " << date2 << std::endl;
}

如果有必要,只需将Dec转换为大写。

没有直接内置的内容,因为这种格式用于日期 这是相对罕见的。这里最简单的解决办法是 使用
%
/
将日期分解为年-月-日 运算符(例如,月份为
值/100%100
),然后格式化 使用
std::ostream
并查找 表中的日期。(这显然需要一些错误
检查,因为并非所有整数值都会产生有效日期。)

此处不要使用bash。要走的方法是使用C++中的Boost,因为我有更多的理由要在这里列出,但是最终它会和你遇到的大多数解决方案一样快,除非你的功能绝对是时间关键的,无论如何它不会有很大的不同。 而且,它将比您经常遇到的那些糟糕的硬编码日期转换例程更加灵活和可维护

下面的代码将执行您想要的操作

#include <iostream>
#include <sstream>

#include <boost/date_time/gregorian/gregorian.hpp>
#include <boost/algorithm/string.hpp>

using namespace boost::gregorian;
using namespace std;

int main(int argc, char **argv)
{
    int dateIn = 20111201;

    // Read the date in from ISO format as an int.
    ostringstream ss;
    ss << dateIn;
    date d(from_undelimited_string( ss.str() ));

    // Set the output format
    date_facet *fct = new date_facet("%d%b%Y");    // [1]
    locale loc = locale(locale::classic(), fct);

    // Render the date as a string;
    ss.str("");
    ss.imbue(loc);
    ss << d;
    string dateOut( ss.str() );
    boost::to_upper( dateOut );

    cout << dateOut << endl;
}

只需更改ref
[1]
处的格式字符串
%d%b%Y”
将更改为不同的输出格式,但请记住,我已将其转换为大写。

旧问题的新答案。这个答案通过C++11/14
库传输,而不是C的
tm
boost::date\u time
。否则,它与现有答案非常相似。解析和格式化时需要这样做

#include "tz.h"
#include <iostream>
#include <locale>
#include <sstream>

int
main()
{
    auto date1 = 20111201;

    std::stringstream stream;
    stream.exceptions(std::ios::failbit);
    stream << date1;

    std::chrono::system_clock::time_point tp;
    date::parse(stream, "%Y%m%d", tp);

    auto str = date::format("%d%b%Y", tp);

    auto& ct = std::use_facet<std::ctype<char>>(std::locale::classic());
    ct.toupper(&str.front(), &str.back()+1);
    std::cout << str << '\n';
}
<>使用现代C++日期/时间库的优点之一是可以轻松地进行更改。例如,如果您现在需要解析时间戳,而不是使用日精度,而是毫秒精度,该怎么办?以下是如何做到这一点:

auto date1 = 20111201093357.275L;

std::stringstream stream;
stream.exceptions(std::ios::failbit);
stream << std::fixed << date1;

std::chrono::system_clock::time_point tp;
date::parse(stream, "%Y%m%d%H%M%S", tp);

auto str = date::format("%d%b%Y %T", tp);

auto& ct = std::use_facet<std::ctype<char>>(std::locale::classic());
ct.toupper(&str.front(), &str.back()+1);
std::cout << str << '\n';
或者,可能这些时间戳是已知的,并且您需要UTC格式的时间戳。只需在
解析后添加一行即可:

tp = date::locate_zone("Pacific/Chatham")->to_sys(tp);
现在输出是:

30NOV2011 19:48:57.275000

考虑到任意时区和次秒精度,现在超出了所有其他C++库的能力。< /P>日期被作为int被给出,所以也许用int字符串转换更新答案。执行INT现在的转换。<代码> StpTime<代码>不是标准函数。@ JAMESKANZEZ.CODENTHEMASTRIMTIME/<代码>声称“符合SUV2,POSIX.1-2001”,并且问题指定用BASH作为解决方案运行系统命令,所以…@ HyDe我认为他指的不是C++标准(或C)的一部分。我很高兴看到有人终于知道如何正确地组合这个推进日期(thx,+1.)它仍然比应该的复杂得多(

auto date1 = 20111201093357.275L;

std::stringstream stream;
stream.exceptions(std::ios::failbit);
stream << std::fixed << date1;

std::chrono::system_clock::time_point tp;
date::parse(stream, "%Y%m%d%H%M%S", tp);

auto str = date::format("%d%b%Y %T", tp);

auto& ct = std::use_facet<std::ctype<char>>(std::locale::classic());
ct.toupper(&str.front(), &str.back()+1);
std::cout << str << '\n';
01DEC2011 09:33:57.275000
tp = date::locate_zone("Pacific/Chatham")->to_sys(tp);
30NOV2011 19:48:57.275000