pnaven03
Posts: 139
Joined: Thu Feb 16, 2017 5:21 pm

How to set time and date in boot time

Fri Sep 08, 2017 4:48 am

We are using raspberry pi board and kernel version Linux raspberry pi 4.4.11-v7+ #888 SMP Mon May 23 20:10:33 BST 2016 armv7l GNU/Linux,
How to set time and date in boot time using shell script or c code , in case date and time corrupted how to check c code or shell script and update in run time,please any one help me on this.

klricks
Posts: 6002
Joined: Sat Jan 12, 2013 3:01 am
Location: Grants Pass, OR, USA
Contact: Website

Re: How to set time and date in boot time

Fri Sep 08, 2017 5:11 am

If your RPi is not connected to the Internet then you will need a RTC module with battery backup. Setting the clock depends on which module you get.
Unless specified otherwise my response is based on the latest and fully updated Raspbian Stretch w/ Desktop OS.

pnaven03
Posts: 139
Joined: Thu Feb 16, 2017 5:21 pm

Re: How to set time and date in boot time

Fri Sep 08, 2017 5:15 am

Thank you for the reply,

I need information only set time and date in boot time with ntp server , we are using RTC module in device to write "sudo hwclock -w"
my problem is how to sync time and date from servers in boot time.

User avatar
ab1jx
Posts: 735
Joined: Thu Sep 26, 2013 1:54 pm
Location: Heath, MA USA
Contact: Website

Re: How to set time and date in boot time

Fri Sep 08, 2017 5:43 am

That sounds messy to get working the first time if I'm understanding you correctly. You have a hardware RTC clock? See man ntp and man ntp.conf. It's possible to set NTP to use a hardware clock and ignore the internet but the configuration isn't simple. A few years ago I had a machine getting the time via shortwave radio audio. Beyond the man pages you should probably look at http://www.ntp.org/ There are a few different RTC clocks I think and they'll work slightly differently. Exhaust any documentation that came with it. GPS time is also very accurate and can be used.

Or is it how to use NTP? Look at how to enable the service, then edit /etc/ntp.conf. I think this is mostly the stock file, I set it up a couple years ago.

Code: Select all

# /etc/ntp.conf, configuration for ntpd; see ntp.conf(5) for help

driftfile /var/lib/ntp/ntp.drift


# Enable this if you want statistics to be logged.
#statsdir /var/log/ntpstats/

statistics loopstats peerstats clockstats
filegen loopstats file loopstats type day enable
filegen peerstats file peerstats type day enable
filegen clockstats file clockstats type day enable


# You do need to talk to an NTP server or two (or three).
#server ntp.your-provider.example

# pool.ntp.org maps to about 1000 low-stratum NTP servers.  Your server will
# pick a different set every time it starts up.  Please consider joining the
# pool: <http://www.pool.ntp.org/join.html>
server 0.debian.pool.ntp.org iburst
server 1.debian.pool.ntp.org iburst
server 2.debian.pool.ntp.org iburst
server 3.debian.pool.ntp.org iburst


# Access control configuration; see /usr/share/doc/ntp-doc/html/accopt.html for
# details.  The web page <http://support.ntp.org/bin/view/Support/AccessRestrictions>
# might also be helpful.
#
# Note that "restrict" applies to both servers and clients, so a configuration
# that might be intended to block requests from certain clients could also end
# up blocking replies from your own upstream servers.

# By default, exchange time with everybody, but don't allow configuration.
restrict -4 default kod notrap nomodify nopeer noquery
restrict -6 default kod notrap nomodify nopeer noquery

# Local users may interrogate the ntp server more closely.
restrict 127.0.0.1
restrict ::1

# Clients from this (example!) subnet have unlimited access, but only if
# cryptographically authenticated.
#restrict 192.168.123.0 mask 255.255.255.0 notrust


# If you want to provide time to your local subnet, change the next line.
# (Again, the address is an example only.)
#broadcast 192.168.123.255

