moved generic datetime manipulation function to lgc
authorSylvain Thénault <sylvain.thenault@logilab.fr>
Fri, 05 Feb 2010 11:54:23 +0100
changeset 4466 8b0ca7904820
parent 4465 18fb359f5c7a
child 4467 0e73d299730a
moved generic datetime manipulation function to lgc
devtools/__init__.py
goa/skel/views.py
req.py
schema.py
server/sqlutils.py
uilib.py
utils.py
web/facet.py
web/views/basecontrollers.py
web/views/calendar.py
web/views/old_calendar.py
web/views/plots.py
web/views/timetable.py
web/views/wdoc.py
--- a/devtools/__init__.py	Fri Feb 05 08:55:21 2010 +0100
+++ b/devtools/__init__.py	Fri Feb 05 11:54:23 2010 +0100
@@ -13,8 +13,8 @@
 from os.path import (abspath, join, exists, basename, dirname, normpath, split,
                      isfile, isabs)
 
+from logilab.common.date import strptime
 from cubicweb import CW_SOFTWARE_ROOT, ConfigurationError, schema, cwconfig
-from cubicweb.utils import strptime
 from cubicweb.server.serverconfig import ServerConfiguration
 from cubicweb.etwist.twconfig import TwistedConfiguration
 
--- a/goa/skel/views.py	Fri Feb 05 08:55:21 2010 +0100
+++ b/goa/skel/views.py	Fri Feb 05 11:54:23 2010 +0100
@@ -8,7 +8,8 @@
 """
 from datetime import date
 
-from cubicweb.utils import last_day
+from logilab.common.date import last_day
+
 from cubicweb.web.views import baseviews, boxes, calendar
 from cubicweb.web.htmlwidgets import BoxLink, BoxWidget
 
--- a/req.py	Fri Feb 05 08:55:21 2010 +0100
+++ b/req.py	Fri Feb 05 11:54:23 2010 +0100
@@ -13,10 +13,10 @@
 
 from logilab.common.decorators import cached
 from logilab.common.deprecation import deprecated
+from logilab.common.date import ustrftime, strptime, todate, todatetime
 
 from cubicweb import Unauthorized, RegistryException, typed_eid
 from cubicweb.rset import ResultSet
-from cubicweb.utils import ustrftime, strptime, todate, todatetime
 
 ONESECOND = timedelta(0, 1, 0)
 CACHE_REGISTRY = {}
--- a/schema.py	Fri Feb 05 08:55:21 2010 +0100
+++ b/schema.py	Fri Feb 05 11:54:23 2010 +0100
@@ -32,11 +32,6 @@
 import cubicweb
 from cubicweb import ETYPE_NAME_MAP, ValidationError, Unauthorized
 
-# XXX <3.2 bw compat
-from yams import schema
-schema.use_py_datetime()
-nodes.use_py_datetime()
-
 PURE_VIRTUAL_RTYPES = set(('identity', 'has_text',))
 VIRTUAL_RTYPES = set(('eid', 'identity', 'has_text',))
 
--- a/server/sqlutils.py	Fri Feb 05 08:55:21 2010 +0100
+++ b/server/sqlutils.py	Fri Feb 05 11:54:23 2010 +0100
@@ -14,15 +14,15 @@
 from datetime import datetime, date, timedelta
 
 import logilab.common as lgc
+from logilab.common import db
 from logilab.common.shellutils import ProgressBar
-from logilab.common import db
 from logilab.common.adbh import get_adv_func_helper
 from logilab.common.sqlgen import SQLGenerator
+from logilab.common.date import todate, todatetime
 
 from indexer import get_indexer
 
 from cubicweb import Binary, ConfigurationError
-from cubicweb.utils import todate, todatetime
 from cubicweb.uilib import remove_html_tags
 from cubicweb.toolsutils import restrict_perms_to_user
 from cubicweb.schema import PURE_VIRTUAL_RTYPES
--- a/uilib.py	Fri Feb 05 08:55:21 2010 +0100
+++ b/uilib.py	Fri Feb 05 11:54:23 2010 +0100
@@ -15,8 +15,8 @@
 from StringIO import StringIO
 
 from logilab.mtconverter import xml_escape, html_unescape
+from logilab.common.date import ustrftime
 
-from cubicweb.utils import ustrftime
 
 def rql_for_eid(eid):
     """return the rql query necessary to fetch entity with the given eid.  This
--- 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)
--- a/web/facet.py	Fri Feb 05 08:55:21 2010 +0100
+++ b/web/facet.py	Fri Feb 05 11:54:23 2010 +0100
@@ -13,16 +13,16 @@
 from datetime import date, datetime, timedelta
 
 from logilab.mtconverter import xml_escape
