Maison  >  Article  >  développement back-end  >  Explication détaillée de la méthode de représentation de l'heure actuelle en Python

Explication détaillée de la méthode de représentation de l'heure actuelle en Python

PHPz
PHPzavant
2023-04-12 11:40:111263parcourir

Explication détaillée de la méthode de représentation de l'heure actuelle en Python

Obtenir l'heure actuelle en Python est un excellent point de départ pour de nombreuses opérations liées au temps. Un cas d’utilisation très important consiste à créer des horodatages. Dans ce didacticiel, vous apprendrez comment obtenir, afficher et formater l'heure actuelle à l'aide du module datetime.

Nous apprendrons à lire les propriétés de l'heure actuelle, telles que l'année, les minutes ou les secondes. Pour faciliter la lecture de l’heure, la possibilité d’imprimer l’heure sera explorée. Nous examinerons également différents formats d'heure, apprendrons comment les ordinateurs représentent l'heure, comment sérialiser l'heure et comment gérer les problèmes de fuseau horaire.

Comment obtenir l'heure en Python

Le moyen le plus direct d'obtenir et d'imprimer l'heure actuelle est d'utiliser la classe datetime dans le module datetime​<span style="font-size: 14px;">.now()</span>​ code>​Méthode. <code style="background-color: rgb(231, 243, 237); padding: 0px 3px; border-radius: 4px; overflow-wrap: break-word; text-indent: 0px;">​<span style="font-size: 14px;">.now()</span>​​方法。

>>> from datetime import datetime
>>> now = datetime.now()
>>> now
datetime(2022, 11, 22, 14, 31, 59, 331225)
>>> print(now)
2022-11-22 14:31:59.331225

类方法 ​​<span style="font-size: 14px;">.now()</span>​​是一个构造方法,返回一个日期时间对象。当REPL评估now变量时,你会得到一个日期时间对象的表示。要知道每个数字的含义可能相当困难。但是如果明确地打印now变量,那么你会得到一个稍微不同的输出,它以熟悉的时间戳格式呈现信息。

你可能会认识到打印的datetime对象的格式。它严格遵循国际标准,即ISO 8601,用于格式化时间和日期。你会在很多地方发现这种格式!

不过,在 Python 使用的格式中,与 ISO 8601 标准有一点偏差。标准规定,时间戳的日期和小时部分应该用T字符分开,但是通过 ​​<span style="font-size: 14px;">print()</span>​​函数传递的默认日期对象用一个空格将它们分开。

Python 是可扩展的和可定制的,它使你能够定制打印时间戳的格式。在打印时,datetime类内部使用它的 ​​<span style="font-size: 14px;">.isoformat()</span>​​方法。由于 ​​<span style="font-size: 14px;">.isoformat()</span>​​只是一个实例方法,你可以从任何日期时间对象中直接调用它来定制ISO时间戳。

>>> datetime.now().isoformat()
'2022-11-22T14:31:59.331225'
>>> datetime.now().isoformat(sep=" ")
'2022-11-22 14:31:59.331225'

你会注意到,当你调用 ​​<span style="font-size: 14px;">.isoformat()</span>​​时,没有任何参数,使用的是标准的ISO 8601分隔符T。然而,datetime类实现其特殊实例方法 ​​<span style="font-size: 14px;">.__str__()</span>​​的方式,是用一个空格作为分隔符参数。

能够得到完整的日期和时间是很好的,但有时你可能会寻找一些特定的东西。例如,你可能只想知道月份或日期。在这些情况下,你可以从一系列的属性中选择。

>>> from datetime import datetime
>>> now = datetime.now()
>>> print(f"""
... {now.month = }
... {now.day = }
... {now.hour = }
... {now.minute = }
... {now.weekday() = }
... {now.isoweekday() = }"""
... )
now.month = 11
now.day = 22
now.hour = 14
now.minute = 31
now.weekday() = 1
now.isoweekday() = 2

