|
Home > Archive > Unix Programming > October 2006 > replacement for timeGetTime
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
replacement for timeGetTime
|
|
| maverick 2006-10-27, 7:15 pm |
| This API in windows returns the time elapsed since the OS started
Is there an equivalent in Linux ?
Help is appreciated
| |
| Bill Medland 2006-10-27, 7:15 pm |
| maverick wrote:
> This API in windows returns the time elapsed since the OS started
>
> Is there an equivalent in Linux ?
>
>
> Help is appreciated
Yes. And google told me in the first 20 hits (obviously a poor search).
try times()
--
Bill Medland
| |
| David Schwartz 2006-10-28, 7:24 am |
|
maverick wrote:
> This API in windows returns the time elapsed since the OS started
>
> Is there an equivalent in Linux ?
>
>
> Help is appreciated
Linux starts its monotonic clock at zero on startup, so the following
works:
#include <time.h>
time_t uptime(void)
{
struct timespec r;
if(clock_gettime(CLOCK_MONOTONIC, &r)!=0) return (time_t) -1;
return r.tv_sec;
}
Link with '-lrt'. If there's a faster or more portable way, I don't
know it.
DS
| |
| Jens Thoms Toerring 2006-10-28, 1:40 pm |
| Bill Medland <billmedland@shaw.ca> wrote:
> maverick wrote:
> Yes. And google told me in the first 20 hits (obviously a poor search).
> try times()
That may not be the solution, since the OP was looking for since,
on the one hand, times() is not guaranteed to return the time since
system start, that's only how it often is implemented, but POSIX
"permits an implementation to make the reference point for the
returned value be the start-up time of the process, rather than
system start-up time"
and, on the other hand, the time resolution is not necessarily in
milli-seconds, but is in units of clock ticks that you are supposed
to determine via a call of "sysconf(_SC_CLK_TCK)". As a third dif-
ference the function will only return the time since system startup
(if at all) on the very first invocation, on further invocations it
will return differences to the time of the last call of it.
Unfortunately, I am not aware of a function that faithfully repro-
duces the Windows timeGetTime() behaviour. The only thing that
immediately comes to mind is to invoke the 'uptime' utility from a
program and extract the time since the last reboot from what it
returns - but only with a resolution of a second. On Linux systems
one could also read the (pseudo-) file /proc/uptime to get the time
since system start with a resolution of about 1/100 of a second. I
guess people usually don't care too much about the system startup
time under Unix and traditionally use the time since the "epoch"
(i.e. January 1st, 1970, 00:00:00) instead. For this there exists
e.g. the function gettimeofday() - the resolution depends on that of
the system clock, but the same also seems to be the case (as far as
I have read) for timeGetTime().
Regards, Jens
--
\ Jens Thoms Toerring ___ jt@toerring.de
\__________________________ http://toerring.de
|
|
|
|
|