C++ 11에는 날짜/시간 표현뿐만 아니라 문자열로의 변환을위한 편리한 데이터 형식과 함수가 포함되어 있습니다. 그와
, 당신은 (꽤 자기 설명, 내가 생각하는) 이런 식으로 일을 할 수있다 : 특히
#include <iostream>
#include <iomanip>
#include <ctime>
int main()
{
std::time_t t = std::time(NULL);
std::tm tm = *std::localtime(&t);
std::cout << "Time right now is " << std::put_time(&tm, "%c %Z") << '\n';
}
를, 데이터 유형 std::time_t
및 std::tm
, 꽤 아주 좋은 IO 조작하는 std::put_time
있다 인쇄. 이 형식 문자열은 cppreference에 잘 설명되어 있습니다.
로케일과 함께 잘 작동하기로되어 있습니다. 일본 시간/날짜 형식 : 불행하게도
std::chrono::time_point<std::chrono::system_clock> now;
now = std::chrono::system_clock::now();
/* The day before today: */
std::time_t now_c = std::chrono::system_clock::to_time_t(
now - std::chrono::hours(24));
하지 다음 chrono
라이브러리는 C++ 11 표준 라이브러리에 포함
std::cout.imbue(std::locale("ja_JP.utf8"));
std::cout << "ja_JP: " << std::put_time(&tm, "%c %Z") << '\n';
도 편리 간단한 시간/날짜 연산을 수행 할 수 있습니다 이 모든 것은 아직 모든 컴파일러에서 사용할 수 있습니다. 특히, std::put_time
함수는 아직 GCC 4.7.1에서 사용할 수있는 것 같지 않습니다. 날짜 및 시간에 대해 이야기 할 때
#include <iostream>
#include <iomanip>
#include <ctime>
int main()
{
std::time_t t = std::time(NULL);
std::tm tm = *std::localtime(&t);
constexpr int bufsize = 100;
char buf[bufsize];
if (std::strftime(buf,bufsize,"%c %Z",&tm) != 0)
std::cout << "Time right now is " << buf << std::endl;
}
날짜 시간 라이브러리를 가져 오는 것이 좋습니다. [boost.datetime] (http://www.boost.org/doc/libs/1_51_0/doc/html/date_time.html)을 참조하십시오. – juanchopanza
부스트 라이브러리로 가거나 필요한 OS "날짜"기능을위한 간단한 클래스 래퍼를 작성하십시오. 데이터베이스에 저장하는 경우 "DateTime"을 사용하고 그렇지 않으면 문자열 (JSON 문자열, 아마도 XML 문자열 또는 플랫 파일)을 사용하면 날짜를 읽고 쓸 수 있습니다. – paulsm4