I recently ran across several of the postings to this list and lucene-user
about DateField not handling dates before 1970.  Some people suggested
encoding the dates using a YYYYMMDD scheme, or modifying DateField to
offset the dates by a few hundred years.

In the documents that I'm indexing, there are a lot of historical dates,
ranging back to 500 BCE or so.  Wanting a much larger date range led me to
expand on the offsetting approach: by offsetting the dates by 2^62 (half
of the max value of a positive long).  This allows dates roughly 150
million years in the past or future (which should handle everything except
astronomical and some geological dates).

In the limited performance tests that I did, there was no change in the
indexing or querying speeds.  Attached is a patch against v1.4 from CVS.

-Esme
-- 
Esme Cowles <[EMAIL PROTECTED]>, UCSD Libraries

"I know not with what weapons World War III will be fought, but World War IV
 will be fought with sticks and stones."  -- attributed to Albert Einstein

--- DateField.java~     Fri Dec 13 13:20:43 2002
+++ DateField.java      Fri Dec 13 13:23:18 2002
@@ -60,16 +60,17 @@
  * Provides support for converting dates to strings and vice-versa.
  * The strings are structured so that lexicographic sorting orders by date.
  * This makes them suitable for use as field values and search terms.
- * <P>
- * Note: currenly dates before 1970 cannot be used, and therefore cannot be
- * indexed.
  */
 public class DateField {
   private DateField() {}
 
+  // shift starting point for long-encoded Dates to middle of positive long val
+  private static long DATE_OFFSET = 4611686018427387904L; // 2^62
+
   // make date strings long enough to last a millenium
-  private static int DATE_LEN = Long.toString(1000L*365*24*60*60*1000,
-                                              Character.MAX_RADIX).length();
+  private static int DATE_LEN = Long.toString(
+    1000L*365*24*60*60*1000 + DATE_OFFSET, Character.MAX_RADIX
+  ).length();
 
   public static String MIN_DATE_STRING() {
     return timeToString(0);
@@ -85,18 +86,15 @@
 
   /**
    * Converts a Date to a string suitable for indexing.
-   * This method will throw a RuntimeException if the date specified in the
-   * method argument is before 1970.
    */
   public static String dateToString(Date date) {
     return timeToString(date.getTime());
   }
   /**
    * Converts a millisecond time to a string suitable for indexing.
-   * This method will throw a RuntimeException if the time specified in the
-   * method argument is negative, that is, before 1970.
    */
   public static String timeToString(long time) {
+    time += DATE_OFFSET;
     if (time < 0)
       throw new RuntimeException("time too early");
 
@@ -118,7 +116,7 @@
 
   /** Converts a string-encoded date into a millisecond time. */
   public static long stringToTime(String s) {
-    return Long.parseLong(s, Character.MAX_RADIX);
+    return Long.parseLong(s, Character.MAX_RADIX) - DATE_OFFSET;
   }
   /** Converts a string-encoded date into a Date object. */
   public static Date stringToDate(String s) {
--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to