如何比较之间UTC时间string(ISO8601)

0

的问题

我有一些问题之间进行比较的时间串其是世界标准格式。

例如:

string1 = "1997-07-16T19:20:30+01:00"
string2 = "2000-07-17T20:20:30+01:00"

我的目标是找出这时是最新的。

我冲浪通过互联网找到关于 mktime(); 但如串是在正常格式,另一方面,我string是在ISO8601格式。

我在寻找一个解决方案,这是与windows和linux。

我真的态度认真一些帮助。

c++ datetime
2021-11-24 02:57:06
1

最好的答案

0

到最容易考虑UTC偏领域、C++20 <chrono> 是的路要走。 如果你有C++11/14/17,存在一个 免费的开放源码头-只有预览 的这一部分C++20,可以使用。 它在Linux和窗户。 如果你使用,这个简单的解决方案是不可能的。

#include <chrono>
#include <iostream>
#include <sstream>

int
main()
{
    using namespace std;
    using namespace std::chrono;

    string string1 = "1997-07-16T19:20:30+01:00";
    string string2 = "2000-07-17T20:20:30+01:00";
    sys_seconds t1, t2;

    string format = "%FT%T%Ez";

    istringstream in{string1};
    in.exceptions(ios::failbit);
    in >> parse(format, t1);
    in.clear();
    in.str(string2);
    in >> parse(format, t2);

    if (t1 > t2)
        cout << string1 << " is later than " << string2 << '\n';
    else if (t2 > t1)
        cout << string2 << " is later than " << string1 << '\n';
    else
        cout << string1 << " is the same time as " << string2 << '\n';
}

以上是用C++20解决方案。

  • 的类型 sys_seconds 是一秒精UTC时间戳。 当这一分析为与 parse,分析的时间被认为是当地时间和修改的分析UTC偏之前被分配到 sys_seconds 参数。 的 E 修改在 %Ez 指示 parse 看看 : 在偏移。 %z (没有 E)看起来对偏移的形式 hhmm.

  • 我设置的 failbit 在流使得任何分析错误的结果,在嘈杂的例外。 如果你想检查 in.fail() 手动,省略设定 failbit.

  • 一旦 t1t2 分析成,他们可以比只是喜欢算类型。

  • 如果你需要使用 免费开放源码头-只有预,只是添加 #include "date/date.h"using namespace date; 上述程序。

如果你可以放心,UTC偏总是平等的,那么一个简单的文字比较就足够了:

if (string1 > string2)
    cout << string1 << " is later than " << string2 << '\n';
else if (string2 > string1)
    cout << string2 << " is later than " << string1 << '\n';
else
    cout << string1 << " is the same time as " << string2 << '\n';

在C(如果UTC偏移可以不同),则将需要分析抵消,并减去他们从当地时间。

2021-11-24 14:20:19

其他语言

此页面有其他语言版本

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................