您的位置:首页 > 运维架构 > Linux

Linux下用C语言设置和获取系统时间

2014-06-16 17:31 746 查看


一、Unix/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、struct tm 结构:由函数gmtime()解析time_t得到

struct tm*gmtime(const time_t*timep);

函数说明:gmtime()将参数timep 所指的time_t 结构中的信息转换成真实世界所使用的时间日期表示方法,然后

将结果由结构tm返回。

结构tm的定义为

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_yday;

int tm_isdst;

};

int tm_sec 代表目前秒数,正常范围为0-59,但允许至61秒

int tm_min 代表目前分数,范围0-59

int tm_hour 从午夜算起的时数,范围为0-23

int tm_mday 目前月份的日数,范围01-31

int tm_mon 代表目前月份,从一月算起,范围从0-11

int tm_year 从1900 年算起至今的年数

int tm_wday 一星期的日数,从星期一算起,范围为0-6

int tm_yday 从今年1月1日算起至今的天数,范围为0-365

int tm_isdst 日光节约时间的旗标

此函数返回的时间日期未经时区转换,而是UTC时间。

返回值:返回结构tm代表目前UTC 时间

7、Unix对时间单位的定义:

#define SEC 1 //


#define MILLISEC 1000 // 毫秒

#define MICROSEC 1000000 // 微秒

#define NANOSEC 1000000000 // 纳秒

8、时间格式化函数:

size_t strftime(char *str,size_t max,char *fmt,struct tm *tp); strftime有点像sprintf,其格式由fmt来指定。

%a : 本第几天名称,缩写

%A : 本第几天名称,全称

%b : 月份名称,缩写

%B : 月份名称,全称

%c : 与ctime/asctime格式相同

%d : 本月第几日名称,由零算起

%H : 当天第几个小时,24小时制,由零算起

%I : 当天第几个小时,12小时制,由零算起

%j : 当年第几天,由零算起

%m : 当年第几月,由零算起

%M : 该小时的第几分,由零算起

%p : AM或PM

%S : 该分钟的第几秒,由零算起

%U : 当年第几,由第一个日开始计算

%W : 当年第几,由第一个一开始计算

%w : 当第几日,由零算起

%x : 当地日期

%X : 当地时间

%y : 两位数的年份

%Y : 四位数的年份

%Z : 时区名称的缩写

%% : %符号

char * strptime(char *s,char *fmt,struct tm *tp); 如同scanf一样,解译字串成为tm格式

%h : 与%b及%B同

%c : 读取%x及%X格式

%C : 读取%C格式

%e : 与%d同

%D : 读取%m/%d/%y格式

%k : 与%H同

%l : 与%I同

%r : 读取"%I:%M:%S %p"格式

%R : 读取"%H:%M"格式

%T : 读取"%H:%M:%S"格式

%y : 读取两位数年份

%Y : 读取四位数年份


二、Unix/Linux系统下获取时间:

