There is a system clock attached to every modern computer, and for internet-connected operating systems, they are frequently synchronized with atomic clocks run on the internet, through a service called ‘network time protocol’. Programs running under an operating system will ask the OS for the time using a system call.
There is a type of rock which when hit with electricity begins to vibrate. And it always vibrates with the same speed. This tiny rock (crystal) is part of every computer.
Programmers pre-set how many times is this crystal supposed to vibrate in a second. So programs can just count the vibrations and know when a second has passed.
The same way electric watches do – a crystal oscillator.
When you pass a voltage across the crystal, it vibrates in a very very specific frequency. Millions of times per second, depending on the crystal. The computer passes a tiny charge through the crystal and then counts how many times it vibrates, since it knows how many times it vibrates per second it can tell time.
This is stored in a separate component on the motherboard (generally), known as ‘the clock’. That way all programs have access to the same time measurement.
There is a chip inside that has a crystal that pulses a certain number of times per second, usually in the millions or billions. That pulsing is connected to an interrupt, which is connected to a counter. When the computer turns on, the crystal starts counting, and it knows how many counts equals 1 second.
In order to store time between power turning on and off, the memory of what time it is needs to be saved. Most computers (motherboards) have a small battery that keeps a circuit on even when the computer is off to keep time.
If there is no saved time, the device can connect to the internet (or another device) and ask “what time is it?” And then keep count from there.
For the five year old:
Computers have a clock inside them.
For the 10 year old:
Computers have a clock inside them and there is a special function that programs can call to get the current time.
For the 14 year old:
When programming your application you can call a function to get the the current or elapsed time. You can also program it so it asks the computer to “call it back” when a certain amount of time has elapsed.
The computer keeps track of the current time in 2 ways. First there is a special chip that keeps track of the time, and it has a small battery which keeps power to it. The computer references this when it’s turned on. This is why it knows the current time even when power is removed. Second, when connected to a network or internet, it sends a request for the current time, and the closest time server responds with the current time.
For the 16 year old:
(Above +) Computers also have a special circuit called a “clock” which operates at a know frequency. The clock “ticks” at the speed in which a computer can process instructions at the lowest level (CPU instructions, not application code). Because the frequency is known, the number of ticks per second is also known. Most CPUs and microprocessors use a quarts crystal, which vibrates at a known frequency, to generate these ticks.
Latest Answers