c语言中time函数的用法 http://blog.csdn.net/wangluojisuan/article/details/7045592

来源:互联网 发布:sql中delete是删除什么 编辑:程序博客网 时间:2024/04/29 02:42
 

c语言中time函数的用法

分类: C语言 35531人阅读 评论(4) 收藏 举报
语言ctimerstruct日历null
头文件time.h 
@函数名称:     localtime 
函数原型:     struct tm *localtime(const time_t *timer) 
函数功能:     返回一个以tm结构表达的机器时间信息 
函数返回:     以tm结构表达的时间,结构tm定义如下: 
[cpp] view plaincopy
  1. struct  tm{  
  2.        int tm_sec;  
  3.        int tm_min;  
  4.        int tm_hour;  
  5.        int tm_mday;  
  6.        int tm_mon;  
  7.        int tm_year;  
  8.        int tm_wday;  
  9.        int tm_yday;  
  10.        int tm_isdst;  
  11.      };   


参数说明:     timer-使用time()函数获得的机器时间 
[cpp] view plaincopy
  1. #include <time.h>   
  2. #include <stdio.h>   
  3. #include <dos.h>   
  4. int main() {  
  5.      time_t timer;  
  6.      struct tm *tblock;  
  7.      timer=time(NULL);  
  8.      tblock=localtime(&timer);  
  9.      printf("Local time is: %s",asctime(tblock));  
  10.      return 0;   
  11. }   

@函数名称:     asctime 
函数原型:     char* asctime(struct tm * ptr) 
函数功能:     得到机器时间(日期时间转换为ASCII码) 
函数返回:     返回的时间字符串格式为:星期,月,日,小时:分:秒,年 
参数说明:     结构指针ptr应通过函数localtime()和gmtime()得到 
所属文件:     <time.h> 
[cpp] view plaincopy
  1. #include <stdio.h>   
  2. #include <string.h>   
  3. #include <time.h>  
  4.  int main() {  
  5.      struct tm t;  
  6.      char str[80];  
  7.      t.tm_sec=1;  
  8.      t.tm_min=3;  
  9.      t.tm_hour=7;  
  10.      t.tm_mday=22;  
  11.      t.tm_mon=11;  
  12.      t.tm_year=56;  
  13.      t.tm_wday=4;  
  14.      t.tm_yday=0;  
  15.      t.tm_isdst=0;  
  16.      strcpy(str,asctime(&t));  
  17.      printf("%s",str);  
  18.      return 0;   
  19. }   


@函数名称:     ctime 
函数原型:     char *ctime(long time) 
函数功能:     得到日历时间 
函数返回:     返回字符串格式:星期,月,日,小时:分:秒,年 
参数说明:     time-该参数应由函数time获得 
所属文件:     <time.h> 
[cpp] view plaincopy
  1. #include <stdio.h>   
  2. #include <time.h>   
  3. int main() {  
  4.      time_t t;  
  5.      time(&t);  
  6.      printf("Today's date and time: %s",ctime(&t));  
  7.      return 0;   
  8. }   

@函数名称:     difftime 
函数原型:     double difftime(time_t time2, time_t time1) 
函数功能:     得到两次机器时间差,单位为秒 
函数返回:     时间差,单位为秒 
参数说明:     time1-机器时间一,time2-机器时间二.该参数应使用time函数获得 
所属文件:     <time.h> 
[cpp] view plaincopy
  1. #include <time.h>   
  2. #include <stdio.h>   
  3. #include <dos.h>   
  4. #include <conio.h>   
  5. int main() {  
  6.      time_t first, second;  
  7.      clrscr();  
  8.      first=time(NULL);  
  9.      delay(2000);  
  10.      second=time(NULL);  
  11.      printf("The difference is: %f seconds",difftime(second,first));  
  12.      getch();  
  13.      return 0;   
  14. }   

