在c++中是否有跨平台的方法来获取当前的日期和时间?
当前回答
我发现这个链接对我的实现非常有用: c++日期和时间
下面是我在实现中使用的代码,以获得清晰的“YYYYMMDD HHMMSS”输出格式。参数in用于在UTC和本地时间之间切换。您可以轻松地修改我的代码以满足您的需要。
#include <iostream>
#include <ctime>
using namespace std;
/**
* This function gets the current date time
* @param useLocalTime true if want to use local time, default to false (UTC)
* @return current datetime in the format of "YYYYMMDD HHMMSS"
*/
string getCurrentDateTime(bool useLocalTime) {
stringstream currentDateTime;
// current date/time based on current system
time_t ttNow = time(0);
tm * ptmNow;
if (useLocalTime)
ptmNow = localtime(&ttNow);
else
ptmNow = gmtime(&ttNow);
currentDateTime << 1900 + ptmNow->tm_year;
//month
if (ptmNow->tm_mon < 9)
//Fill in the leading 0 if less than 10
currentDateTime << "0" << 1 + ptmNow->tm_mon;
else
currentDateTime << (1 + ptmNow->tm_mon);
//day
if (ptmNow->tm_mday < 10)
currentDateTime << "0" << ptmNow->tm_mday << " ";
else
currentDateTime << ptmNow->tm_mday << " ";
//hour
if (ptmNow->tm_hour < 10)
currentDateTime << "0" << ptmNow->tm_hour;
else
currentDateTime << ptmNow->tm_hour;
//min
if (ptmNow->tm_min < 10)
currentDateTime << "0" << ptmNow->tm_min;
else
currentDateTime << ptmNow->tm_min;
//sec
if (ptmNow->tm_sec < 10)
currentDateTime << "0" << ptmNow->tm_sec;
else
currentDateTime << ptmNow->tm_sec;
return currentDateTime.str();
}
输出(UTC, EST):
20161123 000454
20161122 190454
其他回答
老问题的新答案:
这个问题没有指定在哪个时区。有两种合理的可能性:
在UTC。 在计算机的本地时区。
对于1,你可以使用这个日期库和下面的程序:
#include "date.h"
#include <iostream>
int
main()
{
using namespace date;
using namespace std::chrono;
std::cout << system_clock::now() << '\n';
}
这对我来说只是输出:
2015-08-18 22:08:18.944211
日期库本质上只是为std::chrono::system_clock::time_point添加了一个流操作符。它还添加了许多其他不错的功能,但在这个简单的程序中没有使用这些功能。
如果您更喜欢2(本地时间),则有一个timezone库构建在日期库之上。这两个库都是开源和跨平台的,假设编译器支持c++ 11或c++ 14。
#include "tz.h"
#include <iostream>
int
main()
{
using namespace date;
using namespace std::chrono;
auto local = make_zoned(current_zone(), system_clock::now());
std::cout << local << '\n';
}
这对我来说只是输出:
2015-08-18 18:08:18.944211 EDT
make_zoned的结果类型是一个date::zoned_time,它是一个date::time_zone和std::chrono::system_clock::time_point的配对。这个时间对表示本地时间,但也可以表示UTC,这取决于您如何查询它。
通过上面的输出,您可以看到我的计算机目前处于UTC偏移量为-4h的时区,以及EDT的缩写。
如果需要使用其他时区,也可以实现这一点。例如,要查找澳大利亚悉尼的当前时间,只需将变量local的构造更改为:
auto local = make_zoned("Australia/Sydney", system_clock::now());
输出变为:
2015-08-19 08:08:18.944211 AEST
适用于c++ 20
这个库现在在c++ 20中被广泛采用。命名空间date已经消失,现在所有内容都在命名空间std::chrono中。并使用zoned_time代替make_time。删除标题“date.h”和“tz.h”,只使用<chrono>。
#include <chrono>
#include <iostream>
int
main()
{
using namespace std::chrono;
auto local = zoned_time{current_zone(), system_clock::now()};
std::cout << local << '\n'; // 2021-05-03 15:02:44.130182 EDT
}
在我写这篇文章的时候,部分实现刚刚开始出现在一些平台上。
c++标准库没有提供合适的日期类型。c++继承了C语言中用于日期和时间操作的结构和函数,以及一些考虑本地化的日期/时间输入和输出函数。
// Current date/time based on current system
time_t now = time(0);
// Convert now to tm struct for local timezone
tm* localtm = localtime(&now);
cout << "The local date and time is: " << asctime(localtm) << endl;
// Convert now to tm struct for UTC
tm* gmtm = gmtime(&now);
if (gmtm != NULL) {
cout << "The UTC date and time is: " << asctime(gmtm) << endl;
}
else {
cerr << "Failed to get the UTC date and time" << endl;
return EXIT_FAILURE;
}
你可以使用c++ 11时间类:
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
time_t now = chrono::system_clock::to_time_t(chrono::system_clock::now());
cout << put_time(localtime(&now), "%F %T") << endl;
return 0;
}
把:
2017-08-25 12:30:08
#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;
}
c++与C共享它的日期/时间函数。tm结构可能是c++程序员最容易使用的结构——下面打印今天的日期:
#include <ctime>
#include <iostream>
int main() {
std::time_t t = std::time(0); // get time now
std::tm* now = std::localtime(&t);
std::cout << (now->tm_year + 1900) << '-'
<< (now->tm_mon + 1) << '-'
<< now->tm_mday
<< "\n";
}
推荐文章
- 如何构建和使用谷歌TensorFlow c++ api
- 前一个月的Python日期
- 断言是邪恶的吗?
- 下面这些短语在c++中是什么意思:0 -,default-和value-initialization?
- 在STL地图中,使用map::insert比[]更好吗?
- C++ Linux的想法?
- 如何在python中验证日期字符串格式?
- 如何为Fedora安装g++ ?
- Std::cin输入空格?
- c++标准是否要求iostreams的性能很差,或者我只是在处理一个糟糕的实现?
- gcc在哪里查找C和c++头文件?
- 为什么我们需要require require ?
- 如何减去X天从一个日期对象在Java?
- 解析c++中的命令行参数?
- 我如何在c++中创建一个随机的字母数字字符串?