tm结构在c/c++中处理日期和时间相关的操作时,显得尤为重要,tm结构以c结构的形式保存日期和时间,大多数与事件相关的函数都使用了tm结构,下面的实例使用了tm结构和各种与日期和时间相关的函数
#include <iostream>
#include <ctime>
using namespace std;
int main()
{
time_t now=time(0);
cout<<"Number of sec since January 1,1970:"<<now<<endl;
tm *ltm=localtime(&now);
cout<<"Year:"<<1900+ltm->tm-year<<endl;
cout<<"Month:"<<1+ltm->tm_mon<<endl;
cout<<"Day:"<<ltm->tm_day<<endl;
cout<<"Time:"<<1+ltm->tm_hour<<":";
cout<<1+ltm->tm_min<<":";
cout<<1+ltm->tm_sec<<endl;
}
显示结果
Number of sec since Janurary 1,1970:1294548235
Year:2011
Month:1
Day:8
Time:22:44:59