欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Linux应用---时间编程

程序员文章站 2024-01-24 08:59:52
...

时间应用编程

描述时间的结构

相关头文件 <sys/time.h > <time.h>
time.h 是ISO C99 标准日期头文件。
sys/time.h 是Linux系统的日期头文件。

注: sys/time.h 通常会包含include “time.h”

时间和时区

<sys/time.h>
/* Structure crudely representing a timezone.
   This is obsolete and should never be used.  */
struct timezone
  {
    int tz_minuteswest;		/* Minutes west of GMT.  */
    int tz_dsttime;		/* Nonzero if DST is ever in effect.  */
  };
上面的过时了不应该使用,而且在这个头文件中也找不到timeval的定义了
新的定义在下面的头文件中,不过使用的时候一般调用sys/time.h就可以了
很多的接口还在这个头文件中的,
<linux/time.h>
///注意这个不是内核的头文件,是在usr/include目录下的,内核空间驱动的头文件在kernel目录下的。
197011日起经过的秒数和us
struct timespec {
	__kernel_time_t	tv_sec;			/* seconds */
	long		tv_nsec;		/* nanoseconds */
};
#endif

struct timeval {
	__kernel_time_t		tv_sec;		/* seconds */
	__kernel_suseconds_t	tv_usec;	/* microseconds */
};

struct timezone {
	int	tz_minuteswest;	/* minutes west of Greenwich */
	int	tz_dsttime;	/* type of dst correction */
};
/*
 * The IDs of the various system clocks (for POSIX.1b interval timers):
 */
#define CLOCK_REALTIME			0			//1970年1.1日算起
#define CLOCK_MONOTONIC			1			//系统的启动时间不能被设置
#define CLOCK_PROCESS_CPUTIME_ID	2		//本进程运行时间
#define CLOCK_THREAD_CPUTIME_ID		3		//本线程运行时间
#define CLOCK_MONOTONIC_RAW		4
#define CLOCK_REALTIME_COARSE		5
#define CLOCK_MONOTONIC_COARSE		6
#define CLOCK_BOOTTIME			7
#define CLOCK_REALTIME_ALARM		8
#define CLOCK_BOOTTIME_ALARM		9
timespec和timeval的精度不一样携带的一个是ns和us
IDs 定义的宏一般用于clock_gettime接口用来获取特定的时钟时间带出timespec 描述

相关接口

<sys/time.h>
/* Get the current time of day and timezone information,
   putting it into *TV and *TZ.  If TZ is NULL, *TZ is not filled.
   Returns 0 on success, -1 on errors.
   NOTE: This form of timezone information is obsolete.
   Use the functions and variables declared in <time.h> instead.  */
extern int gettimeofday (struct timeval *__restrict __tv,
			 __timezone_ptr_t __tz) __THROW __nonnull ((1));
			 
# define TIMEVAL_TO_TIMESPEC(tv, ts) {                                   \
        (ts)->tv_sec = (tv)->tv_sec;                                    \
        (ts)->tv_nsec = (tv)->tv_usec * 1000;                           \
}
# define TIMESPEC_TO_TIMEVAL(tv, ts) {                                   \
        (tv)->tv_sec = (ts)->tv_sec;                                    \
        (tv)->tv_usec = (ts)->tv_nsec / 1000;                           \
}
下面几个宏接口依赖timevals的
Convenience macros for operations on timevals.
   NOTE: `timercmp' does not work for >= or <=
timerisset(tvp)->>((tvp)->tv_sec || (tvp)->tv_usec)		//判断是否有值
timerclear(tvp)->>((tvp)->tv_sec = (tvp)->tv_usec = 0) 	//清空属性
timercmp(a, b, CMP)		//CMP是比较符 > < = 返回真假
timeradd(a, b, result)   //a+b >> result
timersub(a, b, result)	//a-b >>result

获取今天的实时时间,通过tv和tz带出来,tz空则不赋值
通过宏可以将一个tv值转化成ts的值

绝对时间

<time.h>
time_t msctime ------1900年到现今的秒数
/* Used by other time functions.  */
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]*/
  ....
 }

相关接口

<time.h>
/* Return the current time and put it in *TIMER if TIMER is not NULL.  */
extern time_t time (time_t *__timer) __THROW;
/* Return the `struct tm' representation
   of *TIMER in the local timezone.  */
extern struct tm *localtime (__const time_t *__timer) __THROW;
/* Return a string of the form "Day Mon dd hh:mm:ss yyyy\n"
   that is the representation of TP in this format.  */
extern char *asctime (__const struct tm *__tp) __THROW;

通过time(&msctime ),来获取当前秒数
localtime 相当于格式化一下time那个大数
struct tm *tm= localtime(&msctime );
asctime 格式化字符串的形式,这样就可以直接printf打出来了