You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@allura.apache.org by br...@apache.org on 2013/03/26 22:02:42 UTC
[08/22] git commit: [#2835] ticket:287 Add ago_string helper
[#2835] ticket:287 Add ago_string helper
Project: http://git-wip-us.apache.org/repos/asf/incubator-allura/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-allura/commit/556cfdbd
Tree: http://git-wip-us.apache.org/repos/asf/incubator-allura/tree/556cfdbd
Diff: http://git-wip-us.apache.org/repos/asf/incubator-allura/diff/556cfdbd
Branch: refs/heads/master
Commit: 556cfdbde2a4b382a7003889432d634aca21b56b
Parents: 5b1eba0
Author: Igor Bondarenko <je...@gmail.com>
Authored: Tue Mar 5 15:39:17 2013 +0000
Committer: Dave Brondsema <db...@geek.net>
Committed: Tue Mar 26 20:57:35 2013 +0000
----------------------------------------------------------------------
Allura/allura/lib/helpers.py | 6 ++++++
Allura/allura/tests/test_helpers.py | 4 ++++
2 files changed, 10 insertions(+), 0 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/556cfdbd/Allura/allura/lib/helpers.py
----------------------------------------------------------------------
diff --git a/Allura/allura/lib/helpers.py b/Allura/allura/lib/helpers.py
index d078072..5e32882 100644
--- a/Allura/allura/lib/helpers.py
+++ b/Allura/allura/lib/helpers.py
@@ -269,6 +269,12 @@ def ago(start_time):
def ago_ts(timestamp):
return ago(datetime.utcfromtimestamp(timestamp))
+def ago_string(s):
+ try:
+ return ago(parse(s, ignoretz=True))
+ except (ValueError, AttributeError):
+ return 'unknown'
+
class DateTimeConverter(FancyValidator):
def _to_python(self, value, state):
http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/556cfdbd/Allura/allura/tests/test_helpers.py
----------------------------------------------------------------------
diff --git a/Allura/allura/tests/test_helpers.py b/Allura/allura/tests/test_helpers.py
index 4eaa7ec..8407ef4 100644
--- a/Allura/allura/tests/test_helpers.py
+++ b/Allura/allura/tests/test_helpers.py
@@ -94,6 +94,10 @@ def test_ago():
import time
assert_equals(h.ago(datetime.utcnow() - timedelta(days=2)), '2 days ago')
assert_equals(h.ago_ts(time.time() - 60*60*2), '2 hours ago')
+ d_str = (datetime.utcnow() - timedelta(hours=3)).isoformat()
+ assert_equals(h.ago_string(d_str), '3 hours ago')
+ assert_equals(h.ago_string('bad format'), 'unknown')
+ assert_equals(h.ago_string(None), 'unknown')
def test_urlquote_unicode():
# No exceptions please