Welcome 微信登录
编程资源 图片资源库 蚂蚁家优选 PDF转换器

首页 / 操作系统 / Linux / Linux C时间函数 time_t struct tm

Linux C时间函数 time_t struct tm#include<time.h>关于时间的类型:time_t long型,表示从1970年1月1日到现在经过的秒数。struct tm {
          int tm_sec;     /* 秒 – 取值区间为[0,59] */
          int tm_min;     /* 分 - 取值区间为[0,59] */
          int tm_hour;      /* 时 - 取值区间为[0,23] */
          int tm_mday;      /* 一个月中的日期 - 取值区间为[1,31] */
          int tm_mon;     /* 月份(从一月开始,0代表一月) - 取值区间为[0,11] */
          int tm_year;      /* 年份,其值等于实际年份减去1900 */
          int tm_wday;      /* 星期 – 取值区间为[0,6],其中0代表星期天,1代表星期一,以此类推 */
          int tm_yday;      /* 从每年的1月1日开始的天数 – 取值区间为[0,365],其中0代表1月1日,1代表1月2日 };一般用time_t:定义 time_t now;通过now =time(NULL)返回当前的时间(秒数),time函数形式是“time_t time(time_t*)”,也可以像这样获取“time(&now)”。用户看这个大的Long型数据是没啥意义,一般通过这个函数先后得到时间计算时间差比较方便。srand(unsigned(time(NULL))获取种子的用法运用的就是这个秒数。想清晰得得到现在的年月日信息,就不能通过这个time_t了,需要用struct tm来显示,而将time_t和struct tm转换的函数就是localtime(time_t*),返回一个struct tm*类型,注意是指针类型。然后可以得到tm中的成员,如上,皆为整形。如果想直接获取时间的字符串。用 char * asctime(const struct tm * timeptr);根据struct tm结构或者用 char* ctime(time_t* t);//根据time_t输出的形式都是像这样 "Tue Jan  6 13:53:16 2015 "注意思考为什么我们只需要定义一个struct tm*的指针,并没有申请空间,却可以获取其成员?因为locatime返回指针是一个静态变量的地址。同样asctime也是同样原理。------------------------------分割线------------------------------C++ Primer Plus 第6版 中文版 清晰有书签PDF+源代码 http://www.linuxidc.com/Linux/2014-05/101227.htm读C++ Primer 之构造函数陷阱 http://www.linuxidc.com/Linux/2011-08/40176.htm读C++ Primer 之智能指针 http://www.linuxidc.com/Linux/2011-08/40177.htm读C++ Primer 之句柄类 http://www.linuxidc.com/Linux/2011-08/40175.htm将C语言梳理一下,分布在以下10个章节中:
  1. Linux-C成长之路(一):Linux下C编程概要 http://www.linuxidc.com/Linux/2014-05/101242.htm
  2. Linux-C成长之路(二):基本数据类型 http://www.linuxidc.com/Linux/2014-05/101242p2.htm
  3. Linux-C成长之路(三):基本IO函数操作 http://www.linuxidc.com/Linux/2014-05/101242p3.htm
  4. Linux-C成长之路(四):运算符 http://www.linuxidc.com/Linux/2014-05/101242p4.htm
  5. Linux-C成长之路(五):控制流 http://www.linuxidc.com/Linux/2014-05/101242p5.htm
  6. Linux-C成长之路(六):函数要义 http://www.linuxidc.com/Linux/2014-05/101242p6.htm
  7. Linux-C成长之路(七):数组与指针 http://www.linuxidc.com/Linux/2014-05/101242p7.htm
  8. Linux-C成长之路(八):存储类,动态内存 http://www.linuxidc.com/Linux/2014-05/101242p8.htm
  9. Linux-C成长之路(九):复合数据类型 http://www.linuxidc.com/Linux/2014-05/101242p9.htm
  10. Linux-C成长之路(十):其他高级议题
本文永久更新链接地址:http://www.linuxidc.com/Linux/2015-01/111472.htm