我是靠谱客的博主 爱撒娇星月,这篇文章主要介绍python对时间转换的相关函数方法,现在分享给大家,希望可以做个参考。

python对时间转换的相关函数方法
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import time import datetime import secrets import json class TimeException(Exception): """ 自定义关于时间异常的异常类 """ def __init__(self, desc): self.desc = desc def __str__(self): return self.desc class TimesTool: """ 处理时间工具类 """ @staticmethod def datetime_of_str(datetime_value=None, str_format='%Y-%m-%d %H:%M:%S'): """ 转换datetime类型值为指定字符串格式时间日期;若无datetime类型值则默认转换当前日期时间 为指定字符串格式时间日期 :param datetime_value: datetime类型值 :param str_format: 字符串格式化时间的时间格式 """ if not datetime_value: return datetime.datetime.now().strftime(str_format) if not isinstance(datetime_value, datetime.datetime): raise TimeException('datetime_value要求为datetime类型值') return datetime_value.strftime(str_format) @staticmethod def convert_of_stamp(stamp: int): """ 时间戳的转换 :param stamp: 时间戳;毫秒级(13位)、秒级(10位) return "%Y-%m-%d %H:%M:%S" 时间 """ if len(str(stamp)) == 10: return time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(stamp)) return time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(stamp / 1000)) @staticmethod def convert_to_stamp(datetime_, stamp_type='ms'): """ @param datetime_:datetime时间类型 @param stamp_type: 毫秒ms, 秒s @return: """ if stamp_type == 'ms': return int(round(datetime_.timestamp()*1000)) return int(round(datetime_.timestamp())) @staticmethod def now_time_to_stamp(stamp_type='ms'): """ 当前时间转换为时间戳 :param stamp_type: 时间戳类型;ms、s 默认为毫秒 return 时间戳 """ now = time.time() # 返回float数据 # 毫秒级时间戳 if stamp_type == 'ms': return int(round(now * 1000)) # 秒级时间戳 return int(now) @staticmethod def is_valid_date(date_str: str): """ 校验是否合法的字符串日期格式 @param date_str: @return: """ if not isinstance(date_str, str): return False try: datetime.datetime.strptime(date_str, "%Y-%m-%d") except ValueError: return False return True @staticmethod def str_to_datetime_date_type(date_str, separator='-'): """ 将日期字符串转换为datetime.date时间类型 @param date_str: 日期字符串 @param separator: 字符串间隔默认 - eg:年-月-日 @return: datetime.date """ return datetime.date(*map(int, date_str.split(separator))) @staticmethod def get_now_weekday_num(): """ 获取当前星期几数字 1-7 @return: """ return datetime.datetime.now().isoweekday() @staticmethod def is_valid_time(time_str: str): """ 校验是否合法的字符串日期时间格式 @param time_str: @return: """ if not isinstance(time_str, str): return False try: res = datetime.datetime.strptime(time_str, "%Y-%m-%d") except ValueError: return False return res @staticmethod def now_datetime(): """ 当前datetime类型日期时间 @return: """ now_time = datetime.datetime.now() now_datetime = datetime.datetime.strptime( now_time.strftime("%Y-%m-%d %H:%M:%S"), "%Y-%m-%d %H:%M:%S") return now_datetime @staticmethod def plus_or_minus_of_hour(datetime_: datetime.datetime, hours: int = 24, action: str = 'minus'): """ 针对datetime时间类型的小时时间(hours)进行加减计算 @param datetime_: @param hours: @param action: @return: """ if action == 'minus': return datetime_ - datetime.timedelta(hours=hours) return datetime_ + datetime.timedelta(hours=hours) @staticmethod def convert_to_utc_str(date_time, utc_format='%Y-%m-%dT%H:%M:%SZ'): """ 将datetime时间转换为utc字符串格式 @param date_time: @param utc_format:utc时间格式 @return: """ return date_time.astimezone(pytz.utc).strftime(utc_format) @staticmethod def convert_of_utc(target_time: str): """ UTC世界标准时间(包含T和Z) 转 北京时间 :param target_time: :return: """ _date = datetime.datetime.strptime(target_time, "%Y-%m-%dT%H:%M:%S%fZ") local_time = _date + datetime.timedelta(hours=8) end_time = local_time.strftime("%Y-%m-%d %H:%M:%S") return end_time @staticmethod def differential_datetime(start_time, end_time): """ datetime时间差值计算 @param start_time: 开始时间 @param end_time: 结束时间 @return: days,hours, minute,seconds """ days = (end_time - start_time).days hour_total_seconds = (end_time - start_time).seconds hours = hour_total_seconds // 60 // 60 minutes = hour_total_seconds // 60 % 60 seconds = hour_total_seconds % 60 % 60 return days, hours, minutes, seconds

最后

以上就是爱撒娇星月最近收集整理的关于python对时间转换的相关函数方法的全部内容,更多相关python对时间转换内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(54)

评论列表共有 0 条评论

立即
投稿
返回
顶部