When the raspberry turns on and it doesn't have an internet connection, the system time is wrong... On the context that I'm using the raspberry, it will not have any internet connection, but it will have access to the local network where it could get the correct current time from a different device on this local network. So, my doubt is: if I have the epoch timestamp of now, like 1608146451904, is it possible to make the system synchronize its time with this timestamp? Is there any command I can use or file I can edit that makes it possible?
-
What device could provide the time? Maybe they can act as local NTP servers? E.g. a simple linux or Windows machine would be sufficient.– FelixJNCommented Dec 16, 2020 at 19:40
-
@Fixman Actually, I'm using an Android app that sends this timestamp to a REST API that I have on my raspberry... I've searched about NTP servers on Android now and it seems I could implement this NTP server on my app too. But I think the timestamp solution that sets the time only once would be simpler.– raylightCommented Dec 16, 2020 at 20:01
2 Answers
Use date -s ‘@1608146451904’
to set the system time to that many seconds from the Epoch.
You normally need root permissions to set the date.
man date
-
Thanks! That was what I was looking for... Just an observation is that this command doesn't accept milliseconds on the epoch. So I had to take out the last three digits of this example:
date -s ‘@1608146451'
– raylightCommented Dec 16, 2020 at 21:43
If your Pi has no Internet access you can copy the date from the host by running
ssh [email protected] sudo date -s$(date -Ins)
before connection.
I prefer date -Ins
because it generates readable dates, but any form can be used
2020-12-17T09:42:37,146342060+11:00
It should be possible to setup something similar, which runs on the Pi, but I have never done this.