Merge branch 'master' of git://github.com/mono/mono
[mono.git] / mcs / tools / monodoc / Lucene.Net / Lucene.Net / Document / DateField.cs
1 /*\r
2  * Copyright 2004 The Apache Software Foundation\r
3  * \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
7  * \r
8  * http://www.apache.org/licenses/LICENSE-2.0\r
9  * \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
15  */\r
16 using System;\r
17 namespace Monodoc.Lucene.Net.Documents\r
18 {\r
19         \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
23         /// \r
24         /// <P>\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
33         /// \r
34         /// <P>\r
35         /// Note: dates before 1970 cannot be used, and therefore cannot be\r
36         /// indexed when using this class.\r
37         /// </summary>\r
38         public class DateField\r
39         {\r
40                 private DateField()\r
41                 {\r
42                 }\r
43                 \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
47                 \r
48                 public static System.String MIN_DATE_STRING()\r
49                 {\r
50                         return TimeToString(0);\r
51                 }\r
52                 \r
53                 public static System.String MAX_DATE_STRING()\r
54                 {\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
58                                 buffer[i] = c;\r
59                         return new System.String(buffer);\r
60                 }\r
61                 \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
65                 /// </summary>\r
66                 public static System.String DateToString(System.DateTime date)\r
67                 {\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
71                 }\r
72 \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
76                 /// </summary>\r
77                 public static System.String TimeToString(long time)\r
78                 {\r
79                         if (time < 0)\r
80                                 throw new System.SystemException("time too early");\r
81                         \r
82             System.String s = SupportClass.Number.ToString(time, SupportClass.Number.MAX_RADIX);\r
83                         \r
84                         if (s.Length > DATE_LEN)\r
85                                 throw new System.SystemException("time too late");\r
86                         \r
87                         // Pad with leading zeros\r
88                         if (s.Length < DATE_LEN)\r
89                         {\r
90                                 System.Text.StringBuilder sb = new System.Text.StringBuilder(s);\r
91                                 while (sb.Length < DATE_LEN)\r
92                                         sb.Insert(0, 0);\r
93                                 s = sb.ToString();\r
94                         }\r
95                         \r
96                         return s;\r
97                 }\r
98 \r
99                 /// <summary>Converts a string-encoded date into a millisecond time. </summary>\r
100                 public static long StringToTime(System.String s)\r
101                 {\r
102             return SupportClass.Number.Parse(s, SupportClass.Number.MAX_RADIX);\r
103                 }\r
104 \r
105                 /// <summary>Converts a string-encoded date into a Date object. </summary>\r
106                 public static System.DateTime StringToDate(System.String s)\r
107                 {\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
112             return date;\r
113 \r
114             /*\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
117             */\r
118                 }\r
119         }\r
120 }