davenull
Posts: 1159
Joined: Thu Oct 22, 2015 7:22 am
Location: a small planet close to Betelgeuze

RasPi system date and time

Sat Apr 23, 2016 7:23 am

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?
#define S sqrt(t+2*i*i)<2
#define F(a,b) for(a=0;a<b;++a)
float x,y,r,i,s,j,t,n;int main(){F(y,64){F(x,99){r=i=t=0;s=x/33-2;j=y/32-1;F(n,50&S){t=r*r-i*i;i=2*r*i+j;r=t+s;}if(S){PointOut(x,y);}}}for(;;);}

User avatar
rpdom
Posts: 17173
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: RasPi system date and time

Sat Apr 23, 2016 7:43 am

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.

davenull
Posts: 1159
Joined: Thu Oct 22, 2015 7:22 am
Location: a small planet close to Betelgeuze

Re: RasPi system date and time

Sat Apr 23, 2016 7:52 am

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?
#define S sqrt(t+2*i*i)<2
#define F(a,b) for(a=0;a<b;++a)
float x,y,r,i,s,j,t,n;int main(){F(y,64){F(x,99){r=i=t=0;s=x/33-2;j=y/32-1;F(n,50&S){t=r*r-i*i;i=2*r*i+j;r=t+s;}if(S){PointOut(x,y);}}}for(;;);}

User avatar
rpdom
Posts: 17173
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: RasPi system date and time

Sat Apr 23, 2016 8:02 am

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.

davenull
Posts: 1159
Joined: Thu Oct 22, 2015 7:22 am
Location: a small planet close to Betelgeuze

Re: RasPi system date and time

Sat Apr 23, 2016 8:11 am

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?
#define S sqrt(t+2*i*i)<2
#define F(a,b) for(a=0;a<b;++a)
float x,y,r,i,s,j,t,n;int main(){F(y,64){F(x,99){r=i=t=0;s=x/33-2;j=y/32-1;F(n,50&S){t=r*r-i*i;i=2*r*i+j;r=t+s;}if(S){PointOut(x,y);}}}for(;;);}

davenull
Posts: 1159
Joined: Thu Oct 22, 2015 7:22 am
Location: a small planet close to Betelgeuze

Re: RasPi system date and time

Sat Apr 23, 2016 10:23 am

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?
#define S sqrt(t+2*i*i)<2
#define F(a,b) for(a=0;a<b;++a)
float x,y,r,i,s,j,t,n;int main(){F(y,64){F(x,99){r=i=t=0;s=x/33-2;j=y/32-1;F(n,50&S){t=r*r-i*i;i=2*r*i+j;r=t+s;}if(S){PointOut(x,y);}}}for(;;);}

davenull
Posts: 1159
Joined: Thu Oct 22, 2015 7:22 am
Location: a small planet close to Betelgeuze

Re: RasPi system date and time

Mon Apr 25, 2016 7:59 am

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? :geek:
#define S sqrt(t+2*i*i)<2
#define F(a,b) for(a=0;a<b;++a)
float x,y,r,i,s,j,t,n;int main(){F(y,64){F(x,99){r=i=t=0;s=x/33-2;j=y/32-1;F(n,50&S){t=r*r-i*i;i=2*r*i+j;r=t+s;}if(S){PointOut(x,y);}}}for(;;);}

Return to “Beginners”