@函数名称:     gmtime 
函数原型:     struct tm *gmtime(time_t  *time) 
函数功能:     得到以结构tm表示的时间信息 
函数返回:     以结构tm表示的时间信息指针 
参数说明:     time-用函数time()得到的时间信息 
所属文件:     <time.h> 
[cpp] view plaincopy
  1. #include <stdio.h>   
  2. #include <stdlib.h>   
  3. #include <time.h>   
  4. #include <dos.h>   
  5. char *tzstr="TZ=PST8PDT";   
  6. int main() {  
  7.      time_t t;  
  8.      struct tm *gmt, *area;  
  9.      putenv(tzstr);  
  10.      tzset();  
  11.      t=time(NULL);  
  12.      area=localtime(&t);  
  13.      printf("Local time is:%s", asctime(area));  
  14.      gmt=gmtime(&t);  
  15.      printf("GMT is:%s", asctime(gmt));  
  16.      return 0;   
  17. }   

@函数名称:     time 
函数原型:     time_t time(time_t *timer) 
函数功能:     得到机器的日历时间或者设置日历时间 
函数返回:     机器日历时间 
参数说明:     timer=NULL时得到机器日历时间,timer=时间数值时,用于设置日历时间,time_t是一个long类型 
所属文件:     <time.h> 
[cpp] view plaincopy
  1. #include <time.h>   
  2. #include <stdio.h>   
  3. #include <dos.h>   
  4. int main() {  
  5.      time_t t;  
  6.      t=time();  
  7.      printf("The number of seconds since January 1,1970 is %ld",t);  
  8.      return 0;   
  9. }   

@函数名称:     tzset 
函数原型:     void tzset(void) 
函数功能:     UNIX兼容函数,用于得到时区,在DOS环境下无用途 
函数返回: 
参数说明: 
所属文件:     <time.h> 
[cpp] view plaincopy
  1. #include <time.h>   
  2. #include <stdlib.h>   
  3. #include <stdio.h>   
  4. int main() {  
  5.      time_t td;  
  6.      putenv("TZ=PST8PDT");  
  7.      tzset();  
  8.      time(&td);  
  9.      printf("Current time=%s",asctime(localtime(&td)));  
  10.      return 0;   
  11. }  


版权声明:本文为博主原创文章,未经博主允许不得转载。

0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 上眼皮干燥起皮怎么办 怀孕下面湿又痒怎么办 内裤穿久了有异味怎么办 刨腹产私处捂烂了怎么办 猫咬手指出血了怎么办 老有白带涌出来怎么办 耳朵里塞了珠子怎么办 树脂发光字烧了怎么办 马蹄莲长得太高怎么办 百合球的芽断了怎么办 种的百合开败怎么办 盆栽百合花开完之后怎么办 土养百合花谢了怎么办 多肉叶子不饱满怎么办 冬天富贵竹叶子发黄怎么办水养 富贵竹叶子发黄怎么办水养 水养富贵竹有虫怎么办 水养的富贵竹叶子发黄怎么办 百合花水里的盐放多了怎么办 芦荟叶子发黄干瘪了怎么办 水插百合不开花怎么办 干百合冷水泡了怎么办 牡丹籽油过期了怎么办 ps画布建小了怎么办 腰突然疼的受不了了怎么办 微信里面打不开表格怎么办? 浏览器未正常加载相关控件怎么办 猫的眼睛发炎了怎么办 橡胶手机壳松了怎么办 橡胶手机壳小了怎么办 苹果手机下载不了软件了怎么办 苹果手机浏览器下载不了软件怎么办 小狗吃了硬的棉花怎么办 小狗吃了隔尿垫里的棉花怎么办 眼睫毛掉进眼睛里怎么办 爱掉头发怎么办吃什么 头皮屑多头发干燥脱发怎么办 剪了头发后悔了怎么办 2岁宝宝发际线高怎么办 脱发怎么办吃什么能长发 后面头发睡平了怎么办