|
发表于 2006-4-26 13:28:05
|
显示全部楼层
这是我改过的读取和写入RTC的函数,可参考!
我以前碰到过这个问题,就这样搞定的!
static time_t read_rtc(int utc)
{
int rtc;
struct tm tm;
char *oldtz = 0;
time_t t = 0;
if (( rtc = open ( "/dev/rtc", O_RDONLY )) < 0 ) {
if (( rtc = open ( "/dev/misc/rtc", O_RDONLY )) < 0 )
bb_perror_msg_and_die ( "Could not access RTC" );
}
memset ( &tm, 0, sizeof( struct tm ));
if ( ioctl ( rtc, RTC_RD_TIME, &tm ) < 0 )
bb_perror_msg_and_die ( "Could not read time from RTC" );
tm. tm_isdst = -1; // not known
printf("year:%i, month:%i, date:%i\n",tm.tm_year,tm.tm_mon,tm.tm_mday);
close ( rtc );
if ( utc ) {
oldtz = getenv ( "TZ" );
setenv ( "TZ", "UTC 0", 1 );
tzset ( );
}
//add by wenbin on 2005/10/20
//-------------------------------------------
if (tm.tm_year > 1900)
tm.tm_year -= 1900;
printf("year:%i, month:%i, date:%i\n",tm.tm_year,tm.tm_mon,tm.tm_mday);
//-------------------------------------------
t = mktime ( &tm );
if ( utc ) {
if ( oldtz )
setenv ( "TZ", oldtz, 1 );
else
unsetenv ( "TZ" );
tzset ( );
}
return t;
}
static void write_rtc(time_t t, int utc)
{
int rtc;
struct tm tm;
if (( rtc = open ( "/dev/rtc", O_WRONLY )) < 0 ) {
if (( rtc = open ( "/dev/misc/rtc", O_WRONLY )) < 0 )
bb_perror_msg_and_die ( "Could not access RTC" );
}
printf("year:%i, month:%i, date:%i\n",t.year,t.mon,t.mday);
printf("hour:%i, mins:%i, sec:%i\n",t.hour,t.min,t.sec);
tm = *( utc ? gmtime ( &t ) : localtime ( &t ));
//Add by wenbin on 2005/10/20
//-----------------------------------------------
if (tm.tm_year<1900)
tm.tm_year += 1900;
//-----------------------------------------------
tm. tm_isdst = 0;
if ( ioctl ( rtc, RTC_SET_TIME, &tm ) < 0 )
bb_perror_msg_and_die ( "Could not set the RTC time" );
close ( rtc );
} |
|