How does the time
function know the current time of the PC? I know how computers know the time. But how does the C++ time function implemented? Does it use the Internet to sync with a time server as the computers do? Or does it use the internal clock of the computer?
Asked
Active
Viewed 80 times
0

Ngoc Dat
- 15
- 5
-
It asks the OS, and the OS might use an NTP (time server) to update itself. If there is no internet connection (and the computer is powered off) the clock is run from a battery to keeps it up to date – Pepijn Kramer Jul 23 '22 at 13:20
-
2There's a tiny bit of crystal inside every computer, that vibrates at a known frequency. There's also a tiny leprechaun that sits next to the crystal and counts how many times the crystal vibrates. There's also a battery that keeps the leprechaun awake when the computer is asleep, and/or the leprechaun asks an Internet server when your computer wakes up what's the current time, then the leprechaun simply counts the crystal vibrations and thus can keep track of the current time. `time` simply asks your tiny leprechaun what time it is, and gets an answer. – Sam Varshavchik Jul 23 '22 at 13:26
2 Answers
1
C++ asks your device(computer) about the time details and it doesn't require the internet you can check it by disconnecting your device's internet and then running the function

Veer-Khatri
- 35
- 6
1
The function returns the current calendar time, which the program got by asking the computer what the current time is.
The function doesn't require internet, just your computer's internal clock :)
If you want to learn more on this topic, I highly recommend reading
Programiz's C-Time.

Luchik3333333
- 76
- 5