r/AskComputerScience • • 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?

49 Upvotes

62 comments sorted by

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

11

u/svick 2d ago

How come the battery running out was a problem in the past, but is not anymore? Just better batteries?

35

u/garfgon 2d ago

That and better circuits which use less power. Even in the past the BIOS batteries would last for many years.

8

u/blackasthesky 2d ago

When I jobbed in a computer repair shop, people always said these would last eight to ten years.

10

u/Cheap-Chapter-5920 2d ago

10 years is essential the shelf life of the battery, even if not plugged in. If I design something I just need to be comfortably above that target. New smoke detectors, even TPMS are doing this now, 10 year battery and buy a new device when it dies.

3

u/nixiebunny 2d ago

Many computers use a rechargeable lithium coin cell instead of a single-use one. It can be small because it only needs to keep the clock running until the next power cycle.

2

u/PhotoFenix 2d ago

Older drives didn't declare their configuration, that had to be set in BIOS. When they memory cleared after a dead battery we'd have to enter the drive characteristics (sectors, cylinders, heads, etc) to work properly.

I remember having to do this for several days in a row when our CMOS battery went out until we could get a new one.

2

u/everyonemr 2d ago

Old computers stored bios settings in volatile memory that needed constant power or the data is lost.

Now we use flash memory, but some computers emulate the old behavior and wipe the settings if you pull the battery.

2

u/rake66 2d ago

They emulate it because for a lot of them it's the only factory reset for BIOS

1

u/Critical-Echo-923 1d ago

it pisses me off when there's no cmos reset pin and i have to take the gpu out just to reset the bios

2

u/penguindab0 1d ago

It can still be a problem for older devices. I had to replace one last year (litttle over 6 year old desktop PC). I have the habit of turning off the power strip. Well, every time I did, my PC forgot which disk to boot from.

2

u/MikeUsesNotion 2d ago

Depending on what time period you're comparing to, it's now the norm for the OS to sync clocks with a time server when booting up. Before that you had to adjust your computer clock like you would for your car/oven/microwave.

2

u/tehbeard 2d ago

IIRC, very old motherboards used an SRAM (static ram) chip for settings. Since it's RAM, you need to keep it powered to keep the data onboard.

