5 namespace Lucene
.Net
.Documents
7 /* ====================================================================
8 * The Apache Software License, Version 1.1
10 * Copyright (c) 2001 The Apache Software Foundation. All rights
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
17 * 1. Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
20 * 2. Redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in
22 * the documentation and/or other materials provided with the
25 * 3. The end-user documentation included with the redistribution,
26 * if any, must include the following acknowledgment:
27 * "This product includes software developed by the
28 * Apache Software Foundation (http://www.apache.org/)."
29 * Alternately, this acknowledgment may appear in the software itself,
30 * if and wherever such third-party acknowledgments normally appear.
32 * 4. The names "Apache" and "Apache Software Foundation" and
33 * "Apache Lucene" must not be used to endorse or promote products
34 * derived from this software without prior written permission. For
35 * written permission, please contact apache@apache.org.
37 * 5. Products derived from this software may not be called "Apache",
38 * "Apache Lucene", nor may "Apache" appear in their name, without
39 * prior written permission of the Apache Software Foundation.
41 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
42 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
43 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
44 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
45 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
46 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
47 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
48 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
49 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
50 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
51 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
53 * ====================================================================
55 * This software consists of voluntary contributions made by many
56 * individuals on behalf of the Apache Software Foundation. For more
57 * information on the Apache Software Foundation, please see
58 * <http://www.apache.org/>.
62 /// Provides support for converting dates to strings and vice-versa.
63 /// The strings are structured so that lexicographic sorting orders by date.
64 /// This makes them suitable for use as field values and search terms.
66 /// Note: currenly dates before 1970 cannot be used, and therefore cannot be
69 public class DateField
71 private DateField() {}
74 /// make date strings long enough to last a millenium
76 private static int DATE_LEN
= Lucene
.Net
.Util
.Number
.ToString(
77 1000L*365*24*60*60*1000,
78 Lucene
.Net
.Util
.Number
.MAX_RADIX
).Length
;
80 public static String
MIN_DATE_STRING()
82 return TimeToString(0);
85 public static String
MAX_DATE_STRING()
87 char[] buffer
= new char[DATE_LEN
];
89 for (int i
= 0 ; i
< DATE_LEN
; i
++)
91 return new string(buffer
);
95 /// Converts a Date to a string suitable for indexing.
96 /// This method will throw a Exception if the date specified in the
97 /// method argument is before 1970.
99 /// <param name="date"></param>
100 /// <returns></returns>
101 public static String
DateToString(DateTime date
)
103 return TimeToString(Date
.GetTime(date
));
107 /// Converts a millisecond time to a string suitable for indexing.
108 /// This method will throw a Exception if the time specified in the
109 /// method argument is negative, that is, before 1970.
111 /// <param name="time"></param>
112 /// <returns></returns>
113 public static String
TimeToString(long time
)
116 throw new Exception("time too early");
118 string s
= Lucene
.Net
.Util
.Number
.ToString(
119 time
, Lucene
.Net
.Util
.Number
.MAX_RADIX
122 if (s
.Length
> DATE_LEN
)
123 throw new Exception("time too late");
125 while (s
.Length
< DATE_LEN
)
126 s
= "0" + s
; // pad with leading zeros
128 // Pad with leading zeros
129 if (s
.Length
< DATE_LEN
)
131 StringBuilder sb
= new StringBuilder(s
);
132 while (sb
.Length
< DATE_LEN
)
141 /// Converts a string-encoded date into a millisecond time.
143 /// <param name="s"></param>
144 /// <returns></returns>
145 public static long StringToTime(String s
)
147 return Lucene
.Net
.Util
.Number
.Parse(s
, Lucene
.Net
.Util
.Number
.MAX_RADIX
);
151 /// Converts a string-encoded date into a Date object.
153 /// <param name="s"></param>
154 /// <returns></returns>
155 public static DateTime
StringToDate(String s
)
157 long ticks
= StringToTime(s
) * TimeSpan
.TicksPerMillisecond
;
158 DateTime date
= new DateTime(ticks
);
159 date
= date
.Add(TimeZone
.CurrentTimeZone
.GetUtcOffset(date
));
160 date
= date
.AddYears(1969);