电脑如何知道当前日期和时间? [英] How does the computer know the present date and time?

查看:171
本文介绍了电脑如何知道当前日期和时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请原谅我对此的无知.我可能很久以前,也就是多年前在Charles Petzold的 Programming Windows,第四版.书中读到的这篇文章,但我一定已经忘记了这一切.我记得第一章对CPU的定时器分辨率和时钟速度有话要说,但是我忘记了细节.我认为他回答了这个问题.但是我可能在想像.已经好久了.

虽然这里有一个相关问题,但答案并不令人满意,因为它没有回答更细微的差别我对此感兴趣,下面将对此进行介绍.

很高兴有人能对此问题提供系统的答案.

  1. 硬件中是否内置了某种时钟,可以告诉该时钟世界标准时间?然后通过添加时区偏移量来计算用户选择的语言环境中的本地时间?

    此时钟存储时间信息的单位是什么?这如何转化为我们人类的日期和时间概念?该翻译由谁执行-硬件还是CPU?

    我假设有一种与操作系统无关的方法来获取所有操作系统中的UTC时间.

    在Windows下,我记得曾经有一个Win32函数,您会调用该函数将指向它的struct指针传递给它.该功能如何得知UTC时间是什么?

    就此而言,任何操作系统怎么知道?

  2. 最后,计算是CPU绑定的还是设备/硬件/IO绑定的?

当然,(1)的答案也将回答(2).

解决方案

以下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.

这篇关于电脑如何知道当前日期和时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