ClaudiaGR
ClaudiaGR

Reputation: 11

Change date in a linux system but after reboot the new date is lost

I am programming an embedded system that takes the current date and time of an NMEA message. If the system date and time differ by more than 5 minutes from the NMEA message, I set the new date and time in the system and then I reboot it. The problem is that after reboot the system, it starts with the old date and time. The code is:

if (difference > 5){
    time_t t = mktime(&tmdate);
    timeval systemdate;
    systemdate.tv_sec = t;
    systemdate.tv_usec = 0;
    settimeofday(&systemdate,0);
    sync();
    reboot(RB_AUTOBOOT);
}

Upvotes: 0

Views: 1191

Answers (1)

vladimir
vladimir

Reputation: 148

Your code change system time, but not hardwaretime. "man rtc" for details. You need something like this:

    struct rtc_time {
       int tm_sec;
       int tm_min;
       int tm_hour;
       int tm_mday;
       int tm_mon;
       int tm_year;
    };

    struct rtc_time rt;
    /* set values from NMEA to rt */
    fd = open("/dev/rtc", O_RDONLY);
    ioctl(fd, RTC_SET_TIME, &rt);
    close(fd);

Upvotes: 1

Related Questions