>Let's say you don't have any more documentation than man pages, and
you want to figure out how to do the equivalent of
$ date
in C. I.e. just simply print out the current date and time. How to
proceed thru the forest of date and time related man pages?
1. Call time() to get the current time as a time_t.
#include <time.h>
time_t now;
now = time((time_t *)0);
2. Call localtime() or gmtime() to convert the time_t into a struct tm which
contains the time in more human-understandable terms (month, day, hour, etc).
struct tm *tp;
tp = localtime(&now) ;
3. Call asctime() or strftime() to format the string the way you want it.
4. printf() the string.
It is possible to combine step 2 and 3 with ctime(), whose format rather
resembles the UNIX date command output except for not including a time zone.
However, getting a time zone name to put in the date command output
has to be done unportably. Some implementations of strftime() may
support %Z for this.
Gordon L. Burditt