多服务器在多时区部署时, 需要按utc时间统一计算。
可以使用boost::date_time
#include <boost/date_time.hpp>
#include <iostream>
#include <string>
using namespace std;
using namespace boost;
using namespace boost::gregorian;
using namespace boost::posix_time;
int main(int, char *[])
{
ptime ct2(second_clock::universal_time()); //输出的就是UTC时间。 如果是使用second_clock::local_time()构造, 得到的就是localtime
cout << to_simple_string(ct2) << endl;
return 0;
}无需其他库、或者自己写的程序, 太易出错了
本文介绍如何利用boost::date_time库在多服务器多时区环境下统一计算时间,确保UTC时间的一致性。通过使用second_clock::universal_time()函数获取UTC时间,并通过to_simple_string()函数将其转换为易读格式进行输出。

1万+

被折叠的 条评论
为什么被折叠?



