Time and clock RTS Functions
From Texas Instruments Embedded Processors Wiki
The compiler RTS library supports two low-level time-related standard C functions:
- time_t time(time_t *timer);
- clock_t clock(void);
time() gives wall-clock time. clock() returns the number of clock cycles since the program began executing; it has nothing to do with wall-clock time.
Function time()
The time() function requires interaction with the outside world in order to learn the correct time. A program running on an embedded processor needs to discover the time from some external source. When running under CCS, CCS itself serves as the external source; CCS in turn discovers the time from the host operating system (e.g. Windows or Linux).
When not running under CCS, the program needs to find some other source from which it can discover time. In order to do that, a new implementation of time() must be provided. If the program is running under an operating system, that operating system should provide an implementation of time(); otherwise, the user must provide one.
Consider an embedded processor as similar to a wristwatch. Perhaps the wristwatch maintains the time internally, but during initial power-on, the time is not reliable, and must be set by some external source.
epoch
The function time() returns the number of seconds since an "epoch." On POSIX systems, the epoch is defined as the number of seconds since midnight UTC January 1, 1970
However, the C standard does not require any particular epoch, and the TI version of time() uses a different epoch: midnight UTC-6 Jan 1, 1900.
This causes problems for people porting code using raw time_t values. See SDSCM00037169. See also the talk page for comments extracted from that defect report.
Function clock()
clock() returns the number of clock cycles since the program began executing. This information might be available in a register on the device itself, but this varies from platform to platform, so the compiler doesn't provide a standard function to do it. Instead, the compiler RTS provides an implementation that discovers the clock cycles through CCS, which has a common interface. When CCS is not available, the user must provide an implementation of clock() which can discover the information from the appropriate location.
Leave a CommentComments
Extracted from the internal comments of SDSCM00037169:
- I agree that the C standard does not specify the epoch. However, the epoch is 1970 by convention and this is well known. I have never encountered a C standard library time function that did not use 1970 as an epoch before. There are non-standard time function which use other epoch, but these have a different name. Any embedded programmer using the standard named functions is going to expect it to use 1970 as did the customer who originally reported the problem. Any systems that exchange time information in the "standard" unix time format are going to expect it to be 1970-based.
- So while technically you may be correct that it is not violating a standard it is definitely non-standard and anybody who uses these time functions that depend on an agreed upon epoch are going to encounter this problem. It also means that the customer has to put build-time conditionals around any time-using code, making it dependent on which tools are being used. The time handling code that works one way for every other toolchain they have ever used will produce a different result when switching to the TI compiler.
