를 가장 쉽게 계정 UTC 오프셋,필드 C++20 <chrono>
는 방법입니다. 이 있는 경우 C++11/14/17,존재 무료 오픈 소스,헤더만 미리보기 의이 부분의 C++20 는 사용할 수 있습니다. 고 그것은 모두에서 작동합 리눅스와 Windows. 를 사용하는 경우 C,이 쉬운 솔루션을 사용할 수 없습니다.
#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
.
면 t1
고 t2
고 있으로 구문 분석,그들은 비교할 수 있습처럼산 유형입니다.
필요하신 경우에는 사용 료,오픈 소스,헤더만 미리보기추가 #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 오프셋이 다를 수 있습니다),당신은 분석 오프셋 및 빼기에서 현지 시간을 수 있습니다.