Page 1 of 1
RasPi system date and time
Posted: Sat Apr 23, 2016 7:23 am
by davenull
hi,
as it seemed to me, the Raspi 2 can get the correct date and time even when it had been unplugged from power supply intermediately and even if there is no internet connection available currently.
OTOH, I can't see a battery cell on the Pi for buffering, like for the BIOS on my PC mainboard or like on a RTC backpane.
Is that true?
How does the Pi get date and time then accordingly?
Re: RasPi system date and time
Posted: Sat Apr 23, 2016 7:43 am
by rpdom
davenull wrote:How does the Pi get date and time then accordingly?
It doesn't.
What it does is to save the current time and date to a file when it shuts down and once an hour as well.
When it boots up it looks at that file and sets the time and date to that value. If an internet connection is available it will update that time to the correct current time.
So if you shut down a Pi that is on a network, then boot it up without the network, the date and time will be out by a few minutes.
If you leave it switched off for a month, it will be a month out.
Re: RasPi system date and time
Posted: Sat Apr 23, 2016 7:52 am
by davenull
aaah, now I see, thank you!
Then maybe it does actually make sense to use a RTC additionally.
Having both "clocks" simultaneously running (RTC + internet time) and occasionally having 2 different "system times":
Is there a good way to check which one is correct and if either one might be faulty?
Re: RasPi system date and time
Posted: Sat Apr 23, 2016 8:02 am
by rpdom
I'd usually trust the internet time, rather than an RTC. Internet time is usually from one of many trusted time sources which are compared with each other. The ntp program will ignore any internet time that seems many seconds out from the internal clock (but that is over-ridden when the Pi starts up, as the internal time will usually be wrong).
An RTC will drift.
Re: RasPi system date and time
Posted: Sat Apr 23, 2016 8:11 am
by davenull
yes exactly, both times may be faulty, but if the Pi boots up or is already running a few seconds or minutes:
how to set one or the other correct time?
if no updated internet time => take RTC time
if updated internet time => reset and sync new RTC time.
But how will one know (from a C/C++ program) which one to take and what to set or reset?
Re: RasPi system date and time
Posted: Sat Apr 23, 2016 10:23 am
by davenull
is there perhaps a Raspi system flag like
Code: Select all
_SYSTEMTIME_UPDATED_ // (1==current time updated, 0==last time read from file)
which one could read from a C/C++ program to be sure about it?
edit:
now from
http://manpages.ubuntu.com/manpages/pre ... eebsd.html I tried this one
(yes I know it's Ubuntu, but could not find something for Jessie...

)
Code: Select all
#include <sys/timex.h>
#include <iostream>
using namespace std;
int main()
{
ntptimeval ntpdummy;
int result = ntp_gettime(&ntpdummy);
if (result == TIME_OK) {
cout << "internet time ok" << endl;
}
else if (result == TIME_ERROR) {
cout << "internet time error" << endl;
}
else {
cout << "something different " << endl;
}
return 0;
}
but even if the Raspi intermediately has been shut down, power off, Wifi removed, power again plugged, restartet:
it always says "internet time ok" .
what can be faulty?
perhaps different proposals about how to check if system time is synced / updated or not?
Re: RasPi system date and time
Posted: Mon Apr 25, 2016 7:59 am
by davenull
although I meanwhile have a possible workaround, I'm actually curious why the code posted above
(using ntptimeval:
viewtopic.php?f=91&t=145585&p=960716#p959622 )
does not work as expected.
Any sophisticated ideas?
