Python工具类(二)—— 操作时间相关

简介: Python工具类(二)—— 操作时间相关
1 #!/usr/bin/env python
  2 # -*- coding: utf-8 -*-
  3 """
  4 __title__ = '操作时间的工具类'
  5 
  6 """
  7 import datetime
  8 import time
  9 
 10 
 11 # ==========================
 12 # ========== time ==========
 13 # ==========================
 14 
 15 
 16 def getCurrentMilliSecondTime():
 17     """
 18     description:  获取当前时间-毫秒级
 19     return:       1557730376981 -> str
 20     """
 21     timestamps = str(round(time.time() * 1000))
 22     return timestamps
 23 
 24 
 25 def getCurrentSecondTime():
 26     """
 27     description:  获取当前时间-秒级
 28     return:       1557730377 -> str
 29     """
 30     timestamps = str(round(time.time()))
 31     return timestamps
 32 
 33 
 34 def getCurrentTimeTuple(times=time.time()):
 35     """
 36     description:  接受秒级时间戳并返回时间元组(与mktime(tuple)相反)
 37     times:        默认当前时间 可传second
 38     return:       (tm_year=2019, tm_mon=5, tm_mday=13, tm_hour=10, tm_min=9, tm_sec=18, tm_wday=0, tm_yday=133, tm_isdst=0) -> tuple
 39     tips:         time.localtime() 不传参则取当前时间
 40     """
 41     timestamps = time.localtime(times)
 42     return timestamps
 43 
 44 
 45 def getTimeByTuple(tupleTime=time.localtime()):
 46     """
 47     description:  接受时间元组并返回秒级时间戳(与localtime(sec)相反)
 48     tupleTime:    默认当前时间的元组 可通过time.localtime() or datetime.datetime.now().timetuple()获取
 49     return:       1557733061 -> str
 50     """
 51     timestamps = str(round(time.mktime(tupleTime)))
 52     return timestamps
 53 
 54 
 55 def getCurrentFormatTimeStr(times=time.time()):
 56     """
 57     description:  将指定时间元组格式化为字符串
 58     times:        默认当前时间 可传second
 59     return:       2019-05-13 15:00:47 -> str
 60     tips:         %y 两位数的年份表示(00-99)    %Y 四位数的年份表示(000-9999)   %m 月份(01-12)    %d 月内中的一天(0-31)
 61                   %H 24小时制小时数(0-23)      %I 12小时制小时数(01-12)        %M 分钟数(00=59)  %S 秒(00-59)   %w 星期(0-6)
 62     """
 63     timestamps = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(times))
 64     return timestamps
 65 
 66 
 67 def getCurrentTimeTupleByFormatStr(time_str=str(datetime.datetime.now()).split(".")[0], format_type="%Y-%m-%d %H:%M:%S"):
 68     """
 69     description:  接受格式化字符串返回时间元组
 70     time_str:     格式化字符串   如:2019-05-13 15:00:47    默认当前时间
 71     format_type:  格式化规则    如:%Y-%m-%d %H:%M:%S      默认%Y-%m-%d %H:%M:%S
 72     return:       (tm_year=2019, tm_mon=5, tm_mday=13, tm_hour=10, tm_min=9, tm_sec=18, tm_wday=0, tm_yday=133, tm_isdst=0) -> tuple
 73     """
 74     return time.strptime(time_str, format_type)
 75 
 76 
 77 def getCurrentTimeStr():
 78     """
 79     description:  获取当前时间的可读形式字符串
 80     return:       Mon May 13 11:27:42 2019 -> str
 81     """
 82     return time.ctime()
 83 
 84 
 85 def getCurrentTimeStrByTuple(tupleTime=time.localtime()):
 86     """
 87     description:  获取指定时间的可读形式字符串
 88     tupleTime:    时间元组 可通过time.localtime() or datetime.datetime.now().timetuple()获取 默认当前时间的元组
 89     return:       Mon May 13 11:27:42 2019 -> str
 90     """
 91     return time.asctime(tupleTime)
 92 
 93 
 94 def sleepTime():
 95     """
 96     description:  推迟调用线程的运行
 97     """
 98     for i in range(4):
 99         print(i)
