Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
308 views
in Technique[技术] by (71.8m points)

windows - How does the computer know the present date and time?

Please pardon my ignorance about this. I may have read this a long time ago, many years ago in Charles Petzold's Programming Windows, 4th Ed. book, and I must have forgotten it all. I remember the very first chapter had something to say about timer resolutions and clock-speeds of the CPU but I forget the details. I think in that he answers this question. But I could be imagining things. It's been a long while.

While there is a related question here, the answer is not to my satisfaction as it does not answer the finer nuances I am interested in, which are mentioned below.

It'll be nice to have someone provide a systematic answer to this question.

  1. Is there some kind of a clock built into the hardware that tells it the universal time? And then it computes the local time in the user selected locale by adding the timezone offset?

    What is the unit in which this clock stores time information? How does that translate to our human concept of date and time? Who does this translation -- the hardware or the CPU?

    I am assuming there is an operating system agnostic way of getting the UTC time in all operating systems.

    Under Windows, I recall there used to be a Win32 function that you would call passing to it a pointer to a struct, which it would populate. How does that function get to know what the UTC time is?

    For that matter, how does any OS know?

  2. Finally, is the computation a CPU-bound one or a device/hardware/IO bound one?

Of course, the answer to (1) will answer (2) as well.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

This is covered in the following MSDN documentation:

System Time

System time is the current date and time of day. The system keeps time so that your applications have ready access to accurate time. The system bases system time on coordinated universal time (UTC). UTC-based time is loosely defined as the current date and time of day in Greenwich, England.

When the system first starts, it sets the system time to a value based on the real-time clock of the computer and then regularly updates the time. To retrieve the system time, use the GetSystemTime function. GetSystemTime copies the time to a SYSTEMTIME structure that contains individual members for month, day, year, weekday, hour, minute, second, and milliseconds. It is easy to display this format to a user.

You can also obtain the system time in file time format using the GetSystemTimeAsFileTime function. GetSystemTimeAsFileTime copies the time to a FILETIME structure.

To set the system time, use the SetSystemTime function. SetSystemTime assumes you have specified a UTC-based time.

The GetSystemTimeAdjustment and SetSystemTimeAdjustment functions synchronize the time-of-day clock with another time source using a periodic time adjustment applied at each clock interrupt.

Note that the system can periodically refresh the time by synchronizing with a time source. Because the system time can be adjusted either forward or backward, do not compare system time readings to determine elapsed time. Instead, use one of the methods described in Windows Time.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...