81
$\begingroup$

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?

$\endgroup$
2
  • 4
    $\begingroup$ Is this question really on topic for this site? It seems more appropriate for Super User to me. $\endgroup$ Commented Mar 28, 2016 at 13:34
  • 5
    $\begingroup$ @IlmariKaronen This does seem to count as question about "computer architecture", if I am understanding the term correctly. $\endgroup$ Commented Mar 29, 2016 at 2:09

3 Answers 3

114
$\begingroup$

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.

$\endgroup$
8
  • 1
    $\begingroup$ Can 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? $\endgroup$
    – cat
    Commented Mar 26, 2016 at 0:54
  • 1
    $\begingroup$ Yes. 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. $\endgroup$
    – Chris Nava
    Commented Mar 26, 2016 at 3:38
  • 1
    $\begingroup$ The quartz crystal is not the special hardware device that keeps track of time (although it's one component). $\endgroup$ Commented Mar 26, 2016 at 5:32
  • 1
    $\begingroup$ @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). $\endgroup$
    – Ruslan
    Commented Mar 26, 2016 at 19:00
  • 1
    $\begingroup$ I'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). $\endgroup$ Commented Mar 26, 2016 at 20:54
15
$\begingroup$

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.

$\endgroup$
5
  • 3
    $\begingroup$ Many 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. $\endgroup$ Commented Mar 27, 2016 at 19:49
  • 4
    $\begingroup$ @JamesSnell That will however mess up time stamps in logfiles as well as any file modified during boot before NTP is started. $\endgroup$
    – kasperd
    Commented Mar 28, 2016 at 11:39
  • $\begingroup$ @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. $\endgroup$ Commented Mar 28, 2016 at 12:47
  • 2
    $\begingroup$ @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. $\endgroup$
    – kasperd
    Commented Mar 28, 2016 at 13:09
  • 1
    $\begingroup$ @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. $\endgroup$
    – yoyo_fun
    Commented Apr 15, 2016 at 10:41
2
$\begingroup$

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

$\endgroup$
1
  • $\begingroup$ Interesting. So how does Windows determine the current precise time (to microseconds) to respond to a call from a program (e.g time.time() in python) and how often and how does it update it's current value for the time? $\endgroup$ Commented Oct 1, 2018 at 10:57

Not the answer you're looking for? Browse other questions tagged or ask your own question.