How do computers keep track of time?

53

17

How are computers able to tell the correct time and date every time?

Whenever I close the computer (shut it down) all connections and processes inside stop. How is it that when I open the computer again it tells the exact correct time? Does the computer not shut down completely when I shut it down? Are there some processes still running in it? But then how does my laptop tell the correct time when I take out the battery (and thus forcibly stop all processes) and start it again after a few days?

tatan

Posted 2016-03-25T17:33:09.713

Reputation: 379

2

Is this question really on topic for this site? It seems more appropriate for [su] to me.

– Ilmari Karonen – 2016-03-28T13:34:50.533

2@IlmariKaronen This does seem to count as question about "computer architecture", if I am understanding the term correctly. – PyRulez – 2016-03-29T02:09:33.643

Answers

84

Computers have a "real-time clock" -- a special hardware device (e.g., containing a quartz crystal) on the motherboard that maintains the time. It is always powered, even when you shut your computer off. Also, the motherboard has a small battery that is used to power the clock device even when you disconnect your computer from power. The battery doesn't last forever, but it will last at least a few weeks. This helps the computer keep track of the time even when your computer is shut off. The real-time clock doesn't need much power, so it's not wasting energy. If you take out the clock battery in addition to removing the main battery and disconnecting the power cable then the computer will lose track of time and will ask you to enter the time and date when you restart the computer.

To learn more, see Real-time clock and CMOS battery and Why does my motherboard have a battery.

Also, on many computers, when you connect your computer to an Internet connection, the OS will go find a time server on the network and query the time server for the current time. The OS can use this to very accurately set your computer's local clock. This uses the Network Time Protocol, also called NTP.

D.W.

Posted 2016-03-25T17:33:09.713

Reputation: 83 008

1Can an OS set the BIOS time? What I mean is, if I forcibly reset the hardware clock, then boot and connect to NTP or set the clock myself, will the BIOS recognise this? – cat – 2016-03-26T00:54:26.500

1Yes. Changing the OS's system time sets the real time clock a.k.a. the BIOS time. Internally the clock is just counting the number of seconds (or milliseconds) since the Epoch (usually January 1, 1970 00:00), so it's not doing much work and can be powered by a coin size battery for years. Higher level functions in the OS do the job of converting that count to a date/time to display. – Chris Nava – 2016-03-26T03:38:41.150

The quartz crystal is not the special hardware device that keeps track of time (although it's one component). – immibis – 2016-03-26T05:32:10.947

Does the similar reason follow for-Why I am able to switch on my switched off phone by clicking the power button while it is still shut down? – tatan – 2016-03-26T06:41:37.540

1@tatan no, the phone is powered all that time by its battery. It's mostly the same as you are able to turn on your PC by pressing its power button when the PC is supposed to have been shut down (but not disconnected from wall socket). – Ruslan – 2016-03-26T19:00:35.590

1I'd rephrase the last paragraph, changing computers to OSs. Any PC that connects to Internet might not to, and vice versa - it is OS job to do this, not PCs(implying hardware). – MatthewRock – 2016-03-26T20:54:32.397

@tatan it's slightly similar in that there's a part that's never really turned off that recognizes that the power button has been pushed. Very few devices these days have genuine power switches that completely remove power. – hobbs – 2016-03-28T00:01:29.077

The CMOS battery also comes in handy, if you forget you BIOS password ;) – wawa – 2016-04-20T09:41:46.610

12

If you remove the battery on the motherboard then the computer wouldn't have any way to tell the time.

This is also the case with mobile phones. If you let a phone discharge and then not recharge it for more than a few weeks it will also "forget the time" because the small auxiliary battery is discharged completely and nothing is powering on the real-time clock.

You could try to power on an old mobile phone if you have one and check it yourself to see that it "forgot the time".

This is how the battery looks. https://yandex.com/images/touch/search?text=mobo%20battery&source=tabbar

I had to buy one a few times when mine started to last less then a day. I had to configure the clock everytime i turned on the PC.

yoyo_fun

Posted 2016-03-25T17:33:09.713

Reputation: 521

2Many operating systems now use NTP (Network Time Protocol) and can use that to reset themselves automatically on startup, so they don't actually need the built in clock. – James Snell – 2016-03-27T19:49:44.167

2@JamesSnell That will however mess up time stamps in logfiles as well as any file modified during boot before NTP is started. – kasperd – 2016-03-28T11:39:02.787

@kasperd - timestamps really aren't as important as they used to be as many systems use other metadata (versioning). Look at boot logs from systems which lack a RTC like routers or the raspberry pi and they log when the time was updated (and what to). It might be more important in disconnected systems, but that's becoming less important over time. – James Snell – 2016-03-28T12:47:28.770

1@JamesSnell I have several single board computers capable of running a standard x86 operating system, but they lack a battery backed RTC. In the end I had them run an absolute minimal network stack from within a ramdisk in order to run `ntpdate' before any disk access would take place. – kasperd – 2016-03-28T13:09:03.287

@JamesSnell Yes but laptops and tablets connect to wifi after they boot (at least most of them do that) and they do not have ethernet connection, so battery is still in use in almost all computers. Also desktop computers do not have internet all the time either. Some people close their routers at night or just do not use internet all the time if they want to play games or watch a movie and don't turn the router on. Not everyone wants to be connected to internet all the time. – yoyo_fun – 2016-04-15T10:41:58.473

1

When you start Windows, it gains direct access to the memory of the Real Time Clock (RTC) and uses its date and time values to set the computer date and time. Timer interrupts maintain the computer time when Windows is running. A Time Daemon in Windows runs approximately once each hour after the Windows starts. The Time Daemon compares the time in Windows with the time in the RTC. If the two times are more than one minute apart, Windows changes the time and date to match the RTC. You cannot change the time interval for the Time Daemon to run.

If you use a time synchronizing service, such as the TimeServ.exe tool included with the Windows NT 4.0 Resource Kit, the tool updates the time in Windows and the computer's RTC. If the Windows Time Service runs on a Windows 2000 based-computer, the Time Daemon in Windows cannot run approximately one time each hour after the Windows starts.

For more details visit this link:http://msdn2.microsoft.com/en-us/library/ms724936.aspxhttp://msdn2.microsoft.com/en-us/library/ms724942.aspx

Budhathoki Bijaya

Posted 2016-03-25T17:33:09.713

Reputation: 11