pytest-jequnauto/utils/times_tool/time_control.py

97 lines
2.2 KiB
Python
Raw Normal View History

2023-02-14 11:22:13 +08:00
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
# @Time : 2022/3/28 15:47
# @Author : 李晓杰
"""
import time
from typing import Text
from datetime import datetime
def count_milliseconds():
"""
计算时间
:return:
"""
access_start = datetime.now()
access_end = datetime.now()
access_delta = (access_end - access_start).seconds * 1000
return access_delta
2023-02-23 17:48:37 +08:00
def timestamp_conversion(time_str: Text) -> float:
2023-02-14 11:22:13 +08:00
"""
时间戳转换将日期格式转换成时间戳
:param time_str: 时间
:return:
"""
try:
datetime_format = datetime.strptime(str(time_str), "%Y-%m-%d %H:%M:%S")
2023-02-23 17:48:37 +08:00
# timestamp = int(
# time.mktime(datetime_format.timetuple()) * 1000.0
# + datetime_format.microsecond / 1000.0
# )
timestamp = datetime.timestamp(datetime_format)
2023-02-14 11:22:13 +08:00
return timestamp
except ValueError as exc:
raise ValueError('日期格式错误, 需要传入得格式为 "%Y-%m-%d %H:%M:%S" ') from exc
2023-02-23 17:48:37 +08:00
a = 111
2023-02-14 11:22:13 +08:00
def time_conversion(time_num: int):
"""
时间戳转换成日期
:param time_num:
:return:
"""
if isinstance(time_num, int):
time_stamp = float(time_num / 1000)
time_array = time.localtime(time_stamp)
other_style_time = time.strftime("%Y-%m-%d %H:%M:%S", time_array)
return other_style_time
def now_time():
"""
获取当前时间, 日期格式: 2021-12-11 12:39:25
:return:
"""
localtime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
return localtime
def now_time_day():
"""
获取当前时间, 日期格式: 2021-12-11
:return:
"""
localtime = time.strftime("%Y-%m-%d", time.localtime())
return localtime
def get_time_for_min(minute: int) -> int:
"""
获取几分钟后的时间戳
@param minute: 分钟
@return: N分钟后的时间戳
"""
return int(time.time() + 60 * minute) * 1000
def get_now_time() -> int:
"""
获取当前时间戳, 整形
@return: 当前时间戳
"""
return int(time.time()) * 1000
# GMT 时间处理
def get_GMT():
gmt_format = '%a, %d %b %Y %H:%M:%S GMT'
return datetime.utcnow().strftime(gmt_format)
if __name__ == '__main__':
print(get_now_time())