How to set time

How to do things, solutions, recipes, tutorials
Message
Author
musher0
Posts: 14629
Joined: Mon 05 Jan 2009, 00:54
Location: Gatineau (Qc), Canada

#16 Post by musher0 »

Hello all.

Please find attached as a pet the htpdate utility compiled for the most
recent slacko-700 and ScPup-19.01
(at the time of this writing).

It may or may not be suited for other Pups, since this exec requires
GNU libc version 2.28 to work. Please check the ldd version in your Pup,
like so: open a console and type in it:

Code: Select all

ldd --version
It will tell you.

I tested it thoroughly (I think), but use at your own peril, as usual.
Please take a minute to read the NOTE in the script below and call the
htpdate man page if you need more info. Or leave your question below.

Enjoy.

~~~~~~~~~~~~

Code: Select all

#!/bin/bash
MBINS=/root/my-applications/bin
[ ! -d $MBINS ] && MBINS=/opt/local/bin
# $MBINS/htpdate.sh
# With link in: / Avec lien dans : /root/Startup
#
# Goal: Synchronise the time and date of your Puppy with Internet
# ----- time by probing various http sites.
#
# Objectif : Synchroniser le temps et l'heure de votre Toutou ou 
# ---------- Puppy en sondant divers sites http.
#
# Please read the NOTE below. / S.v.p. lire la NOTE ci-dessous.
#
# (c) musher0, Gatineau (Qc), Canada, 27 décembre 2018. GPL3.
####
[ -f /root/root/last-date ] && date --set="`cat /root/last-date`" || date --set="20181227 14:28"
sleep 2s

htpdate -as www.linux.org www.freebsd.org www.ask.com unix.stackexchange.com puppylinux.com www.gnu.org www.cnrs.fr www.assnat.qc.ca www.parl.ca

date > /root/last-date

exit

~~~~~~~~~~~~~~
~~ NOTE ~~
~~~~~~~~~~~
Replaces /usr/local/ptimesync and /root/Startup/timesync.sh
(please remove or store out of the way) in slacko-700_32-bits. 

As it is, this script should offer a precision between 0.6 and
0.167 of a second.

If you wish, you can test and use other public sites than 
those above.

~~~~~~~~~~~~~~

Remplace /usr/local/ptimesync et /root/Startup/timesync.sh
(s.v.p. enlever ou entreproser ailleurs) dans slacko-700_32-bits.

Tel quel, le script devrait offrir une précision de 0,6 à 0,167
seconde.

Si vous le souhaitez, vous pouvez tester et utiliser d'autres
sites publics que ceux ci-dessus.
~~~~~~~~~~~~~~
In the picture below
-- the first line is the command itself;
-- the second line is the date and time in file "last-date";
-- the third line is the update from "last-date";
-- the fourth line is the new current time and date.

AFAICT, htpdate gives an error if you start it on a computer with a dead
battery. Please correct me if I'm wrong, but htpdate does not seem to be
able to recognize old dates such as "Jan. 3 1980, 00:01 hr".

As a workaround, I devised the following:
The fictitious relatively recent date contained in file "last-date" is fed into
the OS by command < date --set="`cat /root/last-date`" >. If file
"last-date" does not exist it is created. Then htpdate updates from that
time and date. That way, it's easier on the girl! ;)

IHTH.
Attachments
Running-htpdate.sh.jpg
Running htpdate.sh in console will give you something like this.
(18.23 KiB) Downloaded 662 times
htpdate-master-181227.pet
(10.02 KiB) Downloaded 393 times
musher0
~~~~~~~~~~
"You want it darker? We kill the flame." (L. Cohen)

musher0
Posts: 14629
Joined: Mon 05 Jan 2009, 00:54
Location: Gatineau (Qc), Canada

#17 Post by musher0 »

Hello all.

Please find attached an update.

Useful info, I think, from the newly edited script.
# Required: htpdate (in the pet archive containing this script)
# --------- hwclock, grep (already in your Pup).
(...)
# The hwclock "check and set" at lines 17 and 28 was needed
# to allow setting the time for the user's entire session.
(...)

# As it is, this script should offer a precision between 0.6 and
# 0.167 of a second.

# I am using the sites of the Parliament of Canada and of Québec's
# National Assembly among other time servers. If checking those sites
# takes too much time because they are too far away, please feel free to
# test and incorporate in the htpdate URL series above other public or
# governmental sites closer to your location.

# This script should prove useful if your computer battery is dead.
The major change in this version is the use of hwclock to introduce some
permanence of the time data at the HW level for your entire session. This
means that htpdate works only once, at the beginning of your session.

Should you change WM's, say, during your session, or temporarily go to the
initial black console to check something, and then come back in X, the time
data will have been preserved. The time in your task bar or in your analog
clock if you have one, should follow the natural increment for the duration.

