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

Linux 修改系统时间 需要Root权限

2016-03-27 16:44 477 查看
#include <sys/time.h>

#include <unistd.h>

#include <stdio.h>

void setSystemTime(long timep)

{

struct timeval tv;

tv.tv_sec = (time_t)timep;

tv.tv_usec = 0;

if(settimeofday (&tv, (struct timezone *) 0) < 0)

{

printf("Set system datatime error!/n");

//return -1;

}

}

void main()

{

long date = 1459157400L;

time_t time_p;

struct tm * tmp_tm;

time(&time_p);

tmp_tm = localtime(&time_p);

printf("time1=%s\n",asctime(tmp_tm));

setSystemTime(date);

time(&time_p);

tmp_tm = localtime(&time_p);

printf("time2=%s\n",asctime(tmp_tm));

printf("end of main\n");

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