linux下的时间结构体
1、time_t 类型:长整型,一般用来表示从1970-01-01 00:00:00时以来的秒数,精确度:秒;由函数time()获取;
该类型定义在头文件 /usr/include/sys/time.h 中:
#define _TIME_T
typedef long time_t;
#endif
函数定义:time_t time(time_t* lpt);
如:time_t time = time(NULL);
2、struct timeb 结构:它有两个主要成员,一个是秒,另一个是毫秒;精确度:毫秒(10E-3秒);
由函数ftime()获取struct timeb结构的时间;其定义如下:
struct timeb
{
time_t time;
unsigned short millitm;
short timezone;
short dstflag;
};
#include <sys/timeb.h>
int ftime(struct timeb* tp);
调用成功返回0;调用失败返回-1;
3、struct timeval 结构,它有两个成员;一个是秒,另一个表示微秒,精确度:微秒(10E-6);
由函数gettime0fday()获取;
struct timeval结构定义为:
struct timeval
{
long tv_sec;
long tv_usec;
}
读取struct timeval结构数据的函数说明:
#include <sys/time.h>
int gettimeofday(struct timeval* tv,struct timezone* tz);
该函数会提取系统当前时间,并把时间分为秒和微秒两部分填充到结构struct timeval中;同时把当地的时区信
息填充到结构struct timezone中;
返回值:成功则返回0,失败返回-1,错误代码存于errno。附加说明EFAULT指针tv和tz所指的内存空间超出存
取权限。
struct timezone结构的定义为:
struct timezone
{
int tz_minuteswest;
int tz_dsttime;
}
上述两个结构都定义在/usr/include/sys/time.h。tz_dsttime 所代表的状态如下
DST_NONE
DST_USA
DST_AUST
DST_WET
DST_MET
DST_EET
DST_CAN
DST_GB
DST_RUM
DST_TUR
DST_AUSTALT
4、struct timespec 结构:它是POSIX.4标准定义的一个时间结构,精确度:纳秒(10E-9秒);
由函数gethrestime()或gethrestime_lasttick()获取当前系统struct timespec结构的时间;其定义如下:
struct timespec
{
time_t tv_sec;
long tv_nsec;
};
typedef struct timespec timespec_t;
该结构定义在头头文件 /usr/include/sys/time_impl.h 中;
extern void gethrestime(timespec_t*);
extern void gethrestime_lasttick(timespec_t*);
5、clock_t 类型:由函数clock()获取;
#include <time.h>
clock_t clock(void);
该函数以微秒的方式返回CPU的时间;
类型 clock_t 定义在头文件/usr/include/sys/types.h中:
#ifndef _CLOCK_T
#define _CLOCK_T
typedef long clock_t;
#endif
6、tm结构
struct tm
{
int tm_sec; //当前秒
int tm_min; //当前分钟
int tm_hour; //当前小时
int tm_mday; //当前在本月中的天,如11月1日,则为1
int tm_mon; //当前月,范围是0~11
int tm_year; //当前年和1900的差值,如2006年则为36
int tm_wday; //当前在本星期中的天,范围0~6
int tm_yday; //当前在本年中的天,范围0~365
int tm_isdst; //夏令时
}
获取、设置系统时间 //实现函数功能参考本段
int getSystemTime() /*获取操作系统的时间*/
{
time_t timer;
struct tm* t_tm;
time(&timer);
t_tm = localtime(&timer);
printf("today is %4d%02d%02d%02d%02d%02d/n", t_tm.tm_year+1900,
t_tm.tm_mon+1, t_tm.tm_mday, t_tm.tm_hour, t_tm.tm_min, t_tm.tm_sec);
return 0;
}
/************************************************
设置操作系统时间
参数:*dt数据格式为"2006-4-20 20:30:30"
调用方法:
char *pt="2006-4-20 20:30:30";
SetSystemTime(pt);
**************************************************/
int SetSystemTime(char *dt)
{
struct rtc_time tm;
struct tm _tm;
struct timeval tv;
time_t timep;
sscanf(dt, "%d-%d-%d %d:%d:%d", &tm.tm_year,
&tm.tm_mon, &tm.tm_mday,&tm.tm_hour,
&tm.tm_min, &tm.tm_sec);
_tm.tm_sec = tm.tm_sec;
_tm.tm_min = tm.tm_min;
_tm.tm_hour = tm.tm_hour;
_tm.tm_mday = tm.tm_mday;
_tm.tm_mon = tm.tm_mon - 1;
_tm.tm_year = tm.tm_year - 1900;
timep = mktime(&_tm);
tv.tv_sec = timep;
tv.tv_usec = 0;
if(settimeofday (&tv, (struct timezone *) 0) < 0)
{
printf("Set system datatime error!/n");
return -1;
}
return 0;
}
关键函数:settimeofday:
NAME
gettimeofday, settimeofday - get / set time
SYNOPSIS
#include <sys/time.h>
int gettimeofday(struct timeval *tv, struct timezone *tz);
int settimeofday(const struct timeval *tv, const struct timezone *tz);
Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
settimeofday(): _BSD_SOURCE
DESCRIPTION
The functions gettimeofday() and settimeofday() can get and set the time as well as a timezone. The tv argument is a struct timeval (as specified in
<sys/time.h>):
struct timeval {
time_t tv_sec; /* seconds */
suseconds_t tv_usec; /* microseconds */
};
and gives the number of seconds and microseconds since the Epoch (see time(2)). The tz argument is a struct timezone:
struct timezone {
int tz_minuteswest; /* minutes west of Greenwich */
int tz_dsttime; /* type of DST correction */
};
If either tv or tz is NULL, the corresponding structure is not set or returned. (However, compilation warnings will result if tv is NULL.)
The use of the timezone structure is obsolete; the tz argument should normally be specified as NULL. (See NOTES below.)
Under Linux, there are some peculiar "warp clock" semantics associated with the settimeofday() system call if on the very first call (after booting) that
has a non-NULL tz argument, the tv argument is NULL and the tz_minuteswest field is nonzero. (The tz_dsttime field should be zero for this case.) In such
a case it is assumed that the CMOS clock is on local time, and that it has to be incremented by this amount to get UTC system time. No doubt it is a bad
idea to use this feature.