网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

c语言time函数转成 c语言时间转换函数

c语言中time函数的用法

头文件time.h

为宁陵等地区用户提供了全套网页设计制作服务,及宁陵网站建设行业解决方案。主营业务为网站设计制作、成都网站设计、宁陵网站设计,以传统方式定制建设网站,并提供域名空间备案等一条龙服务,秉承以专业、用心的态度为用户提供真诚的服务。我们深信只要达到每一位用户的要求,就会得到认可,从而选择与我们长期合作。这样,我们也可以走得更远!

@函数名称: localtime

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

函数功能: 返回一个以tm结构表达的机器时间信息

函数返回: 以tm结构表达的时间,结构tm定义如下:

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;

};

参数说明: timer-使用time()函数获得的机器时间

#include time.h

#include stdio.h

#include dos.h

int main()

{

time_t timer;

struct tm *tblock;

timer=time(NULL);

tblock=localtime(timer);

printf("Local time is: %s",asctime(tblock));

return 0;

}

@函数名称: asctime

函数原型: char* asctime(struct tm * ptr)

函数功能: 得到机器时间(日期时间转换为ASCII码)

函数返回: 返回的时间字符串格式为:星期,月,日,小时:分:秒,年

参数说明: 结构指针ptr应通过函数localtime()和gmtime()得到

所属文件: time.h

#include stdio.h

#include string.h

#include time.h

int main()

{

struct tm t;

char str[80];

t.tm_sec=1;

t.tm_min=3;

t.tm_hour=7;

t.tm_mday=22;

t.tm_mon=11;

t.tm_year=56;

t.tm_wday=4;

t.tm_yday=0;

t.tm_isdst=0;

strcpy(str,asctime(t));

printf("%s",str);

return 0;

}

@函数名称: ctime

函数原型: char *ctime(long time)

函数功能: 得到日历时间

函数返回: 返回字符串格式:星期,月,日,小时:分:秒,年

参数说明: time-该参数应由函数time获得

所属文件: time.h

#include stdio.h

#include time.h

int main()

{

time_t t;

time(t);

printf("Today's date and time: %s",ctime(t));

return 0;

}

@函数名称: difftime

函数原型: double difftime(time_t time2, time_t time1)

函数功能: 得到两次机器时间差,单位为秒

函数返回: 时间差,单位为秒

参数说明: time1-机器时间一,time2-机器时间二.该参数应使用time函数获得

所属文件: time.h

#include time.h

#include stdio.h

#include dos.h

#include conio.h

int main()

{

time_t first, second;

clrscr();

first=time(NULL);

delay(2000);

second=time(NULL);

printf("The difference is: %f seconds",difftime(second,first));

getch();

return 0;

}

@函数名称: gmtime

函数原型: struct tm *gmtime(time_t *time)

函数功能: 得到以结构tm表示的时间信息

函数返回: 以结构tm表示的时间信息指针

参数说明: time-用函数time()得到的时间信息

所属文件: time.h

#include stdio.h

#include stdlib.h

#include time.h

#include dos.h

char *tzstr="TZ=PST8PDT";

int main()

{

time_t t;

struct tm *gmt, *area;

putenv(tzstr);

tzset();

t=time(NULL);

area=localtime(t);

printf("Local time is:%s", asctime(area));

gmt=gmtime(t);

printf("GMT is:%s", asctime(gmt));

return 0;

}

@函数名称: time

函数原型: time_t time(time_t *timer)

函数功能: 得到机器的日历时间或者设置日历时间

函数返回: 机器日历时间

参数说明: timer=NULL时得到机器日历时间,timer=时间数值时,用于设置日历时间,time_t是一个long类型

所属文件: time.h

#include time.h

#include stdio.h

#include dos.h

int main()

{

time_t t;

t=time();

printf("The number of seconds since January 1,1970 is %ld",t);

return 0;

}

@函数名称: tzset

函数原型: void tzset(void)

函数功能: UNIX兼容函数,用于得到时区,在DOS环境下无用途

函数返回:

参数说明:

所属文件: time.h

#include time.h

#include stdlib.h

#include stdio.h

int main()

{

time_t td;

putenv("TZ=PST8PDT");

tzset();

time(td);

printf("Current time=%s",asctime(localtime(td)));

return 0;

}

C语言时间函数time_t

1、time_t // 时间类型(time.h 定义) 

struct tm { // 时间结构,time.h 定义如下: 

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; 

time ( rawtime ); // 获取时间,以秒计,从1970年1月一日起算,存于rawtime 

localtime ( rawtime ); //转为当地时间,tm 时间结构 

asctime() // 转为标准ASCII时间格式: 

//就是直接打印tm,tm_year 从1900年计算,所以要加1900,月tm_mon,从0计算,所以要加1

2、time函数使用示例

#include stdio.h  

#include time.h    

int main()

{  

time_t rawtime;  

struct tm * timeinfo;  

time ( rawtime );  

timeinfo = localtime ( rawtime );  

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

return 0;

}

C语言 time(NULL)

C语言time(NULL)是以当前时间为种子,产生随意数。

其中,time(NULL)用来获取当前时间,本质上得到的是一个大整数,然后用这个数来随机数。

time()这个函数其实保存的是一个历史时间,所以需要用NULL把这个历史时间清空一下,time()就会自动保存当前时间了。你可以简单的理解为NULL就是给time()初始化。

c语言调用time()函数括号里为什么要用NULL?

time是这样声明的:time_ttime(time_t*timer)

用法是你先自己定义一个time_t变量,让后把变量的地址传给它。函数会返回自1970年1月1日0点走过的秒数,同时把这个返回值保存在你传进来的那个time_t*指向的变量里面

如果你传进来NULL的话,就不保存。


分享标题:c语言time函数转成 c语言时间转换函数
网页URL:http://bjjierui.cn/article/hihojs.html

其他资讯