iriver Dicple D88

RTC取得、設定


參考資料:
1. rtc.c

讀取

#include <ctime>
#include <sys/time.h>
#include <linux/rtc.h>
#include <sys/ioctl.h>

void get_rtc(void)
{
    char buf[80] = {0};
    int fd = open("/dev/rtc", O_RDWR);

    if (fd > 0) {
        struct tm tstruct = {0};
        ioctl(fd, RTC_RD_TIME, &tstruct);
        close(fd);

        time_t t = mktime(&tstruct);
        struct timeval tv = {t, 0};
        settimeofday(&tv, NULL);

        system("hwclock --systohc &");
        strftime(buf, sizeof(buf), "%F %R", &tstruct);
    }
}

更新

#include <ctime>
#include <sys/time.h>
#include <linux/rtc.h>
#include <sys/ioctl.h>

void set_rtc(const char* timestamp)
{
	int imonth, iday, iyear, ihour, iminute;
	sscanf(timestamp, "%d-%d-%d %d:%d", &iyear, &imonth, &iday, &ihour, &iminute);
	struct tm datetime = {0};

	datetime.tm_year = iyear - 1900;
	datetime.tm_mon  = imonth - 1;
	datetime.tm_mday = iday;
	datetime.tm_hour = ihour;
	datetime.tm_min  = iminute;
	datetime.tm_sec  = 0;

	if (datetime.tm_year < 0) {
        datetime.tm_year = 0;
    }

	time_t t = mktime(&datetime);
    struct timeval tv = {t, 0};
    settimeofday(&tv, NULL);
    system("hwclock --systohc &");

    int fd = open("/dev/rtc", O_RDWR);
    if (fd > 0) {
        ioctl(fd, RTC_SET_TIME, t);
        close(fd);
    }
}


返回上一頁