C语言时间与日期

typedef long time_t;//日历时间

 

struct tm{

      int tm_sec;/*seconds after the minute:[0,60]*/

      int tm_min;/*minutes after the hour:[0,59]*/

      int tm_hour;/*hours after midnight:[0,23]*/

      int tm_mdaty;/*day of 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 1 January*/

      int tm_isdst;

 

 

time_t time(time_t *tp)// 返回当前的日历时间(从1970年1月1日以来的秒数),若tp不为NULL也把该值存入tp所指的对象中

 

char *ctime(const time_*t_ptr) 把t_ptr所指向的日历时间转为系统提供的串

 

char *asctime(const struct tm*tp) 把tp指向的分解时间转化为系统提供的串

 

double difftime(time_t t0,time_t t1);计算t1-t0

 

struct tm*gmtime(const time_t *tpr);将time_t转化为struct tm//格林威治时间

 

struct tm*localtime(const time_t *tpr);将time_t转化为struct tm//当地时间

 

time_t mktime(struct tm*tp);//把tp所指的时间转化为相应的日历时间,函数忽略tm_wday,tm_tday的值

//计算离当前时间很好的函数

例如:

struct tm *tp;

time_t now,later;

 

now=time(NULL);

tp=localtime(&now);

tp->tm_mday+=1000;

later=mktime(tp);

printf("/n1000 days from now: %s/n",ctime(&later));

 

size_t strftime(char *s,size_t n,const char *cntrl_str,const struct tm *tp)//由cntrl_str指向的控制串指导下,把字符指向写入有s指向的串中如:

char s[100];

time_t now;

now=time(NULL);

strftime(s,100,"%H:%M%S on %A,%d %B %Y,localtime(&now));

printf("%s/n/n",s);

如会显示:13:01:15 on Tuesday,19 January 2010

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值