DateField.cs :  » Search-Engines » dotLucene » Lucene » Net » Documents » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Search Engines » dotLucene 
dotLucene » Lucene » Net » Documents » DateField.cs
/*
 * Copyright 2004 The Apache Software Foundation
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

using System;
using PrefixQueryLucene.Net.Search.PrefixQuery;
using RangeQueryLucene.Net.Search.RangeQuery;

namespace Lucene.Net.Documents{
  
  /// <summary> Provides support for converting dates to strings and vice-versa.
  /// The strings are structured so that lexicographic sorting orders by date,
  /// which makes them suitable for use as field values and search terms.
  /// 
  /// <P>Note that this class saves dates with millisecond granularity,
  /// which is bad for {@link RangeQuery} and {@link PrefixQuery}, as those
  /// queries are expanded to a BooleanQuery with a potentially large number 
  /// of terms when searching. Thus you might want to use
  /// {@link DateTools} instead.
  /// 
  /// <P>
  /// Note: dates before 1970 cannot be used, and therefore cannot be
  /// indexed when using this class. See {@link DateTools} for an
  /// alternative without such a limitation.
  /// 
  /// </summary>
  /// <deprecated> If you build a new index, use {@link DateTools} instead. For 
  /// existing indices you can continue using this class, as it will not be 
  /// removed in the near future despite being deprecated.
  /// </deprecated>
  public class DateField
  {
    
    private DateField()
    {
    }
    
    // make date strings long enough to last a millenium
    private static int DATE_LEN = System.Convert.ToString(
            1000L * 365 * 24 * 60 * 60 * 1000, 36).Length;
    
    public static System.String MIN_DATE_STRING()
    {
      return TimeToString(0);
    }
    
    public static System.String MAX_DATE_STRING()
    {
      char[] buffer = new char[DATE_LEN];
      char c = SupportClass.Character.ForDigit(36 - 1, SupportClass.Character.MAX_RADIX);
      for (int i = 0; i < DATE_LEN; i++)
        buffer[i] = c;
      return new System.String(buffer);
    }
    
    /// <summary> Converts a Date to a string suitable for indexing.</summary>
    /// <throws>  RuntimeException if the date specified in the </throws>
    /// <summary> method argument is before 1970
    /// </summary>
    public static System.String DateToString(System.DateTime date)
    {
            TimeSpan ts = date.Subtract(new DateTime(1970, 1, 1));
            ts = ts.Subtract(TimeZone.CurrentTimeZone.GetUtcOffset(date));
            return TimeToString(ts.Ticks / TimeSpan.TicksPerMillisecond);
        }

    /// <summary> Converts a millisecond time to a string suitable for indexing.</summary>
    /// <throws>  RuntimeException if the time specified in the </throws>
    /// <summary> method argument is negative, that is, before 1970
    /// </summary>
    public static System.String TimeToString(long time)
    {
      if (time < 0)
        throw new System.SystemException("time too early");
      
      System.String s = SupportClass.Number.ToString(time, SupportClass.Number.MAX_RADIX);
      
      if (s.Length > DATE_LEN)
        throw new System.SystemException("time too late");
      
      // Pad with leading zeros
      if (s.Length < DATE_LEN)
      {
        System.Text.StringBuilder sb = new System.Text.StringBuilder(s);
        while (sb.Length < DATE_LEN)
          sb.Insert(0, 0);
        s = sb.ToString();
      }
      
      return s;
    }
    
    /// <summary>Converts a string-encoded date into a millisecond time. </summary>
    public static long StringToTime(System.String s)
    {
      return SupportClass.Number.Parse(s, SupportClass.Number.MAX_RADIX);
    }
    /// <summary>Converts a string-encoded date into a Date object. </summary>
    public static System.DateTime StringToDate(System.String s)
    {
            return new System.DateTime(StringToTime(s));

            // {{Aroush-1.9}} Will the line above do it or do we need the lines below?!

            /*
            long ticks = StringToTime(s) * TimeSpan.TicksPerMillisecond;
            System.DateTime date = new System.DateTime(1970, 1, 1);
            date = date.AddTicks(ticks);
            date = date.Add(TimeZone.CurrentTimeZone.GetUtcOffset(date));
            return date;
            */
    }
  }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.