您的位置:首页 > 编程语言 > C语言/C++

C/C++中关于时间的函数 localtime()

2012-04-23 22:22 441 查看
函数:struct tm * localtime ( const time_t * timer );

#include <stdio.h>
#include <time.h>

int main()
{
time_t test;
test = time(NULL);//抓取现在的时间 ——目前距离1970-01-01-00:00:00所经历的秒数
printf("%s\n",ctime(&test));//将上面的秒数转换为人们易读的时间表达方式 ,并在末尾换行
printf("local hour:%d\n",localtime(&test)->tm_hour);
//localtime的返回值是一个指向tm结构的指针,tm见程序最后注释部分
//使用asctime()可以讲 tm 结构转换为人们易读的string格式 (同ctime())
struct tm * ptime;
ptime = localtime(&test);
printf("%s\n",asctime(ptime));

return 0;
}
/*
The structure contains nine members of type int, which are (in any order):
int tm_sec;
int tm_min;
int tm_hour;
int tm_mday;
int tm_mon;
int tm_year;
int tm_wday;
int tm_yday;
int tm_isdst;

tm_sec	seconds after the minute	0-61*
tm_min	minutes after the hour	0-59
tm_hour	hours since midnight	0-23
tm_mday	day of the month	1-31
tm_mon	months since January	0-11
tm_year	years since 1900
tm_wday	days since Sunday	0-6
tm_yday	days since January 1	0-365
tm_isdst	Daylight Saving Time flag
*/


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: