[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Date and time

On Wed, 26 Sep 2001, Lukas wrote:
>   Hi ,
>   Please help me if you can . How can I set date and time on Etrax100LX 
>   from within C programm . What commands should I use ?

There are two clocks here - first you have the battery-backed realtime
clock (I assume that's on the LX dev board, if not, ignore this :), the
RTC. It is read at boot by the kernel, and from then on the kernel keeps
it own clock in software. Actually, if the RTC drifts, the kernel keeps it
in sync, but not the other way around (the assumption is made that the
kernel can know the time better than the RTC generally, since it can use
an NTP server).

So if you want to set the clock, you can either just set the kernel clock
with settimeofday(), but it will be ignored after the next reboot.

Or you can set the RTC using /dev/rtc:

#ifdef __CRIS__
#include <asm/rtc.h>  /* for the RTC_SET_TIME ioctl */

/* open /dev/rtc and use an ioctl to set the time in the RTC chip */

set_rtc(struct timeval *tv)
        int fd;
        struct tm *mytm;
        unsigned int secs;

        /* Open the rtc driver */

        fd = open("/dev/rtc", O_RDONLY);

        if(fd < 0) {
                log("cant open /dev/rtc: %m", errno);

        /* convert from seconds since 1970 to broken down time 
         * (that the RTC needs)
         * remember to round secs depending on usecs

        secs = tv->tv_sec + ((tv->tv_usec > 500000) ? 1 : 0);
        mytm = localtime((time_t *)&secs);
        /* set the RTC time
         * struct rtc_time is the same as struct tm
        if(ioctl(fd, RTC_SET_TIME, mytm) < 0) {
                log("RTC_SET_TIME failed: %m", errno);
        printf("Have no RTC_SET_TIME!");

And you can set both at the same time, of course.