日期与时间函数time.h

原文出自:http://blog.csdn.net/zhoudaxia/article/details/4647136



日期与时间函数在time.h中,主要表示处理器时钟的clock_t类型、表示时间的time_t类型、时钟每秒滴答数CLOCKS_PER_SEC、描述日历时间的struct tm结构、函数clock、time、asctime、ctime、gmtime、localtime、mktime、difftime、strftime、wcsftime(宽字符版本),其他的都是非标准扩展。
    1、time.h:类型clock_t, time_t的定义,宏CLOCKS_PER_SEC,struct tm结构包括秒数、分钟数、小时数、日期数、月份、年份(从1900年算起)、星期、是当年的第几天、夏时制标志共9个成员。

  1. /* ISO C99 Standard: 7.23 日期和时间 <time.h> */  
  2. #ifndef _TIME_H  
  3. #if (! defined __need_time_t && !defined __need_clock_t && /  
  4.      ! defined __need_timespec)  
  5. # define _TIME_H    1  
  6. # include <features.h>  
  7. __BEGIN_DECLS  
  8. #endif  
  9. #ifdef  _TIME_H  
  10. /* 从<stddef.h>中获得size_t和NULL  */  
  11. # define __need_size_t  
  12. # define __need_NULL  
  13. # include <stddef.h>  
  14. /* 这里定义常量CLOCKS_PER_SEC,为处理器每秒的时钟滴答数  */  
  15. # include <bits/time.h>  
  16. /* 这里是针对一同常量的过时的POSIX.1-1988中的宏名  */  
  17. # if !defined __STRICT_ANSI__ && !defined __USE_XOPEN2K  
  18. #  ifndef CLK_TCK  
  19. #   define CLK_TCK  CLOCKS_PER_SEC  
  20. #  endif  
  21. # endif  
  22. #endif /* <time.h> included  */  
  23. #if !defined __clock_t_defined && (defined _TIME_H || defined __need_clock_t)  
  24. # define __clock_t_defined  1  
  25. # include <bits/types.h>  
  26. __BEGIN_NAMESPACE_STD  
  27. typedef __clock_t clock_t;   /* 定义处理器时钟类型,clock函数返回该类型 */  
  28. __END_NAMESPACE_STD  
  29. #if defined __USE_XOPEN || defined __USE_POSIX || defined __USE_MISC  
  30. __USING_NAMESPACE_STD(clock_t)  
  31. #endif  
  32. #endif /* clock_t没有定义,且<time.h>有可能需要clock_t  */  
  33. #undef  __need_clock_t  
  34. #if !defined __time_t_defined && (defined _TIME_H || defined __need_time_t)  
  35. # define __time_t_defined   1  
  36. # include <bits/types.h>  
  37. __BEGIN_NAMESPACE_STD  
  38. typedef __time_t time_t;   /* 定义时间类型,是一个整数类型,time函数返回该类型 */  
  39. __END_NAMESPACE_STD  
  40. #if defined __USE_POSIX || defined __USE_MISC || defined __USE_SVID  
  41. __USING_NAMESPACE_STD(time_t)  
  42. #endif  
  43. #endif /* time_t没有定义,且<time.h>有可能需要time_t */  
  44. #undef  __need_time_t  
  45. #if !defined __clockid_t_defined && /  
  46.    ((defined _TIME_H && defined __USE_POSIX199309) || defined __need_clockid_t)  
  47. # define __clockid_t_defined    1  
  48. # include <bits/types.h>  
  49. typedef __clockid_t clockid_t; /* 定义表示时钟ID的类型,clock和timer函数中会用到它 */  
  50. #endif /* clockid_t没有定义,且<time.h>有可能需要clockid_t  */  
  51. #undef  __clockid_time_t  
  52. #if !defined __timer_t_defined && /  
  53.     ((defined _TIME_H && defined __USE_POSIX199309) || defined __need_timer_t)  
  54. # define __timer_t_defined  1  
  55. # include <bits/types.h>  
  56. typedef __timer_t timer_t;  /* 定义表示时间ID的类型,time_create函数会用到它 */  
  57. #endif /* timer_t没有,且<time.h>有可能需要timer_t  */  
  58. #undef  __need_timer_t  
  59. #if !defined __timespec_defined &&              /  
  60.     ((defined _TIME_H &&                    /  
  61.       (defined __USE_POSIX199309 || defined __USE_MISC)) || /  
  62.       defined __need_timespec)  
  63. # define __timespec_defined 1  
  64. # include <bits/types.h>  /* 这里为我们定义__time_t  */  
  65. /* POSIX.1b中表示时间值的结构,这类似于struct timeval,但使用纳秒而不是微秒 */  
  66. struct timespec  
  67.   {  
  68.     __time_t tv_sec;        /* 秒数  */  
  69.     long int tv_nsec;       /* 纳秒数  */  
  70.   };  
  71. #endif /* timespec没有定义,且<time.h>有可能需要timespec */  
  72. #undef  __need_timespec  
  73. #ifdef  _TIME_H  
  74. __BEGIN_NAMESPACE_STD  
  75. /* 被其他时间函数使用的tm结构  */  
  76. struct tm  
  77. {  
  78.   int tm_sec;           /* 秒数   [0-60] (允许最多1个闰秒) */  
  79.   int tm_min;           /* 分钟数  [0-59] */  
  80.   int tm_hour;          /* 小时数  [0-23] */  
  81.   int tm_mday;          /* 日期   [1-31] */  
  82.   int tm_mon;           /* 月份   [0-11] */  
  83.   int tm_year;          /* 从1900年起的年份数  - 1900.  */  
  84.   int tm_wday;          /* 星期   [0-6] */  
  85.   int tm_yday;          /* 从1月1日算起的天数 [0-365]   */  
  86.   int tm_isdst;         /* 夏时制标志,1夏时制,0非夏时制,-1不确定 [-1/0/1]*/  
  87. #ifdef  __USE_BSD  
  88.   long int tm_gmtoff;       /* UTC以东的秒数  */  
  89.   __const char *tm_zone;    /* 时区缩写  */  
  90. #else  
  91.   long int __tm_gmtoff;     /* UTC以东的秒数  */  
  92.   __const char *__tm_zone;  /* 时区缩写  */  
  93. #endif  
  94. };  
  95. __END_NAMESPACE_STD  
  96. #if defined __USE_XOPEN || defined __USE_POSIX || defined __USE_MISC  
  97. __USING_NAMESPACE_STD(tm)  
  98. #endif  
  99. #ifdef __USE_POSIX199309  
  100. /* POSIX.1b中表示时间初始值和时间间隔的结构  */  
  101. struct itimerspec  
  102.   {  
  103.     struct timespec it_interval;  
  104.     struct timespec it_value;  
  105.   };  
  106. /* 我们可以使用一个简单的前向声明  */  
  107. struct sigevent;  
  108. #endif  /* POSIX.1b */  
  109. #ifdef __USE_XOPEN2K  
  110. # ifndef __pid_t_defined  
  111. typedef __pid_t pid_t;  
  112. #  define __pid_t_defined  
  113. # endif  
  114. #endif  
  115. __BEGIN_NAMESPACE_STD  
  116. /* 程序到目前为止的使用时间(用户时间+系统时间) 
  117.    返回的结果/ClOCKS_PER_SEC即为程序运行的时间(单位为秒) */  
  118. extern clock_t clock (void) __THROW;  
  119. /* 返回当前日历时间,保存到TIMER中,如果TIMER不为NULL的话  */  
  120. extern time_t time (time_t *__timer) __THROW;  
  121. /* 返回两个时间TIME1和TIME0的差值(秒数)  */  
  122. extern double difftime (time_t __time1, time_t __time0)  
  123.      __THROW __attribute__ ((__const__));  
  124. /* 返回TP的time_t表示,并且对TP进行规格化  */  
  125. extern time_t mktime (struct tm *__tp) __THROW;  
  126. /* 根据控制串FORMAT对TP进行格式化,并保存到S中,最多写入MAXSIZE个字符到S中,并返回 
  127.     写入的字符数,如果字符串长超过MAXSIZE,则返回0 */  
  128. extern size_t strftime (char *__restrict __s, size_t __maxsize,  
  129.             __const char *__restrict __format,  
  130.             __const struct tm *__restrict __tp) __THROW;  
  131. __END_NAMESPACE_STD  
  132. # ifdef __USE_XOPEN  
  133. /* 根据FORMAT解析S,把二进制的时间信息保存到TP中。返回值为指向S中第一个未 
  134.     解析的字符的指针 */  
  135. extern char *strptime (__const char *__restrict __s,  
  136.                __const char *__restrict __fmt, struct tm *__tp)  
  137.      __THROW;  
  138. # endif  
  139. # ifdef __USE_GNU  
  140. /* 与上面两个函数类似,但从提供的区域设置中获取信息,而不是用全局的区域设置 */  
  141. # include <xlocale.h>  
  142. extern size_t strftime_l (char *__restrict __s, size_t __maxsize,  
  143.               __const char *__restrict __format,  
  144.               __const struct tm *__restrict __tp,  
  145.               __locale_t __loc) __THROW;  
  146. extern char *strptime_l (__const char *__restrict __s,  
  147.              __const char *__restrict __fmt, struct tm *__tp,  
  148.              __locale_t __loc) __THROW;  
  149. # endif  
  150. __BEGIN_NAMESPACE_STD  
  151. /* 返回TIMER的struct tm表示(为格林尼治标准时间UTC) */  
  152. extern struct tm *gmtime (__const time_t *__timer) __THROW;  
  153. /* 返回TIMER的struct tm表示(为本地时间) */  
  154. extern struct tm *localtime (__const time_t *__timer) __THROW;  
  155. __END_NAMESPACE_STD  
  156. # if defined __USE_POSIX || defined __USE_MISC  
  157. /* 返回TIMER的struct tm表示(为格林尼治标准时间UTC), 
  158.     使用TP来存放结果 */  
  159. extern struct tm *gmtime_r (__const time_t *__restrict __timer,  
  160.                 struct tm *__restrict __tp) __THROW;  
  161. /* 返回TIMER的struct tm表示(为本地时间),使用 
  162.     TP来存放结果 */  
  163. extern struct tm *localtime_r (__const time_t *__restrict __timer,  
  164.                    struct tm *__restrict __tp) __THROW;  
  165. # endif /* POSIX or misc */  
  166. __BEGIN_NAMESPACE_STD  
  167. /* 返回TP的可打印日期与时间字符串,格式为"Day Mon dd hh:mm:ss yyyy/n" */  
  168. extern char *asctime (__const struct tm *__tp) __THROW;  
  169. /* 等价于asctime(localtime (timer))  */  
  170. extern char *ctime (__const time_t *__timer) __THROW;  
  171. __END_NAMESPACE_STD  
  172. # if defined __USE_POSIX || defined __USE_MISC  
  173. /* 上述函数的可重入版本 */  
  174. /* 返回TP的可打印日期与时间字符串,格式为"Day Mon dd hh:mm:ss yyyy/n", 
  175.     存放在BUF中 */  
  176. extern char *asctime_r (__const struct tm *__restrict __tp,  
  177.             char *__restrict __buf) __THROW;  
  178. /* 等价于asctime_r(localtime_r(timer, *TMP*), buf)  */  
  179. extern char *ctime_r (__const time_t *__restrict __timer,  
  180.               char *__restrict __buf) __THROW;  
  181. # endif /* POSIX or misc */  
  182. /* 在localtime.c中已经定义了  */  
  183. extern char *__tzname[2];   /* 当前时区名称  */  
  184. extern int __daylight;      /* 如果夏时制在使用的话  */  
  185. extern long int __timezone; /* UTC以西的秒数  */  
  186. /* 下面是其他的一些扩展函数 */  
  187. __END_DECLS  
  188. #endif /* <time.h> included.  */  
  189. #endif /* <time.h> not already included.  */  

    2、clock函数:返回处理器时间的近似值,用clock_t类型表示,通常以微秒为单位。标准C的clock函数只是处理了一下调用出错时的情况(返回-1并转换成clock_t类型)。真正的实现使用的是Linux的clock函数,标准C的clock函数被直接映射到了Linux的clock函数。

  1. /* clock.c:clock函数的实现 */  
  2. #include <sys/times.h>  
  3. #include <time.h>  
  4. #include <errno.h>  
  5. /* 返回程序到目前为止的使用时间(用户时间+系统时间)  */  
  6. clock_t  
  7. clock ()  
  8. {  
  9.   __set_errno (ENOSYS);  
  10.   return (clock_t) -1;  
  11. }  
  12. stub_warning (clock)  
  13. #include <stub-tag.h>  

    3、time函数:返回当前日历时间,用time_t类型表示。time函数与上面的clock函数类似,直接使用Linux的time函数实现(独自地处理了调用出错时的情况,即返回-1并转换成time_t类型)。

  1. /* time.c:time函数的实现  */  
  2. #include <errno.h>  
  3. #include <time.h>  
  4. /* 返回当前日历时间,保存到TIMER中,如果TIMER不为NULL的话  */  
  5. time_t  
  6. time (timer)  
  7.      time_t *timer;  
  8. {  
  9.   __set_errno (ENOSYS);  
  10.   if (timer != NULL)  
  11.     *timer = (time_t) -1;  
  12.   return (time_t) -1;  
  13. }  
  14. libc_hidden_def (time)  
  15. stub_warning (time)  
  16. #include <stub-tag.h> 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值