Lucene.Net  3.0.3
Lucene.Net is a port of the Lucene search engine library, written in C# and targeted at .NET runtime users.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties Pages
DateField.cs
Go to the documentation of this file.
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 using System;
19 using Lucene.Net.Search;
20 using Lucene.Net.Support;
21 using NumericUtils = Lucene.Net.Util.NumericUtils;
22 using PrefixQuery = Lucene.Net.Search.PrefixQuery;
23 using TermRangeQuery = Lucene.Net.Search.TermRangeQuery;
24 // for javadoc
25 
26 namespace Lucene.Net.Documents
27 {
28  // for javadoc
29 
30  // do not remove in 3.0, needed for reading old indexes!
31 
32  /// <summary> Provides support for converting dates to strings and vice-versa.
33  /// The strings are structured so that lexicographic sorting orders by date,
34  /// which makes them suitable for use as field values and search terms.
35  ///
36  /// <p/>Note that this class saves dates with millisecond granularity,
37  /// which is bad for <see cref="TermRangeQuery" /> and <see cref="PrefixQuery" />, as those
38  /// queries are expanded to a BooleanQuery with a potentially large number
39  /// of terms when searching. Thus you might want to use
40  /// <see cref="DateTools" /> instead.
41  ///
42  /// <p/>
43  /// Note: dates before 1970 cannot be used, and therefore cannot be
44  /// indexed when using this class. See <see cref="DateTools" /> for an
45  /// alternative without such a limitation.
46  ///
47  /// <p/>
48  /// Another approach is <see cref="NumericUtils" />, which provides
49  /// a sortable binary representation (prefix encoded) of numeric values, which
50  /// date/time are.
51  /// For indexing a <see cref="DateTime" />, convert it to unix timestamp as
52  /// <c>long</c> and
53  /// index this as a numeric value with <see cref="NumericField" />
54  /// and use <see cref="NumericRangeQuery{T}" /> to query it.
55  ///
56  /// </summary>
57  /// <deprecated> If you build a new index, use <see cref="DateTools" /> or
58  /// <see cref="NumericField" /> instead.
59  /// This class is included for use with existing
60  /// indices and will be removed in a future (possibly Lucene 4.0)
61  /// </deprecated>
62  [Obsolete("If you build a new index, use DateTools or NumericField instead.This class is included for use with existing indices and will be removed in a future release (possibly Lucene 4.0).")]
63  public class DateField
64  {
65 
66  private DateField()
67  {
68  }
69 
70  // make date strings long enough to last a millenium
71  private static int DATE_LEN = Number.ToString(1000L * 365 * 24 * 60 * 60 * 1000, Number.MAX_RADIX).Length;
72 
73  public static System.String MIN_DATE_STRING()
74  {
75  return TimeToString(0);
76  }
77 
78  public static System.String MAX_DATE_STRING()
79  {
80  char[] buffer = new char[DATE_LEN];
82  for (int i = 0; i < DATE_LEN; i++)
83  buffer[i] = c;
84  return new System.String(buffer);
85  }
86 
87  /// <summary> Converts a Date to a string suitable for indexing.</summary>
88  /// <throws> RuntimeException if the date specified in the </throws>
89  /// <summary> method argument is before 1970
90  /// </summary>
91  public static System.String DateToString(System.DateTime date)
92  {
93  TimeSpan ts = date.Subtract(new DateTime(1970, 1, 1));
94  ts = ts.Subtract(TimeZone.CurrentTimeZone.GetUtcOffset(date));
95  return TimeToString(ts.Ticks / TimeSpan.TicksPerMillisecond);
96  }
97  /// <summary> Converts a millisecond time to a string suitable for indexing.</summary>
98  /// <throws> RuntimeException if the time specified in the </throws>
99  /// <summary> method argument is negative, that is, before 1970
100  /// </summary>
101  public static System.String TimeToString(long time)
102  {
103  if (time < 0)
104  throw new System.SystemException("time '" + time + "' is too early, must be >= 0");
105 
106  System.String s = Number.ToString(time, Character.MAX_RADIX);
107 
108  if (s.Length > DATE_LEN)
109  throw new System.SystemException("time '" + time + "' is too late, length of string " + "representation must be <= " + DATE_LEN);
110 
111  // Pad with leading zeros
112  if (s.Length < DATE_LEN)
113  {
114  System.Text.StringBuilder sb = new System.Text.StringBuilder(s);
115  while (sb.Length < DATE_LEN)
116  sb.Insert(0, 0);
117  s = sb.ToString();
118  }
119 
120  return s;
121  }
122 
123  /// <summary>Converts a string-encoded date into a millisecond time. </summary>
124  public static long StringToTime(System.String s)
125  {
126  return Number.Parse(s, Number.MAX_RADIX);
127  }
128  /// <summary>Converts a string-encoded date into a Date object. </summary>
129  public static System.DateTime StringToDate(System.String s)
130  {
131  long ticks = StringToTime(s) * TimeSpan.TicksPerMillisecond;
132  System.DateTime date = new System.DateTime(1970, 1, 1);
133  date = date.AddTicks(ticks);
134  date = date.Add(TimeZone.CurrentTimeZone.GetUtcOffset(date));
135  return date;
136  }
137  }
138 }