没有互联网连接的datetime.datetime.now()如何工作? [英] How datetime.datetime.now() works without internet connection?

查看:121
本文介绍了没有互联网连接的datetime.datetime.now()如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在python中,通过导入 datetime模块并使用 class datetime.datetime 的各种功能,我们可以使用格式化甚至偶数日期算术获得基本日期

In python, by importing datetime module and using various functions of class datetime.datetime we could get basic dates with formatting and even date arithmetic for deployment.

例如, datetime.datetime.now()将返回今天的日期。

For example, datetime.datetime.now() will return today's date.

但是,今天,当我运行该程序时,计算机上没有互联网连接,但仍然

But, today when I run this program there was no internet connection in my computer but still it outputs today's date.

所以, datetime.datetime.now()如何返回正确的日期?算法是否会在24小时后自动递增?

So, how datetime.datetime.now() could return proper date? Is the algorithm automatically increment after 24 hours time ?

推荐答案

tl; dr datetime.datetime .now()使用计算机内置的时钟。

tl;dr datetime.datetime.now() uses the clock built into your computer.

计算机能够将相当准确的时间保持更长的时间

Computers have been able to keep fairly accurate time for much longer than the Internet has existed.

例如,PC具有称为实时时钟(RTC)。它由电池供电,即使关闭计算机也可以保持时间。

For example, PCs feature what's called a real-time clock (RTC). It is battery-powered and can keep the time even when the computer is switched off.

有趣的是,某些分布式算法需要非常准确的时钟以可靠地运行。所需的精度远远超过了简单的基于振荡器的时钟所能提供的精度。

Interestingly, some distributed algorithms require very accurate clocks in order to operate reliably. The required accuracy far exceeds anything that a simple oscillator-based clock can provide.

结果,像Google这样的公司经营 GPS 原子数据中心中的时钟(甚至显示出来的时钟也没有潜在问题,例如, 2017年1月26日,当某些GPS时钟以13微秒的速度持续了十个小时)。

As a result, companies like Google operate GPS and atomic clocks in their data centres (and even those are not without potential issues, as was demonstrated, for example, on 26 January 2017, when some GPS clocks were out by 13 microseconds for ten hours).

即使数据中心已连接到互联网, GPS和原子钟都不需要Internet连接即可运行。此外, 某人 需要保持所有Internet时间基础架构的运行...不可能每个人都从互联网上获取时间。 ;)

Even though the data centres are connected to the Internet, neither GPS nor atomic clocks require an Internet connection to operate. Besides, someone needs to keep all that Internet time infrastructure running... it can't be that everyone gets their time "off the Internet". ;)

现在我们正在跨计算机网络分配时间,主要的协议是 NTP(网络时间协议)和PTP(精确时间协议)

Now that we're on the subject of distributing the time across computer networks, the main protocols for doing that are NTP (Network Time Protocol) and PTP (Precision Time Protocol).

这篇关于没有互联网连接的datetime.datetime.now()如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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