Hướng dẫn set timezone python
Tổng quanGầ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. Show
Nội dung chính
Tips 1: Chuẩn hóa múi giờ sử dụngĐầu tiên, cũng là quan trọng nhất, chuẩn hóa múi giờ sử dụng để convert các kiểu thời gian. Mình lựa chọn sử dụng chuẩn múi giờ UTC để chuẩn hóa thời gian cho cả hệ thống cũng như làm múi giờ chuẩn để xử lý các loại format thời gian khác nhau. Tham khảo thêm về thời gian UTC tại Tips 2: Convert String thành DatetimeXử lý bằng hàm
Xử lý bằng strptime
Lưu ý:
Tips 3: Chuyển hóa kiểu Datetime sang Timestamp PythonTimestamp là kiểu thời gian thông dụng của hệ thông Unix, tìm hiểu thêm tại
Tips 4: Chuyển hóa kiểu native date sang UTC timezoneKiểm tra kiểu timezone
Lưu ý: không sử dụng hàm replace, hàm replace sẽ chỉ thay đổi tzinfo không qui đổi thời gian từ múi giờ này sang múi giờ khác Tips 5: Convert UTC sang timezone khácNếu bạn ở múi giờ Việt Nam (GMT + 7), tức nếu hiện tại là 8 giờ sáng (giờ Việt Nam) thì quy ra giờ UTC tức 1 giờ sáng (giờ UTC). Vậy nếu Datetime đang ở múi giờ UTC, ta phải convert nó sáng giờ Việt Nam
Tips 6: Chuyên từ Timestamp sang Datetime
Tips 7: Convert Datetime sang ISO 8601ISO 8601 là một tiêu chuẩn quốc tế, được đưa ra bởi Tổ chức tiêu chuẩn hóa quốc tế (ISO) lần đầu tiên năm 1988, mô tả quy cách viết ngày tháng và thời gian theo cách đơn giản nhất mà máy tính có thể hiểu được. Còn đối với Python ISO 8601 Datetime là string format time thông dụng.
Nguồnhttps://vi.wikipedia.org/wiki/Th%E1%BA%A3o_lu%E1%BA%ADn:M%C3%BAi_gi%E1%BB%9D https://vi.wikipedia.org/wiki/Th%E1%BA%A3o_lu%E1%BA%ADn:M%C3%BAi_gi%E1%BB%9D Thực hiện bởi cloud365.vn What I need to do I have a timezone-unaware datetime object, to which I need to add a time zone in order to be able to compare it with other timezone-aware datetime objects. I do not want to convert my entire application to timezone unaware for this one legacy case. What I've Tried First, to demonstrate the problem:
First, I tried astimezone:
It's not terribly surprising this failed, since it's actually trying to do a conversion. Replace seemed like a better choice (as per How do I get a value of datetime.today() in Python that is "timezone aware"?):
But as you can see, replace seems to set the tzinfo, but not make the object aware. I'm getting ready to fall back to doctoring the input string to have a timezone before parsing it (I'm using dateutil for parsing, if that matters), but that seems incredibly kludgy. Also, I've tried this in both Python 2.6 and Python 2.7, with the same results. Context I am writing a parser for some data files. There is an old format I need to support where the date string does not have a timezone indicator. I've already fixed the data source, but I still need to support the legacy data format. A one time conversion of the legacy data is not an option for various business BS reasons. While in general, I do not like the idea of hard-coding a default timezone, in this case it seems like the best option. I know with reasonable confidence that all the legacy data in question is in UTC, so I'm prepared to accept the risk of defaulting to that in this case. |