How to get the time of the day in C?
mike at bria.UUCP
mike at bria.UUCP
Sat Jan 12 08:15:22 AEST 1991
In article <2584 at trlluna.trl.oz> shiva.trl.oz!hui (Alvaro Hui) writes:
>Hi all experts:
>
> I am writing a small program to generate random numbers in C and
>an idea came up in my mind: it is a good idea to use the time of the
>day as seed to my random generator!
>
> Unfortunately, I "man -k time" and search through all my 'C' books
>and couldn't came up an idea how to get current time in C.
>
> Can some of you folks help me?
>
Try ...
#include <time.h>
long now; /* number of seconds since the Epoch */
time(&now);
printf("%s",ctime(&now)); /* time string */
'now' will contain the number of seconds since 00:00 GMT on 1 Jan 1970
(aka "the Epoch"); the ctime() function was thrown in for giggles.
If a long is too high, then you could use:
long now;
int today;
struct tm *t; /* localtime structure defined in time.h */
time(&now);
t = localtime(&now);
today = t->tm_sec + (t->tm_min * 60) + (t->tm_hour * 3600);
where 'today' is the number of seconds elapsed since midnight.
Hope that helps ...
--
Michael Stefanik, Systems Engineer (JOAT), Briareus Corporation
UUCP: ...!uunet!bria!mike
--
technoignorami (tek'no-ig'no-ram`i) a group of individuals that are constantly
found to be saying things like "Well, it works on my DOS machine ..."
More information about the Comp.unix.programmer
mailing list