WikiCrawler/Utility/DateUtility.cs
/// <summary>
/// Parses the specified semi-natural date string into a Commons marked-up date.
/// </summary>
Parses the specified semi-natural date string into a Commons marked-up date.
Parses the specified semi - natural date string into a Commons marked - up date .
public static string ParseDate(string date, out DateParseMetadata parseMetadata, bool possibleEmptyDates = false)
	{
		DateTime parseResult;
		if (DateTime.TryParse(date, out parseResult))
		{
			if (possibleEmptyDates
				&& parseResult.Month == 1 && parseResult.Day == 1 && parseResult.Hour == 0 && parseResult.Minute == 0 && parseResult.Second == 0)
			{
				parseMetadata = DateParseMetadata.Unknown;
				return date;
			}
			parseMetadata = new DateParseMetadata(parseResult.Year);
			return string.Format("{0:0000}-{1:00}-{2:00}", parseResult.Year, parseResult.Month, parseResult.Day);
		}
		date = date.Trim('.');
		if (string.IsNullOrEmpty(date))
		{
			parseMetadata = DateParseMetadata.Unknown;
			return "{{unknown|date}}";
		}
		else if (date.EndsWith("~") || date.EndsWith("?"))
		{
			string dateStr = ParseDate(date.Substring(0, date.Length - 1).Trim(), out parseMetadata);
			parseMetadata.PreciseYear = 0;
			return "{{other date|ca|" + dateStr + "}}";
		}
		else if (date.StartsWith("c.", StringComparison.InvariantCultureIgnoreCase))
		{
			int rml = "c.".Length;
			string dateStr = ParseDate(date.Substring(rml, date.Length - rml).Trim(), out parseMetadata);
			parseMetadata.PreciseYear = 0;
			return "{{other date|ca|" + dateStr + "}}";
		}
		else if (date.StartsWith("ca.", StringComparison.InvariantCultureIgnoreCase))
		{
			int rml = "ca.".Length;
			string dateStr = ParseDate(date.Substring(rml, date.Length - rml).Trim(), out parseMetadata);
			parseMetadata.PreciseYear = 0;
			return "{{other date|ca|" + dateStr + "}}";
		}
		else if (date.StartsWith("circa ", StringComparison.InvariantCultureIgnoreCase))
		{
			int rml = "circa ".Length;
			string yearStr = date.Substring(rml, date.Length - rml).Trim();
			parseMetadata.PreciseYear = 0;
			return "{{other date|ca|" + ParseDate(yearStr, out parseMetadata) + "}}";
		}
		else if (date.StartsWith("before"))
		{
			int rml = "before".Length;
			string yearStr = date.Substring(rml, date.Length - rml).Trim();
			int yearInt;
			parseMetadata = int.TryParse(yearStr, out yearInt)
				? new DateParseMetadata(0, 0, yearInt - 1)
				: DateParseMetadata.Unknown;
			return "{{other date|before|" + yearStr + "}}";
		}
		else if (date.StartsWith("voor/before"))
		{
			int rml = "voor/before".Length;
			string yearStr = date.Substring(rml, date.Length - rml).Trim();
			int yearInt;
			parseMetadata = int.TryParse(yearStr, out yearInt)
				? new DateParseMetadata(0, 0, yearInt - 1)
				: DateParseMetadata.Unknown;
			return "{{other date|before|" + yearStr + "}}";
		}
		else if (date.StartsWith("between "))
		{
			string[] components = date.Substring("between ".Length).Split(new string[] { " and ", "-" }, StringSplitOptions.None);
			if (components.Length == 2)
			{
				DateParseMetadata parseA, parseB;
				string a = ParseDate(components[0], out parseA);
				string b = ParseDate(components[1], out parseB);
				parseMetadata = DateParseMetadata.Combine(parseA, parseB);
				return "{{other date|between|" + a + "|" + b + "}}";
			}
			else
			{
				parseMetadata = DateParseMetadata.Unknown;
				return date;
			}
		}
		else
		{
			string[] dashsplit = date.Split(s_dateSplit);
			if (dashsplit.Length == 2 && dashsplit[0].Length == 4
				&& dashsplit[1].Length == 4)
			{
				DateParseMetadata parseA, parseB;
				string a = ParseDate(dashsplit[0], out parseA);
				string b = ParseDate(dashsplit[1], out parseB);
				parseMetadata = DateParseMetadata.Combine(parseA, parseB);
				return "{{other date|between|" + a + "|" + b + "}}";
			}
			else
			{
				return ParseSingleDate(date, out parseMetadata);
			}
		}
	}
