Every now and then, you’ll find that in the midst of an application, you really need to know the time from the system clock. Even more likely, you need to have your application wait for a specific amount of time. Linux’s timing functions are relatively straightforward; however, most people overlook them until they need to use one in an application.
Every now and then, you’ll find that in the midst of an application, you really need to know the time from the system clock. Even more likely, you need to have your application wait for a specific amount of time. Linux’s timing functions are relatively straightforward; however, most people overlook them until they need to use one in an application.
Since chance favors the prepared mind, we’re going to spend this month exploring the time functions provided by Linux and how to use them effectively. In next month’s column, we will delve more deeply into the Linux mechanism for keeping time and explore how each of these functions works.
Getting the Time
Linux provides three main system calls for obtaining the current time from the system: time(), ftime(), and gettimeofday(). Each provides essentially the same information but in varying degrees of accuracy and usability. The prototype for the time() function is as follows (as found in <time.h>):
time_t time (time_t *t);
This function takes in a pointer to a time_t and returns the time as type time_t. Taking a closer look at <time.h>, we see that a time_t is simply defined as the following:
typedef __time_t time_t;
Following the chain of typedefs (to discover the true type of time_t), we see that __time_t is simply defined (in <bits/types.h>) as:
typedef long int __time_t;
So, the time() function simply takes a pointer to…
Please log in to view this content.
Not Yet a Member?
Register with LinuxMagazine.com and get free access to the entire archive, including: