在c++中是否有跨平台的方法来获取当前的日期和时间?
当前回答
老问题的新答案:
这个问题没有指定在哪个时区。有两种合理的可能性:
在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
}
在我写这篇文章的时候,部分实现刚刚开始出现在一些平台上。
其他回答
你可以使用boost和chrono库:
#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)));
}
老问题的新答案:
这个问题没有指定在哪个时区。有两种合理的可能性:
在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++中获取当前的系统日期和时间:
#include <iostream>
#include <time.h> //It may be #include <ctime> or any other header file depending upon
// compiler or IDE you're using
using namespace std;
int main() {
// current date/time based on current system
time_t now = time(0);
// convert now to string form
string dt = ctime(&now);
cout << "The local date and time is: " << dt << endl;
return 0;
}
PS:访问这个网站了解更多信息。
auto time = std::time(nullptr);
std::cout << std::put_time(std::localtime(&time), "%F %T%z"); // ISO 8601 format.
使用std::time()或std::chrono::system_clock::now()(或其他时钟类型)获取当前时间。
std::put_time() (c++ 11)和strftime() (C)提供了很多格式化程序来输出这些时间。
#include <iomanip>
#include <iostream>
int main() {
auto time = std::time(nullptr);
std::cout
// ISO 8601: %Y-%m-%d %H:%M:%S, e.g. 2017-07-31 00:42:00+0200.
<< std::put_time(std::gmtime(&time), "%F %T%z") << '\n'
// %m/%d/%y, e.g. 07/31/17
<< std::put_time(std::gmtime(&time), "%D");
}
格式化程序的顺序很重要:
std::cout << std::put_time(std::gmtime(&time), "%c %A %Z") << std::endl;
// Mon Jul 31 00:00:42 2017 Monday GMT
std::cout << std::put_time(std::gmtime(&time), "%Z %c %A") << std::endl;
// GMT Mon Jul 31 00:00:42 2017 Monday
strftime()的格式化器类似:
char output[100];
if (std::strftime(output, sizeof(output), "%F", std::gmtime(&time))) {
std::cout << output << '\n'; // %Y-%m-%d, e.g. 2017-07-31
}
通常,大写格式器表示“完整版本”,小写格式器表示缩写(例如Y: 2017, Y: 17)。
区域设置改变输出:
#include <iomanip>
#include <iostream>
int main() {
auto time = std::time(nullptr);
std::cout << "undef: " << std::put_time(std::gmtime(&time), "%c") << '\n';
std::cout.imbue(std::locale("en_US.utf8"));
std::cout << "en_US: " << std::put_time(std::gmtime(&time), "%c") << '\n';
std::cout.imbue(std::locale("en_GB.utf8"));
std::cout << "en_GB: " << std::put_time(std::gmtime(&time), "%c") << '\n';
std::cout.imbue(std::locale("de_DE.utf8"));
std::cout << "de_DE: " << std::put_time(std::gmtime(&time), "%c") << '\n';
std::cout.imbue(std::locale("ja_JP.utf8"));
std::cout << "ja_JP: " << std::put_time(std::gmtime(&time), "%c") << '\n';
std::cout.imbue(std::locale("ru_RU.utf8"));
std::cout << "ru_RU: " << std::put_time(std::gmtime(&time), "%c");
}
可能的输出(Coliru,编译器浏览器):
undef: Tue Aug 1 08:29:30 2017
en_US: Tue 01 Aug 2017 08:29:30 AM GMT
en_GB: Tue 01 Aug 2017 08:29:30 GMT
de_DE: Di 01 Aug 2017 08:29:30 GMT
ja_JP: 2017年08月01日 08時29分30秒
ru_RU: Вт 01 авг 2017 08:29:30
我已经使用std::gmtime()转换为UTC。提供Std::localtime()来转换为本地时间。
注意,在其他答案中提到的asctime()/ctime()现在被标记为已弃用,strftime()应该是首选。
是的,你可以这样做的格式规则指定的当前填充区域:
#include <iostream>
#include <iterator>
#include <string>
class timefmt
{
public:
timefmt(std::string fmt)
: format(fmt) { }
friend std::ostream& operator <<(std::ostream &, timefmt const &);
private:
std::string format;
};
std::ostream& operator <<(std::ostream& os, timefmt const& mt)
{
std::ostream::sentry s(os);
if (s)
{
std::time_t t = std::time(0);
std::tm const* tm = std::localtime(&t);
std::ostreambuf_iterator<char> out(os);
std::use_facet<std::time_put<char>>(os.getloc())
.put(out, os, os.fill(),
tm, &mt.format[0], &mt.format[0] + mt.format.size());
}
os.width(0);
return os;
}
int main()
{
std::cout << timefmt("%c");
}
输出:周五9月6日20:33:31 2013
推荐文章
- 如何构建和使用谷歌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++中创建一个随机的字母数字字符串?