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 How do I get the UTC time, i.e. milliseconds since Unix epoch on Jan 1, 1970? Mateen Ulhaq 22.2k16 gold badges86 silver badges127 bronze badges asked Apr 11, 2013 at 3:33 James ClarkeJames Clarke 2,1072 gold badges12 silver badges3 bronze badges 3 Use
For your purposes when you need to calculate an amount of time spent between two dates all that you need is to subtract end and start dates. The results of such subtraction is a From the python docs:
And this means that by default you can get any of the fields mentioned in it's definition - days, seconds, microseconds, milliseconds, minutes, hours, weeks. Also timedelta instance has total_seconds() method that:
Mateen Ulhaq 22.2k16 gold badges86 silver badges127 bronze badges answered Apr 11, 2013 at 3:35 Artsiom RudzenkaArtsiom Rudzenka 27k4 gold badges32 silver badges51 bronze badges 2 Timezone-aware
Timestamp in milliseconds since Unix epoch:
Mateen Ulhaq 22.2k16 gold badges86 silver badges127 bronze badges answered Oct 8, 2019 at 22:34 Tim RichardsonTim Richardson 5,6176 gold badges42 silver badges63 bronze badges 4 In the form closest to your original:
If you need to know the number of seconds from 1970-01-01 rather than a native Python
Python has naming conventions that are at odds with what you might be used to in Javascript, see PEP 8. Also, a function that simply returns the result of another function is rather silly; if it's just a matter of making it more accessible, you can create another name for a function by simply assigning it. The first example above could be replaced with:
answered Apr 11, 2013 at 3:36 Mark RansomMark Ransom 290k40 gold badges383 silver badges606 bronze badges 13
answered Sep 3, 2018 at 8:56 1 Timezone aware with zero external dependencies:
answered Feb 25, 2019 at 16:05 Cesar CanassaCesar Canassa 17k9 gold badges64 silver badges68 bronze badges 3 From datetime.datetime you already can export to timestamps with method strftime. Following your function example:
If you want microseconds, you need to change the export string and cast to float like: answered Mar 21, 2017 at 17:19 hectorcantohectorcanto 1,76713 silver badges17 bronze badges 3 you could use
answered Feb 19, 2021 at 6:41 HCHOHCHO 951 silver badge5 bronze badges why all reply based on datetime and not time? i think is the easy way !
answered Aug 28, 2021 at 14:14 1 |