Recent

Author Topic: now() and time() give wrong time at startup on RasPi Buster  (Read 2481 times)

af0815

  • Hero Member
  • *****
  • Posts: 1291
now() and time() give wrong time at startup on RasPi Buster
« on: November 05, 2019, 10:42:16 am »
Given a Lazarus programm  with this lines
Code: Pascal  [Select][+][-]
  1. procedure TFormMain.ShowTime;
  2. var
  3.   hstr : string;
  4. begin
  5.   DateTimeToString(hstr,'hh:mm',time);
  6.   LblUhrzeit.Caption:= hstr;
  7. end;
  8.  
The program is autostarted on a RasPi with Raspbian Buster and show the false time. Look like the timezone is wrong, because it is one hour.
The internal clock of the Raspi is showing at startup also the false time, but after few seconds the time shown is ok, but not inside of the Lazarus prg. When i close the programm and  restart the correct value is shown.

It looks like the the wrong parameters of the time (eg. timezone) is only requested once by Lazarus/fpc and only at startup. Is it possible to force the reload of the time parameters ? Or avoid this issue ?

regards
Andreas

Thaddy

  • Hero Member
  • *****
  • Posts: 14391
  • Sensorship about opinions does not belong here.
Re: now() and time() give wrong time at startup on RasPi Buster
« Reply #1 on: November 05, 2019, 10:47:53 am »
Quite simple: your program is started before the local time resolving daemon. Start time is always UTC, and only when connected to a time resolver (either from a local network or the internet) , not local time. (And for the better)
Can't you solve this by simply adding a cron job? That should start AFTER the OS is finished with its initialization..
Btw: a Raspberry Pi has no real-time hardware clock at all.... You need a $3 hat for that.... O:-)  Or 50 cents for hardware and 25 cents for a battery .... and a soldering iron...or a conducting glue pen...
Try the time on a Pi that is disconnected from the internet.... I "guess" it is actually 1-1-1970.
Must have something to do with FreePascal, because so many core developers are from the same year, did you know that?  :P :-X :-X :-X :-X
« Last Edit: November 05, 2019, 11:08:11 am by Thaddy »
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

af0815

  • Hero Member
  • *****
  • Posts: 1291
Re: now() and time() give wrong time at startup on RasPi Buster
« Reply #2 on: November 05, 2019, 11:07:15 am »
The time is correct synced after a short time on the RasPi. You can see this on the original clock in the statebar. So it is no problem on the system itself. Timezone and Daylightsaving is interpreted correct. The connection to the timeserver is ok, and no problem with the Ethernet connection.

I have FPC fixes32 and the actual Lazarus fixes20.

When i restart the application it reads and show the correct value. But if the system corrects the timezone or daylightstatus (or similar) the new values are not reloaded.

How can i force the reloading of the timezone daylight and similar infos in Lazarus/FPC.
regards
Andreas

Thaddy

  • Hero Member
  • *****
  • Posts: 14391
  • Sensorship about opinions does not belong here.
Re: now() and time() give wrong time at startup on RasPi Buster
« Reply #3 on: November 05, 2019, 11:10:10 am »
Can you show a little code? because I can not reproduce it and if that really happens I agree there is a serious problem, although that would indicate a problem at the Raspbian side...
I am not busy today and have a lot of these toys.
I haven't tested your previous example on a RPi3, just RPi 4, but I will repeat it.

[edit][modified for readability]
When I reduce your routine to its core, I can not reproduce your issue:
Full program:
Code: Pascal  [Select][+][-]
  1. program ShowTime;
  2. uses sysutils;
  3. begin
  4.   writeln(DateTimeToStr(time));
  5. end.
Check your code first!
« Last Edit: November 05, 2019, 11:27:46 am by Thaddy »
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

af0815

  • Hero Member
  • *****
  • Posts: 1291
Re: now() and time() give wrong time at startup on RasPi Buster
« Reply #4 on: November 05, 2019, 11:16:00 am »
I have found a small hint - this issue is per design !!!

https://www.freepascal.org/docs-html/rtl/sysutils/getlocaltimeoffset.html

Quote
Note that on Linux/Unix, this information may be inaccurate around the DST time changes (for optimization). In that case, the unix.ReReadLocalTime unit must be used to re-initialize the timezone information.
regards
Andreas

Thaddy

  • Hero Member
  • *****
  • Posts: 14391
  • Sensorship about opinions does not belong here.
Re: now() and time() give wrong time at startup on RasPi Buster
« Reply #5 on: November 05, 2019, 11:17:26 am »
see my example, posts crossed. But indeed it is the timezone information. This is not only FPC, the Gnu compilers behave the same.
Note that given enough time this will happen automatically since many daemons rely on time information.
Advice is: take a note and be patient...since the Pi has no clock.
« Last Edit: November 05, 2019, 11:24:43 am by Thaddy »
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

