Skip to content

Instantly share code, notes, and snippets.

@dkeza
Last active November 25, 2021 20:54
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dkeza/9d24e3ba392e0fb5ba9016e513ebd890 to your computer and use it in GitHub Desktop.
Save dkeza/9d24e3ba392e0fb5ba9016e513ebd890 to your computer and use it in GitHub Desktop.
Fix wrong date time on Raspberry Pi 3 in LibreELEC (Krypton) v8.0.1 MR (Kodi), when connected over WiFi
#!/usr/bin/env python
## Python 2.7 script
## Use it as workaround for Kodi LibreELEC wrong date time bug, when used on Raspberry Pi 3 over WiFi.
## Log in into LibreELEC over SSH, create this script as executable,
##
## $ nano fixtime.py
## $ chmod +x fixtime.py
##
## and call it from autostart.sh
##
## $ nano /storage/.config/autostart.sh
##
## Add delay 10 sec in autostart.sh:
##
## (sleep 10;
## /storage/./fixtime.py
## ) &
import urllib2
import sys
import subprocess
from datetime import datetime, timedelta
## Returns date (GMT) from Google HTTP response header in this format
## (2017, 5, 25, 9, 40, 23, 0, 1, 0)
d = urllib2.urlopen('http://www.google.com/').info().getdate('Date')
## Create python datetime type
dp = datetime(d[0], d[1], d[2], d[3], d[4], d[5], d[6])
## Add 2 hours for CET time zone
df = dp + timedelta(hours=2)
## Create tuple, to get datetime parts
t = df.timetuple()
## Convert to format needed for date command in terminal window on BusyBox in LibreELEC
## [YYYY.]MM.DD-hh:mm[:ss]
s = str(t.tm_year) + '.' + str(t.tm_mon).zfill(2) + '.' + str(t.tm_mday).zfill(2) + '-' +str(t.tm_hour).zfill(2) +':'+str(t.tm_min).zfill(2) + ':' + str(t.tm_sec).zfill(2)
## Print it in terminal window for visual check
print s
## Call OS date command with our new date
subprocess.check_output(['date', '--set', s])
sys.exit(0)
@snowa1
Copy link

snowa1 commented Jun 24, 2020

Hi - Appreciate that this is an old post, but having trouble with this script - it looks to be exactly the solution I need!
This is what I get:
LibreELEC:/.config # ./fixtime.py
Traceback (most recent call last):
File "./fixtime.py", line 27, in
d = urllib2.urlopen('http://www.google.com/').info().getdate('Date')
File "/usr/lib/python2.7/urllib2.py", line 154, in urlopen
File "/usr/lib/python2.7/urllib2.py", line 429, in open
File "/usr/lib/python2.7/urllib2.py", line 447, in _open
File "/usr/lib/python2.7/urllib2.py", line 407, in _call_chain
File "/usr/lib/python2.7/urllib2.py", line 1228, in http_open
File "/usr/lib/python2.7/urllib2.py", line 1198, in do_open
urllib2.URLError: <urlopen error [Errno -2] Name or service not known>
LibreELEC:
/.config #

Any ideas?

@dkeza
Copy link
Author

dkeza commented Jun 24, 2020

It looks like your DNS is not working properly, or your Pi has no internet acces.
Try with

urllib2.urlopen('http://172.217.9.238/').info().getdate('Date')

using directly google ip address.

@snowa1
Copy link

snowa1 commented Jun 25, 2020

Tried that - same error, but ping to 172.217.9.238 is successful when SSH'd into the PI
Just tried to run iplayer - similar error, also checked the logs and the - URLError: <urlopen error [Errno -2] Name or service not known> appears at every call to the internet
Now trying ethernet connection to see if that makes a difference
Date issue only started when I upgraded KODI version, then had to downgrade as TVHeadend add-on did not work
Next stop fresh install on a PI 4

@snowa1
Copy link

snowa1 commented Jun 25, 2020

Ethernet - OK
WLAN - changed to DHCP now works
Original issue may well have been that broken WLAN did not pick up ntp server
But script is now running

@cpassuel
Copy link

You saved my day, this bug was driving me crazy.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment