#include 库函数详解

  • Post author:
  • Post category:其他


本文转自 http://blog.csdn.net/chenyiming_1990/article/details/8682552

time.h

有人总结成这么几句,的确是经典,自己好好编程试试效果吧,

两个类型:


time_t:表示距离 UTC 时间 1970-01-01 00:00:00 的秒数。也叫做日历时,类型是  long

clock_t: 只用于程序计时,貌似其他的没它什么事。


struct tm:通常用于存储本地时。




常用函数


clock: 获取程序开始执行后占用的处理器时间,返回值clock_t。

time:获取当前系统时间(UTC时间)的time_t值。

ctime:将time_t值转换为表示本地时间的字符串。

gmttime:将time_t值转换为表示GMT时间的字符串。

localtime:将time_t转换为表示本地时间的strunct tm结构。

mktime:将表示本地时间的struct tm转换为time_t。

asctime:将struct tm转换为字符串形式。

difftime:得到两个日历时之间的差值。

strftime:自定义把结构体tm的日期与时间信息转换为制定的格式。

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



@函数名称: clock

函数原型:

clock_t clcok()


函数功能: 获取程序开始执行后占用的处理器时间

函数返回: 一般

返回值/CLOCK_PER_SEC

来已秒来表示时间



@函数名称: localtime

函数原型:

struct tm *localtime(const time_t *timer)


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

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



  1. struct




    tm




  2. {


  3. int


    tm_sec;


    /* Seconds: 0-59 (K&R says 0-61?) */





  4. int


    tm_min;


    /* Minutes: 0-59 */





  5. int


    tm_hour;


    /* Hours since midnight: 0-23 */





  6. int


    tm_mday;


    /* Day of the month: 1-31 */





  7. int


    tm_mon;


    /* Months *since* january: 0-11 */





  8. int


    tm_year;


    /* Years since 1900 */





  9. int


    tm_wday;


    /* Days since Sunday (0-6) */





  10. int


    tm_yday;


    /* Days since Jan. 1: 0-365 */





  11. int


    tm_isdst;


    /* +1 Daylight Savings Time, 0 No DST,* -1 don’t know */




  12. };


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




  1. #include <time.h>





  2. #include <stdio.h>





  3. #include <dos.h>





  4. int


    main()


  5. {


  6. time_t


    timer;



  7. struct




    tm


    *tblock;


  8. timer=time(NULL);

  9. tblock=localtime(&timer);

  10. printf(

    “Local time is: %s”


    ,asctime(tblock));



  11. return


    0;


  12. }



@函数名称: asctime

函数原型:

char* asctime(struct tm * ptr)


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

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

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



  1. #include <stdio.h>





  2. #include <string.h>





  3. #include <time.h>





  4. int


    main()


  5. {


  6. struct




    tm


    t;


    //通过自己为每个tm成员赋值,没有什么实际意思,大多数情况下都是通过系统函数计算time_t来得到tm中的各个数值





  7. char


    str[80];


  8. t.tm_sec=1;

  9. t.tm_min=3;

  10. t.tm_hour=7;

  11. t.tm_mday=22;

  12. t.tm_mon=11;

  13. t.tm_year=56;

  14. t.tm_wday=4;

  15. t.tm_yday=0;

  16. t.tm_isdst=0;

  17. strcpy(str,asctime(&t));

  18. printf(

    “%s”


    ,str);



  19. return


    0;


  20. }



@函数名称: ctime


函数原型:

char *ctime(const time_t *tp)


函数功能: 得到日历时间

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

参数说明: time-该参数应由函数time获得  等同于 astime(  localtime(tp) )



  1. #include <stdio.h>





  2. #include <time.h>





  3. int


    main()


  4. {


  5. time_t


    t;


  6. time(&t);

  7. printf(

    “Today’s date and time: %s”


    ,ctime(&t));



  8. return


    0;


  9. }



@函数名称: difftime

函数原型:

