Advertisement
Guest User

Untitled

a guest
Aug 4th, 2015
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 22.97 KB | None | 0 0
  1. var dateTimeString = "Jul 28 00:00";
  2. var required = dateTimeString.ToDateTime().ToString("yyyy-MM-dd tt 00:00");
  3.  
  4. using System;
  5. using System.Text.RegularExpressions;
  6. /// <summary>
  7. /// Miscellaneous and parsing methods for DateTime
  8. /// </summary>
  9. public static class DateTimeRoutines
  10. {
  11. #region miscellaneous methods
  12.  
  13. /// <summary>
  14. /// Amount of seconds elapsed between 1970-01-01 00:00:00 and the date-time.
  15. /// </summary>
  16. /// <param name="date_time">date-time</param>
  17. /// <returns>seconds</returns>
  18. public static uint GetSecondsSinceUnixEpoch(this DateTime date_time)
  19. {
  20. TimeSpan t = date_time - new DateTime(1970, 1, 1);
  21. int ss = (int)t.TotalSeconds;
  22. if (ss < 0)
  23. return 0;
  24. return (uint)ss;
  25. }
  26.  
  27. #endregion
  28.  
  29. #region parsing definitions
  30.  
  31. /// <summary>
  32. /// Defines a substring where date-time was found and result of conversion
  33. /// </summary>
  34. public class ParsedDateTime
  35. {
  36. /// <summary>
  37. /// Index of first char of a date substring found in the string
  38. /// </summary>
  39. readonly public int IndexOfDate = -1;
  40. /// <summary>
  41. /// Length a date substring found in the string
  42. /// </summary>
  43. readonly public int LengthOfDate = -1;
  44. /// <summary>
  45. /// Index of first char of a time substring found in the string
  46. /// </summary>
  47. readonly public int IndexOfTime = -1;
  48. /// <summary>
  49. /// Length of a time substring found in the string
  50. /// </summary>
  51. readonly public int LengthOfTime = -1;
  52. /// <summary>
  53. /// DateTime found in the string
  54. /// </summary>
  55. readonly public DateTime DateTime;
  56. /// <summary>
  57. /// True if a date was found within the string
  58. /// </summary>
  59. readonly public bool IsDateFound;
  60. /// <summary>
  61. /// True if a time was found within the string
  62. /// </summary>
  63. readonly public bool IsTimeFound;
  64. /// <summary>
  65. /// UTC offset if it was found within the string
  66. /// </summary>
  67. readonly public TimeSpan UtcOffset;
  68. /// <summary>
  69. /// True if UTC offset was found in the string
  70. /// </summary>
  71. readonly public bool IsUtcOffsetFound;
  72. /// <summary>
  73. /// Utc gotten from DateTime if IsUtcOffsetFound is True
  74. /// </summary>
  75. public DateTime UtcDateTime;
  76.  
  77. public ParsedDateTime(int index_of_date, int length_of_date, int index_of_time, int length_of_time, DateTime date_time)
  78. {
  79. IndexOfDate = index_of_date;
  80. LengthOfDate = length_of_date;
  81. IndexOfTime = index_of_time;
  82. LengthOfTime = length_of_time;
  83. DateTime = date_time;
  84. IsDateFound = index_of_date > -1;
  85. IsTimeFound = index_of_time > -1;
  86. UtcOffset = new TimeSpan(25, 0, 0);
  87. IsUtcOffsetFound = false;
  88. UtcDateTime = new DateTime(1, 1, 1);
  89. }
  90.  
  91. public ParsedDateTime(int index_of_date, int length_of_date, int index_of_time, int length_of_time, DateTime date_time, TimeSpan utc_offset)
  92. {
  93. IndexOfDate = index_of_date;
  94. LengthOfDate = length_of_date;
  95. IndexOfTime = index_of_time;
  96. LengthOfTime = length_of_time;
  97. DateTime = date_time;
  98. IsDateFound = index_of_date > -1;
  99. IsTimeFound = index_of_time > -1;
  100. UtcOffset = utc_offset;
  101. IsUtcOffsetFound = Math.Abs(utc_offset.TotalHours) < 12;
  102. if (!IsUtcOffsetFound)
  103. UtcDateTime = new DateTime(1, 1, 1);
  104. else
  105. {
  106. if (index_of_date < 0)//to avoid negative date exception when date is undefined
  107. {
  108. TimeSpan ts = date_time.TimeOfDay + utc_offset;
  109. if (ts < new TimeSpan(0))
  110. UtcDateTime = new DateTime(1, 1, 2) + ts;
  111. else
  112. UtcDateTime = new DateTime(1, 1, 1) + ts;
  113. }
  114. else
  115. UtcDateTime = date_time + utc_offset;
  116. }
  117. }
  118. }
  119.  
  120. /// <summary>
  121. /// Date that is accepted in the following cases:
  122. /// - no date was parsed by TryParseDateOrTime();
  123. /// - no year was found by TryParseDate();
  124. /// It is ignored if DefaultDateIsNow = true was set after DefaultDate
  125. /// </summary>
  126. public static DateTime DefaultDate
  127. {
  128. set
  129. {
  130. _DefaultDate = value;
  131. DefaultDateIsNow = false;
  132. }
  133. get
  134. {
  135. if (DefaultDateIsNow)
  136. return DateTime.Now;
  137. else
  138. return _DefaultDate;
  139. }
  140. }
  141. static DateTime _DefaultDate = DateTime.Now;
  142.  
  143. /// <summary>
  144. /// If true then DefaultDate property is ignored and DefaultDate is always DateTime.Now
  145. /// </summary>
  146. public static bool DefaultDateIsNow = true;
  147.  
  148. /// <summary>
  149. /// Defines default date-time format.
  150. /// </summary>
  151. public enum DateTimeFormat
  152. {
  153. /// <summary>
  154. /// month number goes before day number
  155. /// </summary>
  156. USA_DATE,
  157. /// <summary>
  158. /// day number goes before month number
  159. /// </summary>
  160. UK_DATE,
  161. ///// <summary>
  162. ///// time is specifed through AM or PM
  163. ///// </summary>
  164. //USA_TIME,
  165. }
  166.  
  167. #endregion
  168.  
  169. #region parsing derived methods for DateTime output
  170.  
  171. /// <summary>
  172. /// Tries to find date and time within the passed string and return it as DateTime structure.
  173. /// </summary>
  174. /// <param name="str">string that contains date and/or time</param>
  175. /// <param name="default_format">format to be used preferably in ambivalent instances</param>
  176. /// <param name="date_time">parsed date-time output</param>
  177. /// <returns>true if both date and time were found, else false</returns>
  178. static public bool TryParseDateTime(this string str, DateTimeFormat default_format, out DateTime date_time)
  179. {
  180. ParsedDateTime parsed_date_time;
  181. if (!TryParseDateTime(str, default_format, out parsed_date_time))
  182. {
  183. date_time = new DateTime(1, 1, 1);
  184. return false;
  185. }
  186. date_time = parsed_date_time.DateTime;
  187. return true;
  188. }
  189.  
  190. /// <summary>
  191. /// Tries to find date and/or time within the passed string and return it as DateTime structure.
  192. /// If only date was found, time in the returned DateTime is always 0:0:0.
  193. /// If only time was found, date in the returned DateTime is DefaultDate.
  194. /// </summary>
  195. /// <param name="str">string that contains date and(or) time</param>
  196. /// <param name="default_format">format to be used preferably in ambivalent instances</param>
  197. /// <param name="date_time">parsed date-time output</param>
  198. /// <returns>true if date and/or time was found, else false</returns>
  199. static public bool TryParseDateOrTime(this string str, DateTimeFormat default_format, out DateTime date_time)
  200. {
  201. ParsedDateTime parsed_date_time;
  202. if (!TryParseDateOrTime(str, default_format, out parsed_date_time))
  203. {
  204. date_time = new DateTime(1, 1, 1);
  205. return false;
  206. }
  207. date_time = parsed_date_time.DateTime;
  208. return true;
  209. }
  210.  
  211. /// <summary>
  212. /// Tries to find time within the passed string and return it as DateTime structure.
  213. /// It recognizes only time while ignoring date, so date in the returned DateTime is always 1/1/1.
  214. /// </summary>
  215. /// <param name="str">string that contains time</param>
  216. /// <param name="default_format">format to be used preferably in ambivalent instances</param>
  217. /// <param name="time">parsed time output</param>
  218. /// <returns>true if time was found, else false</returns>
  219. public static bool TryParseTime(this string str, DateTimeFormat default_format, out DateTime time)
  220. {
  221. ParsedDateTime parsed_time;
  222. if (!TryParseTime(str, default_format, out parsed_time, null))
  223. {
  224. time = new DateTime(1, 1, 1);
  225. return false;
  226. }
  227. time = parsed_time.DateTime;
  228. return true;
  229. }
  230.  
  231. /// <summary>
  232. /// Tries to find date within the passed string and return it as DateTime structure.
  233. /// It recognizes only date while ignoring time, so time in the returned DateTime is always 0:0:0.
  234. /// If year of the date was not found then it accepts the current year.
  235. /// </summary>
  236. /// <param name="str">string that contains date</param>
  237. /// <param name="default_format">format to be used preferably in ambivalent instances</param>
  238. /// <param name="date">parsed date output</param>
  239. /// <returns>true if date was found, else false</returns>
  240. static public bool TryParseDate(this string str, DateTimeFormat default_format, out DateTime date)
  241. {
  242. ParsedDateTime parsed_date;
  243. if (!TryParseDate(str, default_format, out parsed_date))
  244. {
  245. date = new DateTime(1, 1, 1);
  246. return false;
  247. }
  248. date = parsed_date.DateTime;
  249. return true;
  250. }
  251.  
  252. #endregion
  253.  
  254. #region parsing derived methods for ParsedDateTime output
  255.  
  256. /// <summary>
  257. /// Tries to find date and time within the passed string and return it as ParsedDateTime object.
  258. /// </summary>
  259. /// <param name="str">string that contains date-time</param>
  260. /// <param name="default_format">format to be used preferably in ambivalent instances</param>
  261. /// <param name="parsed_date_time">parsed date-time output</param>
  262. /// <returns>true if both date and time were found, else false</returns>
  263. static public bool TryParseDateTime(this string str, DateTimeFormat default_format, out ParsedDateTime parsed_date_time)
  264. {
  265. if (DateTimeRoutines.TryParseDateOrTime(str, default_format, out parsed_date_time)
  266. && parsed_date_time.IsDateFound
  267. && parsed_date_time.IsTimeFound
  268. )
  269. return true;
  270.  
  271. parsed_date_time = null;
  272. return false;
  273. }
  274.  
  275. /// <summary>
  276. /// Tries to find time within the passed string and return it as ParsedDateTime object.
  277. /// It recognizes only time while ignoring date, so date in the returned ParsedDateTime is always 1/1/1
  278. /// </summary>
  279. /// <param name="str">string that contains date-time</param>
  280. /// <param name="default_format">format to be used preferably in ambivalent instances</param>
  281. /// <param name="parsed_time">parsed date-time output</param>
  282. /// <returns>true if time was found, else false</returns>
  283. static public bool TryParseTime(this string str, DateTimeFormat default_format, out ParsedDateTime parsed_time)
  284. {
  285. return TryParseTime(str, default_format, out parsed_time, null);
  286. }
  287.  
  288. /// <summary>
  289. /// Tries to find date and/or time within the passed string and return it as ParsedDateTime object.
  290. /// If only date was found, time in the returned ParsedDateTime is always 0:0:0.
  291. /// If only time was found, date in the returned ParsedDateTime is DefaultDate.
  292. /// </summary>
  293. /// <param name="str">string that contains date-time</param>
  294. /// <param name="default_format">format to be used preferably in ambivalent instances</param>
  295. /// <param name="parsed_date_time">parsed date-time output</param>
  296. /// <returns>true if date or time was found, else false</returns>
  297. static public bool TryParseDateOrTime(this string str, DateTimeFormat default_format, out ParsedDateTime parsed_date_time)
  298. {
  299. parsed_date_time = null;
  300.  
  301. ParsedDateTime parsed_date;
  302. ParsedDateTime parsed_time;
  303. if (!TryParseDate(str, default_format, out parsed_date))
  304. {
  305. if (!TryParseTime(str, default_format, out parsed_time, null))
  306. return false;
  307.  
  308. DateTime date_time = new DateTime(DefaultDate.Year, DefaultDate.Month, DefaultDate.Day, parsed_time.DateTime.Hour, parsed_time.DateTime.Minute, parsed_time.DateTime.Second);
  309. parsed_date_time = new ParsedDateTime(-1, -1, parsed_time.IndexOfTime, parsed_time.LengthOfTime, date_time, parsed_time.UtcOffset);
  310. }
  311. else
  312. {
  313. if (!TryParseTime(str, default_format, out parsed_time, parsed_date))
  314. {
  315. DateTime date_time = new DateTime(parsed_date.DateTime.Year, parsed_date.DateTime.Month, parsed_date.DateTime.Day, 0, 0, 0);
  316. parsed_date_time = new ParsedDateTime(parsed_date.IndexOfDate, parsed_date.LengthOfDate, -1, -1, date_time);
  317. }
  318. else
  319. {
  320. DateTime date_time = new DateTime(parsed_date.DateTime.Year, parsed_date.DateTime.Month, parsed_date.DateTime.Day, parsed_time.DateTime.Hour, parsed_time.DateTime.Minute, parsed_time.DateTime.Second);
  321. parsed_date_time = new ParsedDateTime(parsed_date.IndexOfDate, parsed_date.LengthOfDate, parsed_time.IndexOfTime, parsed_time.LengthOfTime, date_time, parsed_time.UtcOffset);
  322. }
  323. }
  324.  
  325. return true;
  326. }
  327.  
  328. #endregion
  329.  
  330. #region parsing base methods
  331.  
  332. /// <summary>
  333. /// Tries to find time within the passed string (relatively to the passed parsed_date if any) and return it as ParsedDateTime object.
  334. /// It recognizes only time while ignoring date, so date in the returned ParsedDateTime is always 1/1/1
  335. /// </summary>
  336. /// <param name="str">string that contains date</param>
  337. /// <param name="default_format">format to be used preferably in ambivalent instances</param>
  338. /// <param name="parsed_time">parsed date-time output</param>
  339. /// <param name="parsed_date">ParsedDateTime object if the date was found within this string, else NULL</param>
  340. /// <returns>true if time was found, else false</returns>
  341. public static bool TryParseTime(this string str, DateTimeFormat default_format, out ParsedDateTime parsed_time, ParsedDateTime parsed_date)
  342. {
  343. parsed_time = null;
  344.  
  345. string time_zone_r;
  346. if (default_format == DateTimeFormat.USA_DATE)
  347. time_zone_r = @"(?:s*(?'time_zone'UTC|GMT|CST|EST))?";
  348. else
  349. time_zone_r = @"(?:s*(?'time_zone'UTC|GMT))?";
  350.  
  351. Match m;
  352. if (parsed_date != null && parsed_date.IndexOfDate > -1)
  353. {//look around the found date
  354. //look for <date> hh:mm:ss <UTC offset>
  355. m = Regex.Match(str.Substring(parsed_date.IndexOfDate + parsed_date.LengthOfDate), @"(?<=^s*,?s+|^s*ats*|^s*[T-]s*)(?'hour'd{2})s*:s*(?'minute'd{2})s*:s*(?'second'd{2})s+(?'offset_sign'[+-])(?'offset_hh'd{2}):?(?'offset_mm'd{2})(?=$|[^dw])", RegexOptions.Compiled);
  356. if (!m.Success)
  357. //look for <date> [h]h:mm[:ss] [PM/AM] [UTC/GMT]
  358. m = Regex.Match(str.Substring(parsed_date.IndexOfDate + parsed_date.LengthOfDate), @"(?<=^s*,?s+|^s*ats*|^s*[T-]s*)(?'hour'd{1,2})s*:s*(?'minute'd{2})s*(?::s*(?'second'd{2}))?(?:s*(?'ampm'AM|am|PM|pm))?" + time_zone_r + @"(?=$|[^dw])", RegexOptions.Compiled);
  359. if (!m.Success)
  360. //look for [h]h:mm:ss [PM/AM] [UTC/GMT] <date>
  361. m = Regex.Match(str.Substring(0, parsed_date.IndexOfDate), @"(?<=^|[^d])(?'hour'd{1,2})s*:s*(?'minute'd{2})s*(?::s*(?'second'd{2}))?(?:s*(?'ampm'AM|am|PM|pm))?" + time_zone_r + @"(?=$|[s,]+)", RegexOptions.Compiled);
  362. if (!m.Success)
  363. //look for [h]h:mm:ss [PM/AM] [UTC/GMT] within <date>
  364. m = Regex.Match(str.Substring(parsed_date.IndexOfDate, parsed_date.LengthOfDate), @"(?<=^|[^d])(?'hour'd{1,2})s*:s*(?'minute'd{2})s*(?::s*(?'second'd{2}))?(?:s*(?'ampm'AM|am|PM|pm))?" + time_zone_r + @"(?=$|[s,]+)", RegexOptions.Compiled);
  365. }
  366. else//look anywhere within string
  367. {
  368. //look for hh:mm:ss <UTC offset>
  369. m = Regex.Match(str, @"(?<=^|s+|s*Ts*)(?'hour'd{2})s*:s*(?'minute'd{2})s*:s*(?'second'd{2})s+(?'offset_sign'[+-])(?'offset_hh'd{2}):?(?'offset_mm'd{2})?(?=$|[^dw])", RegexOptions.Compiled);
  370. if (!m.Success)
  371. //look for [h]h:mm[:ss] [PM/AM] [UTC/GMT]
  372. m = Regex.Match(str, @"(?<=^|s+|s*Ts*)(?'hour'd{1,2})s*:s*(?'minute'd{2})s*(?::s*(?'second'd{2}))?(?:s*(?'ampm'AM|am|PM|pm))?" + time_zone_r + @"(?=$|[^dw])", RegexOptions.Compiled);
  373. }
  374.  
  375. if (!m.Success)
  376. return false;
  377.  
  378. //try
  379. //{
  380. int hour = int.Parse(m.Groups["hour"].Value);
  381. if (hour < 0 || hour > 23)
  382. return false;
  383.  
  384. int minute = int.Parse(m.Groups["minute"].Value);
  385. if (minute < 0 || minute > 59)
  386. return false;
  387.  
  388. int second = 0;
  389. if (!string.IsNullOrEmpty(m.Groups["second"].Value))
  390. {
  391. second = int.Parse(m.Groups["second"].Value);
  392. if (second < 0 || second > 59)
  393. return false;
  394. }
  395.  
  396. if (string.Compare(m.Groups["ampm"].Value, "PM", true) == 0 && hour < 12)
  397. hour += 12;
  398. else if (string.Compare(m.Groups["ampm"].Value, "AM", true) == 0 && hour == 12)
  399. hour -= 12;
  400.  
  401. DateTime date_time = new DateTime(1, 1, 1, hour, minute, second);
  402.  
  403. if (m.Groups["offset_hh"].Success)
  404. {
  405. int offset_hh = int.Parse(m.Groups["offset_hh"].Value);
  406. int offset_mm = 0;
  407. if (m.Groups["offset_mm"].Success)
  408. offset_mm = int.Parse(m.Groups["offset_mm"].Value);
  409. TimeSpan utc_offset = new TimeSpan(offset_hh, offset_mm, 0);
  410. if (m.Groups["offset_sign"].Value == "-")
  411. utc_offset = -utc_offset;
  412. parsed_time = new ParsedDateTime(-1, -1, m.Index, m.Length, date_time, utc_offset);
  413. return true;
  414. }
  415.  
  416. if (m.Groups["time_zone"].Success)
  417. {
  418. TimeSpan utc_offset;
  419. switch (m.Groups["time_zone"].Value)
  420. {
  421. case "UTC":
  422. case "GMT":
  423. utc_offset = new TimeSpan(0, 0, 0);
  424. break;
  425. case "CST":
  426. utc_offset = new TimeSpan(-6, 0, 0);
  427. break;
  428. case "EST":
  429. utc_offset = new TimeSpan(-5, 0, 0);
  430. break;
  431. default:
  432. throw new Exception("Time zone: " + m.Groups["time_zone"].Value + " is not defined.");
  433. }
  434. parsed_time = new ParsedDateTime(-1, -1, m.Index, m.Length, date_time, utc_offset);
  435. return true;
  436. }
  437.  
  438. parsed_time = new ParsedDateTime(-1, -1, m.Index, m.Length, date_time);
  439. //}
  440. //catch(Exception e)
  441. //{
  442. // return false;
  443. //}
  444. return true;
  445. }
  446.  
  447. /// <summary>
  448. /// Tries to find date within the passed string and return it as ParsedDateTime object.
  449. /// It recognizes only date while ignoring time, so time in the returned ParsedDateTime is always 0:0:0.
  450. /// If year of the date was not found then it accepts the current year.
  451. /// </summary>
  452. /// <param name="str">string that contains date</param>
  453. /// <param name="default_format">format to be used preferably in ambivalent instances</param>
  454. /// <param name="parsed_date">parsed date output</param>
  455. /// <returns>true if date was found, else false</returns>
  456. static public bool TryParseDate(this string str, DateTimeFormat default_format, out ParsedDateTime parsed_date)
  457. {
  458. parsed_date = null;
  459.  
  460. if (string.IsNullOrEmpty(str))
  461. return false;
  462.  
  463. //look for dd/mm/yy
  464. Match m = Regex.Match(str, @"(?<=^|[^d])(?'day'd{1,2})s*(?'separator'[\/.])+s*(?'month'd{1,2})s*'separator'+s*(?'year'd{2}|d{4})(?=$|[^d])", RegexOptions.Compiled | RegexOptions.IgnoreCase);
  465. if (m.Success)
  466. {
  467. DateTime date;
  468. if ((default_format ^ DateTimeFormat.USA_DATE) == DateTimeFormat.USA_DATE)
  469. {
  470. if (!convert_to_date(int.Parse(m.Groups["year"].Value), int.Parse(m.Groups["day"].Value), int.Parse(m.Groups["month"].Value), out date))
  471. return false;
  472. }
  473. else
  474. {
  475. if (!convert_to_date(int.Parse(m.Groups["year"].Value), int.Parse(m.Groups["month"].Value), int.Parse(m.Groups["day"].Value), out date))
  476. return false;
  477. }
  478. parsed_date = new ParsedDateTime(m.Index, m.Length, -1, -1, date);
  479. return true;
  480. }
  481.  
  482. //look for [yy]yy-mm-dd
  483. m = Regex.Match(str, @"(?<=^|[^d])(?'year'd{2}|d{4})s*(?'separator'[-])s*(?'month'd{1,2})s*'separator'+s*(?'day'd{1,2})(?=$|[^d])", RegexOptions.Compiled | RegexOptions.IgnoreCase);
  484. if (m.Success)
  485. {
  486. DateTime date;
  487. if (!convert_to_date(int.Parse(m.Groups["year"].Value), int.Parse(m.Groups["month"].Value), int.Parse(m.Groups["day"].Value), out date))
  488. return false;
  489. parsed_date = new ParsedDateTime(m.Index, m.Length, -1, -1, date);
  490. return true;
  491. }
  492.  
  493. //look for month dd yyyy
  494. m = Regex.Match(str, @"(?:^|[^dw])(?'month'Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)[uarychilestmbro]*s+(?'day'd{1,2})(?:-?st|-?th|-?rd|-?nd)?s*,?s*(?'year'd{4})(?=$|[^dw])", RegexOptions.Compiled | RegexOptions.IgnoreCase);
  495. if (!m.Success)
  496. //look for dd month [yy]yy
  497. m = Regex.Match(str, @"(?:^|[^dw:])(?'day'd{1,2})(?:-?sts+|-?ths+|-?rds+|-?nds+|-|s+)(?'month'Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)[uarychilestmbro]*(?:s*,?s*|-)'?(?'year'd{2}|d{4})(?=$|[^dw])", RegexOptions.Compiled | RegexOptions.IgnoreCase);
  498. if (!m.Success)
  499. //look for yyyy month dd
  500. m = Regex.Match(str, @"(?:^|[^dw])(?'year'd{4})s+(?'month'Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)[uarychilestmbro]*s+(?'day'd{1,2})(?:-?st|-?th|-?rd|-?nd)?(?=$|[^dw])", RegexOptions.Compiled | RegexOptions.IgnoreCase);
  501. if (!m.Success)
  502. //look for month dd hh:mm:ss MDT|UTC yyyy
  503. m = Regex.Match(str, @"(?:^|[^dw])(?'month'Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)[uarychilestmbro]*s+(?'day'd{1,2})s+d{2}:d{2}:d{2}s+(?:MDT|UTC)s+(?'year'd{4})(?=$|[^dw])", RegexOptions.Compiled | RegexOptions.IgnoreCase);
  504. if (!m.Success)
  505. //look for month dd [yyyy]
  506. m = Regex.Match(str, @"(?:^|[^dw])(?'month'Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)[uarychilestmbro]*s+(?'day'd{1,2})(?:-?st|-?th|-?rd|-?nd)?(?:s*,?s*(?'year'd{4}))?(?=$|[^dw])", RegexOptions.Compiled | RegexOptions.IgnoreCase);
  507. if (m.Success)
  508. {
  509. int month = -1;
  510. int index_of_date = m.Index;
  511. int length_of_date = m.Length;
  512.  
  513. switch (m.Groups["month"].Value)
  514. {
  515. case "Jan":
  516. case "JAN":
  517. month = 1;
  518. break;
  519. case "Feb":
  520. case "FEB":
  521. month = 2;
  522. break;
  523. case "Mar":
  524. case "MAR":
  525. month = 3;
  526. break;
  527. case "Apr":
  528. case "APR":
  529. month = 4;
  530. break;
  531. case "May":
  532. case "MAY":
  533. month = 5;
  534. break;
  535. case "Jun":
  536. case "JUN":
  537. month = 6;
  538. break;
  539. case "Jul":
  540. month = 7;
  541. break;
  542. case "Aug":
  543. case "AUG":
  544. month = 8;
  545. break;
  546. case "Sep":
  547. case "SEP":
  548. month = 9;
  549. break;
  550. case "Oct":
  551. case "OCT":
  552. month = 10;
  553. break;
  554. case "Nov":
  555. case "NOV":
  556. month = 11;
  557. break;
  558. case "Dec":
  559. case "DEC":
  560. month = 12;
  561. break;
  562. }
  563.  
  564. int year;
  565. if (!string.IsNullOrEmpty(m.Groups["year"].Value))
  566. year = int.Parse(m.Groups["year"].Value);
  567. else
  568. year = DefaultDate.Year;
  569.  
  570. DateTime date;
  571. if (!convert_to_date(year, month, int.Parse(m.Groups["day"].Value), out date))
  572. return false;
  573. parsed_date = new ParsedDateTime(index_of_date, length_of_date, -1, -1, date);
  574. return true;
  575. }
  576.  
  577. return false;
  578. }
  579.  
  580. static bool convert_to_date(int year, int month, int day, out DateTime date)
  581. {
  582. if (year >= 100)
  583. {
  584. if (year < 1000)
  585. {
  586. date = new DateTime(1, 1, 1);
  587. return false;
  588. }
  589. }
  590. else
  591. if (year > 30)
  592. year += 1900;
  593. else
  594. year += 2000;
  595.  
  596. try
  597. {
  598. date = new DateTime(year, month, day);
  599. }
  600. catch
  601. {
  602. date = new DateTime(1, 1, 1);
  603. return false;
  604. }
  605. return true;
  606. }
  607.  
  608. #endregion
  609.  
  610. public static class Extensions
  611. {
  612. public static DateTime ToDateTime(this string dateValue, DateTimeRoutines.DateTimeFormat dateFormat = DateTimeRoutines.DateTimeFormat.USA_DATE)
  613. {
  614. DateTime date;
  615. dateValue.TryParseDateOrTime(dateFormat, out date);
  616. return date;
  617. }
  618. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement