cs_lang:python:things-to-know:date-and-time
import time #pattern = "%a %b %d %H:%M:%S %Y" pattern = '%Y-%m-%d %H:%M:%S' date_time = '2007-02-05 16:15:18' epoch = int(time.mktime(time.strptime(date_time, pattern))) print epoch
def delta_time(dt, delta, fmt='%Y-%m-%d %H:%M:%S'): """ Adds a time delta to a datetime object with a specific format. Example of call: >>> new_dt = delta_time('2012-10-27 12:05:54', datetime.timedelta(minutes=45)) >>> print("%s:%s:%s" % (new_dt.hour, new_dt.minute, new_dt.second)) 12:50:54 """ return datetime.datetime(*time.strptime(dt, fmt)[:6]) + delta
cs_lang/python/things-to-know/date-and-time.txt · Last modified: 2012/06/20 11:14 by cedric