ANSI C get system time


I'm looking for a way (ideally using ANSI C) to gather the system time, and print it in a human readable form.

Who is Participating?
The ANSI C standard way to print the current time in human readable form:
use time() and ctime():

#include <time.h>

printf("The current time is: %s\n", ctime(time(NULL)));

For pure ANSI, you want time():

If you're willing to live in the MS world, strtime():

gives it to you already in a string, but it's not standard ANSI.
on unixes try
 int gettimeofday(struct timeval *tv, struct timezone *tz);
where timeval is:
 struct timeval {
               time_t         tv_sec;        /* seconds */
               suseconds_t    tv_usec;  /* microseconds */
an example:
struct timeval now;
 gettimeofday (&now, NULL);
fprintf(stderr," %i %i \n", now.tv_sec, now.tv_usec);

on windows that could also work...
  struct timeval tv;
  gettimeofday (&tv, NULL);
nerdmikeAuthor Commented:
According to man ctime, it requires a CLOCK object pointer. Time() returns a time_t type, I don't think thats right.
nerdmikeAuthor Commented:
wait, nevermind.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.