Many programs have a need to measure time. This can be in terms of what the wall clock time is now, or in terms of how much CPU time a process is using. Measurements of time in both these senses are available via system calls in Linux.
The hardware of a PC contains a system clock. Linux reads the clock during system boot and then maintains its own clock counter which it uses to time stamp events within the system, like the creation of a file or the time a user logs in. The value of this (wall) clock is also available to your own programs by using the time() system call:
#include <sys/types.h> #include <time.h> time_t time(time_t *loc);
This system call returns a time_t value and, if the parameter loc is a pointer to a variable of this type, then the return value is also copied into the variable to which this pointer points.
The data type time_t is actually defined by a typedef, which is accessed via <time.h> and has an underlying data type of long. Therefore, the time() system call returns the current wall clock time in a long, but in a strange format - the number of seconds that have elapsed since midnight on 1 January 1970 GMT. (I suppose the clock had to start somewhere... .)
A side issue at this point is just how much time can be counted by a long before it runs out? If you restrict yourself to the positive range of values that can be held in a 32-bit long (I'm not sure what a negative time means anyway!), then it comes to just over 68 years, or up to the start of the year 2038 (plenty of time to dream up a replacement).
This format for times is actually quite useful if you want to work out how much time passed between two events (e.g the time you login and the time you logout), as you just subtract the later time from the earlier time to obtain the number of seconds between the events.
The format is less useful, however, where you want to display the day, the month, the year or the time of day. To simplify these applications the standard library contains some useful functions to do the conversions for you. Two such functions are:
#include <time.h> struct tm *gmtime(time_t *loc); struct tm *localtime(time_t *loc);
Both functions take a parameter which is a pointer to a variable containing a value of the 'seconds since 1970' variety. And both functions return a pointer to a structure whose fields contain all the required information:
struct tm { int tm_sec; int tm_min; int tm_hour; int tm_mday; int tm_mon; int tm_year; int tm_wday; int tm_yday; int tm_isdst; }
The fields tm_hour tm_min and tm_sec give the wall clock time in 24-hour clock format tm_mday is the day of the month in the range 1 to 31 tm_mon is the month number in the range 0 to 11, with January as 0, tm_year is the number of years since 1900, tm_wday is the day of the week in the range 0 to 6, with Sunday as 0, tm_yday is the day of the year in the range 0 to 365, with 1 January as 0, and tm_isdst is a flag to specify whether or not daylight saving time is in effect (if the information is available).
The main difference between the two functions is that gmtime() gives its information relative to Greenwich Mean Time (GMT), whereas localtime() gives its information relative to your local timezone (which was set up during system installation and configuration).
The other times that may be of interest are those related to processes and their CPU usage. This information may be obtained by the use of the times() system call:
#include <sys/times.h> clock_t times(struct tms *buf);
Here the parameter to times() is a pointer to a struct tms which will be filled in by the call. The definition of this structure is:
struct tms { clock_t tms_utime; clock_t tms_stime; clock_t tms_cutime; clock_t tms_cstime; }
The CPU time for a process is split into two parts: the time it spends executing instructions in its own text segment (user time) and the time it spends executing instructions in the kernel, usually via a system call (system time).
The tms_utime field is the total user time used by the calling process and the tms_stime is its total system time. The other two fields (tms_cutime and tms_cstime) are the same thing but totalled over all the child processes of the calling process, for which a successful wait() has been performed.
The return value from the times() system call is the amount of time that has elapsed since the system was last booted up.
All of the times associated with the times() system call are measured (for Linux) in hundredths of a second.