ANSI C get system time

Hi

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

-Mike
nerdmikeAsked:
Who is Participating?
 
brettmjohnsonCommented:
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)));

0
 
cookreCommented:
For pure ANSI, you want time():
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclib/html/_crt_time.asp?frame=true

If you're willing to live in the MS world, strtime():
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccore98/html/_crt__strtime.2c_._wstrtime.asp?frame=true

gives it to you already in a string, but it's not standard ANSI.
0
 
_iskywalker_Commented:
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);
0
 
nerdmikeAuthor Commented:
According to man ctime, it requires a CLOCK object pointer. Time() returns a time_t type, I don't think thats right.
0
 
nerdmikeAuthor Commented:
wait, nevermind.
0
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.