Hướng dẫn python tzinfo - trăn tzinfo

Please note: The first part of this answer is or version 1.x of pendulum. See below for a version 2.x answer.

Nội dung chính ShowShow

  • Tổng quan
  • Tips 1: Chuẩn hóa múi giờ sử dụng
  • Tips 2: Convert String thành Datetime
  • Tips 3: Chuyển hóa kiểu Datetime sang Timestamp Python
  • Tips 4: Chuyển hóa kiểu native date sang UTC timezone
  • Tips 5: Convert UTC sang timezone khác
  • Tips 6: Chuyên từ Timestamp sang Datetime
  • Tips 7: Convert Datetime sang ISO 8601
  • Múi giờ (timezone) là gì?
  • Vậy ta lấy gì làm mốc?
  • Còn vấn đề nào khác?
  • Best practices
  • Convert timezone
  • Sử dụng UTC trong tính toán nội bộ
  • Không nên dùng offset-aware datetime

I hope I'm not too late!

The pendulum library excels at this and other date-time calculations.

>>> import pendulum
>>> some_time_zones = ['Europe/Paris', 'Europe/Moscow', 'America/Toronto', 'UTC', 'Canada/Pacific', 'Asia/Macao']
>>> heres_a_time = '1996-03-25 12:03 -0400'
>>> pendulum_time = pendulum.datetime.strptime(heres_a_time, '%Y-%m-%d %H:%M %z')
>>> for tz in some_time_zones:
...     tz, pendulum_time.astimezone(tz)
...     
('Europe/Paris', )
('Europe/Moscow', )
('America/Toronto', )
('UTC', )
('Canada/Pacific', )
('Asia/Macao', )