af0815

  • Hero Member
  • *****
  • Posts: 1291
Re: now() and time() give wrong time at startup on RasPi Buster
« Reply #6 on: November 05, 2019, 11:54:37 am »
The problem is not the missing clock, the problem is, the information is normally never refreshed. But now i know it, and can refresh the tzinformation.

And delay an autostart is also not an easy job :-)

But for me, all is fixed and working now (Without an HW-Clock).
regards
Andreas

Thaddy

  • Hero Member
  • *****
  • Posts: 14391
  • Sensorship about opinions does not belong here.
Re: now() and time() give wrong time at startup on RasPi Buster
« Reply #7 on: November 05, 2019, 12:34:59 pm »
the information is normally never refreshed.
That is not true, there is a time out on the sync, about a minute on idle.
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

af0815

  • Hero Member
  • *****
  • Posts: 1291
Re: now() and time() give wrong time at startup on RasPi Buster
« Reply #8 on: November 05, 2019, 06:11:43 pm »
the information is normally never refreshed.
That is not true, there is a time out on the sync, about a minute on idle.
No, i have some RasPi running the whole day with different systemtime and time in a Lazarus app. NO Refresh, only restart of the app or now programmed refresh of the settings works (simply call  unix.ReReadLocalTime on a raspi to refresh).

BTW: Your testprogramm cannot show the issue
Code: Pascal  [Select][+][-]
  1. program ShowTime;
  2. uses sysutils;
  3. begin
  4.   writeln(DateTimeToStr(time));
  5. end.
because every start the information is refreshed.

Make a GUI app, with a timer and call now and show the result
Put this app in a autostart of the desktop

Only now you can see this issue if the dayligth or similar informatiion is changed. The systemclock will reconize the changes after few time, but the app not. Because the app is caching the tzconfiguration and never updating.   

My bad was, the code of mine (first post) is called in a timer every half minute. This was not clear.
« Last Edit: November 05, 2019, 06:23:35 pm by af0815 »
regards
Andreas

Thaddy

  • Hero Member
  • *****
  • Posts: 14391
  • Sensorship about opinions does not belong here.
Re: now() and time() give wrong time at startup on RasPi Buster
« Reply #9 on: November 05, 2019, 08:49:51 pm »
I can simply not reproduce your issue.

I am very sorry, but this is neither a Lazarus issue nor an FPC issue. I ran my code as a cron job that fires every minute and after a couple of hours the difference is microseconds, in other words, can be neglected. I am not putting any more time in this. At least I tried. And the time is always correct at every start.
« Last Edit: November 05, 2019, 09:00:47 pm by Thaddy »
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

af0815

  • Hero Member
  • *****
  • Posts: 1291
Re: now() and time() give wrong time at startup on RasPi Buster
« Reply #10 on: November 05, 2019, 09:15:15 pm »
It is definitive an FPC problem by design. And documetated, if you find the correct place :-) And i am not talking about seconds or less - i am talking about one hour.

Normally you have only to deal with this if the DST ist changing (actual twice a year). if you have a daemon running 24/7 you must know this.

The problem is, if you use linux like raspbian and using a RasPi without RTC you must know that the DST information (and maybe more) is cached and not refreshed in lifecycle of the app after starting. The goal of the raspi is, he can change the information depending of the information of the NTP and this information is maybe delivered AFTER a autostart.

Today i have seen a diffence between Jessi and Buster. The App was originally designed for/on Jessi, but the new setup is now Buster. And i have seen the issue today only on the Buster RasPis. From Today it is no problem for me, because i make a refresh of the information and so it will be working correct after few minutes.

The electric infrastucture is not so bad in hungaria, so it is no problem, and a reboot from time to time in a greater brownout is no problem for the app. :-)   

 
regards
Andreas

avra

  • Hero Member
  • *****
  • Posts: 2514
    • Additional info
Re: now() and time() give wrong time at startup on RasPi Buster
« Reply #11 on: November 06, 2019, 01:15:22 pm »
Normally you have only to deal with this if the DST ist changing
Do you catch linux DST change, or you just call ReReadLocalTime() from time to time?
ct2laz - Conversion between Lazarus and CodeTyphon
bithelpers - Bit manipulation for standard types
pasettimino - Siemens S7 PLC lib

af0815

  • Hero Member
  • *****
  • Posts: 1291
Re: now() and time() give wrong time at startup on RasPi Buster
« Reply #12 on: November 06, 2019, 01:18:25 pm »
I simply call ReReadLocalTime() from time to time on a RasPi
regards
Andreas

 

TinyPortal © 2005-2018