Reputation: 79
Currently I am able to receive the current date time with the help of functions like localtime()
gettimeofday()
etc from time.h
Suppose a user modifies the date and time settings in Linux to change the date to some previous date, I am getting those changes when I call the functions mentioned above.
I would still like to get the current time i.e say today's time irrespective of any user changing the system date & time settings.
Is there any way I could achieve the same ?
Upvotes: 0
Views: 2070
Reputation: 279215
If a user with appropriate privileges changes the system time, then the system believes the new value.
So, the only way you can get someone else's opinion of the correct time is to go outside the system. For example, you could connect to a time server via the NTP protocol.
For a cheap and cheerful solution you could grab a web page such as http://www.whattimeisit.com/. Of course that will be inaccurate by (at best) the latency of the download, which is one of the things NTP clients cleverly deal with.
If what you want is not so much the "correct" time, as just a time that doesn't jump around while you're using it, then you could look into clock_gettime
and specifically CLOCK_MONOTONIC
.
Upvotes: 2
Reputation: 500167
As far as your computer is concerned, the system time is the current time.
If you can't trust the system you're on to provide accurate time, you might want to look into embedding an NTP client into your program.
Upvotes: 8
Reputation: 3906
If it's not synchronised to system time, you could look into reading the hardware clock, take a look at the hwclock man page.
The notes section on that page has some info about how to check for synchronisation too.
Upvotes: 0