C++ 如何从字符串解析日期/时间?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3786201/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 13:43:45  来源:igfitidea点击:

How to parse date/time from string?

c++datetimeboostutcboost-date-time

提问by Gabriel Schreiber

Input: strings with date and optional time. Different representations would be nice but necessary. The strings are user-supplied and can be malformed. Examples:

输入:带有日期和可选时间的字符串。不同的表示会很好,但也是必要的。字符串是用户提供的,可能格式不正确。例子:

  • "2004-03-21 12:45:33"(I consider this the default layout)
  • "2004/03/21 12:45:33"(optional layout)
  • "23.09.2004 04:12:21"(german format, optional)
  • "2003-02-11"(time may be missing)
  • "2004-03-21 12:45:33"(我认为这是默认布局)
  • "2004/03/21 12:45:33"(可选布局)
  • "23.09.2004 04:12:21"(德语格式,可选)
  • "2003-02-11"(时间可能会丢失)

Needed Output: Seconds since Epoch (1970/01/01 00:00:00) or some other fixed point.

所需输出:自纪元 (1970/01/01 00:00:00) 或其他某个固定点以来的秒数。

Bonus: Also, reading the UTC-offset of the local system time would be great.

奖励:另外,读取本地系统时间的 UTC 偏移量会很棒。

The input is assumed to be a local time on the machine in question. The output needs to be UTC. System is Linux only (Debian Lenny and Ubuntu needed).

假设输入是相关机器上的本地时间。输出需要是 UTC。系统仅为 Linux(需要 Debian Lenny 和 Ubuntu)。

I have tried to use boost/date_time, but must admit I can't wrap my head around the documentation. The following works without the needed conversion from system local time to UTC:

我曾尝试使用boost/date_time,但必须承认我无法理解文档。以下不需要从系统本地时间到 UTC 的转换:

std::string date = "2000-01-01";
boost::posix_time::ptime ptimedate = boost::posix_time::time_from_string(date);
ptimedate += boost::posix_time::hours(Hardcoded_UTC_Offset);// where to get from?
struct tm = boost::posix_time::to_tm(ptimedate);
int64_t ticks = mktime(&mTmTime);

I think boost::date_timecan provide the needed UTC offset, but I wouldn't know how.

我认为boost::date_time可以提供所需的 UTC 偏移量,但我不知道如何。

回答by Cubbi

Although I don't know how to format a single-digit month input in boost, I can do it after the two-digit edit:

虽然我不知道如何在boost中格式化一位数的月份输入,但我可以在两位数编辑后进行:

#include <iostream>
#include <boost/date_time.hpp>
namespace bt = boost::posix_time;
const std::locale formats[] = {
std::locale(std::locale::classic(),new bt::time_input_facet("%Y-%m-%d %H:%M:%S")),
std::locale(std::locale::classic(),new bt::time_input_facet("%Y/%m/%d %H:%M:%S")),
std::locale(std::locale::classic(),new bt::time_input_facet("%d.%m.%Y %H:%M:%S")),
std::locale(std::locale::classic(),new bt::time_input_facet("%Y-%m-%d"))};
const size_t formats_n = sizeof(formats)/sizeof(formats[0]);

std::time_t pt_to_time_t(const bt::ptime& pt)
{
    bt::ptime timet_start(boost::gregorian::date(1970,1,1));
    bt::time_duration diff = pt - timet_start;
    return diff.ticks()/bt::time_duration::rep_type::ticks_per_second;

}
void seconds_from_epoch(const std::string& s)
{
    bt::ptime pt;
    for(size_t i=0; i<formats_n; ++i)
    {
        std::istringstream is(s);
        is.imbue(formats[i]);
        is >> pt;
        if(pt != bt::ptime()) break;
    }
    std::cout << " ptime is " << pt << '\n';
    std::cout << " seconds from epoch are " << pt_to_time_t(pt) << '\n';
}
int main()
{
    seconds_from_epoch("2004-03-21 12:45:33");
    seconds_from_epoch("2004/03/21 12:45:33");
    seconds_from_epoch("23.09.2004 04:12:21");
    seconds_from_epoch("2003-02-11");
}

note that the seconds-from-epoch output will be assuming the date was in UTC:

请注意,从纪元输出的秒数将假设日期为 UTC:

~ $ ./test | head -2
ptime is 2004-Mar-21 12:45:33
seconds from epoch are 1079873133
~ $ date -d @1079873133
Sun Mar 21 07:45:33 EST 2004

You could probably use boost::posix_time::c_time::localtime()from #include <boost/date_time/c_time.hpp>to get this conversion done assuming the input is in the current time zone, but it is rather inconsistent: for me, for example, the result will be different between today and next month, when daylight saving ends.

假设输入在当前时区,您可能可以使用boost::posix_time::c_time::localtime()from#include <boost/date_time/c_time.hpp>来完成此转换,但它相当不一致:例如,对我而言,当夏令时结束时,今天和下个月的结果会有所不同。

回答by Steve Townsend

boost::gregorianhas some of the stuff you need without you doing any more work:

boost::gregorian有一些你需要的东西,而无需你做更多的工作:

using namespace boost::gregorian;
{
  // The following date is in ISO 8601 extended format (CCYY-MM-DD)
  std::string s("2000-01-01");
  date d(from_simple_string(s));
  std::cout << to_simple_string(d) << std::endl;
}

There is an example on how to use UTC offsets with boost::posix_timehere.

有关于如何使用UTC偏移用一个例子boost::posix_time在这里

You can provide generation of date and time from custom input string formats using date_input_facetand time_input_facet. There is an I/O tutorial on this pagethat should help you get going.

您可以使用date_input_facet和从自定义输入字符串格式生成日期和时间time_input_facet此页面上有一个 I/O 教程,可以帮助您入门。

回答by stefaanv

If c-style is acceptable: strptime() is the way to go, because you can specify the format and it can take locale in account:

如果 c-style 是可以接受的:strptime() 是要走的路,因为您可以指定格式并且它可以考虑区域设置:

tm brokenTime;
strptime(str.c_str(), "%Y-%m-%d %T", &brokenTime);
time_t sinceEpoch = timegm(brokenTime);

Different layouts will have to be checked with the return value (if possible). Timezone will have to be added to by checking the system clock (localtime_r() with time(), tm_zone)

必须使用返回值检查不同的布局(如果可能)。必须通过检查系统时钟来添加时区(localtime_r() 和 time(), tm_zone)

回答by Vijay Mathew

the simplest, portable solution is to use scanf:

最简单、便携的解决方案是使用scanf

int year, month, day, hour, minute, second = 0;
int r = 0;

r = scanf ("%d-%d-%d %d:%d:%d", &year, &month, &day,
           &hour, &minute, &second);
if (r == 6) 
{
  printf ("%d-%d-%d %d:%d:%d\n", year, month, day, hour, minute,
          second);
}
else 
{
    r = scanf ("%d/%d/%d %d:%d:%d", &year, &month, &day,
           &hour, &minute, &second);
    // and so on ...

Initialize a struct tmwith the intvalues and pass it to mktimeto get a calendar time as time_t. For timezone conversions, please see informationon gmtime.

使用值初始化 astruct tm并将其int传递给以mktime获取日历时间为time_t。对于时区转换,请参阅信息gmtime