linux的时间相关函数

来源:互联网 发布:尔雅公选课软件 编辑:程序博客网 时间:2024/05/17 20:34

linux的系统时间有三种表示格式:

1)time_t为日历时间,表示的时间以秒为单位,表示从1970年1月1日00:00:00到现在的时间

2)struct tm数据结构表示以年月日格式表示的时间

struct tm
{
  int tm_sec;            /* Seconds.    [0-60] (1 leap second) */
  int tm_min;            /* Minutes.    [0-59] */
  int tm_hour;            /* Hours.    [0-23] */
  int tm_mday;            /* Day.        [1-31] */
  int tm_mon;            /* Month.    [0-11] */
  int tm_year;            /* Year    - 1900.  */
  int tm_wday;            /* Day of week.    [0-6] */
  int tm_yday;            /* Days in year.[0-365]    */
  int tm_isdst;            /* DST.        [-1/0/1]*/

#ifdef    __USE_BSD
  long int tm_gmtoff;        /* Seconds east of UTC.  */
  __const char *tm_zone;    /* Timezone abbreviation.  */
#else
  long int __tm_gmtoff;        /* Seconds east of UTC.  */
  __const char *__tm_zone;    /* Timezone abbreviation.  */
#endif
};

3)以字符串格式表示的时间


获取时间都用time()函数从系统获取time_t日历时间,然后可以在三种格式的时间间进行转换

相关时间处理的函数原型:

extern time_t time (time_t *__timer) __THROW;

extern time_t mktime (struct tm *__tp) __THROW;

extern char *asctime (__const struct tm *__tp) __THROW;

extern char *ctime (__const time_t *__timer) __THROW;

extern struct tm *gmtime (__const time_t *__timer) __THROW;

extern struct tm *localtime (__const time_t *__timer) __THROW;

extern size_t strftime (char *__restrict __s, size_t __maxsize,__const char *__restrict __format,   __const struct tm *__restrict __tp) __THROW;

extern int gettimeofday (struct timeval *__restrict __tv,timezone_ptr_t __tz) __THROW __nonnull ((1));

/* Time used by the program so far (user time + system time).
   The result / CLOCKS_PER_SECOND is program time in seconds.  */
extern clock_t clock (void) __THROW;


struct timeval
  {
    __time_t tv_sec;        /* Seconds.  */
    __suseconds_t tv_usec;    /* Microseconds.  */
  };

struct timezone
  {
    int tz_minuteswest;        /* Minutes west of GMT.  */
    int tz_dsttime;        /* Nonzero if DST is ever in effect.  */
  };


跟延时相关的函数:

短延时,这些函数读是忙等待函数,等待超时过程中不会处理其他事情

void ndelay(unsigned long x)

void udelay(unsigned long x)

void mdelay(unsigned long x)

长延时:

void msleep(unsigned int msecs);
unsigned long msleep_interruptible(unsigned int msecs);
void ssleep(unsigned int seconds)

sleep()

schedule_timeout()


示例程序:







原创粉丝点击