현재 날짜 및 시간 (문자열)
현재 날짜와 시간을 형식으로 가져 오는 함수를 작성했습니다 DD-MM-YYYY HH:MM:SS
. 작동하지만 예를 들어 꽤 추합니다. 똑같은 일을 어떻게 더 간단하게 할 수 있습니까?
string currentDateToString()
{
time_t now = time(0);
tm *ltm = localtime(&now);
string dateString = "", tmp = "";
tmp = numToString(ltm->tm_mday);
if (tmp.length() == 1)
tmp.insert(0, "0");
dateString += tmp;
dateString += "-";
tmp = numToString(1 + ltm->tm_mon);
if (tmp.length() == 1)
tmp.insert(0, "0");
dateString += tmp;
dateString += "-";
tmp = numToString(1900 + ltm->tm_year);
dateString += tmp;
dateString += " ";
tmp = numToString(ltm->tm_hour);
if (tmp.length() == 1)
tmp.insert(0, "0");
dateString += tmp;
dateString += ":";
tmp = numToString(1 + ltm->tm_min);
if (tmp.length() == 1)
tmp.insert(0, "0");
dateString += tmp;
dateString += ":";
tmp = numToString(1 + ltm->tm_sec);
if (tmp.length() == 1)
tmp.insert(0, "0");
dateString += tmp;
return dateString;
}
비 C ++ 11 솔루션 : <ctime>
헤더 와 함께 strftime
. 버퍼가 충분히 큰지 확인하십시오. 나중에 오버런하여 혼란을 일으키고 싶지 않을 것입니다.
#include <iostream>
#include <ctime>
int main ()
{
time_t rawtime;
struct tm * timeinfo;
char buffer[80];
time (&rawtime);
timeinfo = localtime(&rawtime);
strftime(buffer,sizeof(buffer),"%d-%m-%Y %H:%M:%S",timeinfo);
std::string str(buffer);
std::cout << str;
return 0;
}
C ++ 11 std::put_time
부터 iomanip
헤더 에서 사용할 수 있습니다 .
#include <iostream>
#include <iomanip>
#include <ctime>
int main()
{
auto t = std::time(nullptr);
auto tm = *std::localtime(&t);
std::cout << std::put_time(&tm, "%d-%m-%Y %H-%M-%S") << std::endl;
}
std::put_time
스트림 조작기이므로 std::ostringstream
날짜를 문자열로 변환하기 위해 함께 사용할 수 있습니다 .
#include <iostream>
#include <iomanip>
#include <ctime>
#include <sstream>
int main()
{
auto t = std::time(nullptr);
auto tm = *std::localtime(&t);
std::ostringstream oss;
oss << std::put_time(&tm, "%d-%m-%Y %H-%M-%S");
auto str = oss.str();
std::cout << str << std::endl;
}
time.h의 asctime () 함수를 사용하여 단순히 문자열을 얻을 수 있습니다.
time_t _tm =time(NULL );
struct tm * curtime = localtime ( &_tm );
cout<<"The current date/time is:"<<asctime(curtime);
샘플 출력 :
The current date/time is:Fri Oct 16 13:37:30 2015
MS Visual Studio 2015 (14)에서 C ++를 사용하여 다음을 사용합니다.
#include <chrono>
string NowToString()
{
chrono::system_clock::time_point p = chrono::system_clock::now();
time_t t = chrono::system_clock::to_time_t(p);
char str[26];
ctime_s(str, sizeof str, &t);
return str;
}
C ++ 11 답변을 사용하고 싶었지만 GCC 4.9가 std :: put_time을 지원하지 않기 때문에 사용할 수 없습니다.
std::put_time implementation status in GCC?
I ended up using some C++11 to slightly improve the non-C++11 answer. For those that can't use GCC 5, but would still like some C++11 in their date/time format:
std::array<char, 64> buffer;
buffer.fill(0);
time_t rawtime;
time(&rawtime);
const auto timeinfo = localtime(&rawtime);
strftime(buffer.data(), sizeof(buffer), "%d-%m-%Y %H-%M-%S", timeinfo);
std::string timeStr(buffer.data());
std::time_t ct = std::time(0);
char* cc = ctime(&ct);
참고URL : https://stackoverflow.com/questions/16357999/current-date-and-time-as-string
'development' 카테고리의 다른 글
이 간단한 서비스가 시작되지 않는 이유는 무엇입니까? (0) | 2020.12.02 |
---|---|
.NET의 스트림에서 MemoryStream을 얻는 방법은 무엇입니까? (0) | 2020.12.02 |
Android L에서 CardView 위젯의 패딩을 설정하는 방법 (0) | 2020.12.02 |
Google Map API V2를 사용하여지도에서 두 지점 사이의 거리 찾기 (0) | 2020.12.02 |
Swift의 UILabel에서 줄 간격을 늘리는 방법 (0) | 2020.12.02 |