When you relaunch a session the next day, later in the day, or some other
day, the script will find a mini file with the previous day's time data in it, so
htpdate does not have to figure out the present time starting from zilch. At
boot, a discrepancy will show in your computer clocks, but it will correct
itself automatically after approx. a minute. During the rest of your session,
this script should now offer "smooth sailing".

If the above is not clear, please leave your question below. Comments and
try-out reports also welcome.

Enjoy.
Attachments
htpdate-0.2.pet
(12.65 KiB) Downloaded 355 times
musher0
~~~~~~~~~~
"You want it darker? We kill the flame." (L. Cohen)

musher0
Posts: 14629
Joined: Mon 05 Jan 2009, 00:54
Location: Gatineau (Qc), Canada

#18 Post by musher0 »

Hello again, all.

I continued testing with htpdate, and this version of the script seems
to work the best. Please ditch any previous version.

I moved the script to /etc/init.d, under the filename start_
htpdate-0.3.sh and removed the symlink in /root/Startup.

Scripts in /etc/init.d with the 'start' prefix are run before the ones in
/root/Startup. This gives the script more time to establish the system
time and date, with the added benefit that there is less of a bottleneck
in /root/Startup at boot time.

I reworded the NOTE to include a solution if the script seems to jam
after a power outage or something as drastic.
Please make a mental
note. It is:
# IMPORTANT: After a power outage or any sudden break of the OS,
# this script may fail. To restore normal operation, open a console in
# /root/my-applications/bin and type, exactly:
# `grep htpdate htpdate-0.3.sh | grep -v "#"`
# rerun this script and then reboot.
Unless i get some problematic feedback for you guys, I think this will be
the last version.

Enjoy.
Attachments
htpdate-0.3a.pet
This is potentially the last version, so please remove any previous one.
(15.44 KiB) Downloaded 363 times
musher0
~~~~~~~~~~
"You want it darker? We kill the flame." (L. Cohen)

User avatar
gychang
Posts: 414
Joined: Sat 29 Nov 2008, 20:30
Location: San Diego, CA

Re: How to set time

#19 Post by gychang »

labbe5 wrote:In Trinitydog :

First, i installed ntp.
Then i added ca.pool.ntp.org in /etc/systemd/timesyncd.conf.

The file looks like this :

[Time]
NTP=ca.pool.ntp.org #nearest server for me

Testing in terminal :
timedatectl status

Resulting values :
Network time on: yes
NTP synchronized: yes
RTC in local TZ: no

Finally, reboot.
Now time should be correct.

In Stretchdog with Openbox (from mklive-stretch script & default config)

An alternative way of setting system time.

UTC is used, and there is a mismatch between UTC and local time.

To set the time :
# date -s hh:mm:ss #in Conky, UTC is now set to local time
To set the hardware clock :
# hwclock --systohc --utc

Now local time is ok and is set persistently.

Website of interest related to time zones : http://www.timebie.com/std/edt.php?q=15.5

Further reading :
http://www.putorius.net/2015/04/setting ... hat-7.html
If you are getting wrong time with the USA pacific zone, make sure on initial quick setup screen set "America_Los_Angeles" NOT "US/Pacific*" does not keep proper time.

User avatar
fredx181
Posts: 4448
Joined: Wed 11 Dec 2013, 12:37
Location: holland

#20 Post by fredx181 »

I use this for to geolocate timezone (will set the timezone automatically, according to where you are):
https://github.com/cdown/tzupdate
But note that python is required to have installed.
Just downloaded tzupdate.py :
https://github.com/cdown/tzupdate/raw/d ... zupdate.py
(right click > Save link as)
Put it in /usr/local/bin and make executable:

Code: Select all

chmod +x /usr/local/bin/tzupdate.py
Then create bash script that runs tzupdate.py followed by ntpdate :

Code: Select all

/usr/local/bin/tzupdate.py
sleep 1
ntpdate -s ntp.ubuntu.com
There must be a working network connection before running this.
Best is to let it scan for connection first, so then becomes like this:

Code: Select all

#!/bin/bash
check_network () {
busybox ping -c1 duckduckgo.com 2> /dev/null
    if [[ $? -ne 0 ]]; then
echo "No network connection yet..."
fi

while true
do
busybox ping -c1 duckduckgo.com 2> /dev/null
    if [[ $? -eq 0 ]]; then
echo "Connected"
break
    fi
    sleep 2
done
}
export -f check_network

/bin/bash -c check_network

/usr/local/bin/tzupdate.py
sleep 1
ntpdate -s ntp.ubuntu.com
-----------------------------------------------

Probably similar thing (to get your timezone) can be done without python, just bash, e.g. :

