Python date and timestamp conversion
Modules for processing time in Python
The modules that process time in Python include time, datetime and calendar.
How to represent time in Python:
- Timestamp: 10-bit integer digits and several decimal digits, for example 1551153156.6358607
- Tuple (struct_time): A tuple containing 9 elements, such as (tm_year=2011, tm_mon=9, tm_mday=28, tm_hour=10, tm_min=0, tm_sec=0, tm_wday=2, tm_yday=271, tm_isdst=-1)
- Format string: Formatted time string, for example ‘2019-02-26 12:45:46’
time module,Tuple (struct_time) is the coreImplement mutual conversion of timestamps and formatted time strings.
datetime module,datetime class instance objectImplement mutual conversion of timestamps and formatted time strings for the core.
Python time module
The time module is implemented by calling the C library, so it cannot be used on some platforms. Most interfaces are consistent with the C standard library.
Use the time module to convert formatted time strings and timestamps.
Convert timestamps to formatted strings
Use the time module to get the timestamp of the current time:
>>> import time >>> () 1551157481.034565
Convert timestamps to tuples (struct_time):
>>> (()) time.struct_time(tm_year=2019, tm_mon=2, tm_mday=26, tm_hour=13, tm_min=2, tm_sec=1, tm_wday=1, tm_yday=57, tm_isdst=0)
Convert a tuple (struct_time) to a formatted time string:
>>> ('%Y-%m-%d %H:%M:%S', (())) '2019-02-26 13:04:41'
Python's datetime module
Compared with the time module, the datetime module provides a more direct and easy-to-use interface and has more powerful functions.
The datetime module provides classes that handle dates and times, both simple and complex. Although it supports date and time algorithms, its implementation focuses on output formatting operations and more efficient attribute extraction functions.
Classes defined in the datetime module (the objects of these classes are immutable):
: Represents a date. Commonly used attributes include year, month and day
: Represents time. Commonly used attributes include hour, minute, second and microsecond
: Indicates date and time
: Represents the time interval between two date, time and datetime instances, with a minimum unit of up to microseconds
: Abstract base class for time zone related objects, used by time and datetime classes
: New functionality added in Python 3.2 to implement the tzinfo abstract base class class, representing a fixed offset from UTC
Convert timestamps to formatted time strings using datetime class in datetime module
Use the datetime class to convert timestamps to datetime instances:
>>> import time >>> from datetime import datetime >>> dt = (()) >>> dt (2019, 2, 26, 15, 27, 28, 678923)
Convert datetime instance to formatted string:
>>> ('%Y-%m-%d %H:%M:%S') '2019-02-26 15:27:28'
Convert formatted time strings to timestamps using the datetime class in the datetime module
Use the datetime class to convert the formatted string '2019-02-26 15:27:28' to datetime instance:
>>> st = '2019-02-26 15:27:28' >>> dt = (st, '%Y-%m-%d %H:%M:%S') >>> dt (2019, 2, 26, 15, 27, 28)
Turn datetime instance into tuple (struct_time):
>>> tp = () >>> tp time.struct_time(tm_year=2019, tm_mon=2, tm_mday=26, tm_hour=15, tm_min=27, tm_sec=28, tm_wday=1, tm_yday=57, tm_isdst=-1)
Convert tuples (struct_time) to timestamps:
>>> (tp) 1551166048.0
You can also use the timestamp() function of the datetime instance to directly get the timestamp:
>>> () 1551166048.0
Use datetime class to get the current date and time
Use the datetime class to get the current date:
>>> ().date().strftime('%Y-%m-%d') '2019-02-26'
Use the datetime class to get the current time:
>>> ().time().strftime('%H:%M:%S') '15:48:43'
This is the end of this article about the implementation of Python date and timestamp conversion. For more related content on Python date and timestamp conversion, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!