Ich habe eine Funktion geschrieben, um ein aktuelles Datum und eine Uhrzeit im Format zu erhalten: DD-MM-YYYY HH:MM:SS
. Es funktioniert, aber sagen wir mal, es ist ziemlich hässlich. Wie kann ich genau das Gleiche tun aber einfacher?
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;
}
Lösung ohne C++ 11: Mit dem <ctime>
-Header können Sie strftime
verwenden. Stellen Sie sicher, dass Ihr Puffer groß genug ist. Sie möchten ihn nicht überlaufen lassen und später Verwüstungen anrichten.
#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;
}
Seit C++ 11 können Sie std::put_time
aus iomanip
verwenden:
#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
ist ein Stream-Manipulator. Daher kann er zusammen mit std::ostringstream
verwendet werden, um das Datum in einen String zu konvertieren:
#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;
}
sie können die Funktion asctime () von time.h verwenden, um einfach eine Zeichenfolge zu erhalten.
time_t _tm =time(NULL );
struct tm * curtime = localtime ( &_tm );
cout<<"The current date/time is:"<<asctime(curtime);
Beispielausgabe:
The current date/time is:Fri Oct 16 13:37:30 2015
Mit C++ in MS Visual Studio 2015 (14) verwende ich:
#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;
}
Ich wollte die C++ 11-Antwort verwenden, konnte jedoch nicht, da GCC 4.9 std :: put_time nicht unterstützt.
std :: put_time Implementierungsstatus in GCC?
Am Ende habe ich etwas C++ 11 verwendet, um die Antwort auf Nicht-C++ 11 etwas zu verbessern. Für diejenigen, die GCC 5 nicht verwenden können, aber dennoch C++ 11 in ihrem Datums-/Uhrzeitformat möchten:
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);