测试日期和时间
作者:Aliot
发布时间:2024-04-07
评论:0
阅读:7
下面的实例获取当前系统的日期和时间,包括本地时间和协调世界时(utc)。
#include <iostream>
#include <ctime>
using namespace std;
int main( )
{
// 基于当前系统的当前日期/时间
time_t now = time(0);
// 把 now 转换为字符串形式
char* dt = ctime(&now);
cout << "本地日期和时间:" << dt << endl;
// 把 now 转换为 tm 结构
tm *gmtm = gmtime(&now);
dt = asctime(gmtm);
cout << "utc 日期和时间:"<< dt << endl;
}
当上面的代码被编译和执行时,它会产生下列结果:
本地日期和时间:sat jan 8 20:07:41 2011 utc 日期和时间:sun jan 9 03:07:41 2011