From f7c9ad7d3452da7bd9615ba11b6b3d6b1ae80386 Mon Sep 17 00:00:00 2001 From: Wirawan Purwanto Date: Tue, 5 Mar 2013 23:44:31 -0500 Subject: [PATCH] * Added functions time_diff() and shift_time() for UNIX time manipulation. --- datetime.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/datetime.py b/datetime.py index 528800d..9a9182b 100644 --- a/datetime.py +++ b/datetime.py @@ -18,5 +18,22 @@ import time def date8(): + """Returns a standard 8-digit representation of the current date.""" return time.strftime("%Y%m%d") +def time_diff(time1, time2): + """Returns the time difference (time1 - time2) in seconds.""" + from time import mktime + return mktime(time1) - mktime(time2) + +def shift_time(t, dt, localtime=True): + """Shifts a time data by an amount in dt (specified in seconds).""" + if isinstance(t, time.struct_time): + t1 = time.mktime(t) + dt + else: + t1 = t + dt + if localtime: + return time.localtime(t1) + else: + return time.gmtime(t1) +