Lucene.Net  3.0.3
Lucene.Net is a .NET port of the Java Lucene Indexing Library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties
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 
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 
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  }
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 
124  public static long StringToTime(System.String s)
125  {
126  return Number.Parse(s, Number.MAX_RADIX);
127  }
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 }