날짜/시간 문자열을 time_t에 저장 한 다음 정확히 원래 문자열로 변환하고 싶습니다. 문자열을 time_t로 변환 한 다음 time_t를 문자열로 다시 변환하십시오.
하지만 출력에서 출력됩니다"2016-04-25_10:10:05"
아래의 코드와 시간은 date_str
을 변경하여 잘못된 것입니다.
코드를 std::string date_str = "1470-04-25_09:10:05";
으로 변경하면 결과가 올바르게 표시됩니다.
#include <iostream>
#include <ctime>
#include <string>
#include <sstream>
#include <iomanip>
int main()
{
// try changing year, hour will be incorrect
std::string date_str = "2016-04-25_09:10:05";
std::tm tm{};
std::istringstream str_stream(date_str);
str_stream >> std::get_time(&tm, "%Y-%m-%d_%T");
std::time_t time = std::mktime(&tm);
std::stringstream stream;
stream << std::put_time(std::localtime(&time), "%F_%T");
std::cout << stream.str() << std::endl;
}
시간대에 문제가 있습니까? 어떤 시간대에 있니? 일광 절약 시간제? 자세한 내용은'tm' 구조체를 점검 했습니까 (예 : 디버거)? –
1 시간 간격은 의미가 있습니다 ... 내가 얻지 못하는 것은 1470 년입니다. – pah
@JoachimPileborg 동일한 기계에 있습니다. 하지만 시간대를 독립적으로 만드는 방법. – Christophe