Time


bash

date +%Y%m%d # 20101201
date +%s     # 1292048782
date `date +%m%d%H%M%Y.%S --date="-8 hour"` # reduce 8 hour
date -d "2010-12-23 10:39:38" +%s

# -d/--date 选项全解
# 此选项用来指明要显示的时间。如果是确切的时间,则直接显示对应的时间
# 否则在当前的基础上进行数学运算,默认为加。
#
# +和next等价, -和ago等价
#
# date可以理解的英文单词
# 不能使用数字修饰 yesterday today tomorrow 
# 可以使用数字修饰 second minute hour day week month year
#
date -d 3day  # "3 day" "next 3 day"
date -d -3day # "-3 day" "3 day ago"
date -d yesterday
date -d "2005-02-02 13:00"
date -d @1298609080  # local
date -ud @1298609080 # UTC

Python

from time import gmtime, localtime, mktime, strftime, strptime
#
# get the current time in epoch timestamp
strftime('%s')
#
# convert epoch timestamp to local time tuple
localtime(1298630912) # (2011, 2, 25, 18, 48, 32, 4, 56, 0)
#
# convert epoch timestamp to UTC time tuple
gmtime(1298630912) # (2011, 2, 25, 10, 48, 32, 4, 56, 0)
#
# convert specified format time to time tuple
strptime('2011-02-25 10:48:32', '%Y-%m-%d %H:%M:%S') # (2011, 2, 25, 10, 48, 32, 4, 56, -1)
#
# convert time tuple to UTC timestamp(float) according to the specified format
mktime(strptime('2011-02-25 10:48:32', '%Y-%m-%d %H:%M:%S')) # 1298602112.0 on a machine with CST timezone
# 
strftime('%Y-%m-%d %H:%M:%S') # current time in the specified format