Если использовать макрос RTC_SET_TIME и передавать в функцию ioctl структуру с нужным временем, то всё работает:
Spoiler
Код: Выделить всё
#include <stdio.h>
#include <stdlib.h>
#include <linux/rtc.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <time.h>
int main(int argc, char **argv) {
int fd;
struct rtc_time settime_hw;
struct rtc_time currtime_hw;
settime_hw.tm_sec = 32;
settime_hw.tm_min = 16;
settime_hw.tm_hour = 8;
settime_hw.tm_mday = 13;
settime_hw.tm_mon = 6 - 1;
settime_hw.tm_year = 2013 - 1900;
fd = open("/dev/rtc", O_RDONLY);
if (fd < 0) {
printf("Error: %d\n", errno);
exit(EXIT_FAILURE);
}
if (ioctl(fd, RTC_RD_TIME, &currtime_hw) < 0) {
printf("Error: %d\n", errno);
exit(EXIT_FAILURE);
}
printf("Time befor set: %d.%02d.%02d %02d:%02d:%02d\n",
currtime_hw.tm_year + 1900, currtime_hw.tm_mon + 1,
currtime_hw.tm_mday, currtime_hw.tm_hour,
currtime_hw.tm_min, currtime_hw.tm_sec);
if (ioctl(fd, RTC_SET_TIME, &settime_hw) < 0) {
printf("Error: %d\n", errno);
exit(EXIT_FAILURE);
}
if (ioctl(fd, RTC_RD_TIME, &currtime_hw) < 0) {
printf("Error: %d\n", errno);
exit(EXIT_FAILURE);
}
printf("Time after set: %d.%02d.%02d %02d:%02d:%02d\n",
currtime_hw.tm_year + 1900, currtime_hw.tm_mon + 1,
currtime_hw.tm_mday, currtime_hw.tm_hour,
currtime_hw.tm_min, currtime_hw.tm_sec);
close(fd);
return EXIT_SUCCESS;
}Но если использовать RTC_EPOCH_SET и в функцию ioctl передавать timestamp в виде unsigned long, то получаю в errno ошибку 25 (Not a typewriter):
Spoiler
Код: Выделить всё
#include <stdio.h>
#include <stdlib.h>
#include <linux/rtc.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <time.h>
int main(int argc, char **argv) {
int fd;
struct rtc_time currtime_hw;
struct tm time_test;
time_t time_stamp;
unsigned long time_output;
fd = open("/dev/rtc", O_RDONLY);
if (fd < 0) {
printf("Error: %d\n", errno);
exit(EXIT_FAILURE);
}
if (ioctl(fd, RTC_RD_TIME, &currtime_hw) < 0) {
printf("Error: %d\n", errno);
exit(EXIT_FAILURE);
}
printf("Time befor set: %d.%02d.%02d %02d:%02d:%02d\n",
currtime_hw.tm_year + 1900, currtime_hw.tm_mon + 1,
currtime_hw.tm_mday, currtime_hw.tm_hour,
currtime_hw.tm_min, currtime_hw.tm_sec);
time_test.tm_sec = 32;
time_test.tm_min = 16;
time_test.tm_hour = 8;
time_test.tm_mday = 13;
time_test.tm_mon = 6 - 1;
time_test.tm_year = 2013 - 1900;
time_stamp = mktime(&time_test);
time_output = (unsigned long) time_stamp;
if (ioctl(fd, RTC_EPOCH_SET, time_output) < 0) {
printf("Error: %d\n", errno);
exit(EXIT_FAILURE);
}
if (ioctl(fd, RTC_RD_TIME, &currtime_hw) < 0) {
printf("Error: %d\n", errno);
exit(EXIT_FAILURE);
}
printf("Time after set: %d.%02d.%02d %02d:%02d:%02d\n",
currtime_hw.tm_year + 1900, currtime_hw.tm_mon + 1,
currtime_hw.tm_mday, currtime_hw.tm_hour,
currtime_hw.tm_min, currtime_hw.tm_sec);
close(fd);
return EXIT_SUCCESS;
}Всё под рутом.
Подскажите.