Độ lệch UTC

Làm việc với Ngày và Giờ trong Python

Max Shron

Data Scientist and Author

Làm việc với Ngày và Giờ trong Python

Làm việc với Ngày và Giờ trong Python

Làm việc với Ngày và Giờ trong Python

Làm việc với Ngày và Giờ trong Python

Làm việc với Ngày và Giờ trong Python

Làm việc với Ngày và Giờ trong Python

UTC

# Import relevant classes
from datetime import datetime, timedelta, timezone
Làm việc với Ngày và Giờ trong Python

UTC

# Import relevant classes
from datetime import datetime, timedelta, timezone

# US Eastern Standard time zone
ET = timezone(timedelta(hours=-5))

# Timezone-aware datetime dt = datetime(2017, 12, 30, 15, 9, 3, tzinfo = ET)
print(dt)
2017-12-30 15:09:03-05:00
Làm việc với Ngày và Giờ trong Python

UTC

# India Standard time zone
IST = timezone(timedelta(hours=5, minutes=30))

# Convert to IST print(dt.astimezone(IST))
2017-12-31 01:39:03+05:30
Làm việc với Ngày và Giờ trong Python

Điều chỉnh múi giờ vs thay đổi tzinfo

print(dt)
2017-12-30 15:09:03-05:00

print(dt.replace(tzinfo=timezone.utc))
2017-12-30 15:09:03+00:00
# Change original to match UTC
print(dt.astimezone(timezone.utc))
2017-12-30 20:09:03+00:00
Làm việc với Ngày và Giờ trong Python

Độ lệch UTC

Làm việc với Ngày và Giờ trong Python

Preparing Video For Download...