Commits

Thomas Waldmann  committed a17d567

DATETIME.to_text - extend type support to text_type (unicode/str)

If whoosh parses a query for a DATETIME field, it makes quite some effort to
make sense out of numeric strings, so something like 2011 or
20111231235959999999 is parsed as one would expect it.

In this changeset, I modified method .to_text (which is used at indexing time)
to also try to make sense out of numeric strings (in about the same way).

It calls floor() so that 2011 means the same as 201101010000000000 (floor
makes most sense, 42 and 42.0000000 is usually considered same thing, so,
following same logic, if you just say 2011, but we need some specific
datetime, that shall be the very first moment in that year).

  • Participants
  • Parent commits 0aec288

Comments (0)

Files changed (1)

File src/whoosh/fields.py

                                        unique=unique, shift_step=8)
     
     def to_text(self, x, shift=0):
-        if isinstance(x, datetime.datetime):
-            x = datetime_to_long(x)
-        elif not isinstance(x, integer_types):
-            raise ValueError("DATETIME.to_text field doesn't know what to do "
-                             "with %r" % x)
+        from whoosh.support.times import floor
+        try:
+            if isinstance(x, text_type):
+                # for indexing, support same strings as for query parsing
+                x = self._parse_datestring(x)
+                x = floor(x) # this makes most sense (unspecified = lowest possible value)
+            if isinstance(x, datetime.datetime):
+                x = datetime_to_long(x)
+            elif not isinstance(x, integer_types):
+                raise TypeError()
+        except Exception:
+            raise ValueError("DATETIME.to_text can't convert from %r" % (x, ))
         
         return super(DATETIME, self).to_text(x, shift=shift)