Chinaunix首页 | 论坛 | 博客
  • 博客访问: 889510
  • 博文数量: 380
  • 博客积分: 3495
  • 博客等级: 中校
  • 技术积分: 3996
  • 用 户 组: 普通用户
  • 注册时间: 2007-12-02 09:35
文章分类

全部博文(380)

文章存档

2015年(2)

2014年(5)

2013年(9)

2012年(9)

2011年(67)

2010年(103)

2009年(182)

2008年(3)

我的朋友

分类: LINUX

2009-04-12 08:41:34

Linux在编程时使用了以下几种时间类型,
1.typedef long time_t;      //,最常用的
2.struct timeval {          //,最小单位是微秒
   time_t      tv_sec;
   suseconds_t tv_usec;
}
3.struct timespec{           //,最小单位是纳秒
   time_t tv_sec;
   long   tv_nsec;
}
4.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_yda;
  int tm_isdst;
  #ifdef _BSD_SOURCE
  long tm_gmtoff;
  xonst char *tm_zone;
  #endif
}

知道了上面的几个结构体,那么开始使用它们吧。
得到当前时间时间:
time_t  time(time_t *t);
这个函数返回的是自1970/1/1 0时开始的秒数。
所以看起来,真的不好懂。
再来一个,
int gettimeofday(struct timeval *tv,
                struct timezone *tz); //tz只能传入NULL,因为不用了。

下一个,使用timespec的
int clock_gettime(clockid_t clock_id,
                  struct timespec *ts);
其中clock_id有下面几个值:
CLOCK_REALTIME,CLOCK_MONOTONIC,CLOCK_PROCESS_CPUTIME_ID,CLOCK_THREAD_CPUTIME_ID

相应的设定系统时间的函数也是三个:
int stime(time_t *t);
int settimeofday(const struct timeval *tv,
                 const struct timezone *tz);
int clock_settime(clockid_t clock_id,
                  const struct timespec *ts);
不过通常第三个函数是只支持CLOCK_REALTIME参数的。

还有一个常用的时间函数是得到进程本身的运行时间
struct tms{
  clock_t tms_utime;  //用户空间运行时间
  clock_t tms_stime;  //系统空间运行时间
  clock_t tms_cutime;  //子进程的用户空间运行时间
  clock_t tms_cstime;  //子进程的系统空间运行时间
}
clock_t times(struct tms *buf);

不过我想大家最希望用的应该是tm这个结构体吧。
下面是关于它的函数
char *asctime(const struct tm*tm);
char *asctime_r(const struct tm *tm, char *buf);//buf最少要26个字符长
与time_t的转换
time_t mktime(struct tm *tm);
char * ctime(const time_t *timep;
char * ctime_r(const time_t *timep, char *buf);

struct tm *gmtime(const time_t *timep);
struct tm *gmtime_r(const time_t *timep,struct tm *result);

struct tm *localtime(const time_t *timep);
struct tm *localtime_r(const time_t *timep, struct tm *result);

接下来是调整时间的函数,LINX本身调整时间建议不直接用设定时间的函数,如果时间差太大是会对系统造成影响的。所以引入了调整时间函数。
int adjtime(const struct timeval *delta,
            struct timeval *olddelta);
还有一个,int adjtimex(struct timex *adj);
需要知道的是这个调整不是马上就能完成的,它会是一个渐进的过程,
如果要调整的时间比当前时间晚,那么就加快系统的时钟,
相反,就减慢系统时钟,直到与相调整的时间相同。

sleep,这个相对比较简单,只要调用一下sleep函数就可以了。
不过由于精度不同,LINUX有好几个呢。
unsigned int sleep(unsigned int seconds);
void usleep(unsigned long usec);
int usleep(useconds_t usec);
int nanosleep(const struct timespec *req,
              struct timespec *res);
依次提供了秒级,微秒级,纳秒级的sleep函数
不过标准的POSIX是用下的函数的。
int clock_nanosleep(clockid_t clock_id,
                    int flags,
                    const struct timespec *req,
                    struct timespec *res);
最古老的确是下面的
int select(int n,
           fd_set *readfds,
           fd_set *writefds,
           fd_set *execptfds,
           struct timeval *timeout);
接下来是最后关于时间的东西了,
timer

unsigned int alarm(unsigned int seconds);
对应的处理函数
void alarm_handler(int signum);
signal(SIGALRM, alarm_handler);

另外的函数
int getitimer(int which,
             struct itimerval *value);
int setitimer(int which,
             const struct itimerval *value,
             struct itimerval *ovalue);
POSIX的标准是
int timer_create(clockid_t clockid,
                 struct sigevent *evp,
                 timer_t *timerid);




 
阅读(807) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~