在这个片段中,使用了一个三引号的f-字符串,在大括号内使用=符号来输出表达式和其结果。

继续探索不同的属性和方法,用一个datetime对象调用 ​​<span style="font-size: 14px;">dir()</span>​​函数,列出当前范围内可用的名称。或者你可以查看一下datetime的文档。无论哪种方式,你都会发现有大量的选项。

你会注意到,最后一个例子的结果一般都是数字。这可能很适合你,但也许将工作日显示为数字并不理想。由于 ​​<span style="font-size: 14px;">.weekday()</span>​​和 ​​<span style="font-size: 14px;">.isoweekday()</span>​

>>> from datetime import datetime
>>> datetime.now().strftime("%A, %B %d")
'Tuesday, November 22'

Méthode de classe​​<span style="font-size: 14px;">.now()</span>​​ est une méthode constructeur qui renvoie un objet date et heure. Lorsque le REPL évalue la variable now, vous obtenez une représentation d'un objet datetime. Il peut être assez difficile de savoir ce que signifie chaque chiffre. Mais si vous imprimez explicitement la variable now, vous obtenez une sortie légèrement différente qui présente les informations dans le format d'horodatage familier. 🎜🎜Vous reconnaîtrez peut-être le format de l'objet datetime imprimé. Il suit strictement la norme internationale ISO 8601 pour le formatage de l’heure et de la date. Vous retrouverez ce format dans pas mal d'endroits ! 🎜🎜 Il existe cependant un léger écart par rapport à la norme ISO 8601 dans le format utilisé par Python. La norme stipule que les parties date et heure de l'horodatage doivent être séparées par un caractère T, mais cela se fait par wrap: break-word; text-indent: 0px;">​print()​​L'objet date par défaut passé par la fonction utilise un espace. Séparez-les. 🎜🎜Python est extensible et personnalisable, ce qui vous permet de personnaliser le format des horodatages imprimés. Lors de l'impression, la classe datetime utilise en interne son ​<span style="font-size: 14px;">.isoformat()</span>​​ méthode. Parce que​​<span style="font-size: 14px;">.isoformat()</span>​​ n'est qu'une méthode d'instance, vous pouvez l'appeler directement à partir de n'importe quel objet datetime pour personnaliser l'horodatage ISO. 🎜
>>> f"{datetime.now():%A, %B %d}"
'Tuesday, November 22'
🎜Vous remarquerez que lorsque vous appelez -indent: 0px;">​<span style="font-size: 14px;">.isoformat()</span>​​, sans aucun paramètre, le délimiteur standard ISO 8601 est utilisé T. Cependant, la classe datetime implémente ses méthodes d'instance spéciales​​<span style="font-size: 14px;">.__str__()</span>​​ utilise un espace comme paramètre de délimiteur. 🎜🎜C'est bien de pouvoir obtenir la date et l'heure complètes, mais parfois vous recherchez peut-être quelque chose de spécifique. Par exemple, vous souhaiterez peut-être uniquement connaître le mois ou le jour. Dans ces cas-là, vous pouvez choisir parmi une gamme de propriétés. 🎜
>>> from datetime import datetime
>>> datetime.now().timestamp()
1669123919.331225
🎜Dans cet extrait, une chaîne f entre guillemets triples est utilisée, en utilisant le symbole = entre accolades pour afficher l'expression et son résultat. 🎜🎜Continuez à explorer les différentes propriétés et méthodes, en appelant avec un objet datetime​<span style="font-size: 14px;">dir()</span>​​, répertorie les noms disponibles dans la portée actuelle. Ou vous pouvez consulter la documentation datetime. Quoi qu’il en soit, vous constaterez qu’il existe une multitude d’options. 🎜🎜Vous remarquerez que les résultats du dernier exemple sont généralement des nombres. Cela pourrait bien fonctionner pour vous, mais peut-être qu'afficher les jours ouvrables sous forme de chiffres n'est pas idéal. Parce que​​<span style="font-size: 14px;">.weekday()</span>​​et​​<span style="font-size: 14px;">.isoweekday()</span>​​ Les méthodes renvoyant des nombres différents peuvent également être particulièrement déroutantes. 🎜

