写脚本的时候会用到到但又不算太频繁,导致经常要去翻google....干脆都记下来
先是python的
-
>>> import time
-
>>> print time.localtime()
-
(2013, 8, 9, 14, 17, 30, 4, 221, 0)
-
>>> print type(time.localtime())
-
<type 'time.struct_time'>
-
>>> curTime = time.strftime('%Y-%m-%d %H:%M:%S',time.localtime())
-
>>> print curTime
-
2013-08-09 14:17:11
-
>>> print type(curTime)
-
<type 'str'>
-
>>> time.time()
-
1376029404.869349
-
>>> print type(time.time())
-
<type 'float'>
datetime
http://blog.csdn.net/jgood/article/details/5457284
-
>>> import datetime
-
>>> datetime.datetime.now()
-
datetime.datetime(2013, 8, 9, 14, 26, 47, 350944)
-
>>> loli = datetime.datetime.now()
-
>>> print loli
-
2013-08-09 14:26:57.998630
-
>>> print type(loli)
-
<type 'datetime.datetime'>
-
>>> datetime.datetime.now()-loli
-
datetime.timedelta(0, 40, 878765)
-
>>> lolita = datetime.datetime.now()-loli
-
>>> print lolita
-
0:00:48.414427
-
>>> print lolita.days
-
0
-
>>> print lolita.seconds
-
48
-
>>> date_up = datetime.timedelta(days=3)
-
>>> print type(date_up)
-
<type 'datetime.timedelta'>
-
>>> print date_up.days
-
3
-
>>> lolicon = loli - date_up
-
>>> print lolicon
-
2013-08-06 14:26:57.998630
-
>>> print (lolicon>loli)
-
False
顺便
https://blog.lzhaohao.info/archive/performance-problem-with-strptime/
简单来说就通过正则把时间什么的纷纷作为int传给datetime处理,这样比strptime直接处理字符串要快
而且这样做最大的好处就是....pthon2.4里datetime没有strptime这个模块>_<
-
#!/usr/bin/env python
-
import datetime
-
import os
-
import re
-
import timeit, cProfile
-
-
def strptime():
-
with open('time.txt', 'r') as f:
-
for line in f:
-
line = line.rstrip(os.linesep)
-
dt = datetime.datetime.strptime(line, "%Y-%m-%d %H:%M:%S")
-
-
-
def reg():
-
rep = re.compile(r'(\d{4})-(\d{2})-(\d{2})\s(\d{2}):(\d{2}):(\d{2})')
-
with open('time.txt', 'r') as f:
-
for line in f:
-
line = line.rstrip(os.linesep)
-
m = rep.match(line)
-
dt = datetime.datetime(int(m.group(1)),
-
int(m.group(2)),
-
int(m.group(3)),
-
int(m.group(4)),
-
int(m.group(5)),
-
int(m.group(6))
-
)
-
-
if __name__ == '__main__':
-
-
t1 = timeit.Timer("reg()","from __main__ import reg")
-
t2 = timeit.Timer("strptime()", "from __main__ import strptime")
-
-
cProfile.run("t1.timeit(3);print")
-
print""
-
cProfile.run("t2.timeit(3);print")
一些计算例子
-
#获取昨天的写法
-
-
one_difference_day = datetime.timedelta(1,0,0)
-
today = datetime.datetime.now()
-
yesterday = today - one_difference_day
-
-
#只要日期(这个的返回依旧是datetime类)
-
yesterday.date()
-
-
#分别获取年月日(注意yesterday有hour、minute、second等属性,yesterday.date()没有这些属性)
-
print '%s-%s-%s' % (yesterday.year,yesterday.month,yesterday.day)
-
-
#上面那个写法有明显缺点,就是小于10的日/月前面没0,改良写法
-
print '%s' % yesterday.date().isoformat()
上面写法都是用datetime.date()把datetime类里的小时,分秒毫秒属性去除,还有另外一个方法
可以直接用datetime.replace([year[, month[, day[, hour[, minute[, second[, microsecond[, tzinfo]]]]]]]])把
小时,分秒毫秒属性设置成0,然后再计算。
更多用法参考
http://blog.csdn.net/jgood/article/details/5457284
不同格式的字符串时间传换
str 转
datetime 用 datetime.datetime.strptime(str,'格式')
datetime 转str 用 datetime的方法
strftime('格式')
一个例子:一个字符串格式是30/Dec/2013,我们要把这个时间转换为2013-12-30
可以用如下转换方式
time_str = '
30/Dec/2013'
log_date = datetime.datetime.strptime(time_str,'%d/%b/%Y').strftime('%Y-%m-%d')
上面的加上try还可以用于判断字符串是否为所指定的时间格式,中途转化为datetime以后还可以计算完毕再转换str
昨天的新写法,其实和上面差不多
-
today = datetime.datetime.now().date()
-
difference_day = datetime.timedelta(days=1)
-
yesterday = (today - difference_day).strftime('%Y-%m-%d')
上个月的计算方法,以及上个月天数的计算方法,比较常用
-
year = datetime.datetime.now().year
-
last_moth = datetime.datetime.now().month - 1
-
if last_moth == 0:
-
last_moth = 12
-
year -= 1
-
day_long = (datetime.datetime(datetime.datetime.now().year, datetime.datetime.now().month, 01) -
-
datetime.datetime(year, last_moth, 01)).days
unix时间转datetime
datetime.datetime.fromtimestamp(1396256636)
只算日期
datetime.date.fromtimestamp(1396256636)
混一起说明
print(datetime.datetime.fromtimestamp(int("1284101485")).strftime('%Y-%m-%d %H:%M:%S'))
datetime转unix时间比较麻烦,因为datetime本身不支持直接转unix时间,只能把datetime转成timetuple,再用time.mktime转换成unix时间
today = datetime.datetime.strptime('2014-09-27', '%Y-%m-%d')
int_today = time.mktime(compute_day.date().timetuple())
一个很好的说明图,转自
http://blog.sina.com.cn/s/blog_b09d460201018o0v.html
阅读(1951) | 评论(0) | 转发(0) |