2 * Copyright 2004 The Apache Software Foundation
\r
4 * Licensed under the Apache License, Version 2.0 (the "License");
\r
5 * you may not use this file except in compliance with the License.
\r
6 * You may obtain a copy of the License at
\r
8 * http://www.apache.org/licenses/LICENSE-2.0
\r
10 * Unless required by applicable law or agreed to in writing, software
\r
11 * distributed under the License is distributed on an "AS IS" BASIS,
\r
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
13 * See the License for the specific language governing permissions and
\r
14 * limitations under the License.
\r
17 namespace Monodoc.Lucene.Net.Documents
\r
20 /// <summary> Provides support for converting dates to strings and vice-versa.
\r
21 /// The strings are structured so that lexicographic sorting orders by date,
\r
22 /// which makes them suitable for use as Field values and search terms.
\r
25 /// Note that you do not have to use this class, you can just save your
\r
26 /// dates as strings if lexicographic sorting orders them by date. This is
\r
27 /// the case for example for dates like <code>yyyy-mm-dd hh:mm:ss</code>
\r
28 /// (of course you can leave out the delimiter characters to save some space).
\r
29 /// The advantage with using such a format is that you can easily save dates
\r
30 /// with the required granularity, e.g. leaving out seconds. This saves memory
\r
31 /// when searching with a RangeQuery or PrefixQuery, as Lucene
\r
32 /// expands these queries to a BooleanQuery with potentially very many terms.
\r
35 /// Note: dates before 1970 cannot be used, and therefore cannot be
\r
36 /// indexed when using this class.
\r
38 public class DateField
\r
44 // make date strings long enough to last a millenium
\r
45 private static int DATE_LEN = SupportClass.Number.ToString(
\r
46 1000L * 365 * 24 * 60 * 60 * 1000, SupportClass.Number.MAX_RADIX).Length;
\r
48 public static System.String MIN_DATE_STRING()
\r
50 return TimeToString(0);
\r
53 public static System.String MAX_DATE_STRING()
\r
55 char[] buffer = new char[DATE_LEN];
\r
56 char c = SupportClass.Character.ForDigit(36 - 1, SupportClass.Character.MAX_RADIX);
\r
57 for (int i = 0; i < DATE_LEN; i++)
\r
59 return new System.String(buffer);
\r
62 /// <summary> Converts a Date to a string suitable for indexing.</summary>
\r
63 /// <throws> RuntimeException if the date specified in the </throws>
\r
64 /// <summary> method argument is before 1970
\r
66 public static System.String DateToString(System.DateTime date)
\r
68 TimeSpan ts = date.Subtract(new DateTime(1970, 1, 1));
\r
69 ts = ts.Subtract(TimeZone.CurrentTimeZone.GetUtcOffset(date));
\r
70 return TimeToString(ts.Ticks / TimeSpan.TicksPerMillisecond);
\r
73 /// <summary> Converts a millisecond time to a string suitable for indexing.</summary>
\r
74 /// <throws> RuntimeException if the time specified in the </throws>
\r
75 /// <summary> method argument is negative, that is, before 1970
\r
77 public static System.String TimeToString(long time)
\r
80 throw new System.SystemException("time too early");
\r
82 System.String s = SupportClass.Number.ToString(time, SupportClass.Number.MAX_RADIX);
\r
84 if (s.Length > DATE_LEN)
\r
85 throw new System.SystemException("time too late");
\r
87 // Pad with leading zeros
\r
88 if (s.Length < DATE_LEN)
\r
90 System.Text.StringBuilder sb = new System.Text.StringBuilder(s);
\r
91 while (sb.Length < DATE_LEN)
\r
99 /// <summary>Converts a string-encoded date into a millisecond time. </summary>
\r
100 public static long StringToTime(System.String s)
\r
102 return SupportClass.Number.Parse(s, SupportClass.Number.MAX_RADIX);
\r
105 /// <summary>Converts a string-encoded date into a Date object. </summary>
\r
106 public static System.DateTime StringToDate(System.String s)
\r
108 long ticks = StringToTime(s) * TimeSpan.TicksPerMillisecond;
\r
109 System.DateTime date = new System.DateTime(1970, 1, 1);
\r
110 date = date.AddTicks(ticks);
\r
111 date = date.Add(TimeZone.CurrentTimeZone.GetUtcOffset(date));
\r
115 System.TimeSpan ts = System.TimeSpan.FromMilliseconds(System.DateField.StringToTime(s));
\r
116 return new System.DateTime(1970,1,1).Add(ts).ToLocalTime();
\r