-
 from logilab.common.graph import has_path
 from logilab.common.decorators import cached
+from logilab.common.date import datetime2ticks, ustrftime
 from logilab.common.compat import all
 
 from rql import parse, nodes
 
 from cubicweb import Unauthorized, typed_eid
 from cubicweb.schema import display_name
-from cubicweb.utils import datetime2ticks, make_uid, ustrftime
+from cubicweb.utils import make_uid
 from cubicweb.selectors import match_context_prop, partial_relation_possible
 from cubicweb.appobject import AppObject
 from cubicweb.web.htmlwidgets import HTMLWidget
--- a/web/views/basecontrollers.py	Fri Feb 05 08:55:21 2010 +0100
+++ b/web/views/basecontrollers.py	Fri Feb 05 11:54:23 2010 +0100
@@ -15,9 +15,10 @@
 import simplejson
 
 from logilab.common.decorators import cached
+from logilab.common.date import strptime
 
 from cubicweb import NoSelectableObject, ValidationError, ObjectNotFound, typed_eid
-from cubicweb.utils import strptime, CubicWebJsonEncoder
+from cubicweb.utils import CubicWebJsonEncoder
 from cubicweb.selectors import yes, match_user_groups
 from cubicweb.mail import format_mail
 from cubicweb.web import ExplicitLogin, Redirect, RemoteCallFailed, json_dumps
--- a/web/views/calendar.py	Fri Feb 05 08:55:21 2010 +0100
+++ b/web/views/calendar.py	Fri Feb 05 11:54:23 2010 +0100
@@ -11,10 +11,10 @@
 from datetime import datetime, date, timedelta
 
 from logilab.mtconverter import xml_escape
+from logilab.common.date import strptime, date_range, todate, todatetime
 
 from cubicweb.interfaces import ICalendarable
 from cubicweb.selectors import implements
-from cubicweb.utils import strptime, date_range, todate, todatetime
 from cubicweb.view import EntityView
 
 
--- a/web/views/old_calendar.py	Fri Feb 05 08:55:21 2010 +0100
+++ b/web/views/old_calendar.py	Fri Feb 05 11:54:23 2010 +0100
@@ -9,9 +9,10 @@
 from datetime import date, time, timedelta
 
 from logilab.mtconverter import xml_escape
+from logilab.common.date import (ONEDAY, ONEWEEK, days_in_month, previous_month,
+                                 next_month, first_day, last_day, date_range)
 
 from cubicweb.interfaces import ICalendarViews
-from cubicweb.utils import ONEDAY, ONEWEEK, date_range, first_day, last_day, previous_month, next_month, days_in_month
 from cubicweb.selectors import implements
 from cubicweb.view import EntityView
 
--- a/web/views/plots.py	Fri Feb 05 08:55:21 2010 +0100
+++ b/web/views/plots.py	Fri Feb 05 11:54:23 2010 +0100
@@ -13,9 +13,10 @@
 from simplejson import dumps
 
 from logilab.common import flatten
+from logilab.common.date import datetime2ticks
 from logilab.mtconverter import xml_escape
 
-from cubicweb.utils import make_uid, UStringIO, datetime2ticks
+from cubicweb.utils import make_uid, UStringIO
 from cubicweb.appobject import objectify_selector
 from cubicweb.selectors import multi_columns_rset
 from cubicweb.web.views import baseviews
--- a/web/views/timetable.py	Fri Feb 05 08:55:21 2010 +0100
+++ b/web/views/timetable.py	Fri Feb 05 11:54:23 2010 +0100
@@ -7,10 +7,10 @@
 """
 
 from logilab.mtconverter import xml_escape
+from logilab.common.date import date_range, todatetime
 
 from cubicweb.interfaces import ITimetableViews
 from cubicweb.selectors import implements
-from cubicweb.utils import date_range, todatetime
 from cubicweb.view import AnyRsetView
 
 
--- a/web/views/wdoc.py	Fri Feb 05 08:55:21 2010 +0100
+++ b/web/views/wdoc.py	Fri Feb 05 11:54:23 2010 +0100
@@ -13,11 +13,11 @@
 from datetime import date
 
 from logilab.common.changelog import ChangeLog
+from logilab.common.date import strptime, todate
 from logilab.mtconverter import CHARSET_DECL_RGX
 
 from cubicweb.selectors import match_form_params, yes
 from cubicweb.view import StartupView
-from cubicweb.utils import strptime, todate
 from cubicweb.uilib import rest_publish
 from cubicweb.web import NotFound, action
 _ = unicode