I am running Pi 3 along with Real-Time Clock chip ds1302 and using wiringPI library to read and set date and time to and from ds1302 chip. After I set my Pi's system date and time to local date and time in 12 hour format, I set the Real Time Clock ds1302 chip its date and time by sudo ./ds1302 -sdsc
Then, I read what was written into clock chip and it reads back UTC time in 24 hour format. So, then I continued with the test by issuing sudo ./ds1302 -slc
to set the Pi's date and time from clock chip date and time. Now, my pi's time is one hour ahead in 12 hour format. I am confused. Is there way to force wiringpi library to use only local time in 12 hour format?
-
You could always try reading the data sheet. I would just throw this obsolete chip, which is not supported by Device Tree and is not a particularly good RTC, and use a more modern chip, such as DS3231– MilliwaysDec 22, 2016 at 0:37
2 Answers
The use of UTC time seems to be hardcoded in ds1302
utility. Note the following line:
gmtime_r (&now, &t) ;
This converts your system time to UTC, before writing it to the DS1302 chip. When the chip is read back, your system time is set to whatever is received from the chip, without any conversion (which frankly looks like a bug).
There are two things you can do:
replace
gmtime_r
withlocaltime_r
and recompile. Hopefully, you will have your local time in both RPi and DS1302.configure your RPi to use UTC time with
raspi-config
. Then the conversion done bygmtime_r
will be identical tolocaltime_r
. Whether using UTC time is convenient to you is another story.
Linux uses UTC. So many subsystems depend on it that you'd be better off doing a localtime conversion for the times you need, and let the system clock run on UTC as it's supposed to.
You should be able to use hwclock to set and retrieve the time from a 1302. It should make the conversion to/from UTC for you.