当前位置: 技术问答>linux和unix
请教大家,在linux下编程,用什么方法可以设置系统时间?
来源: 互联网 发布时间:2015-10-21
本文导语: 我刚开始在linux下编程,用的是C语言。 我想修改系统时间,怎么样可以做到呢? 我查了很久,也没有查到。 看到C库里的函数,都是怎么样获取系统时间的,没有能设置系统时间。 请大家帮忙了,谢谢大家! ...
我刚开始在linux下编程,用的是C语言。
我想修改系统时间,怎么样可以做到呢?
我查了很久,也没有查到。
看到C库里的函数,都是怎么样获取系统时间的,没有能设置系统时间。
请大家帮忙了,谢谢大家!
我想修改系统时间,怎么样可以做到呢?
我查了很久,也没有查到。
看到C库里的函数,都是怎么样获取系统时间的,没有能设置系统时间。
请大家帮忙了,谢谢大家!
|
设置BIOS时钟示例:(注意:注释中的代码)
#include
#include
//#include
#define InttoBCD(a) ((a)/10)*16+((a)%10)
//unsigned int InttoBCD(int a);
int set_time ();
#ifndef _ASM_IO_H
#define _ASM_IO_H
#include
static __inline void
outb_wxy (unsigned char value, unsigned short int port)
{
//__asm__ __volatile__("outb %b0,%w1nt"
// "outb %%al,$0x80n"
// :
// :"a" (value), "Nd" (port));
__asm__ __volatile__ ("outb %%al,%%dx"::"a" (value), "d" (port));
}
#endif
/*
unsigned int InttoBCD(int a)
{
return (a/10)*16+(a%10);
}
*/
int
set_time ()
{
unsigned char year, month, day, hour, minute, second;
unsigned int retv;
unsigned short tmp;
printf ("Date format:year-month-day hour-minute-secondn");
printf (" 2-7-18 14-30-50 = 2002/7/18 14:30:50n");
printf ("input year:");
scanf ("%d", &tmp);
year = InttoBCD (tmp);
printf ("input month:");
scanf ("%d", &tmp);
month = InttoBCD (tmp);
printf ("input day:");
scanf ("%d", &tmp);
day = InttoBCD (tmp);
printf ("input hour:");
scanf ("%d", &tmp);
hour = InttoBCD (tmp);
printf ("input minute:");
scanf ("%d", &tmp);
minute = InttoBCD (tmp);
printf ("input second:");
scanf ("%d", &tmp);
second = InttoBCD (tmp);
printf ("Date(hex):%x-%x-%x-%x-%x-%xn", year, month, day, hour, minute,
second);
/*
retv = ioperm (0x70, 2, 1);
if (retv == -1)
{
printf( "get error.n");
return 1;
}
*/
/*
outb_wxy(0x00, 0x70);
outb_wxy(second, 0x71); //sec
outb_wxy(0x02, 0x70);
outb_wxy(minute, 0x71); //min
outb_wxy(0x04, 0x70);
outb_wxy(hour, 0x71); //hour
outb_wxy(0x07, 0x70);
outb_wxy(day, 0x71); //day
outb_wxy(0x08, 0x70);
outb_wxy(month, 0x71); //month
outb_wxy(0x09, 0x70);
outb_wxy(year, 0x71); //year
*/
/*
outb(0x00, 0x70);
outb(second, 0x71); //sec
outb(0x02, 0x70);
outb(minute, 0x71); //min
outb(0x04, 0x70);
outb(hour, 0x71); //hour
outb(0x07, 0x70);
outb(day, 0x71); //day
outb(0x08, 0x70);
outb(month, 0x71); //month
outb(0x09, 0x70);
outb(year, 0x71); //year
//get BIOS time
outb (0x00, 0x70);
tmp = inb(0x71);
printf( "sec:%xn",tmp);
outb (0x02, 0x70);
tmp = inb(0x71);
printf( "min:%xn", tmp);
outb (0x04, 0x70);
tmp = inb(0x71);
printf( "hour:%xn", tmp);
outb (0x07, 0x70);
tmp = inb(0x71);
printf( "day:%xn", tmp);
outb (0x08, 0x70);
tmp = inb(0x71);
printf( "month:%xn", tmp);
outb (0x09, 0x70);
tmp = inb(0x71);
printf( "year:%xn", tmp);
retv = ioperm (0x70, 2, 0);
if (retv == -1)
{
printf( "free error.n");
return 1;
} */
return 0;
}
int
main ()
{
set_time ();
return 0;
}
#include
#include
//#include
#define InttoBCD(a) ((a)/10)*16+((a)%10)
//unsigned int InttoBCD(int a);
int set_time ();
#ifndef _ASM_IO_H
#define _ASM_IO_H
#include
static __inline void
outb_wxy (unsigned char value, unsigned short int port)
{
//__asm__ __volatile__("outb %b0,%w1nt"
// "outb %%al,$0x80n"
// :
// :"a" (value), "Nd" (port));
__asm__ __volatile__ ("outb %%al,%%dx"::"a" (value), "d" (port));
}
#endif
/*
unsigned int InttoBCD(int a)
{
return (a/10)*16+(a%10);
}
*/
int
set_time ()
{
unsigned char year, month, day, hour, minute, second;
unsigned int retv;
unsigned short tmp;
printf ("Date format:year-month-day hour-minute-secondn");
printf (" 2-7-18 14-30-50 = 2002/7/18 14:30:50n");
printf ("input year:");
scanf ("%d", &tmp);
year = InttoBCD (tmp);
printf ("input month:");
scanf ("%d", &tmp);
month = InttoBCD (tmp);
printf ("input day:");
scanf ("%d", &tmp);
day = InttoBCD (tmp);
printf ("input hour:");
scanf ("%d", &tmp);
hour = InttoBCD (tmp);
printf ("input minute:");
scanf ("%d", &tmp);
minute = InttoBCD (tmp);
printf ("input second:");
scanf ("%d", &tmp);
second = InttoBCD (tmp);
printf ("Date(hex):%x-%x-%x-%x-%x-%xn", year, month, day, hour, minute,
second);
/*
retv = ioperm (0x70, 2, 1);
if (retv == -1)
{
printf( "get error.n");
return 1;
}
*/
/*
outb_wxy(0x00, 0x70);
outb_wxy(second, 0x71); //sec
outb_wxy(0x02, 0x70);
outb_wxy(minute, 0x71); //min
outb_wxy(0x04, 0x70);
outb_wxy(hour, 0x71); //hour
outb_wxy(0x07, 0x70);
outb_wxy(day, 0x71); //day
outb_wxy(0x08, 0x70);
outb_wxy(month, 0x71); //month
outb_wxy(0x09, 0x70);
outb_wxy(year, 0x71); //year
*/
/*
outb(0x00, 0x70);
outb(second, 0x71); //sec
outb(0x02, 0x70);
outb(minute, 0x71); //min
outb(0x04, 0x70);
outb(hour, 0x71); //hour
outb(0x07, 0x70);
outb(day, 0x71); //day
outb(0x08, 0x70);
outb(month, 0x71); //month
outb(0x09, 0x70);
outb(year, 0x71); //year
//get BIOS time
outb (0x00, 0x70);
tmp = inb(0x71);
printf( "sec:%xn",tmp);
outb (0x02, 0x70);
tmp = inb(0x71);
printf( "min:%xn", tmp);
outb (0x04, 0x70);
tmp = inb(0x71);
printf( "hour:%xn", tmp);
outb (0x07, 0x70);
tmp = inb(0x71);
printf( "day:%xn", tmp);
outb (0x08, 0x70);
tmp = inb(0x71);
printf( "month:%xn", tmp);
outb (0x09, 0x70);
tmp = inb(0x71);
printf( "year:%xn", tmp);
retv = ioperm (0x70, 2, 0);
if (retv == -1)
{
printf( "free error.n");
return 1;
} */
return 0;
}
int
main ()
{
set_time ();
return 0;
}
|
void set_sys_time(char* tm)
{
// tm format "05/08/14 21:09:08"
char yy1[2],yy[4],mm[2],dd[2],hour[2],min[2],sec[2];
struct tm _tm;
time_t timep;
struct timeval tv;
memcpy(yy1,tm,2);
sprintf(yy,"20%s",yy1);
memcpy(mm,tm+3,2);
memcpy(dd,tm+6,2);
memcpy(hour,tm+9,2);
memcpy(min,tm+12,2);
memcpy(sec,tm+15,2);
_tm.tm_sec = atoi(sec);
_tm.tm_min = atoi(min);
_tm.tm_hour = atoi(hour);
_tm.tm_mday = atoi(dd);
_tm.tm_mon = atoi(mm) - 1;
_tm.tm_year = atoi(yy) - 1900;
timep = mktime(&_tm);
tv.tv_sec = timep;
tv.tv_usec = 0;
if(settimeofday (&tv, (struct timezone *) 0)
{
// tm format "05/08/14 21:09:08"
char yy1[2],yy[4],mm[2],dd[2],hour[2],min[2],sec[2];
struct tm _tm;
time_t timep;
struct timeval tv;
memcpy(yy1,tm,2);
sprintf(yy,"20%s",yy1);
memcpy(mm,tm+3,2);
memcpy(dd,tm+6,2);
memcpy(hour,tm+9,2);
memcpy(min,tm+12,2);
memcpy(sec,tm+15,2);
_tm.tm_sec = atoi(sec);
_tm.tm_min = atoi(min);
_tm.tm_hour = atoi(hour);
_tm.tm_mday = atoi(dd);
_tm.tm_mon = atoi(mm) - 1;
_tm.tm_year = atoi(yy) - 1900;
timep = mktime(&_tm);
tv.tv_sec = timep;
tv.tv_usec = 0;
if(settimeofday (&tv, (struct timezone *) 0)