Detailed explanation of the current time representation method in Python

PHPz
Release: 2023-04-12 11:40:11
forward
1262 people have browsed it

Detailed explanation of the current time representation method in Python

Getting the current time in Python is a great starting point for many time-related operations. A very important use case is creating timestamps. In this tutorial, you will learn how to get, display, and format the current time using the datetime module.

We will learn how to read properties of the current time, such as year, minutes or seconds. To make the time easier to read, the option to print the time will be explored. We'll also look at different time formats, learn how computers represent time, how to serialize time, and how to deal with time zone issues.

How to get the time in Python

The most direct way to get and print the current time is to use the datetime class in the datetime module.​<span style="font-size: 14px;">. now()</span>​​method.

>>> 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
Copy after login

Class method ​​<span style="font-size: 14px;">.now()</span>​​ is a constructor method that returns a date and time object. When the REPL evaluates the now variable, you get a representation of a datetime object. It can be quite difficult to know what each number means. But if you print the now variable explicitly, then you get a slightly different output that presents the information in the familiar timestamp format.

You may recognize the format of the printed datetime object. It strictly follows the international standard, ISO 8601, for formatting time and date. You'll find this format in many places!

However, there is a slight deviation from the ISO 8601 standard in the format used by Python. The standard states that the date and hour parts of a timestamp should be separated by a T character, but the default date object passed through the ​​<span style="font-size: 14px;">print()</span>​​function separates them with a space.

Python is extensible and customizable, which enables you to customize the format of printed timestamps. When printing, the datetime class internally uses its ​<span style="font-size: 14px;">.isoformat()</span>​​ method. Since ​​<span style="font-size: 14px;">.isoformat()</span>​​ is just an instance method, you can call it directly from any datetime object to customize the ISO timestamp.

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

You will notice that when you call​​<span style="font-size: 14px;">.isoformat()</span>​​without any parameters, the standard ISO 8601 delimiter is used T. However, the way the datetime class implements its special instance method ​​<span style="font-size: 14px;">.__str__()</span>​​ is to use a space as the delimiter parameter.

It's nice to be able to get the full date and time, but sometimes you might be looking for something specific. For example, you may only want to know the month or day. In these cases, you can choose from a range of properties.

>>> 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
Copy after login

In this snippet, a triple-quoted f-string is used, using the = symbol within curly braces to output the expression and its result.

Continuing to explore the different properties and methods, call the ​​<span style="font-size: 14px;">dir()</span>​​function with a datetime object to list the names available in the current scope. Or you can check out the datetime documentation. Either way, you'll find there are tons of options.

You will notice that the results of the last example are generally numbers. This might work fine for you, but maybe displaying working days as numbers isn't ideal. Since the ​​<span style="font-size: 14px;">.weekday()</span>​​ and ​​<span style="font-size: 14px;">.isoweekday()</span>​​ methods return different numbers, this also It can be extremely confusing.

一个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'
Copy after login

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

  • %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'
Copy after login

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

在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
Copy after login

​<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'
Copy after login

在这个例子中,首先证明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'
Copy after login

时间会有所不同,你会看到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'
Copy after login

将 ​​<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时间戳,探索了如何明确地表示你的时间戳。为此,你已经涉足了复杂的时区世界,并使你的数据时间对象具有时区意识。

The above is the detailed content of Detailed explanation of the current time representation method in Python. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:51cto.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!