development

현재 날짜 및 시간 (문자열)

big-blog 2020. 12. 2. 21:34
반응형

현재 날짜 및 시간 (문자열)


현재 날짜와 시간을 형식으로 가져 오는 함수를 작성했습니다 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

반응형