double difftime(time_t time2, time_t time1)


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

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

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



  1. #include <time.h>





  2. #include <stdio.h>





  3. #include <dos.h>





  4. #include <conio.h>





  5. int


    main()


  6. {


  7. time_t


    first, second;


  8. System(

    “cls”


    );


  9. first=time(NULL);

  10. delay(2000);

  11. second=time(NULL);

  12. printf(

    “The difference is: %f seconds”


    ,difftime(second,first));


  13. getch();


  14. return


    0;


  15. }



@函数名称: gmtime

函数原型:

struct tm *gmtime(time_t *time)


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

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

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



  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. {


  8. time_t


    t;



  9. struct




    tm


    *gmt, *area;


  10. putenv(tzstr);

  11. tzset();

  12. t=time(NULL);

  13. area=localtime(&t);

  14. printf(

    “Local time is:%s”


    , asctime(area));


  15. gmt=gmtime(&t);

  16. printf(

    “GMT is:%s”


    , asctime(gmt));



  17. return


    0;


  18. }



@函数名称: time


函数原型:

time_t time(time_t *timer)



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

函数返回: 机器日历时间

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



  1. #include <time.h>





  2. #include <stdio.h>





  3. #include <dos.h>





  4. int


    main()


  5. {


  6. time_t


    t;


  7. t=time(NULL);

  8. printf(

    “The number of seconds since January 1,1970 is %ld\n”


    ,t);



  9. return


    0;


  10. }



@函数名称: tzset

函数原型:

void tzset(void)


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

函数返回:

参数说明:



  1. #include <time.h>





  2. #include <stdlib.h>





  3. #include <stdio.h>





  4. int


    main()


  5. {


  6. time_t


    td;


  7. putenv(

    “TZ=PST8PDT”


    );


  8. tzset();

  9. time(&td);

  10. printf(

    “Current time=%s”


    ,asctime(localtime(&td)));



  11. return


    0;


  12. }



@函数名称: strftime

函数原型:

size_t strftime( char *strDest, size_t  maxsize, const char *format, const struct tm  *timeptr);


函数功能: 根据format指向字符串中格式命令把timeptr中保存的时间信息放在strDest指向的字符串中,最多向 strDest中存放maxsize个字符。

该函数返回向strDest指向的字 符串中放置的字符数。

参数说明: 转化结果存在s中,最多maxsize个字符写到s中

函数返回: 写到s中的字符数(不包括’\0’),如果字符数多于ssizemax,函数返回0.



  1. /*



  2. 类似于sprintf(),识别以百分号(%)开始 的格式命令集合,格式化输出结果放在一个字符串中。格式命令列在下面,它们是区分大小写的。



  3. %a 星期几的简写



  4. %A 星期几的全称



  5. %b 月分的简写



  6. %B 月份的全称



  7. %c 标准的日期的时间串



  8. %C 年份的后两位数字



  9. %d 十进制表示的每月的第几天



  10. %D 月/天/年



  11. %e 在两字符域中,十进制表示的每月的第几天



  12. %F 年-月-日



  13. %g 年份的后两位数字,使用基于周的年



  14. %G 年分,使用基于周的年



  15. %h 简写的月份名



  16. %H 24小时制的小时



  17. %I 12小时制的小时



  18. %j 十进制表示的每年的第几天



  19. %m 十进制表示的月份



  20. %M 十时制表示的分钟数



  21. %n 新行符



  22. %p 本地的AM或PM的等价显示



  23. %r 12小时的时间



  24. %R 显示小时和分钟:hh:mm



  25. %S 十进制的秒数



  26. %t 水平制表符



  27. %T 显示时分秒:hh:mm:ss



  28. %u 每周的第几天,星期一为第一天 (值从0到6,星期一为0)



  29. %U 第年的第几周,把星期日做为第一天(值从0到53)



  30. %V 每年的第几周,使用基于周的年



  31. %w 十进制表示的星期几(值从0到6,星期天为0)



  32. %W 每年的第几周,把星期一做为第一天(值从0到53)



  33. %x 标准的日期串



  34. %X 标准的时间串



  35. %y 不带世纪的十进制年份(值从0到99)



  36. %Y 带世纪部分的十进制年份



  37. %z,%Z 时区名称,如果不能得到时区名称则返回空字符。



  38. %% 百分号



  39. */






  40. #include <stdio.h>





  41. #include <time.h>





  42. void


    main()


  43. {


  44. struct




    tm


    *newtime;



  45. char


    tmpbuf[128];



  46. time_t


    lt1;


  47. time( <1 );

  48. newtime=localtime(<1);

  49. strftime( tmpbuf, 128,

    “Today is %A, day %d of %B in the year %Y.\n”


    , newtime);


  50. printf(tmpbuf);



  51. return


    0;


  52. }




  53. /*



  54. 运行结果:



  55. Today is Saturday, day 30 of July in the year 2005.



  56. */



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