# If you want to listen to time broadcasts on your local subnet, de-comment the
# next lines.  Please do this only if you trust everybody on the network!
#disable auth
#broadcastclient
A hardware clock would need a clock line in there and remove or comment out the server lines, from memory.

pnaven03
Posts: 139
Joined: Thu Feb 16, 2017 5:21 pm

Re: How to set time and date in boot time

Fri Sep 08, 2017 6:10 am

ab1jx,
Thank you so much for reply,

I changed as per your configuration, but time and date is invalid,please check the below logs,

pi@raspberrypi:~ $ date
Fri 4 Jul 00:17:20 IST 2014
pi@raspberrypi:~ $ sudo timedatectl status
Local time: Fri 2014-07-04 00:17:15 IST
Universal time: Thu 2014-07-03 18:47:15 UTC
RTC time: n/a
Time zone: Asia/Kolkata (IST, +0530)
NTP enabled: yes
NTP synchronized: yes
RTC in local TZ: no
DST active: n/a

User avatar
ab1jx
Posts: 735
Joined: Thu Sep 26, 2013 1:54 pm
Location: Heath, MA USA
Contact: Website

Re: How to set time and date in boot time

Fri Sep 08, 2017 6:27 am

I don't know, mine says:

Code: Select all

      Local time: Fri 2017-09-08 02:17:01 EDT
  Universal time: Fri 2017-09-08 06:17:01 UTC
        RTC time: n/a
       Time zone: America/New_York (EDT, -0400)
     NTP enabled: no
NTP synchronized: yes
 RTC in local TZ: no
      DST active: yes
 Last DST change: DST began at
                  Sun 2017-03-12 01:59:59 EST
                  Sun 2017-03-12 03:00:00 EDT
 Next DST change: DST ends (the clock jumps one hour backwards) at
                  Sun 2017-11-05 01:59:59 EDT
                  Sun 2017-11-05 01:00:00 EST
I don't get how NTP could be enabled and synchronized yet the local time is wrong. Is the time zone right? If you just changed it, let it sit a while. There are restrictions on how fast the time can change (slew).

Try the command ntpdate and the address of an NTP server to manually get the time from the server.

User avatar
ab1jx
Posts: 735
Joined: Thu Sep 26, 2013 1:54 pm
Location: Heath, MA USA
Contact: Website

Re: How to set time and date in boot time

Fri Sep 08, 2017 6:35 am

Oh, mine says: NTP enabled: no

So I don't know what I'm talking about, I thought it was working fine. Then again it's 02:30. It does agree with my phone.

pnaven03
Posts: 139
Joined: Thu Feb 16, 2017 5:21 pm

Re: How to set time and date in boot time

Fri Sep 08, 2017 10:24 am

Any one help me on this issue.

User avatar
ab1jx
Posts: 735
Joined: Thu Sep 26, 2013 1:54 pm
Location: Heath, MA USA
Contact: Website

Re: How to set time and date in boot time

Fri Sep 08, 2017 2:17 pm

The only thing I can think of is that it's a permissions issue. I think only root can change the time. But I have to wonder why you're having a problem at all. Raspbian was made for Pis which don't have real time clocks and there are millions of them out there. If you've got a spare SD try a fresh load, I think it works out of the box without doing anything. If it does then compare your configs.

Martin Frezman
Posts: 1020
Joined: Mon Oct 31, 2016 10:05 am

Re: How to set time and date in boot time

Fri Sep 08, 2017 4:00 pm

Has OP ever confirmed whether he is or isn't connected to the inter-webs?

You should never need a RTC or have to do anything special at all (assuming running Raspbian) if you are normally connected to the Internet.

The whole idea of an RTC and/or needing to set the time manually only comes into play if you are running a Pi "off-net".

Note, however, that it *is* true that the time doesn't get set until the network connection is up and running. There will be a noticeable delay, especially if your net connection is via wireless, between initial bootup and when the time gets set correctly. I always notice this in that the desktop comes up, then a few seconds later, goes to screensaver. This is because when the wireless connects, it jumps the time forward by a huge leap, making xscreensaver think that the machine has been idle for a long time.

