User Tools

Site Tools


cs_lang:python:things-to-know:date-and-time

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
cs_lang:python:things-to-know:date-and-time [2011/06/17 07:53] – created cedriccs_lang:python:things-to-know:date-and-time [2012/06/20 11:14] (current) cedric
Line 1: Line 1:
-<python>+<code python>
 import time import time
  
Line 8: Line 8:
 epoch = int(time.mktime(time.strptime(date_time, pattern))) epoch = int(time.mktime(time.strptime(date_time, pattern)))
 print epoch print epoch
-</python>+</code> 
 + 
 +<code python
 +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 
 +</code>
cs_lang/python/things-to-know/date-and-time.txt · Last modified: 2012/06/20 11:14 by cedric