[ckan-changes] commit/ckan: dread: [helpers]: #1266 Fix timestamp parsing when there are no microseconds. Add several tests and mark deprecated conversion methods in model.

Bitbucket commits-noreply at bitbucket.org
Mon Aug 8 09:12:59 UTC 2011


1 new changeset in ckan:

http://bitbucket.org/okfn/ckan/changeset/987dc4fe078e/
changeset:   987dc4fe078e
branch:      release-v1.4.3
user:        dread
date:        2011-08-08 11:12:41
summary:     [helpers]: #1266 Fix timestamp parsing when there are no microseconds. Add several tests and mark deprecated conversion methods in model.
affected #:  3 files (1.9 KB)

--- a/ckan/lib/helpers.py	Fri Aug 05 18:35:44 2011 +0100
+++ b/ckan/lib/helpers.py	Mon Aug 08 10:12:41 2011 +0100
@@ -5,7 +5,9 @@
 Consists of functions to typically be used within templates, but also
 available to Controllers. This module is available to templates as 'h'.
 """
-from datetime import datetime
+import datetime
+import re
+
 from webhelpers.html import escape, HTML, literal, url_escape
 from webhelpers.html.tools import mail_to
 from webhelpers.html.tags import *
@@ -28,8 +30,7 @@
     import json
 except ImportError:
     import simplejson as json
-
-ISO_DATE_FORMAT = '%Y-%m-%dT%H:%M:%S.%f'
+    
 
 class Message(object):
     """A message returned by ``Flash.pop_messages()``.
@@ -210,11 +211,11 @@
     '''
     from ckan import model
     date_format = '%Y-%m-%d %H:%M'
-    if isinstance(datetime_, datetime):
+    if isinstance(datetime_, datetime.datetime):
         return datetime_.strftime(date_format)
     elif isinstance(datetime_, basestring):
         try:
-            datetime_ = model.strptimestamp(datetime_)
+            datetime_ = date_str_to_datetime(datetime_)
         except TypeError:
             return ''
         except ValueError:
@@ -223,8 +224,20 @@
     else:
         return ''
 
-def date_str_to_datetime(date_str, format=ISO_DATE_FORMAT):
-    return datetime.strptime(date_str, format)
+def datetime_to_date_str(datetime_):
+    '''Takes a datetime.datetime object and returns a string of it
+    in ISO format.
+    '''
+    return datetime_.isoformat()
 
-def time_ago_in_words_from_str(date_str, format=ISO_DATE_FORMAT, granularity='month'):
-    return date.time_ago_in_words(datetime.strptime(date_str, format), granularity=granularity)
+def date_str_to_datetime(date_str):
+    '''Takes an ISO format timestamp and returns the equivalent
+    datetime.datetime object.
+    '''
+    # Doing this split is more accepting of input variations than doing
+    # a strptime. Also avoids problem with Python 2.5 not having %f.
+    return datetime.datetime(*map(int, re.split('[^\d]', date_str)))
+
+def time_ago_in_words_from_str(date_str, granularity='month'):
+    return date.time_ago_in_words(date_str_to_datetime(date_str), granularity=granularity)
+


--- a/ckan/model/__init__.py	Fri Aug 05 18:35:44 2011 +0100
+++ b/ckan/model/__init__.py	Mon Aug 08 10:12:41 2011 +0100
@@ -226,7 +226,7 @@
                      and 7 (see datetime constructor).
     raises ValueError if any of the numbers are out of range.
     '''
-    
+    # TODO: METHOD DEPRECATED - use ckan.lib.helpers.date_str_to_datetime
     import datetime, re
     return datetime.datetime(*map(int, re.split('[^\d]', s)))
 
@@ -234,6 +234,7 @@
     '''Takes a datetime.datetime and returns it as an ISO string. For
     a pretty printed string, use ckan.lib.helpers.render_datetime.
     '''
+    # TODO: METHOD DEPRECATED - use ckan.lib.helpers.datetime_to_date_str
     return t.isoformat()
 
 def revision_as_dict(revision, include_packages=True, include_groups=True,ref_package_by='name'):


--- a/ckan/tests/lib/test_helpers.py	Fri Aug 05 18:35:44 2011 +0100
+++ b/ckan/tests/lib/test_helpers.py	Mon Aug 08 10:12:41 2011 +0100
@@ -1,5 +1,7 @@
 # -*- coding: utf-8 -*-
 import time
+import datetime
+from nose.tools import assert_equal
 
 from ckan.tests import *
 from ckan.lib import helpers as h
@@ -16,4 +18,35 @@
         
     def test_extract_markdown(self):
         assert "Data exposed" in h.markdown_extract(WITH_HTML)
-        assert "collects information" in h.markdown_extract(WITH_UNICODE)
\ No newline at end of file
+        assert "collects information" in h.markdown_extract(WITH_UNICODE)
+
+    def test_render_datetime(self):
+        res = h.render_datetime(datetime.datetime(2008, 4, 13, 20, 40, 20, 123456))
+        assert_equal(res, '2008-04-13 20:40')
+
+    def test_render_datetime_but_from_string(self):
+        res = h.render_datetime('2008-04-13T20:40:20.123456')
+        assert_equal(res, '2008-04-13 20:40')
+
+    def test_render_datetime_blank(self):
+        res = h.render_datetime(None)
+        assert_equal(res, '')
+
+    def test_datetime_to_date_str(self):
+        res = h.datetime_to_date_str(datetime.datetime(2008, 4, 13, 20, 40, 20, 123456))
+        assert_equal(res, '2008-04-13T20:40:20.123456')
+
+    def test_date_str_to_datetime(self):
+        res = h.date_str_to_datetime('2008-04-13T20:40:20.123456')
+        assert_equal(res, datetime.datetime(2008, 4, 13, 20, 40, 20, 123456))
+
+    def test_date_str_to_datetime_without_microseconds(self):
+        # This occurs in ckan.net timestamps - not sure how they appeared
+        res = h.date_str_to_datetime('2008-04-13T20:40:20')
+        assert_equal(res, datetime.datetime(2008, 4, 13, 20, 40, 20))
+
+    def test_time_ago_in_words_from_str(self):
+        two_months_ago = datetime.datetime.now() - datetime.timedelta(days=65)
+        two_months_ago_str = h.datetime_to_date_str(two_months_ago)
+        res = h.time_ago_in_words_from_str(two_months_ago_str)
+        assert_equal(res, '2 months')

Repository URL: https://bitbucket.org/okfn/ckan/

--

This is a commit notification from bitbucket.org. You are receiving
this because you have the service enabled, addressing the recipient of
this email.




More information about the ckan-changes mailing list