一个ISO时间戳很好,但也许你想要比ISO时间戳更可读的东西。例如,对于一个人来说,毫秒可能有点难读。在下一节中,你将学习如何以任何你喜欢的方式格式化你的时间戳。

格式化时间戳以提高可读性

为了方便以自定义的、人类可读的方式输出时间,datetime有一个方法叫做 ​​<span style="font-size: 14px;">.strftime()</span>​​。​​<span style="font-size: 14px;">.strftime()</span>​​方法需要一个格式代码作为参数。格式代码是一个包含一堆特殊标记的字符串,这些标记将被替换成来自datetime对象的信息。

​<span style="font-size: 14px;">.strftime()</span>​​方法将为你提供大量的选项,以确定如何准确地表示你的日期时间对象。例如,以下面这种格式为例。

>>> from datetime import datetime
>>> datetime.now().strftime("%A, %B %d")
'Tuesday, November 22'

在这个例子中,使用了以下格式代码:

  • %A : 星期的全称
  • %B : 月的全称
  • %d : 本月的数字日期

格式字符串中的逗号和字面空格按原样打印。​​<span style="font-size: 14px;">.strftime()</span>​​方法只替换它所识别的代码。​​<span style="font-size: 14px;">.strftime()</span>​​中的格式代码总是以百分号(%)开始,这遵循了一个旧的C标准。这些代码类似于旧的printf字符串格式化风格,但它们是不一样的。

格式代码的文档中有一个很好的表格,向你展示了可以使用的所有不同的格式代码。

>>> f"{datetime.now():%A, %B %d}"
'Tuesday, November 22'

因此,现在你可以获得时间并按照你的喜好进行格式化。这应该可以满足你的基本时间显示需求,但也许你对计算机如何在内部表示和处理时间以及如何在文件或数据库中存储时间感到好奇。在下一节中,你将会了解到这一点。

在Python中获取当前的Unix时间

计算机喜欢数字。但日期和时间是有趣的人类数字,遵循有趣的规则。一天有24个小时?一小时内有60分钟?这是谁的聪明主意?

为了简化问题,并考虑到计算机并不介意大数字,在开发Unix操作系统的某个时候,人们做出了一个决定。

这个决定是将所有时间表示为自1970年1月1日UTC午夜以来所经过的秒数。这个时间点也被称为Unix纪元。该时间系统被称为Unix时间。今天的大多数计算机系统--甚至是Windows--都使用Unix时间来表示内部时间。

1970年1月1日UTC午夜的Unix时间为零。如果你想知道当前的Unix时间,那么你可以使用另一种日期时间方法。

>>> from datetime import datetime
>>> datetime.now().timestamp()
1669123919.331225

​<span style="font-size: 14px;">.timestamp()</span>​​方法返回自Unix纪元以来的秒数,精度很高。毕竟,在所有的属性和方法之下,每一个日期对于大多数计算机来说都不过是一个大数字而已。

在大多数情况下,你可以不去管Unix时间。这是一种代表时间的方式,对计算机来说很有效,但对那些习惯于人类日历(如公历)的人来说却不是。不过,Unix时间戳会出现在你的日期和时间探索之旅中,所以它们绝对是值得了解的。

一个正确生成的Unix时间戳最棒的一点是,它明确地捕捉了全世界的某个时刻。Unix的纪元总是在UTC,所以在时区偏移方面没有任何歧义--也就是说,如果你能可靠地创建不偏离UTC的时间戳。

但不幸的是,你经常不得不处理混乱的时区问题。不过,千万不要害怕!在下一节中,你将会了解到如何处理时区问题。在下一节中,你将了解认识时区感知的数据时间对象。

获取与时区相关的Python时间和日期对象

