Chinaunix首页 | 论坛 | 博客
  • 博客访问: 228229
  • 博文数量: 46
  • 博客积分: 1400
  • 博客等级: 上尉
  • 技术积分: 620
  • 用 户 组: 普通用户
  • 注册时间: 2009-01-12 18:04
文章分类

全部博文(46)

文章存档

2010年(7)

2009年(39)

我的朋友

分类: LINUX

2009-03-01 19:52:08


1. 时间类型 与 相关系统函数

1.1 系统提供的类型与接口
             

纳秒级访问结构
 struct timespec{
  time_t  tv_sec;         /**//* seconds */
  long    tv_nsec;        /**//* nanoseconds */
  };

微秒级访问结构
 struct timeval {
  long tv_sec;        /**//* seconds */
  long tv_usec;  /**//* microseconds */
       };
      
获取timeval
int gettimeofday(struct timeval *tv, struct timezone *tz);

延迟函数:
unsigned int sleep(unsigned int seconds);
void usleep(unsigned long usec);
int nanosleep(const struct timespec *req, struct timespec *rem);
int select(int n, fd_set *readfds, fd_set *writefds, fd_set *exceptfds,struct timeval *timeout);
int pselect(int   n,   fd_set   *readfds,  fd_set  *writefds,  fd_set *exceptfds, const struct timespec *timeout, const sigset_t *sigmask);
仅通过函数原型中时间参数类型,可以猜测sleep可以精确到秒级,usleep/select可以精确到微妙级,nanosleep和pselect可以精确到纳秒级。
而实际实现中,linux上的nanosleep和alarm相同,都是基于内核时钟机制实现,受linux内核时钟实现的影响,并不能达到纳秒级的精度,man nanosleep也可以看到这个说明,man里给出的精度是:Linux/i386上是10 ms ,Linux/Alpha上是1ms。


1.2 C语言提供的类型与接口

 头文件:定义了时间类型与相应的时间操作函数

时间类型:

a. 均通过数学类型来表示时间。
 
 time_t time_t是一个长整型,一般用来表示用1970年以来的秒数。
 操作函数 time_t time(time_t* tp)
 参数: 待填充的时间存放地址
 返回值: 返回当前计算机日历时间,是个长整型的计数。
 
 clock_t
 操作函数 clock_t clock()
 返回值: 返回当前计算机开机到现在的时钟计数
 注: 通过
      sec = clock()/CLK_PER_SEC 获取开机的秒数计数。

b. stuct tm 类型

struct tm holds the components of a calendar time:
  int tm_sec;  seconds after the minute (0,61)
  int tm_min;  minutes after the hour (0,59)
  int tm_hour;  hours since midnight (0,23)
  int tm_mday;  day of the month (1,31)
  int tm_mon;  months since January (0,11)
  int tm_year;  years since 1900
  int tm_wday;  days since Sunday (0,6)
  int tm_yday;  days since January 1 (0,365)
  int tm_isdst; Daylight Saving Time flag
其中
    tm_isdst 作为该结构的标签来判断,夏令时时间是否有效。
    tm_isdst > 0 夏令时有效
    tm_isdst = 0 夏令时无效
    tm_isdst = -1 整个时间结构不可用
   
c. 时间操作函数

  时间比较函数:
    double difftime(time_t time2, time_t time1)
    difftime returns time2-time1 expressed in seconds.  

  时间转化函数:
  
  struct tm 转化为 time_t
  time_t mktime(struct tm *tp)
  
  mktime converts the local time in the structure *tp into calendar time in the same representation used by time.
  The components will have values in the ranges shown.
  mktime returns the calendar time or -1 if it cannot be represented

  
   struct tm 转化为 char*
  char *asctime(const struct tm *tp)
  asctime*tp into a string of the form
      Sun Jan  3 15:14:13 1988\n\0

  time_t 转化为 char*
  char *ctime(const time_t *tp)
  ctime converts the calendar time *tp to local time; it is equivalent to
      asctime(localtime(tp))

  time_t 转化为 struct tm, 使用格林威治时间
    struct tm *gmtime(const time_t *tp)
  gmtime converts the calendar time *tp into Coordinated Universal Time (UTC).
  It returns NULL if UTC is not available. The name gmtime has historical significance.

  time_t 转化为 struct tm,使用本地时间
  struct tm *localtime(const time_t *tp)
  localtime converts the calendar time *tp into local time.

  将struct tm转化为用户自定义格式的字符串
  size_t strftime(char *s, size_t smax, const char *fmt, const struct tm *tp)
  strftime returns the number of characters, excluding the '\0', or zero if more than smax characters were produced.
  转化符定义: 
  %a abbreviated weekday name.
  %A full weekday name.
  %b abbreviated month name.
  %B full month name.
  %c local date and time representation.
  %d day of the month (01-31).
  %H hour (24-hour clock) (00-23).
  %I hour (12-hour clock) (01-12).
  %j day of the year (001-366).
  %m month (01-12).
  %M minute (00-59).
  %p local equivalent of AM or PM.
  %S second (00-61).
  %U week number of the year (Sunday as 1st day of week) (00-53).
  %w weekday (0-6, Sunday is 0).
  %W week number of the year (Monday as 1st day of week) (00-53).
  %x local date representation.
  %X local time representation.
  %y year without century (00-99).
  %Y year with century.
  %Z time zone name, if any.
  %%   %

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