Path: utzoo!censor!geac!torsqnt!news-server.csri.toronto.edu!bonnie.concordia.ca!uunet!bria!mike Newsgroups: comp.unix.programmer Subject: Re: How to get the time of the day in C? Message-ID: <345@bria> Date: 11 Jan 91 21:15:22 GMT References: <2584@trlluna.trl.oz> Reply-To: mike@bria.UUCP (Michael Stefanik) Organization: Briareus Corporation, Los Angeles, CA Lines: 41 In article <2584@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 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 ..."