moksha/lib/helpers.py | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-)
New commits: commit 172d4fbb2e6a196293b0ba93022fe2ab9e794cd6 Author: Luke Macken lmacken@redhat.com Date: Wed May 13 07:35:16 2009 -0400
DateTimeDisplay improvements.
- Added an 'astimezone' method to convert to a given timezone - I also added some more test cases, although I'm not 100% sure that they are entirely correct.
diff --git a/moksha/lib/helpers.py b/moksha/lib/helpers.py index 120cdb6..5177e02 100644 --- a/moksha/lib/helpers.py +++ b/moksha/lib/helpers.py @@ -934,6 +934,13 @@ class DateTimeDisplay(object): '1 day' >>> d.age(datetime(2010, 7, 10, 10, 10), granularity='minute') '1 year, 1 month, 29 days, 10 hours and 10 minutes' + >>> d.age(tz='US/Eastern') + '1 day and 11 hours' + >>> d = DateTimeDisplay(datetime(2009, 5, 12, 12, 0, 0)) + >>> d.timestamp + datetime.datetime(2009, 5, 12, 12, 0) + >>> d.astimezone('Europe/Amsterdam') + datetime.datetime(2009, 5, 12, 14, 0, tzinfo=<DstTzInfo 'Europe/Amsterdam' CEST+2:00:00 DST>)
""" def __init__(self, timestamp, format='%Y-%m-%d %H:%M:%S'): @@ -949,16 +956,25 @@ class DateTimeDisplay(object): raise Exception("You must provide either a datetime object or a" "string, not %s" % type(timestamp))
+ def astimezone(self, tz): + """ Return `self.datetime` as a different timezone """ + timestamp = self.datetime.replace(tzinfo=utc) + zone = timezone(tz) + return zone.normalize(timestamp.astimezone(zone)) + def age(self, end=None, tz=None, granularity='hour'): """ Return the distance of time in words from `self.datetime` to `end`. """ + start = self.datetime if not end: if tz: + tz = timezone(tz) end = datetime.now(utc) - end = end.astimezone(timezone(tz)) + end = end.astimezone(tz) + start = self.datetime.replace(tzinfo=utc) + start = tz.normalize(start.astimezone(tz)) else: end = datetime.utcnow()
- return distance_of_time_in_words(self.datetime, end, - granularity=granularity) + return distance_of_time_in_words(start, end, granularity=granularity)
moksha-commits@lists.fedorahosted.org