Chinaunix首页 | 论坛 | 博客
  • 博客访问: 206170
  • 博文数量: 32
  • 博客积分: 306
  • 博客等级: 二等列兵
  • 技术积分: 337
  • 用 户 组: 普通用户
  • 注册时间: 2011-11-15 10:02
文章分类
文章存档

2013年(21)

2011年(11)

我的朋友

分类: LINUX

2013-02-01 17:57:17

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用来设置时间

分类: Linux编程
阅读(2107) | 评论(0) | 转发(0) |
0

上一篇:__attribute__((packed))详解

下一篇:没有了

给主人留下些什么吧!~~