003_001 Python 计算昨天和

代码如下:

代码语言:javascript
复制
#encoding=utf-8

print '中国'

import time

#一些函数 time
print time.time()
print time.gmtime()
print time.asctime(time.gmtime())
print time.localtime()
print time.strftime('%Y%m%d %H:%M:%S+%z').decode('gbk')

for i in range(2):
time.sleep(0.5)
print i

#类datetime
import datetime
print datetime.date.today()
print datetime.datetime.now().time()
print datetime.datetime.now()

today = datetime.date.today()
next_year = today.replace(year = today.year + 1).strftime("%Y.%m.%d")
print next_year

print datetime.date(2000,2,29)-datetime.date(2001,2,28)

#类 decimal
import decimal
print 2.3333333345789
print decimal.Decimal(2.3333333345789)
print decimal.Decimal('2.3333333345789')

#decimal精度设置
decimal.getcontext().prec = 6 #设定本线程的精度
print decimal.Decimal(1) /decimal.Decimal(7)

#计算昨天和明天的日期

today = datetime.date.today()
yestoday = today - datetime.timedelta(days=1)
tomorrow = today + datetime.timedelta(days=1)

print 'today',today
print 'yestoday',yestoday
print 'tomorrow',tomorrow


打印结果如下:

中国
1395327738.08
time.struct_time(tm_year=2014, tm_mon=3, tm_mday=20, tm_hour=15, tm_min=2, tm_sec=18, tm_wday=3, tm_yday=79, tm_isdst=0)
Thu Mar 20 15:02:18 2014
time.struct_time(tm_year=2014, tm_mon=3, tm_mday=20, tm_hour=23, tm_min=2, tm_sec=18, tm_wday=3, tm_yday=79, tm_isdst=0)
20140320 23:02:18+中国标准时间
0
1
2014-03-20
23:02:19.084000
2014-03-20 23:02:19.084000
2015.03.20
-365 days, 0:00:00
2.33333333458
2.333333334578899798117390673724003136157989501953125
2.3333333345789
0.142857
today 2014-03-20
yestoday 2014-03-19
tomorrow 2014-03-21