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