r/AskComputerScience • u/sweetgracie76 • 2d ago
How does a computer figure out how long it was turned off?
Something I'd never really thought about before. I always figured it would just hook up to the internet and sync its time from there, but I recently brought a laptop to a spot with no internet and it still displayed roughly the right time.
Sure, the laptop wasn't shut down, just asleep, but I still don't get how that would let it keep tracking time.
Does the processor tally up clock cycles? That sounds like a huge amount of effort and a waste of resources. And anyway, how would the processor know how clock cycles map to actual time? Is that baked in?
72
u/jimmystar889 2d ago
There's a 32.768kHz crystal which is powered by a small coin cell battery. When the computer is off time is still being kept, it's literally a mini quartz wrist watch on the motherboard
9
u/gnosnivek 2d ago
In short, every computer has a small quartz watch inside of it. I usually hear it referred to as the "hardware clock" but I find that term confusing. Wikipedia suggests the term "real-time clock" (RTC), which is probably a better name for it.
When you set the time on your computer, it also sets the RTC, just like setting a time on a watch. From then on, the computer mostly uses the RTC for telling the time, and only occasionally syncs that clock with the internet. These RTCs aren't extremely accurate and will usually drift few seconds each month, so if you keep your system offline for over a year (or, like has happened to me, your network time syncs are being blocked by a firewall and you don't notice), the time the computer displays starts may start to be off by a minute or two.
Random fun fact: on a lot of desktop computers, in order to make sure the RTC (along with other things) survive a power outage, there's actually a small coin battery on the motherboard, just like you'd have in a watch. This keeps the RTC running even when you unplug the desktop from the wall. If you unplug the desktop and then also pull out this battery, the computer will have no idea what time it is when you power it back up.
4
u/green_meklar 2d ago
Computers have tiny clocks inside them, powered by tiny batteries. The clock keeps running even when the computer is turned off and unplugged.
With that said, if you connect to the Internet, the computer can also fetch the current time from a time server. Most everyday PCs and phones calibrate their time from time servers and thus avoid falling out-of-sync.
If the clock battery runs out of charge, or if you remove it, the computer will indeed forget what time it is and might say that it's January 1 1970 or something like that.
9
u/snarfmason 2d ago
In the olden days PCs tracked time by CPU clock cycles, but it obviously couldn't do that when powered off.
Modern (and by modern I mean in the last 30 years) PCs have real time clocks and those RTCs have battery power. Basically your computer also includes the technology of 90s quartz crystal wrist watch.
3
u/Vizeroth1 2d ago
It has an internal clock on a battery. Here’s an article on changing the battery that briefly gives a little more detail:
https://www.corsair.com/us/en/explorer/gamer/gaming-pcs/what-is-a-cmos-battery-and-how-do-you-replace-it/
1
u/MasterGeekMX BSCS 2d ago
It has a small battery inside that keeps the clock advancing. It is also the one that keeps alive the memory that stores the firmware settings.
It is usually a CR2032 coincell battery, but some laptops now are using the regular battery for that.
1
u/sugarw0000kie 2d ago edited 2d ago
If you don’t have a hardware clock like raspberry pi’s for example (tiny single board computer, you can add on hardware clock separately) then when it’s off it doesn’t keep time anymore. It doesn’t have a battery to keep time when it’s off.
So when you turn it back on the system time will be wrong at first and frozen in time whenever it was turned off. It will then try to get the time from an external network time server and re-sync its clock that way. But if it had no internet connection it has no way to know what time it really is so it stays stuck at the last time it knows. So you can take the difference in the old time it starts up with and the current time to figure out how long it was offline. It’ll make a note in logs you can see that it jumped ahead some amount of time.
Like another poster said it’s syncing external time to its real time clock (RTC) that way, and if there’s no hardware clock it relies on that. It will keep syncing occasionally since external time servers are more accurate than quartz on your machine since they’re synced against atomic clocks instead of quartz.
1
u/duane11583 2d ago
Often there is a very low power device that runs from a battery when the computer is off or unplugged
This is why your computer often has a small coin cell type battery some where on the circuit board
In some ways it is like a built in wrist watch
When the computer turns on later it can ask that device for the current time
More technically that watch runs from a crystal that operates at 32768 pulses per second
Why that number? It is a perfect multiple of 2 if you divide this by 2 again and again you get exactly 1 pulse per second if you count seconds you know what time it is
1
u/CounterSilly3999 2d ago
There are even two timers. Timer tick generator is an essential part of the CPU, specifically independent from the processor cycles. Time-sharing in multitasking is based on it. Besides the built in real time clock on the motherboard.
1
u/Doug2825 2d ago
As other people have mentioned: there is a clock with a small battery, the same battery that keeps the volatile storage with your bios settings powered.
Interpreting the time is more complicated. If you use a normal clock to measure duration you'll get a negative duration whenever there is a time change, so you need to get the time monotonicly.
See the following for a list of ways to interpret the clock days https://man7.org/linux/man-pages/man2/clock_gettime.2.html
If you need very high precision you'll typically use a GPS time source. High precision (microsecond) clocks are very hard to make. However GPS relies on broadcasting a very precise time so you can use those clocks instead.
1
u/Kyle_2099 2d ago
There's no magic, there's just a clock inside the computer, running off it's own little battery. When you turn the computer on, it asks the clock what time it is.
1
u/bothunter 2d ago
There's a clock in the computer. Also, it can ask the internet for the current time after it boots up.
1
u/grawmpy 2d ago
Because there is a little battery that keeps the motherboard alive enough to keep the clock still ticking. This is why the time is still the same each time you start up and you don’t have to set it every time you start your computer.
1
u/Far_Oven_3302 2d ago
Real time clock chip powered by a battery, holds a bunch of bios info too
1
u/archlich 2d ago
Since no one has actually addressed the crux of your question… when your computer goes to sleep it marks the current time to a log. When you wake up it marks the current time to a log. You subtract the sleep time from current time and you get the amount of time it slept.
1
u/OatmealCoffeeMix 2d ago
Your laptop's got some very complex computational stuff in it. Shoving in the tech inside a cheap Casio watch is par for the course.
1
u/KSP_HarvesteR 2d ago
Note down the date/time the computer was last powered down, on startup, subtract that from current date/time, you get the time it spent off. No tracking at all.
1
u/Single-Virus4935 1d ago
Built in Real Time Clock.
1
u/Neat-Medicine-1140 1d ago
in addition to the clock everyone is mentioning, its pretty easy to record shutdown, record startup, subtract.
0
u/pokeybill 2d ago
These answers are correct for home PCs, but servers will use NTP - the Network Time Protocol based on Cesium atomic clocks at NIST. This is used to correct the system time, which can suffer from jitter