WikiCrawler/Utility/DateUtility.cs
/// <summary>
/// Parses a simple natural expression like "March 3 1992" into YYYY-MM-DD.
/// </summary>
Parses a simple natural expression like "March 3 1992" into YYYY-MM-DD.
Parses a simple natural expression like " March 3 1992 " into YYYY - MM - DD .
public static string ParseSingleDate(string date, out DateParseMetadata parseMetadata)
	{
		DateTime apiParse;
		if (DateTime.TryParseExact(date, "M/d/yyyy", CultureInfo.InvariantCulture.DateTimeFormat, DateTimeStyles.AllowWhiteSpaces, out apiParse))
		{
			parseMetadata = new DateParseMetadata(apiParse.Year);
			return apiParse.Year.ToString() + "-" + apiParse.Month.ToString() + "-" + apiParse.Day.ToString();
		}
		if (date.Length == 8)
		{
			string year = date.Substring(0, 4);
			string month = date.Substring(4, 2);
			string day = date.Substring(6, 2);
			int iyear, imonth, iday;
			if (int.TryParse(year, out iyear) && int.TryParse(month, out imonth) && int.TryParse(day, out iday))
			{
				parseMetadata = new DateParseMetadata(iyear);
				return iyear.ToString() + "-" + imonth.ToString("00") + "-" + iday.ToString("00");
			}
		}
		string[] dateSplit = date.Split(new char[] { ' ', ',', '.' }, StringSplitOptions.RemoveEmptyEntries);
		if (dateSplit.Length == 3)
		{
			int year, month, day;
			if (TryParseMonth(dateSplit[0], out month)
				&& int.TryParse(dateSplit[1], out day)
				&& int.TryParse(dateSplit[2], out year))
			{
				parseMetadata = new DateParseMetadata(year);
				return year.ToString() + "-" + month.ToString("00") + "-" + day.ToString("00");
			}
		}
		else if (dateSplit.Length == 2)
		{
			int year, month;
			if (TryParseMonth(dateSplit[0], out month)
				&& int.TryParse(dateSplit[1], out year))
			{
				parseMetadata = new DateParseMetadata(year);
				return year.ToString() + "-" + month.ToString("00");
			}
		}
		else if (dateSplit.Length == 1)
		{
			int year;
			if (int.TryParse(dateSplit[0], out year))
			{
				parseMetadata = new DateParseMetadata(year);
				return year.ToString();
			}
		}
		parseMetadata = DateParseMetadata.Unknown;
		return date;
	}
public static string ParseSingleDate ( string date , out DateParseMetadata parseMetadata ) { DateTime apiParse ; if ( DateTime . TryParseExact ( date , " " , CultureInfo . InvariantCulture . DateTimeFormat , DateTimeStyles . AllowWhiteSpaces , out apiParse ) ) { parseMetadata = new DateParseMetadata ( apiParse . Year ) ; return apiParse . Year . ToString ( ) + " " + apiParse . Month . ToString ( ) + " " + apiParse . Day . ToString ( ) ; } if ( date . Length == 8 ) { string year = date . Substring ( 0 , 4 ) ; string month = date . Substring ( 4 , 2 ) ; string day = date . Substring ( 6 , 2 ) ; int iyear , imonth , iday ; if ( int . TryParse ( year , out iyear ) && int . TryParse ( month , out imonth ) && int . TryParse ( day , out iday ) ) { parseMetadata = new DateParseMetadata ( iyear ) ; return iyear . ToString ( ) + " " + imonth . ToString ( " " ) + " " + iday . ToString ( " " ) ; } } string [ ] dateSplit = date . Split ( new char [ ] { ' ' , ' ' , ' ' } , StringSplitOptions . RemoveEmptyEntries ) ; if ( dateSplit . Length == 3 ) { int year , month , day ; if ( TryParseMonth ( dateSplit [ 0 ] , out month ) && int . TryParse ( dateSplit [ 1 ] , out day ) && int . TryParse ( dateSplit [ 2 ] , out year ) ) { parseMetadata = new DateParseMetadata ( year ) ; return year . ToString ( ) + " " + month . ToString ( " " ) + " " + day . ToString ( " " ) ; } } else if ( dateSplit . Length == 2 ) { int year , month ; if ( TryParseMonth ( dateSplit [ 0 ] , out month ) && int . TryParse ( dateSplit [ 1 ] , out year ) ) { parseMetadata = new DateParseMetadata ( year ) ; return year . ToString ( ) + " " + month . ToString ( " " ) ; } } else if ( dateSplit . Length == 1 ) { int year ; if ( int . TryParse ( dateSplit [ 0 ] , out year ) ) { parseMetadata = new DateParseMetadata ( year ) ; return year . ToString ( ) ; } } parseMetadata = DateParseMetadata . Unknown ; return date ; }
Parses a simple natural expression like "March 3 1992" into YYYY-MM-DD.
Parses a simple natural expression like " March 3 1992 " into YYYY - MM - DD .
//M/d/yyyy // yyyyMMdd // natural strings
date
string
DateParseMetadata
out