2013-05-03 86 views
48

我寫了一個函數來獲取在格式的當前日期和時間: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; 
} 
+2

http://pubs.opengroup.org/onlinepubs/9699919799/functions/strftime.html – Mat 2013-05-03 11:37:57

+0

使用['std :: strftime']會不會更簡單(http://en.cppreference.com/ W/CPP /計時/ C/strftime的)? – 2013-05-03 11:38:26

+0

[助推 - 日期時間(http://www.boost.org/doc/libs/1_53_0/doc/html/date_time/examples.html#date_time.examples.dates_as_strings) – M456 2013-05-03 11:40:13

回答

92

非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 %I:%M:%S",timeinfo); 
    std::string str(buffer); 

    std::cout << str; 

    return 0; 
} 
+2

如果它溢出緩衝區呢?我想應該有一個'strftime'的返回值,你需要在真實代碼中檢查否? – 2013-05-03 12:17:56

+2

對於任何給定的格式字符串來說,計算最大可能的字符串長度並不需要太多思考。 – 2013-05-03 13:05:40

+0

我不明白爲什麼你可以通過從頂部的第10行的價值傳遞rawtime,因爲時間加入了一個指向time_t類型的指針:http://www.cplusplus.com/reference/ctime/time/在這個例子中在那個頁面上他們使用時間()的方式我不明白。任何人都可以解釋我嗎? – 2015-01-28 14:23:13

103

由於C++ 11可以使用從std::put_timeiomanip頭:

#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; 
} 
+32

'std :: put_time'在gcc 4.9中仍然缺失。 – 2014-08-15 21:30:19

+4

gcc 5.0有它,但不是早期版本。 – SmallChess 2015-07-14 02:15:08

+0

@soon我有問題。在我的代碼中,我有一個應該返回一個時間的函數。什麼應該是返回當前時間的函數的返回類型? – 2016-09-18 15:57:41

21

您可以使用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 
+2

不,您不能:asctime不能提供問題中要求的日期/時間格式! – Aconcagua 2015-02-02 15:44:37

+2

@Aconcagua雖然它沒有,它確實給你一個字符串的時間。代碼乾淨簡潔。 – SmallChess 2015-10-16 02:36:49

+3

如果它不提供您想要的或需要的東西,那麼簡潔明瞭的代碼將無濟於事。想要一輛卡車(字符串),你會得到一個。想運輸液體,你需要一輛油罐車,並不會很高興得到一輛自卸車... – Aconcagua 2015-10-16 07:47:25

1

我想使用C++ 11的答案,但我不能,因爲GCC 4.9不支持std :: put_time。

std::put_time implementation status in GCC?

我結束了使用一些C++ 11略有提高非C++ 11的答案。對於那些無法使用GCC 5,但仍然希望在他們的日期/時間格式一些C++ 11:

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()); 
1

在MS Visual Studio中使用C++ 2015年(14),我使用:

#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; 
}