How do I set the system time using C / C ++?

I have an embedded system (ARM 9263) running RTOS, IAR tools. The system supports the standard time () function which gives me the current time. I need a callback, that is, I need to set the time - is there a standard "C" way? I googled around, sure this will be obvious, but maybe it is platform dependent? I'm not sure why, since time () is no idea? Thank!


source to share

4 answers

With the IAR tooling, the C API runtime ( time()

) runtime can be overridden using the example in ARM\src\lib\time.c

. The standard procedure always returns -1, which indicates that the CRT does not know at what time. After you provide your own implementation time()

that gets the time of day from a source that depends on your targe and / or RTOS platform, you can set the time of day by updating whatever is in that time source. IAR may have already done this for their RTOSs - I have not used the IAR PowerPac RTOS.

For information on how this works for another RTOS or non-RTOS system, see the IAR C / C ++ Development Guide.

For example, the system I was working on uses an ST Micro STM32 microcontroller, the real time clock (RTC) is set to once a second, and the library function time()

just returns the value in the RTC. Setting a new date / time is setting the RTC with a new value. The RTC time counter is set using a Unix epoch value (seconds since Jan 1, 1970), which allows the rest of the library functions to time.h

work very well (until some time in 2035 when 32-bit overflows start to unfold).

The calendar routines in the IAR DLIB C runtime library support dates up to 2035-12-31 (they overflow until 2038, I suspect, because internal calculations use the epoch of Jan 1, 1900). If you're using the Unic era, the other DLIB routines work more or less - I'm not sure how much effort it will take to use a different era.



There is settimeofday

(2) that allows you to do this, but this is POSIX, not ANSI C.

You can also check clock_settime

, which might be more applicable to embedded systems. See this man page for example.



There is no standard way to do this in C or C ++. You will need to look at the OS vendor for the API and / or your compiler or standard library vendor for any extension.



The IAR tools are not targeted, so nothing in the library knows how to set the time on your hardware (or read it for that matter), that is, before your board support package (which usually maps your hardware to the standard library, among other things).

Not all targets even have an RTC. The IAR tools provide support for a range of specific microcontrollers and off-the-shelf development boards as well, so you might find it already done for you - you just have to look.

Your microcontroller might have an internal RTC, or you might have one look, but "ARM 9263" doesn't define the RTC itself, so you need to be more specific - RTCs are usually added as vendor peripherals. Then it will be a case of obtaining the user's manual of the part and writing the necessary registers.



All Articles