nerdmike
asked on
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
I'm looking for a way (ideally using ANSI C) to gather the system time, and print it in a human readable form.
-Mike
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);
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);
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
According to man ctime, it requires a CLOCK object pointer. Time() returns a time_t type, I don't think thats right.
ASKER
wait, nevermind.
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.