知识点:





1.Coordinated Universal Time(UTC):

协调世界时,又称世界标准时间,也即格林威治标准时间(Greenwich Mean Time,GMT),中国内地的时间与UTC得时差为+8,也即UTC+8,美国为UTC-5。



2.Calendar Time:

日历时间,是用“从一个标准时间点到此时的时间经过的秒数”来表示的时间。标准时间点对不同编译器可能会不同,但对一个编译系统来说,标准时间是不变的。



3.epoch:时间点。

在标准c/c++中是一个整数,用此时的时间和标准时间点相差的秒数(即日历时间)来表示



4.clock tick:

时钟计时单元(而不叫做时钟滴答次数),一个时钟计时单元的时间长短是由cpu控制的,一个clock tick不是cpu的一个时钟周期,而是c/c++的一个基本计时单位。



5.计时函数:

clock_t clock(void);//返回值:从“开启这个程序进程”到“程序中调用clock()函数”时之间的cpu时钟计时单元(clock tick)数,MSDN中称为挂钟时间(wal-clock)




  1. //time.h





  2. #ifndef _CLOCK_T_DEFINED





  3. typedef




    long




    clock_t


    ;



  4. #define _CLOCK_T_DEFINED





  5. #endif





  6. //time.h





  7. #define CLOCKS_PER_SEC ((clock_t)1000) //CLOCK_PER_SEC表示一秒会有多少个时钟计时单元,标准c/c++中,最小的计时单位是一毫秒,





  8. //可通过clock()/CLOCK_T_SEC得到进程自身运行时间





6.存储时间和日期的数据结构tm:




  1. struct




    tm




  2. {


  3. int


    tm_sec;


    //秒,[0-59]





  4. int


    tm_min;


    //分,[0-59]





  5. int


    tm_hour;


    //时,[0-23]





  6. int


    tm_mday;


    //一个月中的日期,[1-31]





  7. int


    tm_mon;


    //月份,[0-11]





  8. int


    tm_year;


    //年份,其值为实际年份-1900





  9. int


    tm_wday;


    //星期,[0-6]





  10. int


    tm_yday;


    //从每年的1月1日开始计数,[0-365]





  11. int


    tm_isdst;


    //夏令标识符,实行夏令时,tm_isdst为正;不实行夏令时,tm_isdst为0;不了解情况时,tm_isdst为负




  12. };


  13. #define _TM_DEFINED





  14. #endif





7.日历时间用time_t存储:




  1. //time.h





  2. #ifndef _TM_DEFINED





  3. //time.h





  4. #ifndef _TIME_T_DEFINED





  5. typedef




    long




    time_t


    ;



  6. #define _TIME_T_DEFINED





  7. #endif



使用time_t表示的日历时间只能存储到2038年1月18日19时14分07秒,为了能表示更久远的时间,一些编译器引入了64位甚至更长的整形数保 存日历时间,如VC中采用_time64_t数据类型,通过_time64()函数获得日历时间(不是32位的time())这样可以计算3001年1月 1日0时0分0秒(不包括该时间点)之前的时间。