Python开发【模块】:time、datatime

时间:2023-03-09 01:55:45
Python开发【模块】:time、datatime

时间模块

时间相关的操作,时间有三种表示方式:

  • 时间戳               1970年1月1日之后的秒,即:time.time()
  • 格式化的字符串    2014-11-11 11:11,    即:time.strftime('%Y-%m-%d')
  • 结构化时间          元组包含了:年、日、星期等... time.struct_time    即:time.localtime()

time模块:

# time模块
import time print(time.time()) # 时间戳
# 1472037866.0750718 print(time.localtime()) # 结构化时间
# time.struct_time(tm_year=2016, tm_mon=8, tm_mday=25, tm_hour=8, tm_min=44, tm_sec=46, tm_wday=3, tm_yday=238, tm_isdst=0) print(time.mktime(time.localtime()))
# 1498545592.0 print(time.strftime('%Y-%m-%d')) # 格式化的字符串
# 2016-08-25
print(time.strftime('%Y-%m-%d', time.localtime()))
# 2016-08-25 print(time.gmtime()) # 结构化时间
# time.struct_time(tm_year=2016, tm_mon=8, tm_mday=25, tm_hour=3, tm_min=8, tm_sec=48, tm_wday=3, tm_yday=238, tm_isdst=0) print(time.strptime('2014-11-11', '%Y-%m-%d')) # 结构化时间
# time.struct_time(tm_year=2014, tm_mon=11, tm_mday=11, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=1, tm_yday=315, tm_isdst=-1) print(time.asctime())
# Thu Aug 25 11:15:10 2016
print(time.asctime(time.localtime()))
# Thu Aug 25 11:15:10 2016
print(time.ctime(time.time()))
# Thu Aug 25 11:15:10 2016

结构化时间:

Python开发【模块】:time、datatime

时间戳、格式化字符串、机构化时间相互转换:

Python开发【模块】:time、datatime

datetime模块:

import datetime

print(datetime.date)    #表示日期的类。常用的属性有year, month, day
#<class 'datetime.date'>
print(datetime.time) #表示时间的类。常用的属性有hour, minute, second, microsecond
#<class 'datetime.time'>
print(datetime.datetime) #表示日期时间
#<class 'datetime.datetime'>
print(datetime.timedelta) #表示时间间隔,即两个时间点之间的长度
#<class 'datetime.timedelta'> print(datetime.datetime.now())
#2016-08-25 14:21:07.722285
print(datetime.datetime.now() - datetime.timedelta(days=5))
#2016-08-20 14:21:28.275460

时间比较、运算:

import time

str = '2017-03-26 3:12'
str2 = '2017-07-26 13:12'
date1 = time.strptime(str, '%Y-%m-%d %H:%M')
date2 = time.strptime(str2, '%Y-%m-%d %H:%M')
if float(time.time()) >= float(time.mktime(date1)) and float(time.time()) <= float(time.mktime(date2)):
print 'cccccccc'
if time.localtime() >= date1 and time.localtime() <= date2:
print 'zzzzzzzz' import datetime str = '2017-03-26 3:12'
str2 = '2017-05-26 13:12'
date1 = datetime.datetime.strptime(str,'%Y-%m-%d %H:%M')
date2 = datetime.datetime.strptime(str2,'%Y-%m-%d %H:%M')
datenow = datetime.datetime.now()
if datenow <date1:
print 'dddddd'

时间戳转为时间字符串:

import time

stime = time.time()
# 1512713749.85
stime = time.gmtime(stime)
# time.struct_time(tm_year=2017, tm_mon=12, tm_mday=8, tm_hour=6, tm_min=15, tm_sec=49, tm_wday=4, tm_yday=342, tm_isdst=0)
stime = time.strftime('%Y-%m-%d %H:%M:%S',stime)
# 2017-12-08 06:15:49

datetime.datetime转为时间戳:

import datetime
import time dtime = datetime.datetime.now()
print(dtime)
# 2018-09-14 14:19:38.291610
print(type(dtime))
# <class 'datetime.datetime'>
timestamp_time = time.mktime(dtime.timetuple())
print(timestamp_time)
# 1536905978.0
print(type(timestamp_time))
# <class 'float'>

定时每天几点执行任务:

    # 定时时间,每天执行
START_TIME_HOUR = 2 # 时
START_TIME_MINUTE = 00 # 分
date = datetime.datetime.now()
excute_time = '%s %s:%s' % (time.strftime('%Y-%m-%d'), START_TIME_HOUR, START_TIME_MINUTE)
excute_time = time.strptime(excute_time, '%Y-%m-%d %H:%M')
excute_time = time.mktime(excute_time)
if date.hour > START_TIME_HOUR:
excute_time += 24 * 60 * 60 # 加一天
elif date.hour == START_TIME_HOUR and date.minute >= START_TIME_MINUTE :
excute_time += 24 * 60 * 60 # 加一天
logging.info('timer wait %s',excute_time-time.time())
timer = threading.Timer(excute_time-time.time(),execute)
timer.start()

  

Mysql数据库中DATETIME字段运算

sql = "SELECT * FROM cdr.tx_cdr_master WHERE  id = '205542246'"
cursor.execute(sql)
database = cursor.fetchone()
print(database)
print(type(database['stime']))
print(database['stime'])
ndate = datetime.datetime.now() - datetime.timedelta(days=0.5)
print(ndate)
if database['stime'] < ndate:
print('***') {'id': 205542246, 'requestid': '', 'callid': '000c29be8df6a5a611e8c0a621d83c3c', 'calltype': 1, 'direction': 1, 'caller': '18612721422', 'callee': '', 'dh': '01053180014', 'callresult': 2, 'stime': datetime.datetime(2018, 9, 25, 17, 33, 59), 'etime': datetime.datetime(2018, 9, 25, 17, 34, 6), 'rec_path': '', 'serverid': 'asterisk2', 'taskid': '', 'agentid': '', 'agentgrpid': '', 'compid': '800085', 'feetime': 6, 'totaltime': 7, 'fee': Decimal('11.00'), 'flags': 3, 'hanguper': 1, 'caller_area': '??-??', 'callee_area': '', 'ifhandle': b'\x01', 'isfee': 1, 'ref': '', 'feedate': datetime.datetime(2018, 9, 25, 17, 34, 25), 'createdate': datetime.datetime(2018, 9, 25, 17, 34, 6), 'SerialNumber': 0, 'userphone': '18612721422', 'user_area': '??-??'}
<class 'datetime.datetime'>
2018-09-25 17:33:59
2018-09-25 23:51:03.852978
***