utils.py
changeset 4466 8b0ca7904820
parent 4335 1314772c1183
child 4478 442fd69ff13b
--- a/utils.py	Fri Feb 05 08:55:21 2010 +0100
+++ b/utils.py	Fri Feb 05 11:54:23 2010 +0100
@@ -12,72 +12,18 @@
 import locale
 import sys
 import decimal
-import datetime as pydatetime
+import datetime
 from md5 import md5
-from datetime import datetime, timedelta, date
-from time import time, mktime
+from time import time
 from random import randint, seed
-from calendar import monthrange
 import decimal
 
+from logilab.common.date import strptime, todate, next_month
+
 # initialize random seed from current time
 seed()
-try:
-    strptime = datetime.strptime
-except AttributeError: # py < 2.5
-    from time import strptime as time_strptime
-    def strptime(value, format):
-        return datetime(*time_strptime(value, format)[:6])
 
-def todate(somedate):
-    """return a date from a date (leaving unchanged) or a datetime"""
-    if isinstance(somedate, datetime):
-        return date(somedate.year, somedate.month, somedate.day)
-    assert isinstance(somedate, date), repr(somedate)
-    return somedate
-
-def todatetime(somedate):
-    """return a date from a date (leaving unchanged) or a datetime"""
-    # take care, datetime is a subclass of date
-    if isinstance(somedate, datetime):
-        return somedate
-    assert isinstance(somedate, date), repr(somedate)
-    return datetime(somedate.year, somedate.month, somedate.day)
-
-def datetime2ticks(date):
-    return mktime(date.timetuple()) * 1000
-
-ONEDAY = timedelta(days=1)
-ONEWEEK = timedelta(days=7)
-
-def days_in_month(date_):
-    return monthrange(date_.year, date_.month)[1]
-
-def days_in_year(date_):
-    feb = pydatetime.date(date_.year, 2, 1)
-    if days_in_month(feb) == 29:
-        return 366
-    else:
-        return 365
-
-def previous_month(date_, nbmonth=1):
-    while nbmonth:
-        date_ = first_day(date_) - ONEDAY
-        nbmonth -= 1
-    return date_
-
-def next_month(date_, nbmonth=1):
-    while nbmonth:
-        date_ = last_day(date_) + ONEDAY
-        nbmonth -= 1
-    return date_
-
-def first_day(date_):
-    return date(date_.year, date_.month, 1)
-
-def last_day(date_):
-    return date(date_.year, date_.month, days_in_month(date_))
-
+# XXX should replace lgc.date.date_range implementation
 def date_range(begin, end, incday=None, incmonth=None):
     """yields each date between begin and end
     :param begin: the start date
@@ -99,21 +45,11 @@
         if not incday:
             incr = ONEDAY
         else:
-            incr = timedelta(incday)
+            incr = datetime.timedelta(incday)
         while begin <= end:
            yield begin
            begin += incr
 
-def ustrftime(date, fmt='%Y-%m-%d'):
-    """like strftime, but returns a unicode string instead of an encoded
-    string which' may be problematic with localized date.
-
-    encoding is guessed by locale.getpreferredencoding()
-    """
-    # date format may depend on the locale
-    encoding = locale.getpreferredencoding(do_setlocale=False) or 'UTF-8'
-    return unicode(date.strftime(str(fmt)), encoding)
-
 
 if sys.version_info[:2] < (2, 5):
     def make_uid(key):
@@ -407,13 +343,13 @@
     class CubicWebJsonEncoder(JSONEncoder):
         """define a simplejson encoder to be able to encode yams std types"""
         def default(self, obj):
-            if isinstance(obj, pydatetime.datetime):
+            if isinstance(obj, datetime.datetime):
                 return obj.strftime('%Y/%m/%d %H:%M:%S')
-            elif isinstance(obj, pydatetime.date):
+            elif isinstance(obj, datetime.date):
                 return obj.strftime('%Y/%m/%d')
-            elif isinstance(obj, pydatetime.time):
+            elif isinstance(obj, datetime.time):
                 return obj.strftime('%H:%M:%S')
-            elif isinstance(obj, pydatetime.timedelta):
+            elif isinstance(obj, datetime.timedelta):
                 return (obj.days * 24 * 60 * 60) + obj.seconds
             elif isinstance(obj, decimal.Decimal):
                 return float(obj)