2

I know there are two ways

  1. sysinfo function call
  2. /proc/uptime

I think sysinfo gets the uptime from the /proc/uptime. But the problem is if the user logouts and logs back in , the timer doesn't get reset. It keeps getting continued.

In windows, GetTickCount64() resets the timer on user logoff and logon. I want something similar in linux.

So how do i find the system elapsed time , since the last logon?

Thank You

edit: c++ code or api function call would be nicer, instead of spawning a process to call who or last command

jww
  • 97,681
  • 90
  • 411
  • 885
  • The `who` command? – David C. Rankin Sep 17 '19 at 05:25
  • Are you sure that it is guaranteed that `GetTickCount64` will give the time since login? The docs don't state anything like that. – t.niese Sep 17 '19 at 05:32
  • If you are looking for the time a user has been logged in, that is what `who` does. The man page is a bit skimpy. It may take finding the `info` page. – David C. Rankin Sep 17 '19 at 05:43
  • @t.niese i tested it manually and it resets it. – Abel Pandian Sep 17 '19 at 05:46
  • See `man 5 utmp`. – Oliv Sep 17 '19 at 05:54
  • @AbelPandian my question was if you are sure that this is a guaranteed behavior for `GetTickCount64`, not if you manually tested it. Relying on observation instead on what the documentation tells you, isn't something you should do. And here a [blog post](https://devblogs.microsoft.com/oldnewthing/20141113-00/?p=43623) of an ms dev `The purpose of Get­Tick­Count is to let you measure intervals of time. [...] applications should not ascribe any meaning to the zero point of the tick counter.` – t.niese Sep 17 '19 at 07:59
  • Possible duplicate of [What API do I call to get the system uptime?](https://stackoverflow.com/q/1540627/608639), [Linux get system time since power on](https://stackoverflow.com/q/17728173/608639), etc. – jww Sep 17 '19 at 18:03

0 Answers0