[comp.lang.c] How to get the time...

rmacgreg@cs.strath.ac.uk (Sorcerer) (02/18/91)

I'm trying to get the time and date using the ctime() function, but I've come
up against a rather large problem...  Ctime() expects a long int as an
argument, but the manual doesn't say how or where you get this long int from.

The manual gives:

     char *ctime(clock)
     long *clock;

     ctime() converts a long integer, pointed to by clock,  to  a
     26-character  string  of the form produced by asctime() . It
     first breaks down clock to a struct  tm  by  calling  local-
     time(),  and  then calls asctime() to convert that struct tm
     to a string.

Which is no help whatsoever.

If it makes any difference I am on a Sun workstation running Un!x...

       The Sorcerer is 'Only visiting this planet' but can be found at:

             JANET: cadx862 @uk.ac.strathclyde.computer-centre-sun 
                    rmacgreg@uk.ac.strathclyde.computer-science
  INTERNET: via nsfnet-relay.ac.uk    BITNET: via ukacrl    UUCP: via ukc.uucp

          or second star to the right and straight on 'till morning.

tchrist@convex.COM (Tom Christiansen) (02/19/91)

From the keyboard of rmacgreg@cs.strath.ac.uk (Sorcerer):
:I'm trying to get the time and date using the ctime() function, but I've come
:up against a rather large problem...  Ctime() expects a long int as an
:argument, but the manual doesn't say how or where you get this long int from.

My manual says:

     char *ctime(const time_t *clock);

usually you feed it something from the output of time(3) or perhaps
the st_[acm]time fields from a stat(2) call.  My time(3) looks like
this:

     time_t time(time_t *tloc);

So in general, do this:
    
    time_t now = time(0);
    char *cnow = ctime(&now);

replace time_t with long on old systems.

--tom
--
Tom Christiansen		tchrist@convex.com	convex!tchrist
 "All things are possible, but not all expedient."  (in life, UNIX, and perl)