public static string ParseDate ( string date , out DateParseMetadata parseMetadata , bool possibleEmptyDates = false ) { DateTime parseResult ; if ( DateTime . TryParse ( date , out parseResult ) ) { if ( possibleEmptyDates && parseResult . Month == 1 && parseResult . Day == 1 && parseResult . Hour == 0 && parseResult . Minute == 0 && parseResult . Second == 0 ) { parseMetadata = DateParseMetadata . Unknown ; return date ; } parseMetadata = new DateParseMetadata ( parseResult . Year ) ; return string . Format ( " " , parseResult . Year , parseResult . Month , parseResult . Day ) ; } date = date . Trim ( ' ' ) ; if ( string . IsNullOrEmpty ( date ) ) { parseMetadata = DateParseMetadata . Unknown ; return " " ; } else if ( date . EndsWith ( " " ) || date . EndsWith ( " " ) ) { string dateStr = ParseDate ( date . Substring ( 0 , date . Length - 1 ) . Trim ( ) , out parseMetadata ) ; parseMetadata . PreciseYear = 0 ; return " " + dateStr + " " ; } else if ( date . StartsWith ( " " , StringComparison . InvariantCultureIgnoreCase ) ) { int rml = " " . Length ; string dateStr = ParseDate ( date . Substring ( rml , date . Length - rml ) . Trim ( ) , out parseMetadata ) ; parseMetadata . PreciseYear = 0 ; return " " + dateStr + " " ; } else if ( date . StartsWith ( " " , StringComparison . InvariantCultureIgnoreCase ) ) { int rml = " " . Length ; string dateStr = ParseDate ( date . Substring ( rml , date . Length - rml ) . Trim ( ) , out parseMetadata ) ; parseMetadata . PreciseYear = 0 ; return " " + dateStr + " " ; } else if ( date . StartsWith ( " " , StringComparison . InvariantCultureIgnoreCase ) ) { int rml = " " . Length ; string yearStr = date . Substring ( rml , date . Length - rml ) . Trim ( ) ; parseMetadata . PreciseYear = 0 ; return " " + ParseDate ( yearStr , out parseMetadata ) + " " ; } else if ( date . StartsWith ( " " ) ) { int rml = " " . Length ; string yearStr = date . Substring ( rml , date . Length - rml ) . Trim ( ) ; int yearInt ; parseMetadata = int . TryParse ( yearStr , out yearInt ) ? new DateParseMetadata ( 0 , 0 , yearInt - 1 ) : DateParseMetadata . Unknown ; return " " + yearStr + " " ; } else if ( date . StartsWith ( " " ) ) { int rml = " " . Length ; string yearStr = date . Substring ( rml , date . Length - rml ) . Trim ( ) ; int yearInt ; parseMetadata = int . TryParse ( yearStr , out yearInt ) ? new DateParseMetadata ( 0 , 0 , yearInt - 1 ) : DateParseMetadata . Unknown ; return " " + yearStr + " " ; } else if ( date . StartsWith ( " " ) ) { string [ ] components = date . Substring ( " " . Length ) . Split ( new string [ ] { " " , " " } , StringSplitOptions . None ) ; if ( components . Length == 2 ) { DateParseMetadata parseA , parseB ; string a = ParseDate ( components [ 0 ] , out parseA ) ; string b = ParseDate ( components [ 1 ] , out parseB ) ; parseMetadata = DateParseMetadata . Combine ( parseA , parseB ) ; return " " + a + " " + b + " " ; } else { parseMetadata = DateParseMetadata . Unknown ; return date ; } } else { string [ ] dashsplit = date . Split ( s_dateSplit ) ; if ( dashsplit . Length == 2 && dashsplit [ 0 ] . Length == 4 && dashsplit [ 1 ] . Length == 4 ) { DateParseMetadata parseA , parseB ; string a = ParseDate ( dashsplit [ 0 ] , out parseA ) ; string b = ParseDate ( dashsplit [ 1 ] , out parseB ) ; parseMetadata = DateParseMetadata . Combine ( parseA , parseB ) ; return " " + a + " " + b + " " ; } else { return ParseSingleDate ( date , out parseMetadata ) ; } } }
Parses the specified semi-natural date string into a Commons marked-up date.
Parses the specified semi - natural date string into a Commons marked - up date .
// don't trust it
date
string
DateParseMetadata
out
possibleEmptyDates
bool