当前位置: 技术问答>linux和unix
用C语言编程如何得到linux系统的时间
来源: 互联网 发布时间:2015-07-05
本文导语: 我用C语言在linux下编程,主要实现获得系统的时间,并可以对系统时间进行修改。不知道如何实现,请各位仁兄指教!!非常感谢! | 有函数: #include time_t time(time_t *calptr); 使用这个函数...
我用C语言在linux下编程,主要实现获得系统的时间,并可以对系统时间进行修改。不知道如何实现,请各位仁兄指教!!非常感谢!
|
有函数:
#include
time_t time(time_t *calptr);
使用这个函数,可以得到当前的时间
但是要输出,则需要使用
struct tm *localtime(const time_t *calptr);
函数将其转换为struct结构体类型。
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;
等
}
接下来该怎么做,不用我说了吧
#include
time_t time(time_t *calptr);
使用这个函数,可以得到当前的时间
但是要输出,则需要使用
struct tm *localtime(const time_t *calptr);
函数将其转换为struct结构体类型。
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;
等
}
接下来该怎么做,不用我说了吧
|
给个例子,以供参考:
unsigned char SetSysTime(struct NetData *net_data)
{
unsigned char h_year,l_year,month,day,hour,minute,second;
struct timeval tv;
struct timezone tz;
struct tm *p;
time_t timep,timeq;
int ret;
ret = IsTime(net_data->Data);
if (ret == FALSE)
{
printf("The net time data error.n");
return DATAERROR;
}
memcpy(&h_year,net_data->Data,1);
memcpy(&l_year,(net_data->Data+1),1);
memcpy(&month,(net_data->Data+2),1);
memcpy(&day,(net_data->Data+3),1);
memcpy(&hour,(net_data->Data+4),1);
memcpy(&minute,(net_data->Data+5),1);
memcpy(&second,(net_data->Data+6),1);
ret = gettimeofday(&tv,&tz);
if (ret == -1)
{
printf("Gettimeofday error:%sn",strerror(errno));
return DOFAILE;
}
ret = time(&timep);
if (ret == (time_t)-1)
{
printf("Time error:%sn",strerror(errno));
return DOFAILE;
}
/*get local current time and date*/
p = localtime(&timep);
p = (struct tm *)alloca(sizeof(struct tm));
p->tm_year = (h_year*256 + l_year) - 1900;
p->tm_mon = month - 1;
p->tm_mday = day;
p->tm_hour = hour;
p->tm_min = minute;
p->tm_sec = second;
timeq = mktime(p);
tv.tv_sec = (long)timeq;
ret = settimeofday(&tv,&tz);
if (ret == -1)
{
printf("Settimeofday error:%sn",strerror(errno));
return DOFAILE;
}
return DOOK;
}
unsigned char SetSysTime(struct NetData *net_data)
{
unsigned char h_year,l_year,month,day,hour,minute,second;
struct timeval tv;
struct timezone tz;
struct tm *p;
time_t timep,timeq;
int ret;
ret = IsTime(net_data->Data);
if (ret == FALSE)
{
printf("The net time data error.n");
return DATAERROR;
}
memcpy(&h_year,net_data->Data,1);
memcpy(&l_year,(net_data->Data+1),1);
memcpy(&month,(net_data->Data+2),1);
memcpy(&day,(net_data->Data+3),1);
memcpy(&hour,(net_data->Data+4),1);
memcpy(&minute,(net_data->Data+5),1);
memcpy(&second,(net_data->Data+6),1);
ret = gettimeofday(&tv,&tz);
if (ret == -1)
{
printf("Gettimeofday error:%sn",strerror(errno));
return DOFAILE;
}
ret = time(&timep);
if (ret == (time_t)-1)
{
printf("Time error:%sn",strerror(errno));
return DOFAILE;
}
/*get local current time and date*/
p = localtime(&timep);
p = (struct tm *)alloca(sizeof(struct tm));
p->tm_year = (h_year*256 + l_year) - 1900;
p->tm_mon = month - 1;
p->tm_mday = day;
p->tm_hour = hour;
p->tm_min = minute;
p->tm_sec = second;
timeq = mktime(p);
tv.tv_sec = (long)timeq;
ret = settimeofday(&tv,&tz);
if (ret == -1)
{
printf("Settimeofday error:%sn",strerror(errno));
return DOFAILE;
}
return DOOK;
}