From 31a21959589e4bdda8ba1a92d4fe586f6808b9f2 Mon Sep 17 00:00:00 2001 From: helmutm Date: Mon, 15 Feb 2010 11:33:06 +0000 Subject: [PATCH] collect more date formatting and parsing utilities in one central place git-svn-id: svn://svn.cy55.de/Zope3/src/cybertools/trunk@3731 fd906abe-77d9-0310-91a1-e0d9ade77398 --- util/date.py | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/util/date.py b/util/date.py index 15bfef1..59b4065 100644 --- a/util/date.py +++ b/util/date.py @@ -25,13 +25,29 @@ $Id$ import time +def getTimeStamp(): + return int(time.time()) + + def timeStamp2ISO(ts, useGM=False, format='%Y-%m-%d %H:%M'): + return formatTimeStamp(ts, useGM, format) + +def formatTimeStamp(ts, useGM=False, format='%Y-%m-%d %H:%M'): + if ts is None: + ts = getTimeStamp() fct = useGM and time.gmtime or time.localtime return time.strftime(format, fct(ts)) #return time.strftime('%Y-%m-%d %H:%M', time.gmtime(ts)) #return time.strftime('%Y-%m-%d %H:%M', time.localtime(ts)) -def getTimeStamp(): - return int(time.time()) +def str2timeStamp(s): + try: + t = time.strptime(s, '%Y-%m-%d %H:%M:%S') + except ValueError: + try: + t = time.strptime(s, '%Y-%m-%d %H:%M') + except ValueError: + t = time.strptime(s, '%Y-%m-%d') + return int(time.mktime(t))