18
using PrefixQuery = Lucene.Net.Search.PrefixQuery; // for javadoc
19
using RangeQuery = Lucene.Net.Search.RangeQuery; // for javadoc
19
using PrefixQuery = Lucene.Net.Search.PrefixQuery;
20
using RangeQuery = Lucene.Net.Search.RangeQuery;
21
22
namespace Lucene.Net.Documents
24
/// <summary> Provides support for converting dates to strings and vice-versa.
25
/// The strings are structured so that lexicographic sorting orders by date,
26
/// which makes them suitable for use as field values and search terms.
28
/// <P>Note that this class saves dates with millisecond granularity,
29
/// which is bad for {@link RangeQuery} and {@link PrefixQuery}, as those
30
/// queries are expanded to a BooleanQuery with a potentially large number
31
/// of terms when searching. Thus you might want to use
32
/// {@link DateTools} instead.
35
/// Note: dates before 1970 cannot be used, and therefore cannot be
36
/// indexed when using this class. See {@link DateTools} for an
37
/// alternative without such a limitation.
40
/// <deprecated> If you build a new index, use {@link DateTools} instead. For
41
/// existing indices you can continue using this class, as it will not be
42
/// removed in the near future despite being deprecated.
44
public class DateField
51
// make date strings long enough to last a millenium
52
private static int DATE_LEN = SupportClass.Number.ToString(
26
/// <summary> Provides support for converting dates to strings and vice-versa.
27
/// The strings are structured so that lexicographic sorting orders by date,
28
/// which makes them suitable for use as field values and search terms.
30
/// <P>Note that this class saves dates with millisecond granularity,
31
/// which is bad for {@link RangeQuery} and {@link PrefixQuery}, as those
32
/// queries are expanded to a BooleanQuery with a potentially large number
33
/// of terms when searching. Thus you might want to use
34
/// {@link DateTools} instead.
37
/// Note: dates before 1970 cannot be used, and therefore cannot be
38
/// indexed when using this class. See {@link DateTools} for an
39
/// alternative without such a limitation.
42
/// <deprecated> If you build a new index, use {@link DateTools} instead. This class is included for use with existing
43
/// indices and will be removed in a future release.
45
public class DateField
52
// make date strings long enough to last a millenium
53
private static int DATE_LEN = SupportClass.Number.ToString(
53
54
1000L * 365 * 24 * 60 * 60 * 1000, 16).Length;
55
public static System.String MIN_DATE_STRING()
57
return TimeToString(0);
60
public static System.String MAX_DATE_STRING()
62
char[] buffer = new char[DATE_LEN];
63
char c = SupportClass.Character.ForDigit(36 - 1, SupportClass.Character.MAX_RADIX);
64
for (int i = 0; i < DATE_LEN; i++)
66
return new System.String(buffer);
69
/// <summary> Converts a Date to a string suitable for indexing.</summary>
70
/// <throws> RuntimeException if the date specified in the </throws>
71
/// <summary> method argument is before 1970
73
public static System.String DateToString(System.DateTime date)
56
public static System.String MIN_DATE_STRING()
58
return TimeToString(0);
61
public static System.String MAX_DATE_STRING()
63
char[] buffer = new char[DATE_LEN];
64
char c = SupportClass.Character.ForDigit(36 - 1, SupportClass.Character.MAX_RADIX);
65
for (int i = 0; i < DATE_LEN; i++)
67
return new System.String(buffer);
70
/// <summary> Converts a Date to a string suitable for indexing.</summary>
71
/// <throws> SystemException if the date specified in the </throws>
72
/// <summary> method argument is before 1970
74
public static System.String DateToString(System.DateTime date)
75
76
TimeSpan ts = date.Subtract(new DateTime(1970, 1, 1));
76
77
ts = ts.Subtract(TimeZone.CurrentTimeZone.GetUtcOffset(date));
77
78
return TimeToString(ts.Ticks / TimeSpan.TicksPerMillisecond);
80
/// <summary> Converts a millisecond time to a string suitable for indexing.</summary>
81
/// <throws> RuntimeException if the time specified in the </throws>
82
/// <summary> method argument is negative, that is, before 1970
84
public static System.String TimeToString(long time)
87
throw new System.SystemException("time '" + time + "' is too early, must be >= 0");
89
System.String s = SupportClass.Number.ToString(time, SupportClass.Number.MAX_RADIX);
91
if (s.Length > DATE_LEN)
92
throw new System.SystemException("time '" + time + "' is too late, length of string " + "representation must be <= " + DATE_LEN);
94
// Pad with leading zeros
95
if (s.Length < DATE_LEN)
97
System.Text.StringBuilder sb = new System.Text.StringBuilder(s);
98
while (sb.Length < DATE_LEN)
106
/// <summary>Converts a string-encoded date into a millisecond time. </summary>
107
public static long StringToTime(System.String s)
109
return SupportClass.Number.Parse(s, SupportClass.Number.MAX_RADIX);
112
/// <summary>Converts a string-encoded date into a Date object. </summary>
113
public static System.DateTime StringToDate(System.String s)
115
long ticks = StringToTime(s) * TimeSpan.TicksPerMillisecond;
116
System.DateTime date = new System.DateTime(1970, 1, 1);
117
date = date.AddTicks(ticks);
118
date = date.Add(TimeZone.CurrentTimeZone.GetUtcOffset(date));
81
/// <summary> Converts a millisecond time to a string suitable for indexing.</summary>
82
/// <throws> SystemException if the time specified in the </throws>
83
/// <summary> method argument is negative, that is, before 1970
85
public static System.String TimeToString(long time)
88
throw new System.SystemException("time '" + time + "' is too early, must be >= 0");
90
System.String s = SupportClass.Number.ToString(time, SupportClass.Number.MAX_RADIX);
92
if (s.Length > DATE_LEN)
93
throw new System.SystemException("time '" + time + "' is too late, length of string " + "representation must be <= " + DATE_LEN);
95
// Pad with leading zeros
96
if (s.Length < DATE_LEN)
98
System.Text.StringBuilder sb = new System.Text.StringBuilder(s);
99
while (sb.Length < DATE_LEN)
107
/// <summary>Converts a string-encoded date into a millisecond time. </summary>
108
public static long StringToTime(System.String s)
110
return SupportClass.Number.Parse(s, SupportClass.Number.MAX_RADIX);
113
/// <summary>Converts a string-encoded date into a Date object. </summary>
114
public static System.DateTime StringToDate(System.String s)
116
long ticks = StringToTime(s) * TimeSpan.TicksPerMillisecond;
117
System.DateTime date = new System.DateTime(1970, 1, 1);
118
date = date.AddTicks(ticks);
119
date = date.Add(TimeZone.CurrentTimeZone.GetUtcOffset(date));
b'\\ No newline at end of file'