Linux下c库函数设置系统时间
设置时间,首先了解时间这一个结构体,在处理时间时,经常用到它:struct tm{int tm_sec; //当前秒int tm_min; //当前分钟int tm_hour; //当前小时int tm_mday; //当前在本月中的天,如11月1日,则为1int tm_mon; //当前月,范围是0~11int tm_year; //当前年和1900的差值,如2006年则为36int tm_wd
设置时间,首先了解时间这一个结构体,在处理时间时,经常用到它:
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 timeSYNOPSIS
#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.
更多推荐



所有评论(0)