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
DateTools.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 NumericUtils = Lucene.Net.Util.NumericUtils;
21 
22 namespace Lucene.Net.Documents
23 {
24 
47  public class DateTools
48  {
49 
50  private static readonly System.String YEAR_FORMAT = "yyyy";
51  private static readonly System.String MONTH_FORMAT = "yyyyMM";
52  private static readonly System.String DAY_FORMAT = "yyyyMMdd";
53  private static readonly System.String HOUR_FORMAT = "yyyyMMddHH";
54  private static readonly System.String MINUTE_FORMAT = "yyyyMMddHHmm";
55  private static readonly System.String SECOND_FORMAT = "yyyyMMddHHmmss";
56  private static readonly System.String MILLISECOND_FORMAT = "yyyyMMddHHmmssfff";
57 
58  private static readonly System.Globalization.Calendar calInstance = new System.Globalization.GregorianCalendar();
59 
60  // cannot create, the class has static methods only
61  private DateTools()
62  {
63  }
64 
76  public static System.String DateToString(System.DateTime date, Resolution resolution)
77  {
78  return TimeToString(date.Ticks / TimeSpan.TicksPerMillisecond, resolution);
79  }
80 
92  public static System.String TimeToString(long time, Resolution resolution)
93  {
94  System.DateTime date = new System.DateTime(Round(time, resolution));
95 
96  if (resolution == Resolution.YEAR)
97  {
98  return date.ToString(YEAR_FORMAT, System.Globalization.CultureInfo.InvariantCulture);
99  }
100  else if (resolution == Resolution.MONTH)
101  {
102  return date.ToString(MONTH_FORMAT, System.Globalization.CultureInfo.InvariantCulture);
103  }
104  else if (resolution == Resolution.DAY)
105  {
106  return date.ToString(DAY_FORMAT, System.Globalization.CultureInfo.InvariantCulture);
107  }
108  else if (resolution == Resolution.HOUR)
109  {
110  return date.ToString(HOUR_FORMAT, System.Globalization.CultureInfo.InvariantCulture);
111  }
112  else if (resolution == Resolution.MINUTE)
113  {
114  return date.ToString(MINUTE_FORMAT, System.Globalization.CultureInfo.InvariantCulture);
115  }
116  else if (resolution == Resolution.SECOND)
117  {
118  return date.ToString(SECOND_FORMAT, System.Globalization.CultureInfo.InvariantCulture);
119  }
120  else if (resolution == Resolution.MILLISECOND)
121  {
122  return date.ToString(MILLISECOND_FORMAT, System.Globalization.CultureInfo.InvariantCulture);
123  }
124 
125  throw new System.ArgumentException("unknown resolution " + resolution);
126  }
127 
140  public static long StringToTime(System.String dateString)
141  {
142  return StringToDate(dateString).Ticks;
143  }
144 
157  public static System.DateTime StringToDate(System.String dateString)
158  {
159  System.DateTime date;
160  if (dateString.Length == 4)
161  {
162  date = new System.DateTime(Convert.ToInt16(dateString.Substring(0, 4)),
163  1, 1, 0, 0, 0, 0);
164  }
165  else if (dateString.Length == 6)
166  {
167  date = new System.DateTime(Convert.ToInt16(dateString.Substring(0, 4)),
168  Convert.ToInt16(dateString.Substring(4, 2)),
169  1, 0, 0, 0, 0);
170  }
171  else if (dateString.Length == 8)
172  {
173  date = new System.DateTime(Convert.ToInt16(dateString.Substring(0, 4)),
174  Convert.ToInt16(dateString.Substring(4, 2)),
175  Convert.ToInt16(dateString.Substring(6, 2)),
176  0, 0, 0, 0);
177  }
178  else if (dateString.Length == 10)
179  {
180  date = new System.DateTime(Convert.ToInt16(dateString.Substring(0, 4)),
181  Convert.ToInt16(dateString.Substring(4, 2)),
182  Convert.ToInt16(dateString.Substring(6, 2)),
183  Convert.ToInt16(dateString.Substring(8, 2)),
184  0, 0, 0);
185  }
186  else if (dateString.Length == 12)
187  {
188  date = new System.DateTime(Convert.ToInt16(dateString.Substring(0, 4)),
189  Convert.ToInt16(dateString.Substring(4, 2)),
190  Convert.ToInt16(dateString.Substring(6, 2)),
191  Convert.ToInt16(dateString.Substring(8, 2)),
192  Convert.ToInt16(dateString.Substring(10, 2)),
193  0, 0);
194  }
195  else if (dateString.Length == 14)
196  {
197  date = new System.DateTime(Convert.ToInt16(dateString.Substring(0, 4)),
198  Convert.ToInt16(dateString.Substring(4, 2)),
199  Convert.ToInt16(dateString.Substring(6, 2)),
200  Convert.ToInt16(dateString.Substring(8, 2)),
201  Convert.ToInt16(dateString.Substring(10, 2)),
202  Convert.ToInt16(dateString.Substring(12, 2)),
203  0);
204  }
205  else if (dateString.Length == 17)
206  {
207  date = new System.DateTime(Convert.ToInt16(dateString.Substring(0, 4)),
208  Convert.ToInt16(dateString.Substring(4, 2)),
209  Convert.ToInt16(dateString.Substring(6, 2)),
210  Convert.ToInt16(dateString.Substring(8, 2)),
211  Convert.ToInt16(dateString.Substring(10, 2)),
212  Convert.ToInt16(dateString.Substring(12, 2)),
213  Convert.ToInt16(dateString.Substring(14, 3)));
214  }
215  else
216  {
217  throw new System.FormatException("Input is not valid date string: " + dateString);
218  }
219  return date;
220  }
221 
233  public static System.DateTime Round(System.DateTime date, Resolution resolution)
234  {
235  return new System.DateTime(Round(date.Ticks / TimeSpan.TicksPerMillisecond, resolution));
236  }
237 
250  public static long Round(long time, Resolution resolution)
251  {
252  System.DateTime dt = new System.DateTime(time * TimeSpan.TicksPerMillisecond);
253 
254  if (resolution == Resolution.YEAR)
255  {
256  dt = dt.AddMonths(1 - dt.Month);
257  dt = dt.AddDays(1 - dt.Day);
258  dt = dt.AddHours(0 - dt.Hour);
259  dt = dt.AddMinutes(0 - dt.Minute);
260  dt = dt.AddSeconds(0 - dt.Second);
261  dt = dt.AddMilliseconds(0 - dt.Millisecond);
262  }
263  else if (resolution == Resolution.MONTH)
264  {
265  dt = dt.AddDays(1 - dt.Day);
266  dt = dt.AddHours(0 - dt.Hour);
267  dt = dt.AddMinutes(0 - dt.Minute);
268  dt = dt.AddSeconds(0 - dt.Second);
269  dt = dt.AddMilliseconds(0 - dt.Millisecond);
270  }
271  else if (resolution == Resolution.DAY)
272  {
273  dt = dt.AddHours(0 - dt.Hour);
274  dt = dt.AddMinutes(0 - dt.Minute);
275  dt = dt.AddSeconds(0 - dt.Second);
276  dt = dt.AddMilliseconds(0 - dt.Millisecond);
277  }
278  else if (resolution == Resolution.HOUR)
279  {
280  dt = dt.AddMinutes(0 - dt.Minute);
281  dt = dt.AddSeconds(0 - dt.Second);
282  dt = dt.AddMilliseconds(0 - dt.Millisecond);
283  }
284  else if (resolution == Resolution.MINUTE)
285  {
286  dt = dt.AddSeconds(0 - dt.Second);
287  dt = dt.AddMilliseconds(0 - dt.Millisecond);
288  }
289  else if (resolution == Resolution.SECOND)
290  {
291  dt = dt.AddMilliseconds(0 - dt.Millisecond);
292  }
293  else if (resolution == Resolution.MILLISECOND)
294  {
295  // don't cut off anything
296  }
297  else
298  {
299  throw new System.ArgumentException("unknown resolution " + resolution);
300  }
301  return dt.Ticks;
302  }
303 
305  public class Resolution
306  {
307 
308  public static readonly Resolution YEAR = new Resolution("year");
309  public static readonly Resolution MONTH = new Resolution("month");
310  public static readonly Resolution DAY = new Resolution("day");
311  public static readonly Resolution HOUR = new Resolution("hour");
312  public static readonly Resolution MINUTE = new Resolution("minute");
313  public static readonly Resolution SECOND = new Resolution("second");
314  public static readonly Resolution MILLISECOND = new Resolution("millisecond");
315 
316  private System.String resolution;
317 
318  internal Resolution()
319  {
320  }
321 
322  internal Resolution(System.String resolution)
323  {
324  this.resolution = resolution;
325  }
326 
327  public override System.String ToString()
328  {
329  return resolution;
330  }
331  }
332  static DateTools()
333  {
334  {
335  // times need to be normalized so the value doesn't depend on the
336  // location the index is created/used:
337  // {{Aroush-2.1}}
338  /*
339  YEAR_FORMAT.setTimeZone(GMT);
340  MONTH_FORMAT.setTimeZone(GMT);
341  DAY_FORMAT.setTimeZone(GMT);
342  HOUR_FORMAT.setTimeZone(GMT);
343  MINUTE_FORMAT.setTimeZone(GMT);
344  SECOND_FORMAT.setTimeZone(GMT);
345  MILLISECOND_FORMAT.setTimeZone(GMT);
346  */
347  }
348  }
349  }
350 }