Link to home
Start Free TrialLog in
Avatar of linux_newb
linux_newb

asked on

System time

How can i retrieve the system time that is elapsed since Linux was started?

Thanks!!
Avatar of sunnycoder
sunnycoder
Flag of India image

uptime
Avatar of linux_newb
linux_newb

ASKER

uptime is a shell command . I want to use this value in my prog. Isn't there any function which can be used to assign this value to a variable inside my program?
SOLUTION
Avatar of sunnycoder
sunnycoder
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
var=`uptime`

where var is ur program variable.

sanjeev
> you can invoke it using popen() and read in the string into a char array

i hope the format of info by uptime remains same for all versions of  redHat... else it would be difficult to extract the reqd part  from the string.
>i hope the format of info by uptime remains same for all versions of  redHat
for redhat it should be the same (cant say definitely about <6) ... any case I feel that scn's method is far superior in terms of portability and ease

from the times man page

The function times returns the number of clock ticks that have elapsed since an arbitrary point in the past. For Linux this point is the moment the system was booted. This return value may overflow the possible range of type clock_t. On error, (clock_t) -1 is returned, and errno is set appropriately.

i meant i am relying on the fact that elapsed time immediately follows word 'up'
>i meant i am relying on the fact that elapsed time immediately follows word 'up'
should work .... if you are concerned then it can also be obtained from the proc filesystem ...
cat /proc/uptime