Creating a ticking watch without referencing to the systemclock

First of all, I wish everyone a happy new year.

Secondly, I tried to build my own clock without referencing to the system clock.

I can write the program in several ways, but it don´t won´t work.

Do you have an clue?

Thanks, BR Bashi

import time
from tkinter import *
 
# Objekt der Klasse Tk erstellt und Fensterüberschrift
fenster = Tk()          
fenster.wm_title("Uhrzeit")
 
# Uhrzeit wird über ein Label angezeigt und mit pack im fenster gezeigt
uhr = Label(master=fenster,
            font=('Arial',30),
            fg='blue',
            width = 15,
            height = 3)
uhr.pack()
 
zeit = ''
 
# Abfrage der Zeit vom laufenden Computer
# mit config wird das Label neu beschriftet
# mit after wird nach 0,2sek die Funktion tick neu aufgerufen
 
def tick(hour,min):
    global zeit
    if len(str(hour))<2:
        hour=f"0{hour}"
    if len(str(min))<2:
        min=f"0{min}"
    print(hour,min)
    neuezeit = f"{hour}:{min}"
    print(neuezeit,zeit)
    if neuezeit != zeit:
        zeit = neuezeit
        uhr.config(text = zeit)
    neuzeit=tick23(hour,min)
    NeuStd=int(neuzeit[:2])
    NeuMin=int(neuzeit[3:])
    time.sleep(5)
    uhr.after(tick(NeuStd,NeuMin))

def tick23(x,y):
    if int(x)<24:
        if int(y)==59:
            x=str(int(x)+1)
            y=0
        else:
            y=str(int(y)+1)
    else:
        x=0
        y=0         
    if len(str(x))<2:
        x=f"0{x}"
    if len(str(y))<2:
        y=f"0{y}"
    return f"{x}:{y}"     
tick(14,30)
uhr.mainloop() # Programm startet

I don’t understand why this is useful.

The program can only work if it has some idea of how much time has passed.

It can say to wait for a certain amount of time (e.g. with time.sleep), but it does this by asking the system to pause it. And then, it doesn’t know if the pause was exact, and it also doesn’t know how much time it has spent on its own calculation.

So checking the system clock is the only thing that makes sense, in order to know the current time. That’s why there is a system clock.

But, I don´t want use the system clock.

Because, if there is no possibility to write this program in python, how will work the system clock.

Conclusion: When the system clock has a program, we can reproduce it in python.

Or??

Not really. Every clock application you see on your system is referencing the same ticking clock. In the pre-internet-everywhere era, when most home computers had to operate entirely on their own, it was vital to have a clock that kept good time even with the computer powered off, without any sort of external reference; and even today, that feature is serving us well, with only minor adjustments needed (NTP is usually just going to slightly tweak the clock to bring it in sync, rather than making visual changes).

So for that to work, you need something that ISN’T a program. You need a dedicated timer chip, one that requires very little power (so it can run off batteries in a powered-off computer), but still is pretty accurate. Ultimately, one way or another, that’s going to maintain some sort of ever-increasing clock value on the motherboard, which the operating system can both query and update, and which then takes care of the actual ticking work.

Your “after” method call here is hiding a call to an OS-provided time-of-day query, as will any kind of “sleep for one second” call, or anything of that nature. It always will, because it’s the only way to be reliable.

The system clock isn’t actually a program, it’s probably an off-the-shelf chip with a little sliver of quartz in it as an oscillator. If done right, all the computer actually has to do is count - which electronic circuits are pretty good at doing!

But how works the timerchip? On the timerchip must be also a program, or?

I know the easy way is to use the os clock, but I would like to reproduce this timerchip.

For the first, it´s for me enough, that this works, when the program starts.
And the second step (far away in the future), to run the program in the background and reference all apps to this time.
Thanks, BR Bash

Modern systems are inevitably more complicated than this, but ultimately, the way a timer chip will most likely work is with some sort of crystal oscillator. Here’s a great explanation from Steve Mould on the subject:

You can’t reproduce that in software. It’s not just running code; it’s making use of the physical and electromagnetic properties of the quartz.

Ok - But the bios or DOS from a PC must somehow communicate with this timerchip?

Yes. Back when I was programming in assembly language, you could use I/O ports to control it, and it would send CPU interrupts at regular intervals. Obviously you won’t be writing code like that from userspace, but that’s what your OS is doing under the hood somewhere.

Ok - Do you have any additional information about this?