19 using Lucene.Net.Search;
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";
58 private static readonly System.Globalization.Calendar calInstance =
new System.Globalization.GregorianCalendar();
76 public static System.String DateToString(System.DateTime date,
Resolution resolution)
78 return TimeToString(date.Ticks / TimeSpan.TicksPerMillisecond, resolution);
92 public static System.String TimeToString(
long time,
Resolution resolution)
94 System.DateTime date =
new System.DateTime(Round(time, resolution));
98 return date.ToString(YEAR_FORMAT, System.Globalization.CultureInfo.InvariantCulture);
102 return date.ToString(MONTH_FORMAT, System.Globalization.CultureInfo.InvariantCulture);
106 return date.ToString(DAY_FORMAT, System.Globalization.CultureInfo.InvariantCulture);
110 return date.ToString(HOUR_FORMAT, System.Globalization.CultureInfo.InvariantCulture);
114 return date.ToString(MINUTE_FORMAT, System.Globalization.CultureInfo.InvariantCulture);
118 return date.ToString(SECOND_FORMAT, System.Globalization.CultureInfo.InvariantCulture);
122 return date.ToString(MILLISECOND_FORMAT, System.Globalization.CultureInfo.InvariantCulture);
125 throw new System.ArgumentException(
"unknown resolution " + resolution);
140 public static long StringToTime(System.String dateString)
142 return StringToDate(dateString).Ticks;
157 public static System.DateTime StringToDate(System.String dateString)
159 System.DateTime date;
160 if (dateString.Length == 4)
162 date =
new System.DateTime(Convert.ToInt16(dateString.Substring(0, 4)),
165 else if (dateString.Length == 6)
167 date =
new System.DateTime(Convert.ToInt16(dateString.Substring(0, 4)),
168 Convert.ToInt16(dateString.Substring(4, 2)),
171 else if (dateString.Length == 8)
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)),
178 else if (dateString.Length == 10)
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)),
186 else if (dateString.Length == 12)
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)),
195 else if (dateString.Length == 14)
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)),
205 else if (dateString.Length == 17)
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)));
217 throw new System.FormatException(
"Input is not valid date string: " + dateString);
233 public static System.DateTime Round(System.DateTime date,
Resolution resolution)
235 return new System.DateTime(Round(date.Ticks / TimeSpan.TicksPerMillisecond, resolution));
252 System.DateTime dt =
new System.DateTime(time * TimeSpan.TicksPerMillisecond);
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);
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);
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);
280 dt = dt.AddMinutes(0 - dt.Minute);
281 dt = dt.AddSeconds(0 - dt.Second);
282 dt = dt.AddMilliseconds(0 - dt.Millisecond);
286 dt = dt.AddSeconds(0 - dt.Second);
287 dt = dt.AddMilliseconds(0 - dt.Millisecond);
291 dt = dt.AddMilliseconds(0 - dt.Millisecond);
299 throw new System.ArgumentException(
"unknown resolution " + resolution);
316 private System.String resolution;
324 this.resolution = resolution;
327 public override System.String ToString()