时间操作

 

import os,time,datetime
f_t = os.stat('filew.txt')
t_t = os.stat('test2.py')
#方法二:
#f_t = os.path.getmtime('filew.txt')
#t_t = os.path.getmtime('test2.py')
#st_atime (访问时间), st_mtime (修改时间), st_ctime(创建时间)
print datetime.datetime.fromtimestamp(f_t.st_mtime)
print datetime.datetime.fromtimestamp(t_t.st_mtime) #将时间戳转换为可读时间格式,如st_ctime=1462412494L,转换后为2016-05-05 09:41:34.484475

dt = datetime.datetime.fromtimestamp(f_t.st_mtime)
#格式化为字符串
dt.strftime('%Y-%m-%d %H:%M:%S') #返回2016-05-05 09:41:34
print dt.strftime('%Y-%m-%d %H:%M:%S')[0:10] #返回2016-05-05,切片
#获取当前时间 time.asctime() time.localtime() #转换时间戳为本地时间 time.localtime(f_t) #如st_ctime=1462412494L,转换后为time.struct_time(tm_year=2016, tm_mon=5, tm_mday=5, tm_hour=9, tm_min=41, tm_sec=34, tm_wday=3, tm_yday=126, tm_isdst=0) time.localtime(t_t) #获取时间差 cha = datetime.datetime.fromtimestamp(ft) - datetime.datetime.fromtimestamp(tt) cha.days cha.seconds

 

时间格式转换为时间戳:

import time,datetime
print time.time() #当前时间戳
print time.ctime() #当前时间,string格式
print time.localtime() #返回 time.struct_time(tm_year=2016, tm_mon=5, tm_mday=12, tm_hour=21, tm_min=15, tm_sec=40, tm_wday=3, tm_yday=133, tm_isdst=0)
starttime=datetime.datetime.now()
print time.mktime(starttime.timetuple()) #当前时间转换为时间戳

 

字符串时间转换为时间戳:

import time
estrime = '2016-05-18 15:30:01'
print time.strptime(estrtime,'%Y-%m-%d %H:%M:%S') #先将字符串格式转换为时间元组。返回:time.struct_time(tm_year=2016, tm_mon=5, tm_mday=18, tm_hour=15, tm_min=30, tm_sec=1, tm_wday=2, tm_yday=139, tm_isdst=-1)
print time.mktime(time.strptime(estrtime,'%Y-%m-%d %H:%M:%S'))#将时间数组转换为时间戳。返回:1463556601.0

 注:时间元组就可比较大小

 

将字符串时间转换为可比较的时间格式:

fdate=‘20160905’
datetime.datetime.fromtimestamp(time.mktime(time.strptime(fdate,'%Y%m%d'))) 

 

 时间加减计算:

import datetime
starttime=datetime.datetime.now()
h2 = datetime.timedelta(hours=10) #返回 10:00:00
d2 = datetime.timedelta(days=1) #返回 1 day, 0:00:00   没有months,years参数
print starttime #返回 2016-05-12 20:43:41.473000
print starttime+h2 #加10h,返回 2016-05-13 06:44:15.823000
print starttime-d2 #减1天,返回 2016-05-11 20:43:41.473000
print (starttime+d2 - starttime).total_seconds() # 返回 86400.0
安装dateutil模块,加减月份
sudo pip install python-dateutil --upgrade
from dateutil.relativedelta import relativedelta
print(datetime.date.today() - relativedelta(months=-3)

 

datetime.datetime.now() #获取本地当前时间
datetime.datetime.now().year #获取年份
datetime.datetime.now() + datetime.timedelta(days=2) #+2天

 

datetime.date(2016,3,2).strftime('%Y-%m-%d')  #返回2016-03-02
datetime.time(14,33,22).strftime('%H:%M:%S')  #返回14:33:22

 

print time.time() #返回1464586422.94
thistime = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())) #返回2016-05-30 13:33:42
stoptime = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time() + 3600)) #返回2016-05-30 14:33:42

print thistime > stoptime #string格式,可以进行比较。返回False

 

time和datetime中的strftime在多线程中是非安全的,在多线程中运行“time.strptime('2016-01-01 12:01:00','%Y-%m-%d %H:%M:%S')”会出现错误:AttributeError: _strptime,原因如下:

  in multithreads environment, when one thread is tring to import _strptime, but has not been fully imported, another threads tried to call strptime_module._strptime_time directly. This is why the bug happened.

If you understand well why this bug happend, you should already have the workaround in your heart. Actually it’s really straightforward. All you need to do just is call once strptime before starting your threads.

解决办法:

1.先导入strftime模块:

import _strptime
import time

2.在多线程函数中调用strptime:

def t():
  print time.strptime('2016-01-01 12:01:00','%Y-%m-%d %H:%M:%S')

 

或者直接加锁,相对来说还是先导入_strptime模块较好:

def t():
     with lock:
      print time.strptime('2016-01-01 12:01:00','%Y-%m-%d %H:%M:%S')

 

 

 

#格式化当前时间:

time.strftime('%Y-%m-%d %H:%M:%S',time.localtime())

time.localtime(time.time()) == time.localtime()

help(time)

#格式化当前时间:

from time import strftime,localtime
print 'Now: %s' % strftime('%Y-%m-%d %H:%M:%S',localtime())

 
将 q.pub_date返回值为:datetime.datetime(2016, 2, 2, 9, 8, 29, 490000, tzinfo=<UTC>)
将其格式化为字符串类型:q.pub_date.strftime('%b-%d-%y %H:%M:%S')
posted on 2015-12-30 11:07  momingliu11  阅读(842)  评论(0编辑  收藏  举报