How to set computer time manually via script

To set computer time manually in FreeBSD is basically easy to do, set date as root with two digits every section

Sections are year, month , day and minute.

An another way is to use a script which makes it with graphical dialogs.

If you give illegal numbers, time does not change.

Of course an another way to set time is to use NTP time.

When using NTP time, the offset cannot be too large, because if offset is too large, NTP refuses to accept the time.

===============================

Set time manually script (requires sudo, yad and xterm packages installed and wheel and sudo groups set)

sh:
#!/bin/sh
##
## RJP 18.7.2025
## Set time manually in FreeBSD
## Packages yad, sudo and xterm are needed
# user must be in the wheel group and wheel and sudo must be activated in sudoers file (sudo visudo)
## Free to use and modify
#
touch ~/.Xresources
echo ' ! Use a truetype font and size.
xterm*faceName: Monospace ' | grep -xFv -f $HOME/.Xresources >> $HOME/.Xresources
echo 'xterm*faceSize: 14 ' | grep -xFv -f $HOME/.Xresources >> $HOME/.Xresources
xrdb -merge ~/.Xresources

YEAR=`yad --center --width=100 --height=100 --text-align=center --title="SET YEAR MANUALLY" --text="PUT YEAR TO" --entry --entry-label=BOX --entry-text=""`
MONTH=`yad --center --width=100 --height=100 --text-align=center --title="SET MONTH MANUALLY" --text="PUT MONTH TO" --entry --entry-label=BOX --entry-text=""`
DAY=`yad --center --width=100 --height=100 --text-align=center --title="SET DAY MANUALLY" --text="PUT DAY TO" --entry --entry-label=BOX --entry-text=""`
HOUR=`yad --center --width=100 --height=100 --text-align=center --title="SET HOUR MANUALLY" --text="PUT HOUR TO" --entry --entry-label=BOX --entry-text=""`
MINUTE=`yad --center --width=100 --height=100 --text-align=center --title="SET MINUTE MANUALLY" --text="PUT MINUTE TO" --entry --entry-label=BOX --entry-text=""`

echo "$YEAR" > /tmp/year.txt
sed -i '' 's/20//' /tmp/year.txt

if [ $MONTH -lt 10 ] ; then
    echo "0 $MONTH" > /tmp/month.txt
else
    echo "$MONTH" > /tmp/month.txt
fi

if [ $DAY -lt 10 ] ; then
    echo "0 $DAY" > /tmp/day.txt
else
    echo "$DAY" > /tmp/day.txt
fi

if [ $HOUR -lt 10 ] ; then
    echo "0 $HOUR " > /tmp/hour.txt
else
    echo "$HOUR " > /tmp/hour.txt
fi

if [ $MINUTE -lt 10 ] ; then
    echo "0 $MINUTE " > /tmp/minute.txt
else
    echo "$MINUTE " > /tmp/minute.txt
fi

echo " $(cat /tmp/year.txt) $(cat /tmp/month.txt) $(cat /tmp/day.txt) $(cat /tmp/hour.txt) $(cat /tmp/minute.txt) " > /tmp/time.txt
tr -d ' ' < /tmp/time.txt > /tmp/time2.txt
echo "date $(cat /tmp/time2.txt)" > /tmp/date.txt

yad --form --columns=1 --width=500 --text="Set time manually" --title="Set time manually" \
\
--field="Set time manually ":fbtn "xterm -hold -e 'sudo $(cat /tmp/date.txt)'"  \
--button=Exit:1
sleep 5
cd /tmp && rm year.txt month.txt day.txt hour.txt minute.txt time.txt time2.txt date.txt

 
Back
Top