gpt4 book ai didi

c++ - basic boost date_time 输入格式问题

转载 作者:行者123 更新时间:2023-11-28 01:11:49 31 4
gpt4 key购买 nike

我有一个指向字符串 (char *) 的指针作为输入。日期/时间如下所示:
2010 年 4 月 10 日星期六 19:30:00
我只对日期感兴趣,对时间不感兴趣。我用我想要的格式创建了一个“input_facet”:

boost::date_time::date_input_facet inFmt("%a %d %b %Y");

但我不确定如何处理它。最终我想从字符串创建一个日期对象。我很确定我的输入方面和格式是正确的,但我不知道如何使用它。

谢谢。

最佳答案

由于时区差异,日期可能会更改,因此您不能总是忽略字符串的时间部分。

  • 解析日期/时间你可以使用time_input_facet<>
  • 要从中提取日期部分,您可以使用 .date()方法

示例:

// $ g++ *.cc -lboost_date_time && ./a.out 
#include <iostream>
#include <locale>
#include <sstream>

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

int main() {
using namespace std;
using boost::local_time::local_time_input_facet;
using boost::posix_time::ptime;

stringstream ss;
ss << "Sat, 10 Apr 2010 19:30:00";
ss.imbue(locale(locale::classic(),
new local_time_input_facet("%a, %d %b %Y " "%H:%M:%S")));
ptime t;
ss.exceptions(ios::failbit);
ss >> t;
cout << "date: " << t.date() << '\n' ;
}

运行它:

$ g++ *.cc -lboost_date_time && ./a.out 
date: 2010-Apr-10

关于c++ - basic boost date_time 输入格式问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2612343/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com