Hướng dẫn how do you add timezone in python? - làm thế nào để bạn thêm múi giờ trong python?

Những gì tôi cần làm

Tôi có một đối tượng DateTime không biết thời gian, mà tôi cần thêm một múi giờ để có thể so sánh nó với các đối tượng DateTime nhận biết thời gian khác. Tôi không muốn chuyển đổi toàn bộ ứng dụng của mình thành Timezone không biết cho trường hợp di sản này.

Những gì tôi đã thử

Đầu tiên, để chứng minh vấn đề:

Python 2.6.1 (r261:67515, Jun 24 2010, 21:47:49) 
[GCC 4.2.1 (Apple Inc. build 5646)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import datetime
>>> import pytz
>>> unaware = datetime.datetime(2011,8,15,8,15,12,0)
>>> unaware
datetime.datetime(2011, 8, 15, 8, 15, 12)
>>> aware = datetime.datetime(2011,8,15,8,15,12,0,pytz.UTC)
>>> aware
datetime.datetime(2011, 8, 15, 8, 15, 12, tzinfo=)
>>> aware == unaware
Traceback (most recent call last):
  File "", line 1, in 
TypeError: can't compare offset-naive and offset-aware datetimes

Đầu tiên, tôi đã thử astimezone:

>>> unaware.astimezone(pytz.UTC)
Traceback (most recent call last):
  File "", line 1, in 
ValueError: astimezone() cannot be applied to a naive datetime
>>>

Không có gì đáng ngạc nhiên khi thất bại này, vì nó thực sự đang cố gắng chuyển đổi. Thay thế dường như là một lựa chọn tốt hơn (theo cách tôi nhận được giá trị của DateTime.Today () trong Python đó là "Timezone Aware"?):

>>> unaware.replace(tzinfo=pytz.UTC)
datetime.datetime(2011, 8, 15, 8, 15, 12, tzinfo=)
>>> unaware == aware
Traceback (most recent call last):
  File "", line 1, in 
TypeError: can't compare offset-naive and offset-aware datetimes
>>> 

Nhưng như bạn có thể thấy, thay thế dường như đặt tzinfo, nhưng không làm cho đối tượng nhận thức được. Tôi đã sẵn sàng để quay trở lại với bác sĩ chuỗi đầu vào để có múi giờ trước khi phân tích nó (tôi đang sử dụng DateUtil để phân tích cú pháp, nếu điều đó quan trọng), nhưng điều đó có vẻ vô cùng kludgy.

Ngoài ra, tôi đã thử điều này trong cả Python 2.6 và Python 2.7, với cùng một kết quả.

Định nghĩa bài văn

Tôi đang viết một trình phân tích cú pháp cho một số tệp dữ liệu. Có một định dạng cũ tôi cần hỗ trợ trong đó chuỗi ngày không có chỉ báo múi giờ. Tôi đã sửa được nguồn dữ liệu, nhưng tôi vẫn cần hỗ trợ định dạng dữ liệu kế thừa. Chuyển đổi một lần dữ liệu kế thừa không phải là một lựa chọn cho các lý do BS doanh nghiệp khác nhau. Mặc dù nói chung, tôi không thích ý tưởng mã hóa cứng một thời gian mặc định, trong trường hợp này có vẻ như là lựa chọn tốt nhất. Tôi biết với sự tự tin hợp lý rằng tất cả các dữ liệu kế thừa được đề cập là ở UTC, vì vậy tôi đã sẵn sàng chấp nhận rủi ro mặc định trong trường hợp này.

Bây giờ, hãy để kiểm tra xem đối tượng có nhận thức được timezone hay không sử dụng phương pháp chúng tôi đã sử dụng trong phần 1 của bài viết.

____10

>>> unaware.astimezone(pytz.UTC)
Traceback (most recent call last):
  File "", line 1, in 
ValueError: astimezone() cannot be applied to a naive datetime
>>>
1
2021-08-30 04:35:37.036990+00:00
2
>>> unaware.astimezone(pytz.UTC)
Traceback (most recent call last):
  File "", line 1, in 
ValueError: astimezone() cannot be applied to a naive datetime
>>>
1
2021-08-30T09:45:43.291212+00:00
9

>>> unaware.astimezone(pytz.UTC) Traceback (most recent call last): File "", line 1, in ValueError: astimezone() cannot be applied to a naive datetime >>> 3 >>> unaware.astimezone(pytz.UTC) Traceback (most recent call last): File "", line 1, in ValueError: astimezone() cannot be applied to a naive datetime >>> 4>>> unaware.astimezone(pytz.UTC) Traceback (most recent call last): File "", line 1, in ValueError: astimezone() cannot be applied to a naive datetime >>> 1>>> unaware.astimezone(pytz.UTC) Traceback (most recent call last): File "", line 1, in ValueError: astimezone() cannot be applied to a naive datetime >>> 1 >>> unaware.astimezone(pytz.UTC) Traceback (most recent call last): File "", line 1, in ValueError: astimezone() cannot be applied to a naive datetime >>> 7 >>> unaware.astimezone(pytz.UTC) Traceback (most recent call last): File "", line 1, in ValueError: astimezone() cannot be applied to a naive datetime >>> 8 Aware 2021-08-30 04:46:40.670455+00:001

Aware
2021-08-30 04:46:40.670455+00:00
2
>>> unaware.astimezone(pytz.UTC)
Traceback (most recent call last):
  File "", line 1, in 
ValueError: astimezone() cannot be applied to a naive datetime
>>>
1
>>> unaware.astimezone(pytz.UTC)
Traceback (most recent call last):
  File "", line 1, in 
ValueError: astimezone() cannot be applied to a naive datetime
>>>
1
>>> unaware.astimezone(pytz.UTC)
Traceback (most recent call last):
  File "", line 1, in 
ValueError: astimezone() cannot be applied to a naive datetime
>>>
7
>>> unaware.replace(tzinfo=pytz.UTC)
datetime.datetime(2011, 8, 15, 8, 15, 12, tzinfo=)
>>> unaware == aware
Traceback (most recent call last):
  File "", line 1, in 
TypeError: can't compare offset-naive and offset-aware datetimes
>>> 
5

Đối tượng nhận thức timezone bằng pytzdatetime.now(tz)

Bạn cũng có thể sử dụng mô-đun PYTZ để tạo các đối tượng nhận biết thời gian.tz : Specified time zone of which current time and date is required. (Uses Greenwich Meridian time by default.)

Đối với điều này, chúng tôi sẽ lưu trữ ngày và thời gian hiện tại trong một biến mới bằng cách sử dụng hàm DateTime.now () của mô -đun DateTime và sau đó chúng tôi sẽ thêm múi giờ bằng hàm múi giờ của mô -đun PYTZ.

Python3

import datetime

>>> unaware.astimezone(pytz.UTC)
Traceback (most recent call last):
  File "", line 1, in 
ValueError: astimezone() cannot be applied to a naive datetime
>>>
0____11
>>> unaware.astimezone(pytz.UTC)
Traceback (most recent call last):
  File "", line 1, in 
ValueError: astimezone() cannot be applied to a naive datetime
>>>
2

>>> unaware.astimezone(pytz.UTC)
Traceback (most recent call last):
  File "", line 1, in 
ValueError: astimezone() cannot be applied to a naive datetime
>>>
3
>>> unaware.astimezone(pytz.UTC)
Traceback (most recent call last):
  File "", line 1, in 
ValueError: astimezone() cannot be applied to a naive datetime
>>>
4
>>> unaware.astimezone(pytz.UTC)
Traceback (most recent call last):
  File "", line 1, in 
ValueError: astimezone() cannot be applied to a naive datetime
>>>
1
>>> unaware.astimezone(pytz.UTC)
Traceback (most recent call last):
  File "", line 1, in 
ValueError: astimezone() cannot be applied to a naive datetime
>>>
1
>>> unaware.astimezone(pytz.UTC)
Traceback (most recent call last):
  File "", line 1, in 
ValueError: astimezone() cannot be applied to a naive datetime
>>>
7
>>> unaware.astimezone(pytz.UTC)
Traceback (most recent call last):
  File "", line 1, in 
ValueError: astimezone() cannot be applied to a naive datetime
>>>
8
>>> unaware.astimezone(pytz.UTC)
Traceback (most recent call last):
  File "", line 1, in 
ValueError: astimezone() cannot be applied to a naive datetime
>>>
9

>>> unaware.replace(tzinfo=pytz.UTC)
datetime.datetime(2011, 8, 15, 8, 15, 12, tzinfo=)
>>> unaware == aware
Traceback (most recent call last):
  File "", line 1, in 
TypeError: can't compare offset-naive and offset-aware datetimes
>>> 
0
>>> unaware.replace(tzinfo=pytz.UTC)
datetime.datetime(2011, 8, 15, 8, 15, 12, tzinfo=)
>>> unaware == aware
Traceback (most recent call last):
  File "", line 1, in 
TypeError: can't compare offset-naive and offset-aware datetimes
>>> 
1
>>> unaware.astimezone(pytz.UTC)
Traceback (most recent call last):
  File "", line 1, in 
ValueError: astimezone() cannot be applied to a naive datetime
>>>
1
>>> unaware.astimezone(pytz.UTC)
Traceback (most recent call last):
  File "", line 1, in 
ValueError: astimezone() cannot be applied to a naive datetime
>>>
1
>>> unaware.astimezone(pytz.UTC)
Traceback (most recent call last):
  File "", line 1, in 
ValueError: astimezone() cannot be applied to a naive datetime
>>>
7
>>> unaware.replace(tzinfo=pytz.UTC)
datetime.datetime(2011, 8, 15, 8, 15, 12, tzinfo=)
>>> unaware == aware
Traceback (most recent call last):
  File "", line 1, in 
TypeError: can't compare offset-naive and offset-aware datetimes
>>> 
5

>>> unaware.replace(tzinfo=pytz.UTC)
datetime.datetime(2011, 8, 15, 8, 15, 12, tzinfo=)
>>> unaware == aware
Traceback (most recent call last):
  File "", line 1, in 
TypeError: can't compare offset-naive and offset-aware datetimes
>>> 
6
>>> unaware.replace(tzinfo=pytz.UTC)
datetime.datetime(2011, 8, 15, 8, 15, 12, tzinfo=)
>>> unaware == aware
Traceback (most recent call last):
  File "", line 1, in 
TypeError: can't compare offset-naive and offset-aware datetimes
>>> 
7
>>> unaware.replace(tzinfo=pytz.UTC)
datetime.datetime(2011, 8, 15, 8, 15, 12, tzinfo=)
>>> unaware == aware
Traceback (most recent call last):
  File "", line 1, in 
TypeError: can't compare offset-naive and offset-aware datetimes
>>> 
8
>>> unaware.replace(tzinfo=pytz.UTC)
datetime.datetime(2011, 8, 15, 8, 15, 12, tzinfo=)
>>> unaware == aware
Traceback (most recent call last):
  File "", line 1, in 
TypeError: can't compare offset-naive and offset-aware datetimes
>>> 
9
Unaware
0

Unaware
1
>>> unaware.replace(tzinfo=pytz.UTC)
datetime.datetime(2011, 8, 15, 8, 15, 12, tzinfo=)
>>> unaware == aware
Traceback (most recent call last):
  File "", line 1, in 
TypeError: can't compare offset-naive and offset-aware datetimes
>>> 
5

>>> unaware.replace(tzinfo=pytz.UTC)
datetime.datetime(2011, 8, 15, 8, 15, 12, tzinfo=)
>>> unaware == aware
Traceback (most recent call last):
  File "", line 1, in 
TypeError: can't compare offset-naive and offset-aware datetimes
>>> 
6
>>> unaware.replace(tzinfo=pytz.UTC)
datetime.datetime(2011, 8, 15, 8, 15, 12, tzinfo=)
>>> unaware == aware
Traceback (most recent call last):
  File "", line 1, in 
TypeError: can't compare offset-naive and offset-aware datetimes
>>> 
7
>>> unaware.replace(tzinfo=pytz.UTC)
datetime.datetime(2011, 8, 15, 8, 15, 12, tzinfo=)
>>> unaware == aware
Traceback (most recent call last):
  File "", line 1, in 
TypeError: can't compare offset-naive and offset-aware datetimes
>>> 
8
Unaware
6
Unaware
0

Output:

Unaware

Đối tượng nhận thức timezone bằng cách sử dụng DateTime

Đối với điều này, chúng tôi sẽ lưu trữ thời gian hiện tại trong một biến mới bằng cách sử dụng hàm DateTime.now (). Time () của mô -đun DateTime. Sau đó, chúng tôi sẽ thay thế giá trị của múi giờ trong lớp TZINFO của đối tượng bằng hàm thay thế (). Sau đó chuyển đổi giá trị ngày thành định dạng ISO 8601 bằng phương thức isoforMat ().

Cú pháp: isoformat (sep = xông t, thời gianisoformat(sep=’T’, timespec=’auto’)

Parameters:

  • SEP: Đây là một phân tách một ký tự được đặt giữa ngày và giờ. It is a one character separator placed between date and time.
  • Timespec: Số lượng thành phần bổ sung của thời gian để bao gồm Number of additional component of time to include

Code:

Python3

import datetime

>>> unaware.astimezone(pytz.UTC)
Traceback (most recent call last):
  File "", line 1, in 
ValueError: astimezone() cannot be applied to a naive datetime
>>>
0____11
>>> unaware.astimezone(pytz.UTC)
Traceback (most recent call last):
  File "", line 1, in 
ValueError: astimezone() cannot be applied to a naive datetime
>>>
2

>>> unaware.astimezone(pytz.UTC)
Traceback (most recent call last):
  File "", line 1, in 
ValueError: astimezone() cannot be applied to a naive datetime
>>>
3
>>> unaware.astimezone(pytz.UTC)
Traceback (most recent call last):
  File "", line 1, in 
ValueError: astimezone() cannot be applied to a naive datetime
>>>
4
>>> unaware.astimezone(pytz.UTC)
Traceback (most recent call last):
  File "", line 1, in 
ValueError: astimezone() cannot be applied to a naive datetime
>>>
1
>>> unaware.astimezone(pytz.UTC)
Traceback (most recent call last):
  File "", line 1, in 
ValueError: astimezone() cannot be applied to a naive datetime
>>>
1
>>> unaware.astimezone(pytz.UTC)
Traceback (most recent call last):
  File "", line 1, in 
ValueError: astimezone() cannot be applied to a naive datetime
>>>
7
>>> unaware.astimezone(pytz.UTC)
Traceback (most recent call last):
  File "", line 1, in 
ValueError: astimezone() cannot be applied to a naive datetime
>>>
8
>>> unaware.astimezone(pytz.UTC)
Traceback (most recent call last):
  File "", line 1, in 
ValueError: astimezone() cannot be applied to a naive datetime
>>>
9

>>> unaware.replace(tzinfo=pytz.UTC)
datetime.datetime(2011, 8, 15, 8, 15, 12, tzinfo=)
>>> unaware == aware
Traceback (most recent call last):
  File "", line 1, in 
TypeError: can't compare offset-naive and offset-aware datetimes
>>> 
6
2021-08-30T09:45:43.291212+00:00
7
>>> unaware.astimezone(pytz.UTC)
Traceback (most recent call last):
  File "", line 1, in 
ValueError: astimezone() cannot be applied to a naive datetime
>>>
1
2021-08-30T09:45:43.291212+00:00
9

>>> unaware.replace(tzinfo=pytz.UTC)
datetime.datetime(2011, 8, 15, 8, 15, 12, tzinfo=)
>>> unaware == aware
Traceback (most recent call last):
  File "", line 1, in 
TypeError: can't compare offset-naive and offset-aware datetimes
>>> 
0
>>> unaware.replace(tzinfo=pytz.UTC)
datetime.datetime(2011, 8, 15, 8, 15, 12, tzinfo=)
>>> unaware == aware
Traceback (most recent call last):
  File "", line 1, in 
TypeError: can't compare offset-naive and offset-aware datetimes
>>> 
1
>>> unaware.astimezone(pytz.UTC)
Traceback (most recent call last):
  File "", line 1, in 
ValueError: astimezone() cannot be applied to a naive datetime
>>>
1
>>> unaware.astimezone(pytz.UTC)
Traceback (most recent call last):
  File "", line 1, in 
ValueError: astimezone() cannot be applied to a naive datetime
>>>
1
>>> unaware.astimezone(pytz.UTC)
Traceback (most recent call last):
  File "", line 1, in 
ValueError: astimezone() cannot be applied to a naive datetime
>>>
7
>>> unaware.replace(tzinfo=pytz.UTC)
datetime.datetime(2011, 8, 15, 8, 15, 12, tzinfo=)
>>> unaware == aware
Traceback (most recent call last):
  File "", line 1, in 
TypeError: can't compare offset-naive and offset-aware datetimes
>>> 
5

>>> unaware.replace(tzinfo=pytz.UTC)
datetime.datetime(2011, 8, 15, 8, 15, 12, tzinfo=)
>>> unaware == aware
Traceback (most recent call last):
  File "", line 1, in 
TypeError: can't compare offset-naive and offset-aware datetimes
>>> 
7
Aware
2021-08-30T09:55:15.111556+00:00
4

Output:

2021-08-30T09:45:43.291212+00:00

Đối tượng nhận thức timezone bằng cách sử dụng DateTime

Python3

import datetime

>>> unaware.astimezone(pytz.UTC)
Traceback (most recent call last):
  File "", line 1, in 
ValueError: astimezone() cannot be applied to a naive datetime
>>>
0____11
>>> unaware.astimezone(pytz.UTC)
Traceback (most recent call last):
  File "", line 1, in 
ValueError: astimezone() cannot be applied to a naive datetime
>>>
2

____10

>>> unaware.astimezone(pytz.UTC)
Traceback (most recent call last):
  File "", line 1, in 
ValueError: astimezone() cannot be applied to a naive datetime
>>>
1
2021-08-30 04:35:37.036990+00:00
2
>>> unaware.astimezone(pytz.UTC)
Traceback (most recent call last):
  File "", line 1, in 
ValueError: astimezone() cannot be applied to a naive datetime
>>>
1
2021-08-30T09:45:43.291212+00:00
9

>>> unaware.astimezone(pytz.UTC)
Traceback (most recent call last):
  File "", line 1, in 
ValueError: astimezone() cannot be applied to a naive datetime
>>>
3
>>> unaware.astimezone(pytz.UTC)
Traceback (most recent call last):
  File "", line 1, in 
ValueError: astimezone() cannot be applied to a naive datetime
>>>
4
>>> unaware.astimezone(pytz.UTC)
Traceback (most recent call last):
  File "", line 1, in 
ValueError: astimezone() cannot be applied to a naive datetime
>>>
1
>>> unaware.astimezone(pytz.UTC)
Traceback (most recent call last):
  File "", line 1, in 
ValueError: astimezone() cannot be applied to a naive datetime
>>>
1
>>> unaware.astimezone(pytz.UTC)
Traceback (most recent call last):
  File "", line 1, in 
ValueError: astimezone() cannot be applied to a naive datetime
>>>
7
>>> unaware.astimezone(pytz.UTC)
Traceback (most recent call last):
  File "", line 1, in 
ValueError: astimezone() cannot be applied to a naive datetime
>>>
8
Aware
2021-08-30 04:46:40.670455+00:00
1

Aware
2021-08-30 04:46:40.670455+00:00
2
>>> unaware.astimezone(pytz.UTC)
Traceback (most recent call last):
  File "", line 1, in 
ValueError: astimezone() cannot be applied to a naive datetime
>>>
1
>>> unaware.astimezone(pytz.UTC)
Traceback (most recent call last):
  File "", line 1, in 
ValueError: astimezone() cannot be applied to a naive datetime
>>>
1
>>> unaware.astimezone(pytz.UTC)
Traceback (most recent call last):
  File "", line 1, in 
ValueError: astimezone() cannot be applied to a naive datetime
>>>
7
>>> unaware.replace(tzinfo=pytz.UTC)
datetime.datetime(2011, 8, 15, 8, 15, 12, tzinfo=)
>>> unaware == aware
Traceback (most recent call last):
  File "", line 1, in 
TypeError: can't compare offset-naive and offset-aware datetimes
>>> 
5

>>> unaware.replace(tzinfo=pytz.UTC)
datetime.datetime(2011, 8, 15, 8, 15, 12, tzinfo=)
>>> unaware == aware
Traceback (most recent call last):
  File "", line 1, in 
TypeError: can't compare offset-naive and offset-aware datetimes
>>> 
6
>>> unaware.replace(tzinfo=pytz.UTC)
datetime.datetime(2011, 8, 15, 8, 15, 12, tzinfo=)
>>> unaware == aware
Traceback (most recent call last):
  File "", line 1, in 
TypeError: can't compare offset-naive and offset-aware datetimes
>>> 
7
>>> unaware.replace(tzinfo=pytz.UTC)
datetime.datetime(2011, 8, 15, 8, 15, 12, tzinfo=)
>>> unaware == aware
Traceback (most recent call last):
  File "", line 1, in 
TypeError: can't compare offset-naive and offset-aware datetimes
>>> 
8
>>> unaware.replace(tzinfo=pytz.UTC)
datetime.datetime(2011, 8, 15, 8, 15, 12, tzinfo=)
>>> unaware == aware
Traceback (most recent call last):
  File "", line 1, in 
TypeError: can't compare offset-naive and offset-aware datetimes
>>> 
9
Unaware
0

Unaware
1
>>> unaware.replace(tzinfo=pytz.UTC)
datetime.datetime(2011, 8, 15, 8, 15, 12, tzinfo=)
>>> unaware == aware
Traceback (most recent call last):
  File "", line 1, in 
TypeError: can't compare offset-naive and offset-aware datetimes
>>> 
5

>>> unaware.replace(tzinfo=pytz.UTC)
datetime.datetime(2011, 8, 15, 8, 15, 12, tzinfo=)
>>> unaware == aware
Traceback (most recent call last):
  File "", line 1, in 
TypeError: can't compare offset-naive and offset-aware datetimes
>>> 
6
>>> unaware.replace(tzinfo=pytz.UTC)
datetime.datetime(2011, 8, 15, 8, 15, 12, tzinfo=)
>>> unaware == aware
Traceback (most recent call last):
  File "", line 1, in 
TypeError: can't compare offset-naive and offset-aware datetimes
>>> 
7
>>> unaware.replace(tzinfo=pytz.UTC)
datetime.datetime(2011, 8, 15, 8, 15, 12, tzinfo=)
>>> unaware == aware
Traceback (most recent call last):
  File "", line 1, in 
TypeError: can't compare offset-naive and offset-aware datetimes
>>> 
8
Unaware
6
Unaware
0

>>> unaware.astimezone(pytz.UTC)
Traceback (most recent call last):
  File "", line 1, in 
ValueError: astimezone() cannot be applied to a naive datetime
>>>
0____11
Aware
2021-08-30T09:55:15.111556+00:00
2

>>> unaware.replace(tzinfo=pytz.UTC)
datetime.datetime(2011, 8, 15, 8, 15, 12, tzinfo=)
>>> unaware == aware
Traceback (most recent call last):
  File "", line 1, in 
TypeError: can't compare offset-naive and offset-aware datetimes
>>> 
7
Aware
2021-08-30T09:55:15.111556+00:00
4

Output:

Aware
2021-08-30T09:55:15.111556+00:00

Đối tượng nhận thức timezone bằng pytz

Bạn cũng có thể sử dụng mô-đun PYTZ để tạo các đối tượng nhận biết thời gian.

Đối với điều này, chúng tôi sẽ lưu trữ ngày và thời gian hiện tại trong một biến mới bằng cách sử dụng hàm DateTime.now () của mô -đun DateTime và sau đó chúng tôi sẽ thêm múi giờ bằng hàm múi giờ của mô -đun PYTZ.

Python3

import datetime

import datetime7

>>> unaware.astimezone(pytz.UTC)
Traceback (most recent call last):
  File "", line 1, in 
ValueError: astimezone() cannot be applied to a naive datetime
>>>
0____11
>>> unaware.astimezone(pytz.UTC)
Traceback (most recent call last):
  File "", line 1, in 
ValueError: astimezone() cannot be applied to a naive datetime
>>>
00
>>> unaware.astimezone(pytz.UTC)
Traceback (most recent call last):
  File "", line 1, in 
ValueError: astimezone() cannot be applied to a naive datetime
>>>
01
>>> unaware.astimezone(pytz.UTC)
Traceback (most recent call last):
  File "", line 1, in 
ValueError: astimezone() cannot be applied to a naive datetime
>>>
02

>>> unaware.replace(tzinfo=pytz.UTC)
datetime.datetime(2011, 8, 15, 8, 15, 12, tzinfo=)
>>> unaware == aware
Traceback (most recent call last):
  File "", line 1, in 
TypeError: can't compare offset-naive and offset-aware datetimes
>>> 
7
Aware
2021-08-30T09:55:15.111556+00:00
4

Output:

2021-08-30 04:35:37.036990+00:00

Bây giờ, hãy để kiểm tra xem đối tượng có nhận thức được timezone hay không sử dụng phương pháp chúng tôi đã sử dụng trong phần 1 của bài viết.

Python3

import datetime

import datetime7

>>> unaware.astimezone(pytz.UTC)
Traceback (most recent call last):
  File "", line 1, in 
ValueError: astimezone() cannot be applied to a naive datetime
>>>
0____11
>>> unaware.astimezone(pytz.UTC)
Traceback (most recent call last):
  File "", line 1, in 
ValueError: astimezone() cannot be applied to a naive datetime
>>>
00
>>> unaware.astimezone(pytz.UTC)
Traceback (most recent call last):
  File "", line 1, in 
ValueError: astimezone() cannot be applied to a naive datetime
>>>
01
>>> unaware.astimezone(pytz.UTC)
Traceback (most recent call last):
  File "", line 1, in 
ValueError: astimezone() cannot be applied to a naive datetime
>>>
02

Bây giờ, hãy để kiểm tra xem đối tượng có nhận thức được timezone hay không sử dụng phương pháp chúng tôi đã sử dụng trong phần 1 của bài viết.

>>> unaware.astimezone(pytz.UTC)
Traceback (most recent call last):
  File "", line 1, in 
ValueError: astimezone() cannot be applied to a naive datetime
>>>
3
>>> unaware.astimezone(pytz.UTC)
Traceback (most recent call last):
  File "", line 1, in 
ValueError: astimezone() cannot be applied to a naive datetime
>>>
4
>>> unaware.astimezone(pytz.UTC)
Traceback (most recent call last):
  File "", line 1, in 
ValueError: astimezone() cannot be applied to a naive datetime
>>>
1
>>> unaware.astimezone(pytz.UTC)
Traceback (most recent call last):
  File "", line 1, in 
ValueError: astimezone() cannot be applied to a naive datetime
>>>
1
>>> unaware.astimezone(pytz.UTC)
Traceback (most recent call last):
  File "", line 1, in 
ValueError: astimezone() cannot be applied to a naive datetime
>>>
7
>>> unaware.astimezone(pytz.UTC)
Traceback (most recent call last):
  File "", line 1, in 
ValueError: astimezone() cannot be applied to a naive datetime
>>>
8
2021-08-30T09:45:43.291212+00:00
5

>>> unaware.replace(tzinfo=pytz.UTC)
datetime.datetime(2011, 8, 15, 8, 15, 12, tzinfo=)
>>> unaware == aware
Traceback (most recent call last):
  File "", line 1, in 
TypeError: can't compare offset-naive and offset-aware datetimes
>>> 
6
>>> unaware.replace(tzinfo=pytz.UTC)
datetime.datetime(2011, 8, 15, 8, 15, 12, tzinfo=)
>>> unaware == aware
Traceback (most recent call last):
  File "", line 1, in 
TypeError: can't compare offset-naive and offset-aware datetimes
>>> 
7
>>> unaware.replace(tzinfo=pytz.UTC)
datetime.datetime(2011, 8, 15, 8, 15, 12, tzinfo=)
>>> unaware == aware
Traceback (most recent call last):
  File "", line 1, in 
TypeError: can't compare offset-naive and offset-aware datetimes
>>> 
8
>>> unaware.replace(tzinfo=pytz.UTC)
datetime.datetime(2011, 8, 15, 8, 15, 12, tzinfo=)
>>> unaware == aware
Traceback (most recent call last):
  File "", line 1, in 
TypeError: can't compare offset-naive and offset-aware datetimes
>>> 
9
Unaware
0

Unaware
1
>>> unaware.replace(tzinfo=pytz.UTC)
datetime.datetime(2011, 8, 15, 8, 15, 12, tzinfo=)
>>> unaware == aware
Traceback (most recent call last):
  File "", line 1, in 
TypeError: can't compare offset-naive and offset-aware datetimes
>>> 
5

>>> unaware.replace(tzinfo=pytz.UTC)
datetime.datetime(2011, 8, 15, 8, 15, 12, tzinfo=)
>>> unaware == aware
Traceback (most recent call last):
  File "", line 1, in 
TypeError: can't compare offset-naive and offset-aware datetimes
>>> 
6
>>> unaware.replace(tzinfo=pytz.UTC)
datetime.datetime(2011, 8, 15, 8, 15, 12, tzinfo=)
>>> unaware == aware
Traceback (most recent call last):
  File "", line 1, in 
TypeError: can't compare offset-naive and offset-aware datetimes
>>> 
7
>>> unaware.replace(tzinfo=pytz.UTC)
datetime.datetime(2011, 8, 15, 8, 15, 12, tzinfo=)
>>> unaware == aware
Traceback (most recent call last):
  File "", line 1, in 
TypeError: can't compare offset-naive and offset-aware datetimes
>>> 
8
Unaware
6
Unaware
0

>>> unaware.replace(tzinfo=pytz.UTC)
datetime.datetime(2011, 8, 15, 8, 15, 12, tzinfo=)
>>> unaware == aware
Traceback (most recent call last):
  File "", line 1, in 
TypeError: can't compare offset-naive and offset-aware datetimes
>>> 
7
Aware
2021-08-30T09:55:15.111556+00:00
4

Output:

Aware
2021-08-30 04:46:40.670455+00:00