c语言time头文件作用,C语言中 time相关的函数 头文件

1.  time相关的函数在 time.h 中可以查看原型。如下命令可以找出time.h的路径:

whereis time.h

2. 先看代码,要包含time.h

===================================

void main() {

struct timeval val;

gettimeofday(&val, NULL);

printf("sec=%ld, usec=%ld\n", val.tv_sec, val.tv_usec);

time_t  rawtime; // long type

time ( &rawtime );

printf("rastime = %ld\n", rawtime);

struct tm * timeinfo;

timeinfo = localtime ( &rawtime );

printf("%d-%d-%d %d:%d:%d \n", timeinfo->tm_year+1900, timeinfo->tm_mon+1, \

timeinfo->tm_mday, timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec);

printf ( "The current date/time is: %s", asctime (timeinfo) );

}

======================================

说明:

1> time_t实际上是长整数类型,定义为:typedef long time_t;

time_t 定义的数值也可以 使用 struct timeval.tv_sec 来代替。

int gettimeofday(struct

timeval*tv, struct timezone *tz); //tz 常设置为NULL

struct timeval

{

__time_t tv_sec;

__suseconds_t tv_usec;

};

2>

localtime 函数原型:struct tm *localtime(const time_t *timer)

3>

struct tm {

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;

};

4>

asctime()函数

功 能: 转换日期和时间为相应的字符串(英文简写形式,形如:Mon Feb 16 11:29:26 2009)

用 法: char *asctime(const struct tm *tblock);

参考博客:

https://www.cnblogs.com/wainiwann/archive/2012/11/28/2792133.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值