且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

电脑如何知道当前日期和时间?

更新时间:2022-11-08 23:10:08

以下MSDN文档中对此进行了介绍:

系统时间

系统时间是当前日期和时间.系统会保留时间,以便您的应用程序可以随时访问准确的时间.系统将系统时间基于协调世界时(UTC).基于UTC的时间被粗略地定义为英格兰格林威治的当前日期和时间.

当系统首次启动时,它会根据计算机的实时时钟将系统时间设置为一个值,然后定期更新时间.要检索系统时间,请使用 GetSystemTime 函数. GetSystemTime将时间复制到 SYSTEMTIME 包含月,日,年,工作日,小时,分钟,秒和毫秒的单个成员的结构.很容易向用户显示此格式.

您还可以使用 GetSystemTimeAsFileTime 函数. GetSystemTimeAsFileTime将时间复制到 FILETIME 结构.>

要设置系统时间,请使用 SetSystemTime 功能. SetSystemTime假定您已指定基于UTC的时间.

GetSystemTimeAdjustment Windows时间中描述的方法之一.

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.

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.