char*        GetMacTime(int iLen)
{
static char        MacTime[40];

long    tt;
struct  tm      *vtm;
struct  timeb   mt;

static char wday_name[7][7] = {
"星期日", "星期一", "星期二", "星期三",
"星期四", "星期五", "星期六"
};

ftime(&mt);
time( &tt );
vtm = localtime( &tt );

switch (iLen) {
case 6: /* 时分秒 */
sprintf(MacTime,"%.2d%.2d%.2d",vtm->tm_hour,
vtm->tm_min,vtm->tm_sec);
break;
case 8: /* 年月日 */
sprintf(MacTime,"%.4d%.2d%.2d",(1900+vtm->tm_year),
vtm->tm_mon+1,vtm->tm_mday);
break;
case 108: /* 年月日 */
sprintf(MacTime,"%.4d年%.2d月%.2d日",(1900+vtm->tm_year),
vtm->tm_mon+1,vtm->tm_mday);
break;
case 9: /* 时分秒毫 */
sprintf(MacTime,"%.2d%.2d%.2d%.3d",vtm->tm_hour,
vtm->tm_min,vtm->tm_sec,mt.millitm);
break;
case 106: /* 时分秒 */
sprintf(MacTime,"%.2d时%.2d分%.2d秒",vtm->tm_hour,
vtm->tm_min,vtm->tm_sec);
break;
case 14: /* 年月日时分秒 */
sprintf(MacTime, "%.4d%.2d%.2d%.2d%.2d%.2d",
(1900+vtm->tm_year),vtm->tm_mon+1,
vtm->tm_mday, vtm->tm_hour,
vtm->tm_min, vtm->tm_sec);
break;
case 15: /* 年月日时分秒 */
sprintf(MacTime, "%.2d%.2d%.2d%.2d%.2d%.2d%.3d",
(vtm->;tm_year % 100),vtm->tm_mon+1,
vtm->tm_mday, vtm->tm_hour,
vtm->tm_min, vtm->tm_sec,mt.millitm);
break;
case 17: /* 年月日时分秒毫 */
sprintf(MacTime, "%.4d%.2d%.2d%.2d%.2d%.2d%.3d",
(1900+vtm->tm_year),vtm->tm_mon+1,
vtm->tm_mday, vtm->tm_hour,
vtm->tm_min, vtm->tm_sec,mt.millitm);
break;
default:
sprintf(MacTime, "%s %.4d年%.2d月%.2d日 %.2d:%.2d:%.2d",
wday_name[vtm->tm_wday],
(1900+vtm->tm_year),vtm->tm_mon+1,
vtm->tm_mday, vtm->tm_hour,
vtm->tm_min, vtm->tm_sec);
break;
}
return MacTime;
}



三、Unix/Linux系统下设置时间:

函数原型:
int settimeofday(const struct timeval *tv , const struct timezone *tz);
struct timeval {
time_t      tv_sec;     /* seconds   since   Jan.   1,   1970  */
suseconds_t tv_usec;    /* 微妙 */
};
struct timezone {
int tz_minuteswest;     /* minutes west of Greenwich */
int tz_dsttime;         /* type of DST correction */
};

用于修改当前系统时间(只改变系统时间,不改变硬件时钟时间)

tv表示需要设置的时间,tz表示时区。如果tv或tz某一项为NULL,表示对相关的信息不感兴趣。

如何把我们熟知的时间格式转换成tv->tv_sec:

利用结构体
struct tm {
int tm_sec;  /*  秒 –  取值区间为[0,59] */
int tm_min;  /*  分 -  取值区间为[0,59] */
int tm_hour; /*  时 -  取值区间为[0,23] */
int tm_mday;  /* 一个月中的日期 -  取值区间为[1,31] */
int tm_mon;  /*  月份(从一月开始,0 代表一月) -  取值区间为[0,11] */
int tm_year;  /*  年份,其值等于实际年份减去 1900 */
int tm_wday;  /*  星期 – 取值区间为[0,6],其中0代表星期天,1 代表星期一,以此类推 */
int tm_yday;  /*  从每年的1 月1 日开始的天数 –  取值区间为[0,365],其中0代表 1 月1日,1 代表1 月2日,以此类推 */
int tm_isdst;/*  夏令时标识符,实行夏令时的时候,tm_isdst为正。不实行夏令时的进候,tm_isdst 为0;不了解情况时,tm_isdst()为负。*/
};


把我们熟悉的时间格式存到该结构体中,然后再用mktime()转成time_t.

time_t mktime(struct tm * timeptr);

然后把mktime的返回值存入tv->tv_sec即可。再用settimeofday就可以设置时间了

<pre name="code" class="cpp">int setSystemTime(lua_State *L)
{
unsigned char arg1[MAXLEN] = {'\0'};
strcpy(arg1, luaL_checkstring(L, 1));
unsigned char *ptm = arg1;

time_t tt_sec;
struct tm tptr;
struct timeval tv;

tptr.tm_year = str_DToInt_D(ptm, 4) - 1900;
ptm += 4;
tptr.tm_mon = str_DToInt_D(ptm, 2) - 1;
ptm += 2;
tptr.tm_mday = str_DToInt_D(ptm, 2);
ptm += 2;
tptr.tm_hour = str_DToInt_D(ptm, 2);
ptm += 2;
tptr.tm_min = str_DToInt_D(ptm, 2);
ptm += 2;
tptr.tm_sec = str_DToInt_D(ptm, 2);
tv.tv_sec = mktime(&tptr);
tv.tv_usec = 0;
settimeofday(&tv, NULL);
return 0;
}



                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: