To keep C simpler, we just use the int data type and
let the compiler deal with the actual size. This still doesn't
deal with all the wasted space for storing the data, so we'll
define a new data type — datetime — that on Unix type
systems is 32 bits. We use this to store the number of
seconds since January 1, 1970 Greenwich Mean Time.
Well, that particular time was chosen as the beginning
of the epoch for time keeping in Unix systems, so it is now
available in most PC type systems. We declare this as:
typedef long time_t;
/* time in sec since 1 Jan 1970 0000 GMT */
Just FYI, this will store the number of seconds to get
us to Jan 19, 2038 where it will roll back to 1901 and
likely cause headaches much like the Y2K bug was
supposed to (but didn't). So, since the Mayan datetime
data type was wrong and the world didn't end December
21, 2012, keep an eye out for 2038.
We also have a variable for storing the time since a
software process started as a function of processor ticks,
typedef long clock_t;
/* time in ticks since process started */
This variable depends entirely on the processor and
how it counts ticks. Since AVRs tick at millions of times
per second, this is more useful for determining how long a
particular section of code takes to complete than it is for
keeping track of time on a human scale.
Finally, for more human friendly units, we define
CLOCKS_PER_SEC which is a device dependent number
that tells how many ticks in clock_t are equal to one
Time Manipulation Functions
time() — Returns the time since the beginning of a
system epoch. In the case of Unix, that is the number of
seconds since Jan. 1, 1970.
clock() — Returns a processor tick count.
difftime() — Calculates the difference between
Format Conversion Functions
Uses the tm calendar type.
asctime() — Converts a given calendar time to a
ctime() — Converts a given time since the epoch
start to a local calendar time and then to a textual
strftime() — Converts date and time data from a given
calendar time to a formatted null terminated string.
Uses the time_t — time since epoch type.
gmtime() — Converts a given time since the epoch
start to a calendar time expressed as Coordinated
Universal Time (UTC).
localtime() — Converts a given time since the epoch
start to a local calendar time.
mktime() — Converts calendar time to a time since
the epoch start.
Using time.c on a PC
Let's see how this all plays out on a PC by getting
some time. First, let's see how many system ticks there are
in a second on my PC. We run the following
Date Time Test1.c in Pelles C:
int main(int argc, char *argv)
// define two variables of type clock_t
clock_t ticks1, ticks2;
ticks1=clock(); // get the number of ticks
// since the process started
ticks2=ticks1; // make a copy of it
// wait for 1 second to pass while getting
// the ticks
printf("Took %ld ticks to wait one
printf("They should be the same.\n");
Turns out that there are 1,000 system ticks per second
on my PC. This should allow us to time intervals down to
a single millisecond.
■ FIGURE 4. Clocks per second.