100         time.sleep(3)
101 
102 
103 # ======================
104 # ====== datetime ======
105 # ======================
106 
107 
108 def getNowDateTime():
109     """
110     description:  获取当前日期&时间
111     return:       2019-05-13 14:41:15 -> str
112     """
113     timestamps = str(datetime.datetime.now()).split(".")[0]
114     return timestamps
115 
116 
117 def getNowTime():
118     """
119     description:  获取当前时间
120     return:       14:41:15 -> str
121     """
122     timestamps = str(datetime.datetime.now().time()).split(".")[0]
123     return timestamps
124 
125 
126 def getTodayDate():
127     """
128     description:  获取当前日期
129     return:       2019-05-13 -> str
130     tipe:         datetime.datetime.now().date()有相同效果
131     """
132     timestamps = str(datetime.date.today())
133     return timestamps
134 
135 
136 def getTimeDate(times=time.time()):
137     """
138     description:  获取指定时间戳的日期
139     time:         秒 默认当前时间
140     return:       2019-05-13 -> str
141     tips:         一天86400秒
142     """
143     timestamps = str(datetime.date.fromtimestamp(round(times)))
144     return timestamps
145 
146 
147 # 获取距离现在时间的任意时间的日期     正数 加,负数 减  return:2019-05-12
148 def getAnyDateTime(day, hour=0, min=0, sec=0):
149     """
150     description:  获取距离现在时间的任意时间的日期&时间
151     day:          天数 1代表当前时间+1天    -1代表当前时间-1天
152     hour:         小时 2代表当前时间+2h     -2代表当前时间-2h     默认=0
153     min:          分钟 30代表当前时间+30min -30代表当前时间-30m   默认=0
154     sec:          秒   120代表当前时间+120s -120代表当前时间-120s 默认=0
155     return:       2019-05-15 15:37:41 -> str
156     """
157     return str(datetime.datetime.now() + datetime.timedelta(days=day, hours=hour, minutes=min, seconds=sec)).split(".")[0]
158 
159 
160 def getAnyDateSecondTime(day, hour=0, min=0, sec=0):
161     """
162     description:  获取距离现在时间的任意时间的秒数
163     day:          天数 1代表当前时间+1天    -1代表当前时间-1天
164     hour:         小时 2代表当前时间+2h     -2代表当前时间-2h     默认=0
165     min:          分钟 30代表当前时间+30min -30代表当前时间-30m   默认=0
166     sec:          秒   120代表当前时间+120s -120代表当前时间-120s 默认=0
167     return:       1557902182 -> str
168     """
169     anyDay = datetime.datetime.now() + datetime.timedelta(days=day, hours=hour, minutes=min, seconds=sec)
170     return str(round(time.mktime(anyDay.timetuple())))
171 
172 
173 def getTodayTime():
174     """
175     description:  获取当天0点的时间戳
176     return:       1557676800 -> str
177     """
178     return str(round(time.mktime(datetime.date.today().timetuple())))
179 
180 
181 def getCurrentWeekTime():
182     """
183     description:  获取本周周一0点
184     return:       1557676800 -> str
185     tips:         可替换成: timestamps = time.mktime(time.strptime(time.strftime("%Y-%m-%d", time.localtime(times)), "%Y-%m-%d"))
186     """
187     week = int(time.strftime("%w", time.localtime()))
188     times = round(time.time()) - (week - 1) * 86400
189     timestamps = time.mktime(datetime.date.fromtimestamp(times).timetuple())
190     return str(round(timestamps))
191 
192 
193 def test():
194     print(getCurrentMilliSecondTime())
195     print(getCurrentSecondTime())
196     print(getCurrentFormatTimeStr())
197     print(getCurrentTimeTupleByFormatStr())
198     print("=======")
199     print(getCurrentTimeStr())
200     print(getCurrentTimeStrByTuple(time.localtime()))
201     print(getTimeByTuple(time.localtime()))
202     print("=======")
203     print(getNowDateTime())
204     print(getNowTime())
205     print(getNowDateTime())
206     print(getTodayDate())
207     print(getTimeDate(time.time() - 86400))
208     print("=======")
209     print(getAnyDateTime(2))
210     print(getAnyDateSecondTime(2))
211     print("=======")
212     print(getTodayTime())
213     print(getCurrentWeekTime())
214 
215 
216 if __name__ == '__main__':
217     print(test())
相关文章
|
存储 监控 数据处理
💻Python高手必备!文件系统操作秘籍,让你的数据存取如臂使指
【7月更文挑战第29天】在数据驱动时代, Python以简洁语法、丰富库生态和强大跨平台能力, 成为数据科学等领域首选。本文探讨Python文件系统操作秘籍, 助力高效数据处理。
140 11
|
索引 Python
Python的列表操作有哪些?
Python的列表操作非常丰富,包括列表的创建、元素的访问、修改、添加、删除、切片、排序等多个方面。
259 12
|
监控 网络协议 网络安全
SMTP操作使用详解并通过python进行smtp邮件发送示例
SMTP操作使用详解并通过python进行smtp邮件发送示例
607 3
|
数据挖掘 数据处理 Python
🔍深入Python系统编程腹地:文件系统操作与I/O管理,打造高效数据处理流水线
【7月更文挑战第29天】深入Python系统编程腹地:文件系统操作与I/O管理,打造高效数据处理流水线
160 3
|
安全 数据安全/隐私保护 Python
|
Serverless 语音技术 开发工具
函数计算操作报错合集之怎么何集成nls tts python sdk
在使用函数计算服务(如阿里云函数计算)时,用户可能会遇到多种错误场景。以下是一些常见的操作报错及其可能的原因和解决方法,包括但不限于:1. 函数部署失败、2. 函数执行超时、3. 资源不足错误、4. 权限与访问错误、5. 依赖问题、6. 网络配置错误、7. 触发器配置错误、8. 日志与监控问题。
232 2
|
API Python
Python高手修炼手册:精通文件系统操作,掌控I/O管理,提升编程效率
【7月更文挑战第30天】在 Python 编程中, 文件系统操作与 I/O 管理是连接程序与数据的关键。初学者常因路径错误和权限问题受挫, 而高手能自如管理文件。传统 `os` 和 `os.path` 模块易出错, `pathlib` 提供了更直观的对象导向 API。I/O 方面, 同步操作会阻塞程序, 异步 (如使用 `aiofiles`) 则能大幅提升并发能力。真正的高手不仅掌握 API, 更能预见性能瓶颈并优化代码, 实现高效与优雅。
213 1
|
SQL 分布式计算 DataWorks
DataWorks操作报错合集之重新上传后只有SQL无法运行,而Python可以正常运行,是什么导致的
DataWorks是阿里云提供的一站式大数据开发与治理平台,支持数据集成、数据开发、数据服务、数据质量管理、数据安全管理等全流程数据处理。在使用DataWorks过程中,可能会遇到各种操作报错。以下是一些常见的报错情况及其可能的原因和解决方法。
|
Python

推荐镜像

更多