Eventually it moved to flash memory (kinda similar to what's in an SD card) so the power requirement was gone.

This did remove the old method of resetting the BIOS (removing the battery for a minute) so you ended up with dedicated buttons for it now.

1

u/erroneousbosh 2d ago

The batteries have always lasted about ten years, but newer RTC chips use even less power.

1

u/thetrivialstuff 2d ago

It's still a problem today with Dell desktop workstations. If you leave like 20 Dell computers unplugged for a couple years, about 3 of them will fail to start because they somehow corrupted their own BIOS as the battery ran down.

1

u/Short-Bee1550 1d ago

Besides everything else people mentioned, in the past computers had physical power switches that cut off all power. This is in the days when in Windows 95 you went to Start->Shutdown, then it would display “It is now safe to turn off your computer”. Then you use the big chunky switch and it shuts off mains, and the RTC has to use the battery.

With ATX power supplies, it is a soft turn off, controlled either by a momentary switch or the operating system. This is only effectively a standby state, and the standby power supplies the RTC clock.

And since people leave their computers plugged in, the battery is almost never used. Similar laptops usually also have a CR2032, but 99.9% of the time use the main battery.

1

u/Critical-Echo-923 1d ago

it's still a problem, but now we have better batteries and less current usage

i help ppl with this problem all the time, i basically have cmos batteries in my toolkit all the time

lots of weird shit that have no other apparent cause can just be fixed by putting a new one(and yes is the battery not just resetting bios)

my latest was an old man who i've donated a pc to, after describing the problem(strange startup issues that made no sense ) my first question was do you keep the pc unplugged ? yes, don't do that anymore bring it in to replace your battery

1

u/CowBoyDanIndie 2d ago

Older computers used to physically disconnect the power when you flipped the switch. Modern computers provide low power anytime they are plugged in. If you unplug the computer from the wall when you aren’t using the cmos battery will drain faster. Also probably slightly better batteries and circuit.

1

u/snarfmason 2d ago

"it's literally a mini quartz wrist watch on the motherboard" 😂 I said pretty much the same thing.

-4

u/PaulRudin 2d ago

grammar police: it's not *literally* a wrist watch ...

15

u/garfgon 2d ago

grammar police internal affairs: "It's literally a mini quartz wrist watch on the motherboard" is grammatically correct. It may not be factually correct, but that's not a grammar police issue.

13

u/wbrameld4 2d ago

Vocabulary police to the grammar police: You're out of your jurisdiction.

5

u/mkp666 2d ago

Pedantry police: nice try grammar boys, we’ll take it from here.

4

u/xXgreeneyesXx 2d ago

actually, it is literally the same as whats in some wrist watches. Digital clocks, obviously, but its very similar to even electromechanical quartz movements, just with a counter instead of a motor.

-2

u/PaulRudin 2d ago

But that doesn't make it a wrist watch.

1

u/sububi71 2d ago

Thank you for your service!

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.

1

u/fllthdcrb 1d ago

From then on, the computer mostly uses the RTC for telling the time

To be clear, a modern OS will typically have its own system clock that runs independently of the RTC, by e.g. counting ticks of a timer interrupt. This is important for multiple reasons. One is precision: the RTC only gives seconds (even though it obviously internally keeps track of time much more finely), but an OS may provide nanoseconds. Another is accuracy, since as you noted, the RTC drifts. Although the system clock is liable to drift as well, the OS can easily correct for this drift, if given appropriate information, something that is not available in an RTC. It can even measure the RTC's drift, in order to correct it at future boots and get a more accurate time even before asking a time server, or if without an Internet connection.

only occasionally syncs that clock with the internet.

Depends on OS and configuration. In some, it will poll one or even several server(s) every few minutes, and use statistics to decide on reliability before adjusting the system time and tick rate.

1

u/emmfranklin 2d ago

And removing this battery well clear the bios too

5

u/sububi71 2d ago

Important: it doesn't clear the BIOS, it clears the BIOS SETTINGS.

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.

2

u/zazathebassist 2d ago

it might say 1970, but a lot of BIOSes and bits of hardware are written knowing that there's no way it could possibly be before [manufacture year] so it defaults to like Jan 1 2011 or something

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.

11

u/FriendlyTechLead 2d ago

Try 40 years; PCs in the mid-80s had the same coin-cell battery backed clock.

3

u/xenomachina 2d ago

In fact, this is a problem for retro computer and game console collectors: the batteries eventually die, and sometimes leak. Coin cells usually just go flat, but many older boards used rechargeable barrel batteries soldered to the motherboard, which are notorious for leaking electrolyte that corrodes traces and nearby components. They also tend to be harder to remove.

1

u/Objective_Mine MSCS, CS Pro (10+) 2d ago edited 2d ago

My PC AT clone has a battery integrated into the RTC chip that's soldered on the motherboard. It's not even rechargeable, as far as I know. No leaking problems so far but at some point they just run out.

1

u/alaricsp 2d ago

In the early 2000s I would buy Sun IPXes cheap on eBay because they'd died, and replace the (socketed) chip with the clock+config NVAM+battery

2

u/snarfmason 2d ago

Hah. I forgot that 30 years ago is only the mid 90s. 🧑‍🦲

1

u/alaricsp 2d ago

Yep; I believe it was added in the PC AT. MY original IBM PC asked me the current date and time on boot to set the clock, but I later bought an I/O card because I needed a serial port for a mouse, and it had a battery backed real time clock on it - so I could run a small program from autoexec.bat that set the system software clock from the hardware RTC!

1

u/snarfmason 2d ago

I'm not sure which the first one was but that sounds about right. As someone else pointed out, more like 40 years ago. But 1996 can't possibly 30 years ago ...

1

u/Tall-Introduction414 2d ago

Yeah, but not all of them did. A real-time clock was often an upgrade card in the XT era.

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/mugaboo 2d ago

It's amazing how a small amount of "simple" (relatively) tech can solve hard problems.

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.

2

u/Some-Struggle5052 2d ago

Quite a lot of laptop don't use a small battery for that anymore, the main battery is almost always enough.

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/Discordbothost 12h ago

There's a neat way to see this on Linux: read both CLOCK_BOOTTIME and CLOCK_MONOTONIC before and after suspending. Subtract their deltas and you get roughly the time spent asleep.

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/archlich 2d ago

This is also how timers work. You set an alarm for a specific time, when current time > alarm time, ring.

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

1

u/silasmoeckel 2d ago

Regular desktops and typical servers both have RTC in them and outside of a very strange OS all sync their time to NTP or similar (and save that back to the RTC).

PTP is the new hotness in the server space but it's a whole stack of things to make it work.