Answer lists the names of the time zones that may be used with pendulum. (They're the same as for pytz.)

For version 2:

  • >>> import pendulum
    >>> some_time_zones = ['Europe/Paris', 'Europe/Moscow', 'America/Toronto', 'UTC', 'Canada/Pacific', 'Asia/Macao']
    >>> heres_a_time = '1996-03-25 12:03 -0400'
    >>> pendulum_time = pendulum.from_format('1996-03-25 12:03 -0400', 'YYYY-MM-DD hh:mm ZZ')
    >>> for tz in some_time_zones:
    ...     tz, pendulum_time.in_tz(tz)
    ...     
    ('Europe/Paris', DateTime(1996, 3, 25, 17, 3, 0, tzinfo=Timezone('Europe/Paris')))
    ('Europe/Moscow', DateTime(1996, 3, 25, 19, 3, 0, tzinfo=Timezone('Europe/Moscow')))
    ('America/Toronto', DateTime(1996, 3, 25, 11, 3, 0, tzinfo=Timezone('America/Toronto')))
    ('UTC', DateTime(1996, 3, 25, 16, 3, 0, tzinfo=Timezone('UTC')))
    ('Canada/Pacific', DateTime(1996, 3, 25, 8, 3, 0, tzinfo=Timezone('Canada/Pacific')))
    ('Asia/Macao', DateTime(1996, 3, 26, 0, 3, 0, tzinfo=Timezone('Asia/Macao')))
    
    3 is a list of the names of the time zones that might be used in a program
  • >>> import pendulum
    >>> some_time_zones = ['Europe/Paris', 'Europe/Moscow', 'America/Toronto', 'UTC', 'Canada/Pacific', 'Asia/Macao']
    >>> heres_a_time = '1996-03-25 12:03 -0400'
    >>> pendulum_time = pendulum.from_format('1996-03-25 12:03 -0400', 'YYYY-MM-DD hh:mm ZZ')
    >>> for tz in some_time_zones:
    ...     tz, pendulum_time.in_tz(tz)
    ...     
    ('Europe/Paris', DateTime(1996, 3, 25, 17, 3, 0, tzinfo=Timezone('Europe/Paris')))
    ('Europe/Moscow', DateTime(1996, 3, 25, 19, 3, 0, tzinfo=Timezone('Europe/Moscow')))
    ('America/Toronto', DateTime(1996, 3, 25, 11, 3, 0, tzinfo=Timezone('America/Toronto')))
    ('UTC', DateTime(1996, 3, 25, 16, 3, 0, tzinfo=Timezone('UTC')))
    ('Canada/Pacific', DateTime(1996, 3, 25, 8, 3, 0, tzinfo=Timezone('Canada/Pacific')))
    ('Asia/Macao', DateTime(1996, 3, 26, 0, 3, 0, tzinfo=Timezone('Asia/Macao')))
    
    4 is a sample time, complete with a time zone in the form '-0400'
  • I begin by converting the time to a pendulum time for subsequent processing
  • now I can show what this time is in each of the time zones in
    >>> import pendulum
    >>> some_time_zones = ['Europe/Paris', 'Europe/Moscow', 'America/Toronto', 'UTC', 'Canada/Pacific', 'Asia/Macao']
    >>> heres_a_time = '1996-03-25 12:03 -0400'
    >>> pendulum_time = pendulum.from_format('1996-03-25 12:03 -0400', 'YYYY-MM-DD hh:mm ZZ')
    >>> for tz in some_time_zones:
    ...     tz, pendulum_time.in_tz(tz)
    ...     
    ('Europe/Paris', DateTime(1996, 3, 25, 17, 3, 0, tzinfo=Timezone('Europe/Paris')))
    ('Europe/Moscow', DateTime(1996, 3, 25, 19, 3, 0, tzinfo=Timezone('Europe/Moscow')))
    ('America/Toronto', DateTime(1996, 3, 25, 11, 3, 0, tzinfo=Timezone('America/Toronto')))
    ('UTC', DateTime(1996, 3, 25, 16, 3, 0, tzinfo=Timezone('UTC')))
    ('Canada/Pacific', DateTime(1996, 3, 25, 8, 3, 0, tzinfo=Timezone('Canada/Pacific')))
    ('Asia/Macao', DateTime(1996, 3, 26, 0, 3, 0, tzinfo=Timezone('Asia/Macao')))
    
    5

...

>>> import pendulum
>>> some_time_zones = ['Europe/Paris', 'Europe/Moscow', 'America/Toronto', 'UTC', 'Canada/Pacific', 'Asia/Macao']
>>> heres_a_time = '1996-03-25 12:03 -0400'
>>> pendulum_time = pendulum.from_format('1996-03-25 12:03 -0400', 'YYYY-MM-DD hh:mm ZZ')
>>> for tz in some_time_zones:
...     tz, pendulum_time.in_tz(tz)
...     
('Europe/Paris', DateTime(1996, 3, 25, 17, 3, 0, tzinfo=Timezone('Europe/Paris')))
('Europe/Moscow', DateTime(1996, 3, 25, 19, 3, 0, tzinfo=Timezone('Europe/Moscow')))
('America/Toronto', DateTime(1996, 3, 25, 11, 3, 0, tzinfo=Timezone('America/Toronto')))
('UTC', DateTime(1996, 3, 25, 16, 3, 0, tzinfo=Timezone('UTC')))
('Canada/Pacific', DateTime(1996, 3, 25, 8, 3, 0, tzinfo=Timezone('Canada/Pacific')))
('Asia/Macao', DateTime(1996, 3, 26, 0, 3, 0, tzinfo=Timezone('Asia/Macao')))

Tổng quan

Gần đây mình có làm việc nhiều với kiểu dữ liệu Datetime trong Python. Vấn đề mình gặp phải là xử lý nhiều loại time format khác nhau, chuyển hóa thành dạng Datetime, chuẩn hóa thời gian lưu trữ và lưu vào database. Và sau đây, mình sẽ viết bài chia sẻ về cách mình xử lý kiểu dữ liệu Datetime trong Python. Trong bài mình sẽ sử dụng Python 3 để xử lý kiểu dữ liệu Datetime.

Nội dung chính

  • Tổng quan
  • Tips 1: Chuẩn hóa múi giờ sử dụng
  • Tips 2: Convert String thành Datetime
  • Tips 3: Chuyển hóa kiểu Datetime sang Timestamp Python
  • Tips 4: Chuyển hóa kiểu native date sang UTC timezone
  • Tips 5: Convert UTC sang timezone khác
  • Tips 6: Chuyên từ Timestamp sang Datetime
  • Tips 7: Convert Datetime sang ISO 8601
  • Múi giờ (timezone) là gì?
  • Vậy ta lấy gì làm mốc?
  • Còn vấn đề nào khác?
  • Best practices
  • Convert timezone
  • Sử dụng UTC trong tính toán nội bộ
  • Không nên dùng offset-aware datetime

Tips 1: Chuẩn hóa múi giờ sử dụng

I hope I'm not too late!

The pendulum library excels at this and other date-time calculations.

Tips 2: Convert String thành Datetime

Answer lists the names of the time zones that may be used with pendulum. (They're the same as for pytz.)

import datetime
import pytz
from dateutil.parser import parse

# Dạng string time
date_string = '2019-03-20T03:41:16Z'

# Dạng datetime format
date_time_python = parse(date_string)

For version 2:

import datetime
import pytz
from dateutil.parser import parse

# Dạng string time
date_string = '2019-03-21 03:41:16'

# Strptime
format = '%Y-%m-%d %H:%M:%S'
date_time_python = datetime.datetime.strptime(date_string, format)

>>> import pendulum
>>> some_time_zones = ['Europe/Paris', 'Europe/Moscow', 'America/Toronto', 'UTC', 'Canada/Pacific', 'Asia/Macao']
>>> heres_a_time = '1996-03-25 12:03 -0400'
>>> pendulum_time = pendulum.from_format('1996-03-25 12:03 -0400', 'YYYY-MM-DD hh:mm ZZ')
>>> for tz in some_time_zones:
...     tz, pendulum_time.in_tz(tz)
...     
('Europe/Paris', DateTime(1996, 3, 25, 17, 3, 0, tzinfo=Timezone('Europe/Paris')))
('Europe/Moscow', DateTime(1996, 3, 25, 19, 3, 0, tzinfo=Timezone('Europe/Moscow')))
('America/Toronto', DateTime(1996, 3, 25, 11, 3, 0, tzinfo=Timezone('America/Toronto')))
('UTC', DateTime(1996, 3, 25, 16, 3, 0, tzinfo=Timezone('UTC')))
('Canada/Pacific', DateTime(1996, 3, 25, 8, 3, 0, tzinfo=Timezone('Canada/Pacific')))
('Asia/Macao', DateTime(1996, 3, 26, 0, 3, 0, tzinfo=Timezone('Asia/Macao')))
3 is a list of the names of the time zones that might be used in a program

  • >>> import pendulum
    >>> some_time_zones = ['Europe/Paris', 'Europe/Moscow', 'America/Toronto', 'UTC', 'Canada/Pacific', 'Asia/Macao']
    >>> heres_a_time = '1996-03-25 12:03 -0400'
    >>> pendulum_time = pendulum.from_format('1996-03-25 12:03 -0400', 'YYYY-MM-DD hh:mm ZZ')
    >>> for tz in some_time_zones:
    ...     tz, pendulum_time.in_tz(tz)
    ...     
    ('Europe/Paris', DateTime(1996, 3, 25, 17, 3, 0, tzinfo=Timezone('Europe/Paris')))
    ('Europe/Moscow', DateTime(1996, 3, 25, 19, 3, 0, tzinfo=Timezone('Europe/Moscow')))
    ('America/Toronto', DateTime(1996, 3, 25, 11, 3, 0, tzinfo=Timezone('America/Toronto')))
    ('UTC', DateTime(1996, 3, 25, 16, 3, 0, tzinfo=Timezone('UTC')))
    ('Canada/Pacific', DateTime(1996, 3, 25, 8, 3, 0, tzinfo=Timezone('Canada/Pacific')))
    ('Asia/Macao', DateTime(1996, 3, 26, 0, 3, 0, tzinfo=Timezone('Asia/Macao')))
    
    4 is a sample time, complete with a time zone in the form '-0400'
  • I begin by converting the time to a pendulum time for subsequent processing

Tips 3: Chuyển hóa kiểu Datetime sang Timestamp Python

now I can show what this time is in each of the time zones in

>>> import pendulum
>>> some_time_zones = ['Europe/Paris', 'Europe/Moscow', 'America/Toronto', 'UTC', 'Canada/Pacific', 'Asia/Macao']
>>> heres_a_time = '1996-03-25 12:03 -0400'
>>> pendulum_time = pendulum.from_format('1996-03-25 12:03 -0400', 'YYYY-MM-DD hh:mm ZZ')
>>> for tz in some_time_zones:
...     tz, pendulum_time.in_tz(tz)
...     
('Europe/Paris', DateTime(1996, 3, 25, 17, 3, 0, tzinfo=Timezone('Europe/Paris')))
('Europe/Moscow', DateTime(1996, 3, 25, 19, 3, 0, tzinfo=Timezone('Europe/Moscow')))
('America/Toronto', DateTime(1996, 3, 25, 11, 3, 0, tzinfo=Timezone('America/Toronto')))
('UTC', DateTime(1996, 3, 25, 16, 3, 0, tzinfo=Timezone('UTC')))
('Canada/Pacific', DateTime(1996, 3, 25, 8, 3, 0, tzinfo=Timezone('Canada/Pacific')))
('Asia/Macao', DateTime(1996, 3, 26, 0, 3, 0, tzinfo=Timezone('Asia/Macao')))
5

import datetime

# Dạng datetime
date_time_now = datetime.datetime.now()

# Dạng timestamp
timestamp_now = date_time_now.timestamp()

Tips 4: Chuyển hóa kiểu native date sang UTC timezone

Tips 5: Convert UTC sang timezone khác

import datetime
import pytz

# Dạng native date
date_time_now = datetime.datetime.now()
print(date_time_now.tzname())

# Dạng utc
UTC = pytz.utc
date_time_utc_now = UTC.localize(date_time_now)
print(date_time_utc_now.tzname())

Tips 6: Chuyên từ Timestamp sang Datetime

Tips 5: Convert UTC sang timezone khác

Tips 6: Chuyên từ Timestamp sang Datetime

import datetime
import pytz

# Dạng native date
date_time_now = datetime.datetime.now()
print(date_time_now.tzname())

# Dạng utc
UTC = pytz.utc
date_time_utc_now = UTC.localize(date_time_now)
print(date_time_utc_now.tzname())

# Dạng 'Asia/Ho_Chi_Minh'

VN_TZ = pytz.timezone('Asia/Ho_Chi_Minh')
date_time_vntz_now = date_time_utc_now.astimezone(VN_TZ)
print(date_time_vntz_now.tzname())

Tips 6: Chuyên từ Timestamp sang Datetime

import datetime

date_time_now = datetime.datetime.now()
timestamp_now = date_time_now.timestamp()

# Convert timestamp thành dạng Datetime
timestamp_to_datetime = datetime.datetime.fromtimestamp(timestamp_now)

Tips 7: Convert Datetime sang ISO 8601

Múi giờ (timezone) là gì?

import datetime

date_time_now = datetime.datetime.now()

# Convert Datetime thành dạng ISO 8601
iso_format = date_time_now.isoformat()

Vậy ta lấy gì làm mốc?

Còn vấn đề nào khác?

Còn vấn đề nào khác?


Best practices

Múi giờ (timezone) là gì?

Vậy ta lấy gì làm mốc?

# Zone	NAME		GMTOFF	RULES	FORMAT	[UNTIL]
Zone Asia/Ho_Chi_Minh	7:06:40 -	LMT	1906 Jul  1
			7:06:30	-	PLMT	1911 May  1 # Phù Liễn MT
			7:00	-	+07	1942 Dec 31 23:00
			8:00	-	+08	1945 Mar 14 23:00
			9:00	-	+09	1945 Sep  2
			7:00	-	+07	1947 Apr  1
			8:00	-	+08	1955 Jul  1
			7:00	-	+07	1959 Dec 31 23:00
			8:00	-	+08	1975 Jun 13
			7:00	-	+07

Còn vấn đề nào khác?

Best practices

Cũng chính vì thế mà DST gây ra những vấn đề lớn trong tính toán.

Vậy ta lấy gì làm mốc?

Múi giờ lấy làm mốc bây giờ là UTC. UTC là múi giờ không có DST hay những thay đổi trong quá khứ. Từ UTC bạn có thể chuyển đổi giờ sang giờ địa phương, nhưng ngược lại chưa chắc đã đúng vì lý do đã nêu ở trên.

Luôn luôn tính toán và lưu dữ liệu ở múi giờ UTC. Nếu bạn cần lưu múi giờ tại thời điểm diễn ra, lưu nó riêng biệt. Không bao giờ lưu dữ liệu là “giờ địa phương + múi giờ”.

Còn vấn đề nào khác?

Tuy nhiên trong thư viện chuẩn của Python lại có sai sót trong thiết kế:

  1. import datetime
    import pytz
    from dateutil.parser import parse
    
    # Dạng string time
    date_string = '2019-03-20T03:41:16Z'
    
    # Dạng datetime format
    date_time_python = parse(date_string)
    
    0 module không bao gồm dữ liệu về timezone vì timezone hay thay đổi
  2. Tuy nhiên
    import datetime
    import pytz
    from dateutil.parser import parse
    
    # Dạng string time
    date_string = '2019-03-20T03:41:16Z'
    
    # Dạng datetime format
    date_time_python = parse(date_string)
    
    0 module phải cung cấp api để lưu thông tin về timezone vào
    import datetime
    import pytz
    from dateutil.parser import parse
    
    # Dạng string time
    date_string = '2019-03-20T03:41:16Z'
    
    # Dạng datetime format
    date_time_python = parse(date_string)
    
    0 object
  3. Nó nên cung cấp những object sau:
    import datetime
    import pytz
    from dateutil.parser import parse
    
    # Dạng string time
    date_string = '2019-03-20T03:41:16Z'
    
    # Dạng datetime format
    date_time_python = parse(date_string)
    
    3,
    import datetime
    import pytz
    from dateutil.parser import parse
    
    # Dạng string time
    date_string = '2019-03-20T03:41:16Z'
    
    # Dạng datetime format
    date_time_python = parse(date_string)
    
    4,
    import datetime
    import pytz
    from dateutil.parser import parse
    
    # Dạng string time
    date_string = '2019-03-20T03:41:16Z'
    
    # Dạng datetime format
    date_time_python = parse(date_string)
    
    0 và
    import datetime
    import pytz
    from dateutil.parser import parse
    
    # Dạng string time
    date_string = '2019-03-20T03:41:16Z'
    
    # Dạng datetime format
    date_time_python = parse(date_string)
    
    6

Tuy nhiên một số thứ không đúng. Vấn đề lớn nhất là chúng ta không thể tính toán với datetime object có timezone và datetime object không có chính timezone đó:

>>> import pendulum
>>> some_time_zones = ['Europe/Paris', 'Europe/Moscow', 'America/Toronto', 'UTC', 'Canada/Pacific', 'Asia/Macao']
>>> heres_a_time = '1996-03-25 12:03 -0400'
>>> pendulum_time = pendulum.from_format('1996-03-25 12:03 -0400', 'YYYY-MM-DD hh:mm ZZ')
>>> for tz in some_time_zones:
...     tz, pendulum_time.in_tz(tz)
...     
('Europe/Paris', DateTime(1996, 3, 25, 17, 3, 0, tzinfo=Timezone('Europe/Paris')))
('Europe/Moscow', DateTime(1996, 3, 25, 19, 3, 0, tzinfo=Timezone('Europe/Moscow')))
('America/Toronto', DateTime(1996, 3, 25, 11, 3, 0, tzinfo=Timezone('America/Toronto')))
('UTC', DateTime(1996, 3, 25, 16, 3, 0, tzinfo=Timezone('UTC')))
('Canada/Pacific', DateTime(1996, 3, 25, 8, 3, 0, tzinfo=Timezone('Canada/Pacific')))
('Asia/Macao', DateTime(1996, 3, 26, 0, 3, 0, tzinfo=Timezone('Asia/Macao')))
0

Một vấn đề khác là có 2 cách để tạo datetime object vào thời điểm hiện tại trong Python:

>>> import pendulum
>>> some_time_zones = ['Europe/Paris', 'Europe/Moscow', 'America/Toronto', 'UTC', 'Canada/Pacific', 'Asia/Macao']
>>> heres_a_time = '1996-03-25 12:03 -0400'
>>> pendulum_time = pendulum.from_format('1996-03-25 12:03 -0400', 'YYYY-MM-DD hh:mm ZZ')
>>> for tz in some_time_zones:
...     tz, pendulum_time.in_tz(tz)
...     
('Europe/Paris', DateTime(1996, 3, 25, 17, 3, 0, tzinfo=Timezone('Europe/Paris')))
('Europe/Moscow', DateTime(1996, 3, 25, 19, 3, 0, tzinfo=Timezone('Europe/Moscow')))
('America/Toronto', DateTime(1996, 3, 25, 11, 3, 0, tzinfo=Timezone('America/Toronto')))
('UTC', DateTime(1996, 3, 25, 16, 3, 0, tzinfo=Timezone('UTC')))
('Canada/Pacific', DateTime(1996, 3, 25, 8, 3, 0, tzinfo=Timezone('Canada/Pacific')))
('Asia/Macao', DateTime(1996, 3, 26, 0, 3, 0, tzinfo=Timezone('Asia/Macao')))
1

Một là giờ ở UTC, một là giờ địa phương. Tuy nhiên nó lại không cho bạn biết múi giờ địa phương là gì cũng như với 2 giá trị

import datetime
import pytz
from dateutil.parser import parse

# Dạng string time
date_string = '2019-03-20T03:41:16Z'

# Dạng datetime format
date_time_python = parse(date_string)
7 và
import datetime
import pytz
from dateutil.parser import parse

# Dạng string time
date_string = '2019-03-20T03:41:16Z'

# Dạng datetime format
date_time_python = parse(date_string)
8 bạn không thể biết được cái nào có múi giờ UTC.

Thư viện còn cung cấp

import datetime
import pytz
from dateutil.parser import parse

# Dạng string time
date_string = '2019-03-20T03:41:16Z'

# Dạng datetime format
date_time_python = parse(date_string)
3 object và
import datetime
import pytz
from dateutil.parser import parse

# Dạng string time
date_string = '2019-03-20T03:41:16Z'

# Dạng datetime format
date_time_python = parse(date_string)
4 object. Và cả hai đều không có ý nghĩa gì nữa nếu dính líu đến timezone. Vì nếu chỉ
import datetime
import pytz
from dateutil.parser import parse

# Dạng string time
date_string = '2019-03-20T03:41:16Z'

# Dạng datetime format
date_time_python = parse(date_string)
4 không thì không đủ hoặc nếu là
import datetime
import pytz
from dateutil.parser import parse

# Dạng string time
date_string = '2019-03-20T03:41:16Z'

# Dạng datetime format
date_time_python = parse(date_string)
3 thì nó chỉ mang tính chất địa phương, nó có thể là hôm nay đối với tôi nhưng có thể là hôm qua, hoặc ngày mai đối với bạn.

Best practices

Convert timezone

Nếu bạn muốn tạo

import datetime
import pytz
from dateutil.parser import parse

# Dạng string time
date_string = '2019-03-20T03:41:16Z'

# Dạng datetime format
date_time_python = parse(date_string)
0 object và lưu thông tin timezone, bạn không nên truyền
import datetime
import pytz
from dateutil.parser import parse

# Dạng string time
date_string = '2019-03-21 03:41:16'

# Strptime
format = '%Y-%m-%d %H:%M:%S'
date_time_python = datetime.datetime.strptime(date_string, format)
4 trong constructure mà nên dùng hàm
import datetime
import pytz
from dateutil.parser import parse

# Dạng string time
date_string = '2019-03-21 03:41:16'

# Strptime
format = '%Y-%m-%d %H:%M:%S'
date_time_python = datetime.datetime.strptime(date_string, format)
5 cung cấp bởi
import datetime
import pytz
from dateutil.parser import parse

# Dạng string time
date_string = '2019-03-21 03:41:16'

# Strptime
format = '%Y-%m-%d %H:%M:%S'
date_time_python = datetime.datetime.strptime(date_string, format)
6:

>>> import pendulum
>>> some_time_zones = ['Europe/Paris', 'Europe/Moscow', 'America/Toronto', 'UTC', 'Canada/Pacific', 'Asia/Macao']
>>> heres_a_time = '1996-03-25 12:03 -0400'
>>> pendulum_time = pendulum.from_format('1996-03-25 12:03 -0400', 'YYYY-MM-DD hh:mm ZZ')
>>> for tz in some_time_zones:
...     tz, pendulum_time.in_tz(tz)
...     
('Europe/Paris', DateTime(1996, 3, 25, 17, 3, 0, tzinfo=Timezone('Europe/Paris')))
('Europe/Moscow', DateTime(1996, 3, 25, 19, 3, 0, tzinfo=Timezone('Europe/Moscow')))
('America/Toronto', DateTime(1996, 3, 25, 11, 3, 0, tzinfo=Timezone('America/Toronto')))
('UTC', DateTime(1996, 3, 25, 16, 3, 0, tzinfo=Timezone('UTC')))
('Canada/Pacific', DateTime(1996, 3, 25, 8, 3, 0, tzinfo=Timezone('Canada/Pacific')))
('Asia/Macao', DateTime(1996, 3, 26, 0, 3, 0, tzinfo=Timezone('Asia/Macao')))
2

Bạn có thể thấy với

import datetime
import pytz
from dateutil.parser import parse

# Dạng string time
date_string = '2019-03-20T03:41:16Z'

# Dạng datetime format
date_time_python = parse(date_string)
8 nó sử dụng đúng offset ở thời điểm hiện lại là +8.

Sử dụng UTC trong tính toán nội bộ

Nếu bạn muốn lấy thời gian hiện tại, luôn luôn dùng

import datetime
import pytz
from dateutil.parser import parse

# Dạng string time
date_string = '2019-03-21 03:41:16'

# Strptime
format = '%Y-%m-%d %H:%M:%S'
date_time_python = datetime.datetime.strptime(date_string, format)
8. Khi người dùng nhập giờ địa phương, lập tức chuyển qua UTC rồi tiếp tục xử lý. Nếu bạn không biết múi giờ của người dùng, hãy hỏi họ, không thể tự ý đoán dựa trên IP hay số điện thoại (đây là ý tưởng thực khi mình làm dự án hiện tại).

Không nên dùng offset-aware datetime

import datetime
import pytz
from dateutil.parser import parse

# Dạng string time
date_string = '2019-03-20T03:41:16Z'

# Dạng datetime format
date_time_python = parse(date_string)
0 object là offset-aware khi nó lưu thông tin về timezone, nếu không thì gọi là offset-navie.

Nó có thể là ý tưởng tốt nếu bạn lưu tzinfo trong

import datetime
import pytz
from dateutil.parser import parse

# Dạng string time
date_string = '2019-03-20T03:41:16Z'

# Dạng datetime format
date_time_python = parse(date_string)
0 object. Nhưng sẽ tốt hơn nếu bạn không làm như vậy. Hãy tận dụng hạn chế trong thiết kế API:

  • Trong tính toán nội bộ sử dụng offset-navie
    import datetime
    import pytz
    from dateutil.parser import parse
    
    # Dạng string time
    date_string = '2019-03-20T03:41:16Z'
    
    # Dạng datetime format
    date_time_python = parse(date_string)
    
    0 object và quy ước timezone của nó là UTC
  • Khi có giao tiếp với người dùng, chuyển đổi nó qua giờ địa phương

Tại sao bạn nên làm như vậy? Đầu tiên là nhiều thư viện được viết với ngầm định

import datetime

# Dạng datetime
date_time_now = datetime.datetime.now()

# Dạng timestamp
timestamp_now = date_time_now.timestamp()
2. Thứ hai, sử dụng tzinfo trong khi thư viện chuẩn được thiết kế sai là một ý tưởng tệ hại. Đó cũng là lý do vì sao
import datetime
import pytz
from dateutil.parser import parse

# Dạng string time
date_string = '2019-03-21 03:41:16'

# Strptime
format = '%Y-%m-%d %H:%M:%S'
date_time_python = datetime.datetime.strptime(date_string, format)
6 cung cấp hàm
import datetime
import pytz
from dateutil.parser import parse

# Dạng string time
date_string = '2019-03-21 03:41:16'

# Strptime
format = '%Y-%m-%d %H:%M:%S'
date_time_python = datetime.datetime.strptime(date_string, format)
5 để chuyển đổi timezone vì API của thư viện chuẩn không đủ linh hoạt làm việc đó với hơn 500 timezone. Không sử dụng
import datetime
import pytz
from dateutil.parser import parse

# Dạng string time
date_string = '2019-03-21 03:41:16'

# Strptime
format = '%Y-%m-%d %H:%M:%S'
date_time_python = datetime.datetime.strptime(date_string, format)
4 là cơ hội để bạn trở nên tốt hơn.

Lý do cuối cùng mà bạn không nên dùng offset-aware là bởi vì

import datetime
import pytz
from dateutil.parser import parse

# Dạng string time
date_string = '2019-03-21 03:41:16'

# Strptime
format = '%Y-%m-%d %H:%M:%S'
date_time_python = datetime.datetime.strptime(date_string, format)
4 object của các thư viện của các ngôn ngữ là khác nhau (implementation defined). Hiện nay chưa có chuẩn nào để truyền tzinfo từ ngôn ngữ này sang ngôn ngữ khác hay HTTP ngoài sử dụng UTC offset như hiện tại.