You can also see this in the "dmesg" output, where the first few screenfuls of stuff are time stamped at basically zero, then it jumps forward to the current time.
If this post appears in the wrong forums category, my apologies.

User avatar
ab1jx
Posts: 735
Joined: Thu Sep 26, 2013 1:54 pm
Location: Heath, MA USA
Contact: Website

Re: How to set time and date in boot time

Fri Sep 08, 2017 7:48 pm

Dmesg, yes, also journalctl, 2 places to look for relevant error messages. I don't think NTP normally does any logging unless stats is turned on in /etc/ntp.conf but they appear in /var/log/ntpstats. /var/log/daemon.log might show kind of explanation. In all 3 of those it's probably safe to search for ntp or NTP and start reading there, at least on a first pass. Most of the stuff will be irrelevant.

But also true, if there's no network connection NTP can't do anything. That should get logged somewhere.

Mine in fact has never been changed from the "factory" settings in /etc/ntp.conf. Before I change a file like that I copy it to something like ntp.conf.original and I saw one of those so I thought I might have changed it. I just did a cmp and I haven't changed it, so completely stock is working fine. On 2 Pi 3s and a Zero.

If you do
systemctl -a | grep -i ntp
you should see something like
ntp.service loaded active running LSB: Start NTP daemon

pnaven03
Posts: 139
Joined: Thu Feb 16, 2017 5:21 pm

Re: How to set time and date in boot time

Mon Sep 11, 2017 4:25 am

Thanks for the reply,

My logs messages:

pi@raspberrypi:~ $ systemctl -a | grep -i ntp
ntp.service loaded active running LSB: Start NTP daemon
● openntpd.service masked inactive dead openntpd.service

My raspberry pi connected to Ethernet and wifi network.

pnaven03
Posts: 139
Joined: Thu Feb 16, 2017 5:21 pm

Re: How to set time and date in boot time

Tue Sep 12, 2017 3:58 am

Every reboot date and time is starting from
pi@raspberrypi:~ $ date
Mon 7 Jul 03:47:11 PDT 2014

Not current time , how to get the current date and time , any changes required in raspberry board,
please any one help me on this issue.

User avatar
DougieLawson
Posts: 34358
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website

Re: How to set time and date in boot time

Tue Sep 12, 2017 6:47 am

The last thing fake-hwclock does when you shut down your RPi is write the current date/time to your SDCard. On reboot fake-hwclock restores then time from it's safe store (unless you install RTC hardware).

If those data are not being reliably stored you'll see your symptoms. One thing SDCards do near end-of-life is to go read-only (remember they were designed for cameras - that's to protect your photos). So the best advice I can offer is get a new SDCard and an SDCard reader. Boot the suspect card, use the Raspbian Pi Cloning tool to copy to the new SDCard (in the USB reader) swap cards and see if that solves your problem. Then take the old SDCard and run some card integrity tests on it.

If the tests work, save the card as a backup, if the test fail, toss it in the bin.
Note:Having anything remotely humorous in your signature is completely banned on this forum.

Any DMs sent on Twitter will be answered next month.

Killertechno
Posts: 157
Joined: Wed Jan 02, 2013 8:28 am

Re: How to set time and date in boot time

Sun Dec 09, 2018 6:34 pm

Hi guys, may I ask where raspberry does save date at poweroff?
I mean, where does raspberry read date after boot?
I'm using latest raspbian, I set /etc/fake-hwclock.data file with dummy time (1th Jan 2000) and set it with read-only permissions.
At boot I get time updated to 2016 without RTC and no ntp connection....
Thanks.

DirkS
Posts: 9604
Joined: Tue Jun 19, 2012 9:46 pm
Location: Essex, UK

Re: How to set time and date in boot time

Sun Dec 09, 2018 6:48 pm

Does it set the date to the 2016/04/15?
If yes then check out the fake-hwclock script (it's in /sbin)

Return to “Beginners”