Python 添加作者信息
Python 常用时间函数
import datetime
import time
print("时间戳")
print(int(time.time()))
print("将日期转为秒级时间戳")
dt = '2018-01-01 10:40:30'
ts = int(time.mktime(time.strptime(dt, "%Y-%m-%d %H:%M:%S")))
print(ts)
print("将秒级时间戳转为日期")
ts = 1515774430
dt = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(ts))
print(dt)
print("取当前时间")
dt = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
dt_ms = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S.%f')
print(dt)
print(dt_ms)
print("转结构体时间struct_time")
ta_dt = time.strptime("2018-09-06 21:54:46", '%Y-%m-%d %H:%M:%S')
ta_ms = time.localtime(1486188476)
print(ta_dt)
print(ta_ms)
print("当前时间 0 点")
dt = datetime.datetime.now().strftime('%Y-%m-%d 00:00:00')
print(dt)
print("当前时间 昨天")
dt = datetime.datetime.now().strftime('%Y-%m-%d 00:00:00')
yestoday_dt = (datetime.date.today() + datetime.timedelta(days=-1)).strftime("%Y-%m-%d")
print(yestoday_dt)
print("当前时间 前一周")
lastweekday_today_dt = (datetime.date.today() + datetime.timedelta(days=-7)).strftime("%Y-%m-%d")
print(lastweekday_today_dt)
print("当前时间 后一周")
fuweekday_today_dt = (datetime.date.today() + datetime.timedelta(days=7)).strftime("%Y-%m-%d")
print(fuweekday_today_dt)
print("当前时间 前一个月 第一天, 最后一天")
fistday_lastMonth = datetime.date(datetime.date.today().year, datetime.date.today().month + 1, 1)
lastday_lastMonth = datetime.date(datetime.date.today().year, datetime.date.today().month, 1) - datetime.timedelta(1)
print(fistday_lastMonth, lastday_lastMonth)
print("获取本月 第一天及最后一天")
fistday_lastMonth1 = datetime.date(datetime.date.today().year, datetime.date.today().month, 1)
lastday_lastMonth1 = datetime.date(fistday_lastMonth.year, fistday_lastMonth.month, 1) - datetime.timedelta(1)
print(fistday_lastMonth1, lastday_lastMonth1)
lastyear = datetime.date(datetime.date.today().year - 1, datetime.date.today().month, 1)
print("当前时间 前一年", lastyear)
lastyear = datetime.date.today().year - 1
print("当前时间 前一年", lastyear)
lastyear_dt = datetime.datetime.now().strftime('{}-%m-%d %H:%M:%S'.format(lastyear))
print("前一年的现在,需要注意闰年 2月 lastyear_dt", lastyear_dt)
print("字符串格式化")
str_0 = """组合字符串{0}{1}""".format("str0", "str1")
print("测试报告 testrunner.py 中 过滤执行")
print('''r = unittest.defaultTestLoader.discover("./xxx/xx", pattern='test*normal.py') ''')
过滤方法 unittest + testcase unittest.skip 装饰器
|