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.
Latest Answers