Code: Select all

curl --request GET \
  --url https://freegeoip.app/json/ \
  --header 'accept: application/json' \
  --header 'content-type: application/json'
And then extract the timezone from the output.
EDIT: This should do to extract:

Code: Select all

TZ=$(curl -s --request GET --url https://freegeoip.app/json/ --header 'accept: application/json' --header 'content-type: application/json' | tr ',' '\n' | grep "time_zone" | cut -d ":" -f2|sed s'/.$//;s/"//')
echo $TZ         # display timezone
Fred

musher0
Posts: 14629
Joined: Mon 05 Jan 2009, 00:54
Location: Gatineau (Qc), Canada

#21 Post by musher0 »

Hi Fredx.

Your script does what, besides telling us the timezone? I'm not sure that
I understand. Besides I have a deep hate of all snakes, without
discrimination, even computer ones!

Does your script set the time?
Of course I'm partial to using my own htpdate script! ;)

BFN.
musher0
~~~~~~~~~~
"You want it darker? We kill the flame." (L. Cohen)

User avatar
fredx181
Posts: 4448
Joined: Wed 11 Dec 2013, 12:37
Location: holland

#22 Post by fredx181 »

Hi musher0
Your script does what, besides telling us the timezone?
Running the python script will set the timezone automatically to yours.
And then followed by ntpdate command, the correct time will be set

Code: Select all

/usr/local/bin/tzupdate.py
sleep 1
ntpdate -s ntp.ubuntu.com
But there are more ways to set the timezone and time of course, I just like this one because it's all done automatically.

Fred

musher0
Posts: 14629
Joined: Mon 05 Jan 2009, 00:54
Location: Gatineau (Qc), Canada

#23 Post by musher0 »

Hi fredx.

Thanks for the explanation. htpdate does it in one go.

BFN.
musher0
~~~~~~~~~~
"You want it darker? We kill the flame." (L. Cohen)

User avatar
fredx181
Posts: 4448
Joined: Wed 11 Dec 2013, 12:37
Location: holland

#24 Post by fredx181 »

musher0 wrote:Hi fredx.

Thanks for the explanation. htpdate does it in one go.

BFN.
From what I tested it depends on the timezone correctly set. It doesn't "know" where you are.
E.g. if I set timezone wrong, or to UTC, htpdate goes along with how it is set.
Actually same as how ntp works, it also needs the timezone to be set to your location.

Fred

User avatar
fredx181
Posts: 4448
Joined: Wed 11 Dec 2013, 12:37
Location: holland

#25 Post by fredx181 »

I found that the code to get timezone and sync with ntpdate from here http://murga-linux.com/puppy/viewtopic. ... 17#1019617 using python script tzupdate.py doesn't always work (dependency python-requests required).
So here's a bash-only script that should do (but curl and ntpdate are required to be installed).

Also changed the way to check for network connection (using curl rather than ping, some network admins block ping requests, so using curl is more reliable).

Code: Select all

#!/bin/bash

# automatically get your timezone and set the correct time from internet
# required is a working internet connection and to have ntpdate installed (to sync the time)
# to run at boot, put this script in /root/Startup
# use of curl to check network connection, ping doesn't always work well
 
# check network, wait until there is connection, will stay in loop until there's connection
 case "$(curl -s --max-time 2 -I https://duckduckgo.com | sed 's/^[^ ]*  *\([0-9]\).*/\1/; 1q')" in
  [23]) : ;;
  *) echo "No network connection yet...";;
 esac


while true
do
 case "$(curl -s --max-time 2 -I https://duckduckgo.com | sed 's/^[^ ]*  *\([0-9]\).*/\1/; 1q')" in
  [23]) echo "Connected !"; break;;
  *) : ;;
 esac
sleep 3
done

# get your timezone (from https://freegeoip.app) 
TZONE=$(curl -s --request GET --url https://freegeoip.app/json/ --header 'accept: application/json' --header 'content-type: application/json' | tr ',' '\n' | grep "time_zone" | cut -d ":" -f2|sed s'/.$//;s/"//')
echo $ZONE

# set the timezone
if [ -f "/usr/share/zoneinfo/$TZONE" ]; then
ZONEFOUND=yes
	ln -sf /usr/share/zoneinfo/$TZONE /etc/localtime
	echo $TZONE > /etc/timezone
	Xdialog --title " " --msgbox "$(gettext 'Timezone set to '$TZONE'.')" 0 0 &
else
	Xdialog --title " " --msgbox "$(gettext 'Zone not exist! Cannot synchronize time.')" 0 0
fi

sleep 1

# synchronize time using ntpdate
[ $ZONEFOUND = yes ] && ntpdate -s ntp.ubuntu.com	
Fred

Post Reply