Time2Time
This is a utility tool for converting time formats.
In Python, conversion between different time formats has always been a nuisance.
To tackle this issue, we've developed several conversion functions that allow seamless conversion between datetime, struct_time, timestamp, and time strings.
Below is the relationship diagram of these functions:
If you're curious about how the above diagram was created, you can refer to the Mermaid code below:
graph TD
timestamp(timestamp)
struct_time(struct_time)
datetime(datetime)
str(Time String)
timestamp -->|timestamp2datetime| datetime
timestamp -->|timestamp2time| struct_time
timestamp -->|timestamp2str| str
struct_time -->|time2datetime| datetime
struct_time -->|time2timestamp| timestamp
struct_time -->|time2str| str
datetime -->|datetime2time| struct_time
datetime -->|datetime2timestamp| timestamp
datetime -->|datetime2str| str
str -->|str2time| struct_time
str -->|str2datetime| datetime
str -->|str2timestamp| timestamp
Look at the pictures. First find the conversion function you need, and then search further:
timestamp2datetime
-
Description: Convert a timestamp to a
datetime
object. -
Parameters:
- ts (
Union[int, float]
): The timestamp.
- ts (
-
Returns:
- datetime: The
datetime
object.
- datetime: The
-
Example:
import docsaidkit as D
ts = 1634025600
dt = D.timestamp2datetime(ts)
print(dt)
# >>> 2021-10-12 16:00:00
timestamp2time
-
Description: Convert a timestamp to a
struct_time
object. -
Parameters:
- ts (
Union[int, float]
): The timestamp.
- ts (
-
Returns:
- struct_time: The
struct_time
object.
- struct_time: The
-
Example:
import docsaidkit as D
ts = 1634025600
t = D.timestamp2time(ts)
print(t)
# >>> time.struct_time(tm_year=2021, tm_mon=10, tm_mday=12, tm_hour=16, tm_min=0, tm_sec=0, tm_wday=1, tm_yday=285, tm_isdst=0)
timestamp2str
-
Description: Convert a timestamp to a string representation of time.
-
Parameters:
- ts (
Union[int, float]
): The timestamp. - fmt (
str
): The format of the time.
- ts (
-
Returns:
- str: The string representation of time.
-
Example:
import docsaidkit as D
ts = 1634025600
s = D.timestamp2str(ts, fmt='%Y-%m-%d %H:%M:%S')
print(s)
# >>> '2021-10-12 16:00:00'
time2datetime
-
Description: Convert
struct_time
todatetime
. -
Parameters:
- t (
struct_time
): Thestruct_time
.
- t (
-
Returns:
- datetime: The
datetime
.
- datetime: The
-
Example:
import docsaidkit as D
ts = 1634025600
t = D.timestamp2time(ts)
dt = D.time2datetime(t)
print(dt)
# >>> datetime.datetime(2021, 10, 12, 16, 0)
time2timestamp
-
Description: Convert
struct_time
to a timestamp. -
Parameters:
- t (
struct_time
): Thestruct_time
.
- t (
-
Returns:
- float: The timestamp.
-
Example:
import docsaidkit as D
ts = 1634025600
t = D.timestamp2time(ts)
ts = D.time2timestamp(t)
print(ts)
# >>> 1634025600.0
time2str
-
Description: Convert
struct_time
to a time string. -
Parameters:
- t (
struct_time
): Thestruct_time
. - fmt (
str
): The time format.
- t (
-
Returns:
- str: The time string.
-
Example:
import docsaidkit as D
ts = 1634025600
t = D.timestamp2time(ts)
s = D.time2str(t, fmt='%Y-%m-%d %H:%M:%S')
print(s)
# >>> '2021-10-12 16:00:00'
datetime2time
-
Description: Convert
datetime
tostruct_time
. -
Parameters:
- dt (
datetime
): Thedatetime
.
- dt (
-
Returns:
- struct_time: The
struct_time
.
- struct_time: The
-
Example:
import docsaidkit as D
ts = 1634025600
dt = D.timestamp2datetime(ts)
t = D.datetime2time(dt)
print(t)
# >>> time.struct_time(tm_year=2021, tm_mon=10, tm_mday=12, tm_hour=16, tm_min=0, tm_sec=0, tm_wday=1, tm_yday=285, tm_isdst=-1)
datetime2timestamp
-
Description: Convert
datetime
to a timestamp. -
Parameters:
- dt (
datetime
): Thedatetime
.
- dt (
-
Returns:
- float: The timestamp.
-
Example:
import docsaidkit as D
ts = 1634025600
dt = D.timestamp2datetime(ts)
ts = D.datetime2timestamp(dt)
print(ts)
# >>> 1634025600.0
datetime2str
-
Description: Convert
datetime
to a time string. -
Parameters:
- dt (
datetime
): Thedatetime
. - fmt (
str
): The time format.
- dt (
-
Returns:
- str: The time string.
-
Example:
import docsaidkit as D
ts = 1634025600
dt = D.timestamp2datetime(ts)
s = D.datetime2str(dt, fmt='%Y-%m-%d %H:%M:%S')
print(s)
# >>> '2021-10-12 16:00:00'
str2time
-
Description: Convert a time string to
struct_time
. -
Parameters:
- s (
str
): The time string. - fmt (
str
): The time format.
- s (
-
Returns:
- struct_time: The
struct_time
.
- struct_time: The
-
Example:
import docsaidkit as D
s = '2021-10-12 16:00:00'
t = D.str2time(s, fmt='%Y-%m-%d %H:%M:%S')
print(t)
# >>> time.struct_time(tm_year=2021, tm_mon=10, tm_mday=12, tm_hour=16, tm_min=0, tm_sec=0, tm_wday=1, tm_yday=285, tm_isdst=-1)
str2datetime
-
Description: Convert a time string to
datetime
. -
Parameters:
- s (
str
): The time string. - fmt (
str
): The time format.
- s (
-
Returns:
- datetime:
datetime
.
- datetime:
-
Example:
import docsaidkit as D
s = '2021-10-12 16:00:00'
dt = D.str2datetime(s, fmt='%Y-%m-%d %H:%M:%S')
print(dt)
# >>> datetime.datetime(2021, 10, 12, 16, 0)
str2timestamp
-
Description: Convert a time string to a timestamp.
-
Parameters:
- s (
str
): The time string. - fmt (
str
): The time format.
- s (
-
Returns:
- float: Timestamp.
-
Example:
import docsaidkit as D
s = '2021-10-12 16:00:00'
ts = D.str2timestamp(s, fmt='%Y-%m-%d %H:%M:%S')
print(ts)
# >>> 1634025600.0