3

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?

2
  • 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.
    – FelixJN
    Commented 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.
    – raylight
    Commented Dec 16, 2020 at 20:01

2 Answers 2

4

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

1
  • 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'
    – raylight
    Commented Dec 16, 2020 at 21:43
1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.