Unix时间戳的明确性很有吸引力,但一般来说,用ISO 8601格式来序列化时间和日期会更好,因为除了便于计算机分析外,它也是人类可读的,而且是一个国际标准。

更重要的是,尽管Unix的时间戳在某种程度上是可识别的,但它们可能会被误认为代表其他东西。毕竟,它们只是数字而已。有了ISO时间戳,你马上就知道它代表什么。引用Python之禅的话来说,就是可读性很重要。

如果你想用完全明确的术语表示你的日期时间对象,那么你首先需要让你的对象具有时区意识。一旦你有了一个时区感知的对象,时区信息就会被添加到你的ISO时间戳中。

>>> from datetime import datetime
>>> now = datetime.now()
>>> print(now.tzinfo)
None
>>> now_aware = now.astimezone()
>>> print(now_aware.tzinfo)
Romance Standard Time
>>> now_aware.tzinfo
datetime.timezone(datetime.timedelta(seconds=3600), 'Romance Standard Time')
>>> now_aware.isoformat()
'2022-11-22T14:31:59.331225+01:00'

在这个例子中,首先证明now对象没有任何时区信息,因为它的 ​​<span style="font-size: 14px;">.tzinfo</span>​​属性返回 ​​<span style="font-size: 14px;">none</span>​​。当你在没有任何参数的情况下对now调用 ​​<span style="font-size: 14px;">.astimezone()</span>​​时,本地系统的时区会被用来用一个时区对象填充 ​​<span style="font-size: 14px;">.tzinfo</span>​​。

一个时区对象本质上只是一个与UTC时间的偏移量和一个名称。在这个例子中,本地时区的名称是罗曼斯标准时间,偏移量是3600秒,或一个小时。

现在数据时间对象有一个时区对象,你可以认为它是时区感知的。因此,当你对时区感知对象调用 ​​<span style="font-size: 14px;">.isoformat()</span>​​时,你会注意到+01:00被加在了结尾。这代表了与UTC时间的一小时偏移。

如果你在不同的地方,例如秘鲁的利马,那么你的 ​​<span style="font-size: 14px;">.isoformat()</span>​​输出可能看起来像这样。

>>> now_aware.isoformat()
'2022-11-22T07:31:59.331225-06:00'

时间会有所不同,你会看到UTC的偏移量现在是-06:00。所以,现在你的时间戳看起来不错,而且在代表时间方面是明确的。

你甚至可以像许多人一样,更进一步,将你的时间戳存储为UTC时间,这样一切都会很好地规范化。

>>> from datetime import datetime, timezone
>>> now = datetime.now()
>>> now.isoformat()
'2022-11-22T14:31:59.331225'
>>> now_utc = datetime.now(timezone.utc)
>>> now_utc.isoformat()
'2022-11-22T13:31:59.331225+00:00'

将 ​​<span style="font-size: 14px;">timezone.utc</span>​​时区传递给 ​​<span style="font-size: 14px;">.now()</span>​​构造函数方法将返回一个UTC时间。请注意,在这个例子中,时间与当地时间有偏差。

ISO 8601标准也接受用Z代替+00:00来表示UTC时间。这有时被称为祖鲁时间,这也是它在航空领域的称呼。

在航空领域,必须在UTC时间内操作。无论在什么地方,在一个共同的时间内操作,在像航空这样的领域是至关重要的。想象一下,空中交通管制部门必须处理每架飞机根据其出发地报告的估计降落时间。如若不然会造成混乱和灾难!

结论

在本教程中,我们已经掌握了时间!你已经生成了一个日期时间对象,并看到了如何挑选出该对象的不同属性。你还研究了几种以不同格式输出日期时间对象的方法。

你还熟悉了UNIX时间和ISO时间戳,探索了如何明确地表示你的时间戳。为此,你已经涉足了复杂的时区世界,并使你的数据时间对象具有时区意识。

Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!

Déclaration:
Cet article est reproduit dans:. en cas de violation, veuillez contacter admin@php.cn Supprimer