linux时间函数

1、时间类型。Linux下常用的时间类型:time_t,struct timeval,struct tm

(1)time_t是一个长整型,一般用来表示用1970年以来的秒数。
(2)Struct timeval有两个成员,一个是秒,一个是微妙。

struct timeval

  {
               long tv_sec;       

               long tv_usec;  

       };

(3)struct tm是直观意义上的时间表示方法:

struct tm {
                      int     tm_sec;         

                      int     tm_min;       

                      int     tm_hour;        

                      int     tm_mday;      

                      int     tm_mon;        

                      int     tm_year;        

                      int     tm_wday;        

                      int     tm_yday;       

                      int     tm_isdst;      

              };


2、 时间操作

(1) 时间格式间的转换函数
主要是 time_t、struct tm、时间的字符串格式之间的转换。看下面的函数参数类型以及返回值类型:

char *asctime(const struct tm *tm);
char *ctime(const time_t *timep);


struct tm *gmtime(const time_t *timep);  //返回的是格林威治时间
struct tm *localtime(const time_t *timep);  //返回的是本地时间


time_t mktime(struct tm *tm);

(2) 获取时间函数
两个函数,获取的时间类型看原型就知道了:

time_t time(time_t *t);
int gettimeofday(struct timeval *tv, struct timezone *tz);
前者获取time_t类型,后者获取struct timeval类型,因为类型的缘故,前者只能精确到秒,后者可以精确到微秒。相对于gettimeofday,还有settimeofday用来设置时间

posted @ 2011-10-20 22:57  only_eVonne  阅读(1896)  评论(0编辑  收藏  举报