나는 std :: string과 같은 유닉스 타임 스탬프를 가지고 있고 그것을 아름다운 날짜 문자열로 변환하고 싶습니다. 이미 묻는 질문은 현재 시간을 가져 오는 대화 만 표시합니다 (그리고 모든 위치에서 "auto"를 사용하므로 어떤 클래스가 적합한 지 확실하지 않습니다). 이것은 이미 존재하는 std :: string입니다.
std::string beauty_date; //"%d-%m-%Y"
std::string stamp = "1567555200";
time_t stamp_as_time = (time_t) std::stoi(stamp);
나는 이것을 먼저 unsigned long int (일명 time_t)로 변환해야한다고 생각합니다. 내 질문은 "stamp_as_time"을 beauty_date에 넣는 방법입니다.
미리 감사드립니다!
편집 : 여기에 주석에서 내가 put_time으로 시도한 것입니다.
std::string beauty_date; //"%d-%m-%Y"
std::string stamp = "1567555200";
time_t stamp_as_time = (time_t) std::stoi(stamp);
beauty_date = std::put_time(std::localtime(&stamp_as_time), "%d-%m-%Y");
이것은 잘 작동하지 않습니다.
다음이 효과가있을 것입니다.
#include<iostream>
#include<iomanip>
#include<string>
#include<ctime>
int main() {
std::string ts_str{ "1567555200" };
std::int64_t result = std::stoi(ts_str);
std::time_t tmp = result;
std::tm* t = std::gmtime(&tmp);
std::cout << std::put_time(t, "%d-%m-%Y") << std::endl;
return 0 ;
}
또는 다음과 같이 넣으려면 beauty_date
:
std::stringstream ss;
ss << std::put_time(t, "%d-%m-%Y");
beauty_date = ss.str();
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다