Anonymous avatar Anonymous committed f45a7bc

[svn] Test update for date, added revision info for asset tag and date module. Still working on date module.

Comments (0)

Files changed (3)

 WebHelpers ChangeLog
 
-0.1.3 (*svn*)
+0.2 (**svn**)
+* Brought asset tag up to date with revision info.
+
+0.1.3 (Release)
 * Brought feedgenerator in line with Django's version, which fixed the missing
   support for feed categories and updated classes for new-style. Other minor
   feed updates as well. Now synced as of Django r3143.

tests/test_date.py

         self.assertEqual("about 3 hours", distance_of_time_in_words(from_time, datetime(2004, 3, 7, 0, 41)))
         self.assertEqual("about 4 hours", distance_of_time_in_words(from_time, datetime(2004, 3, 7, 1, 20)))
         self.assertEqual("2 days", distance_of_time_in_words(from_time, datetime(2004, 3, 9, 15, 40)))
+        
+        # test greater date separation
+        self.assertEqual("29 days", distance_of_time_in_words(from_time, datetime(2004, 4, 5, 21, 41, 18)))
+        self.assertEqual("about 1 month", distance_of_time_in_words(from_time, datetime(2004, 4, 6, 21, 41, 18)))
+        self.assertEqual("about 1 month", distance_of_time_in_words(from_time, datetime(2004, 4, 7, 21, 41, 18)))
+        self.assertEqual("2 months", distance_of_time_in_words(from_time, datetime(2004, 5, 6, 21, 41, 18)))
+        self.assertEqual("11 months", distance_of_time_in_words(from_time, datetime(2005, 2, 6, 21, 41, 18)))
+        self.assertEqual("about 1 year", distance_of_time_in_words(from_time, datetime(2005, 4, 6, 21, 41, 18)))
+        self.assertEqual("about 1 year", distance_of_time_in_words(from_time, datetime(2005, 4, 12, 21, 41, 18)))
+        self.assertEqual("over 2 years", distance_of_time_in_words(from_time, datetime(2006, 4, 6, 21, 41, 18)))
+        
+        # include seconds 
+        self.assertEqual("less than a minute", distance_of_time_in_words(from_time, datetime(2004, 3, 6, 21, 41, 19), False))
+        self.assertEqual("less than 5 seconds", distance_of_time_in_words(from_time, datetime(2004, 3, 6, 21, 41, 19), True))
+        self.assertEqual("less than 10 seconds", distance_of_time_in_words(from_time, datetime(2004, 3, 6, 21, 41, 27), True))
+        self.assertEqual("less than 20 seconds", distance_of_time_in_words(from_time, datetime(2004, 3, 6, 21, 41, 37), True))
+        self.assertEqual("half a minute", distance_of_time_in_words(from_time, datetime(2004, 3, 6, 21, 41, 48), True))
+        self.assertEqual("less than a minute", distance_of_time_in_words(from_time, datetime(2004, 3, 6, 21, 42, 17), True))
 
+        self.assertEqual("1 minute", distance_of_time_in_words(from_time, datetime(2004, 3, 6, 21, 42, 18), True))
+        self.assertEqual("1 minute", distance_of_time_in_words(from_time, datetime(2004, 3, 6, 21, 42, 28), True))
+        self.assertEqual("2 minutes", distance_of_time_in_words(from_time, datetime(2004, 3, 6, 21, 42, 48), True))
+        
+        # test to < from
+        self.assertEqual("about 4 hours", distance_of_time_in_words(datetime(2004, 3, 7, 1, 20), from_time))
+        self.assertEqual("less than 20 seconds", distance_of_time_in_words(datetime(2004, 3, 6, 21, 41, 38), from_time, True))
+
+        # test with integers
+        self.assertEqual("less than a minute", distance_of_time_in_words(50))
+        self.assertEqual("about 1 hour", distance_of_time_in_words(60*60))
+
+        # more cumbersome test with integers
+        self.assertEqual("less than a minute", distance_of_time_in_words(0, 50))
+        self.assertEqual("about 1 hour", distance_of_time_in_words(60*60, 0))
+        
         # additional tests
         # exactly 24 hrs should be about 24 hrs - to be the same as Rails (it should be 1 day)
         self.assertEqual("about 24 hours", distance_of_time_in_words(from_time, datetime(2004, 3, 7, 21, 41, 18)))

webhelpers/rails/date.py

-"""
-Date/Time Helpers
-"""
+"""Date/Time Helpers"""
+# Last synced with Rails copy at Revision 4674 on Aug 19th, 2006.
 
 from datetime import datetime
 import time
     
     Set ``include_seconds`` to True if you want more detailed approximations if distance < 1 minute
     """
+    #print from_time, to_time
     if isinstance(from_time, int):
         from_time = time.time()+from_time
     else:
         to_time = time.mktime(to_time.timetuple())
     
     distance_in_minutes = int(round(abs(to_time-from_time)/60))
-    distance_in_seconds = int(round(abs(to_time-from_time)))
+    distance_in_seconds = int(abs(to_time-from_time))
+    print distance_in_seconds, from_time, to_time
     
     if distance_in_minutes <= 1:
         if include_seconds:
         return "about %d hours" % (round(distance_in_minutes / 60.0))
     elif distance_in_minutes <= 2880:
         return "1 day"
+    elif distance_in_minutes <= 43220:
+        return "%d days" % (round(distance_in_minutes / 1440))
+    elif distance_in_minutes <= 86400:
+        return "about 1 month"
+    elif distance_in_minutes <= 525960:
+        return "%d months" % (round(distance_in_minutes / 43200))
+    elif distance_in_minutes <= 1051920:
+        return "about 1 year"
     else:
-        return "%s days" % (distance_in_minutes / 1440)
+        return "over %d years" % (round(distance_in_minutes / 525600))
 
 def time_ago_in_words(from_time, include_seconds=False):
     """
     """
     return distance_of_time_in_words(from_time, datetime.now(), include_seconds)
 
-distance_of_time_in_words_to_now = time_ago_in_words
+def select_date(date=datetime.now(), **options):
+    """
+    Returns a set of html select-tags (one for year, month, day) pre-selected with the ``date``.
+    """
+    return select_year(date, options) + select_month(date, options) + select_day(date, options)
 
+def select_datetime(datetime=datetime.now(), **options):
+    """
+    Returns a set of html select-tags (one for year, month, day, hour and minutes) pre-selected with the ``date``.
+    """
+    return select_year(datetime, options) + select_month(datetime, options) + select_day(datetime, options) + \
+        select_hour(datetime, options) + select_minute(datetime, options)
 
-__all__ = ['distance_of_time_in_words', 'time_ago_in_words', 'distance_of_time_in_words_to_now']
+def select_time(datetime=datetime.now(), **options):
+    """Returns a set of html select-tags (one for hour and minute)
+    
+    ``include_seconds``
+        Boolean to indicate if an additional select field for seconds should
+        also be included.
+    
+    """
+    seconds = ''
+    if options.get('include_seconds'):
+        del options['include_seconds']
+        seconds = select_second(datetime, options)
+    return select_hour(datetime, options) + select_minute(datetime, options) + seconds
+
+def select_second(datetime, **options):
+    """Returns a select tag with options for each of the seconds 0 through 59 with the
+    current second selected.
+    
+    The 
+    """
+    pass
+
+__all__ = ['distance_of_time_in_words', 'time_ago_in_words']
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.