linux 0.11 - linux下的时间相关数据结构和函数

  1. /* *******************************
  2.  * from linux 0.11
  3.  *
  4.  * file: linux/include/time.h
  5. *********************************/
  6. #ifndef _TIME_H
  7. #define _TIME_H

  8. #ifndef _TIME_T
  9. #define _TIME_T
  10. typedef long time_t;
  11. #endif

  12. #ifndef _SIZE_T
  13. #define _SIZE_T
  14. typedef unsigned int size_t
  15. #endif

  16. #define CLOCKS_PER_SEC 100

  17. typedef long clock_t;

  18. struct tm
  19. {
  20.     int tm_sec;
  21.     int tm_min;
  22.     int tm_hour;
  23.     int tm_mday;
  24.     int tm_mon;
  25.     int tm_year;
  26.     int tm_wday;
  27.     int tm_yday;
  28.     int tm_isdst;
  29. };

  30. /* 确定处理器使用时间。返回程序所使用处理器时间(滴答数)的近似值*/
  31. clock_t clock(void);

  32. /* 取时间(秒数).返回自1970.1.1:0:0:0开始的秒数(成为日历时间) */
  33. time_t time(time_t * tp);

  34. /* 计算时间差。 */
  35. double difftime(time_t t1,time_t t2);

  36. /* 将tm结构表示的时间转化成日历时间 */
  37. time_t mktime(struct tm *tmp);

  38. /* 将tm结构表示的时间转换成一个字符串 */
  39. char *asctime(const struct tm *tmp);

  40. /* 将日历时间转换成一个字符串形式 */
  41. char *ctime(const time_t * tp);

  42. /* 将日历时间转换成tm结构表示的时间(UTC) */
  43. struct tm * gmtime(const time_t *tp);

  44. /* 将日历时间转换成tm结构表示的指定时区的时间 */
  45. struct tm * localtime(const time_t *tp);

  46. /* 将tm结构表示的时间利用格式字符串fmt转换成最大长度为smax的字符串并存储在s中 */
  47. size_t strftime(char *s,size_t smax,const char *fmt,const struct tm *tmp);

  48. /* 初始化时间转换信息,使用环境变量TZ,对zname变量进行初始化。在与时区相关的时间转换中自动调用该函数 */
  49. void tzset(void);

  50. #endif
测试程序:

  1. #include<time.h>
  2. #include<stdio.h>

  3. int main()
  4. {
  5.     clock_t ct;
  6.     ct = clock();
  7.     printf("clocks:%ld/n",ct);
  8.     
  9.     time_t tt;
  10.     time(&tt);  /* 获取日历时间 */
  11.     printf("tt -- %ld/n",tt);
  12.     printf("ctime:/t/t%s/n",ctime(&tt));  /* 打印日历时间的字符串形式 */
  13.     
  14.     struct tm *tmp;
  15.     tmp = gmtime(&tt); /* 将日历时间转换成tm结构表示的时间 */
  16.     printf("tm_sec:/t%d/n",tmp->tm_sec);
  17.     printf("tm_min:/t%d/n",tmp->tm_min);
  18.     printf("tm_year:/t%d/n",tmp->tm_year);
  19.     
  20.     char *str = asctime(tmp);  /* 将tm结构时间转换成字符串 */
  21.     printf("asctime:/t%s/n",str);
  22.     
  23.     return 0;
  24. }


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值