是否有一种跨平台的方法来在C++中获取当前日期和时间?
http://www.cplusplus.com/reference/ctime/strftime/
这个内置函数似乎提供了一系列合理的选项。
#include <iostream>
#include <chrono>
#include <boost/date_time/posix_time/posix_time.hpp>
using boost::posix_time::to_iso_extended_string;
using boost::posix_time::from_time_t;
using std::chrono::system_clock;
int main()
{
auto now = system_clock::now();
std::cout << to_iso_extended_string(from_time_t(system_clock::to_time_t(now)));
}
#include <iostream>
#include <chrono>
#include <string>
#pragma warning(disable: 4996)
// Ver: C++ 17
// IDE: Visual Studio
int main() {
using namespace std;
using namespace chrono;
time_point tp = system_clock::now();
time_t tt = system_clock::to_time_t(tp);
cout << "Current time: " << ctime(&tt) << endl;
return 0;
}
#include <Windows.h>
void main()
{
//Following is a structure to store date / time
SYSTEMTIME SystemTime, LocalTime;
//To get the local time
int loctime = GetLocalTime(&LocalTime);
//To get the system time
int systime = GetSystemTime(&SystemTime)
}
void main
甚至不是标准的 C/C++。 - derpface我需要一种在列表更新时插入当前日期时间的方法。 这种简单明了的方法似乎很有效。
#include<bits/stdc++.h>
#include<unistd.h>
using namespace std;
int main()
{ //initialize variables
time_t now;
//blah..blah
/*each time I want the updated stamp*/
now=time(0);cout<<ctime(&now)<<"blah_blah";
}
time()
的答案,所以这个答案并没有添加任何新内容。 - Jan#include <bits/stdc++.h>
是一个非常不好的做法。 - SimonC
tm
结构更好。C++11 方法只返回自 Unix 纪元以来的时间戳,而问题是如何获取日期和时间。 - Snackoverflow