114 lines
3.6 KiB
Python
114 lines
3.6 KiB
Python
import time
|
||
import datetime
|
||
from enum import Enum
|
||
|
||
|
||
class TimeUtils:
|
||
"""
|
||
时间工具类
|
||
功能:
|
||
1. 获取当前时间的各种格式化输出
|
||
2. 时间转换
|
||
3. 时间计算
|
||
"""
|
||
|
||
class Weekday(Enum):
|
||
MONDAY = "星期一"
|
||
TUESDAY = "星期二"
|
||
WEDNESDAY = "星期三"
|
||
THURSDAY = "星期四"
|
||
FRIDAY = "星期五"
|
||
SATURDAY = "星期六"
|
||
SUNDAY = "星期日"
|
||
|
||
@staticmethod
|
||
def get_current_time():
|
||
"""
|
||
获取当前时间,格式:年-月-日-时-分
|
||
返回:str,例如:2023-11-15-14-30
|
||
"""
|
||
now = datetime.datetime.now()
|
||
return now.strftime("%Y-%m-%d-%H-%M")
|
||
|
||
@staticmethod
|
||
def get_current_time_str():
|
||
"""
|
||
获取当前时间,格式:年-月-日-时-分
|
||
返回:str,例如:2023-11-15-14-30
|
||
"""
|
||
now = datetime.datetime.now()
|
||
return now.strftime("%Y%m%d%H%M")
|
||
|
||
@staticmethod
|
||
def get_current_time_with_weekday():
|
||
"""
|
||
获取当前时间和星期几
|
||
返回:str,例如:2023-11-15-14-30 星期三
|
||
"""
|
||
now = datetime.datetime.now()
|
||
weekday = TimeUtils.Weekday((now.weekday() + 1) % 7 + 1).value
|
||
return f"{now.strftime('%Y-%m-%d-%H-%M')} {weekday}"
|
||
|
||
@staticmethod
|
||
def get_timestamp():
|
||
"""
|
||
获取当前时间戳
|
||
返回:int,例如:1636969800
|
||
"""
|
||
return int(time.time())
|
||
|
||
@staticmethod
|
||
def get_formatted_time(timestamp=None, format_str="%Y-%m-%d %H:%M:%S"):
|
||
"""
|
||
格式化时间
|
||
:param timestamp: 时间戳,默认为当前时间
|
||
:param format_str: 格式字符串
|
||
返回:str,例如:2023-11-15 14:30:00
|
||
"""
|
||
if timestamp is None:
|
||
timestamp = time.time()
|
||
return time.strftime(format_str, time.localtime(timestamp))
|
||
|
||
@staticmethod
|
||
def time_str_to_timestamp(time_str, format_str="%Y-%m-%d %H:%M:%S"):
|
||
"""
|
||
时间字符串转时间戳
|
||
:param time_str: 时间字符串
|
||
:param format_str: 格式字符串
|
||
返回:int,例如:1636969800
|
||
"""
|
||
time_array = time.strptime(time_str, format_str)
|
||
return int(time.mktime(time_array))
|
||
|
||
@staticmethod
|
||
def get_time_difference(start_time, end_time=None, format_str="%Y-%m-%d %H:%M:%S"):
|
||
"""
|
||
计算两个时间的差值(秒)
|
||
:param start_time: 开始时间(字符串或datetime对象)
|
||
:param end_time: 结束时间(字符串或datetime对象),默认为当前时间
|
||
:param format_str: 当参数为字符串时的格式
|
||
返回:timedelta对象
|
||
"""
|
||
if isinstance(start_time, str):
|
||
start_time = datetime.datetime.strptime(start_time, format_str)
|
||
if end_time is None:
|
||
end_time = datetime.datetime.now()
|
||
elif isinstance(end_time, str):
|
||
end_time = datetime.datetime.strptime(end_time, format_str)
|
||
|
||
return end_time - start_time
|
||
|
||
|
||
# 使用示例
|
||
if __name__ == "__main__":
|
||
print("当前时间(年-月-日-时-分):", TimeUtils.get_current_time())
|
||
print("当前时间和星期:", TimeUtils.get_current_time_with_weekday())
|
||
print("当前时间戳:", TimeUtils.get_timestamp())
|
||
print("格式化当前时间:", TimeUtils.get_formatted_time())
|
||
|
||
time_str = "2023-11-15 14:30:00"
|
||
print(f"'{time_str}' 转时间戳:", TimeUtils.time_str_to_timestamp(time_str))
|
||
|
||
start_time = "2023-11-15 14:30:00"
|
||
print(f"从 '{start_time}' 到现在的时间差:", TimeUtils.get_time_difference(start_time))
|