python Timestamp、Datetime、UTC时间之间转换练习

来源:转载


#coding=utf-8import timeimport datetimedef get_prev_time(tart_ts, interval = 3): start_array = datetime.datetime.utcfromtimestamp(start_ts) print start_array start_array = start_array + datetime.timedelta(hours=8) #转化为本地时间 print start_array threeDayAgo = (start_array - datetime.timedelta(days = interval)) print threeDayAgo timeStamp = int(time.mktime(threeDayAgo.timetuple())) return timeStampdef get_end_timestamp(start_ts, interval = 30): date_array = datetime.datetime.utcfromtimestamp(start_ts) end_date = date_array + datetime.timedelta(minutes = interval) end_ts = int(time.mktime(end_date.timetuple())) return end_tsstart_ts = time.time()print datetime.datetime.now()print start_tsprint get_prev_time(start_ts)print get_end_timestamp(start_ts)

结果:

2015-10-20 10:49:17.0670001445309357.072015-10-20 02:49:17.0670002015-10-20 10:49:17.0670002015-10-17 10:49:17.06700014450501571445282357


分享给朋友:
您可能感兴趣的文章:
随机阅读: