TIP: Click on subject to list as thread! ANSI
echo: rberrypi
to: ALL
from: DENNIS LEE BIEBER
date: 2019-11-02 21:17:00
subject: Re: Python - set current

On Sat, 2 Nov 2019 20:15:21 +0100, "R.Wieser" 
declaimed the following:

>But ... Really no method to do it from within python (even if I would have
>to use "sudo python3 {script.py}")?    Bizarre.
>
 Why bizarre?

 Okay -- it appears the C runtime supports a settimeofday()... So using
the Python ctypes library/module should permit you to access that function.

 Python isn't really aimed at being a /systems/ language, but more an
application/script language -- and user applications normally don't need
access to functions that can change the system environment.

 I presume https://docs.python.org/3/library/time.html#functions
"""
time.clock_settime(clk_id, time: float)

    Set the time of the specified clock clk_id. Currently, CLOCK_REALTIME
is the only accepted value for clk_id.

    Availability: Unix.

    New in version 3.3.
"""
doesn't affect the desired clock. (I don't know how often ntp does updates)

pi@rpi3bplus-1:~$ sudo python3
Python 3.7.3 (default, Apr  3 2019, 05:39:12)
[GCC 8.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import time
>>> time.clock_gettime(time.CLOCK_REALTIME)
1572743513.3025973
>>> time.time()
1572743525.0147276
>>> time.time()
1572743533.9680963
>>> time.ctime()
'Sat Nov  2 21:12:42 2019'
>>> def fivemin():
...     print(time.ctime())
...     time.clock_settime(time.CLOCK_REALTIME,
time.clock_gettime(time.CLOCK_REALTIME) + (5 * 60.0))
...     print(time.ctime())
...
>>> fivemin()
Sat Nov  2 21:14:48 2019
Sat Nov  2 21:19:48 2019
>>> exit()
pi@rpi3bplus-1:~$ date
Sat 02 Nov 2019 09:15:01 PM EDT
pi@rpi3bplus-1:~$

The program saw a 5-minute time jump, but by the time I exited and checked
from the command line it was back to normal.



--
 Wulfraed                 Dennis Lee Bieber         AF6VN
 wlfraed@ix.netcom.com    http://wlfraed.microdiversity.freeddns.org/

--- SoupGate-Win32 v1.05
* Origin: Agency HUB, Dunedin - New Zealand | FidoUsenet Gateway (3:770/3)

SOURCE: echomail via QWK@docsplace.org

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.