c语言获取系统当前时间的函数,求讲解

structtm*local;//时间结构体time_tt;//把当前时间给tt=time(NULL);//NULL为0local=localtime(&t);//获取当... struct tm *local; //时间结构体
time_t t; //把当前时间给t
t=time(NULL); //NULL为0
local=localtime(&t);//获取当前系统时间
比较渣。。。看不懂注释,求详细讲解~
展开
 我来答
tattackor
2015-10-26 · TA获得超过3.5万个赞
知道大有可为答主
回答量:5083
采纳率:94%
帮助的人:854万
展开全部
1、C语言中读取系统时间的函数为time(),其函数原型为:
#include <time.h>
time_t time( time_t * ) ;
time_t就是long,函数返回从1970年1月1日(MFC是1899年12月31日)0时0分0秒,到现在的的秒数。
2、C语言还提供了将秒数转换成相应的时间格式的函数:
char * ctime(const time_t *timer); //将日历时间转换成本地时间,返回转换后的字符串指针 可定义字符串或是字符指针来接收返回值
struct tm * gmtime(const time_t *timer); //将日历时间转化为世界标准时间(即格林尼治时间),返回结构体指针 可定义struct tm *变量来接收结果
struct tm * localtime(const time_t * timer); //将日历时间转化为本地时间,返回结构体指针 可定义struct tm *变量来接收结果
3、例程:
#include <time.h>
void main()
{
time_t t;
struct tm *pt ;
char *pc ;
time(&t);
pc=ctime(&t) ; printf("ctime:%s", pc );
pt=localtime(&t) ; printf("year=%d", pt->tm_year+1900 );
}

时间结构体struct tm 说明:

struct tm {
int tm_sec; /* 秒 – 取值区间为[0,59] */
int tm_min; /* 分 - 取值区间为[0,59] */
int tm_hour; /* 时 - 取值区间为[0,23] */
int tm_mday; /* 一个月中的日期 - 取值区间为[1,31] */
int tm_mon; /* 月份(从一月开始,0代表一月) - 取值区间为[0,11] */
int tm_year; /* 年份,其值等于实际年份减去1900 */
int tm_wday; /* 星期 – 取值区间为[0,6],其中0代表星期天,1代表星期一,以此类推 */
int tm_yday; /* 从每年的1月1日开始的天数 – 取值区间为[0,365],其中0代表1月1日,1代表1月2日,以此类推 */
int tm_isdst; /* 夏令时标识符,实行夏令时的时候,tm_isdst为正。不实行夏令时的进候,tm_isdst为0;不了解情况时,tm_isdst()为负。*/
};
玻璃麦田bd
2013-06-26 · TA获得超过451个赞
知道小有建树答主
回答量:249
采纳率:0%
帮助的人:270万
展开全部
time_t是个重定义的长整型结构。
调用time函数获取时间,返回的是一个秒数。
当前时间需要调用localtime进行转换一下。
struct tm {
int tm_sec; /* seconds after the minute - [0,59] */
int tm_min; /* minutes after the hour - [0,59] */
int tm_hour; /* hours since midnight - [0,23] */
int tm_mday; /* day of the month - [1,31] */
int tm_mon; /* months since January - [0,11] */
int tm_year; /* years since 1900 */
int tm_wday; /* days since Sunday - [0,6] */
int tm_yday; /* days since January 1 - [0,365] */
int tm_isdst; /* daylight savings time flag */
};
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式