linux+系统时间设置方法

  • 格式:doc
  • 大小:28.50 KB
  • 文档页数:2

Linux 系统时间设置
#include <time.h>
#include <errno.h>
#include <unistd.h>
#include<sys/time.h>
#include<stdlib.h>
unsigned char SetTime(uint8 *timess)
{
struct timeval *settvv=malloc( sizeof( struct timeval ) );
time_t curdate;
struct tm *tdate=malloc( sizeof( struct tm ) );
unsigned char f=0;
tdate->tm_year=timess[0]+2000-1900; //年
tdate->tm_mon=timess[1]-1; // 月
tdate->tm_mday=timess[2]; // 日
tdate->tm_wday=timess[6]; // 星期
tdate->tm_hour=timess[3]; // 小时
tdate->tm_min=timess[4]; // 分钟
tdate->tm_sec=timess[5]; // 秒钟
curdate=mktime(tdate);
settvv->tv_sec=curdate;
settvv->tv_usec=0;
if( settimeofday ( settvv, (struct timezone *)0 )<0 )
{
printf("settime error:%s\n\a",strerror(errno));
f=1;
}
system("hwclock –systohc");//系统时钟和硬件时钟同步:(让系统的时间同步到硬件时钟)
free(tdate);
free(settvv);
return f;
}
命令行修改方法
在命令行输入:
Date:显示当前时间Fri Aug 3 14:15:16 CST 2007
date –s:按字符串方式修改时间
可以只修改日期,不修改时间,输入: date -s 2007-08-03
只修改时间,输入:date -s 14:15:00
同时修改日期时间,注意要加双引号,日期与时间之间有一空格,输入: #date -s "2007-08-03 14:15:00"
更新硬件时间
hwclock -w。