/// <summary> /// 转换为ISO8601规范的时间段字符串 默认格式YYYYMMDD /// </summary> /// <returns>返回ISO8601规范的时间段字符串</returns> public override string ToString() { Int32 no = (Int32)Format; string signTime = string.Empty; string signYear = string.Empty; string signMonth = string.Empty; string signDay = string.Empty; string signHour = string.Empty; string signMinute = string.Empty; string signSecond = string.Empty; int intervalYear = ISO8601TimeIntervalHelper.GetIntervalYear(IntervalValue); int intervalMonth = ISO8601TimeIntervalHelper.GetIntervalMonth(IntervalValue); int intervalDay = ISO8601TimeIntervalHelper.GetIntervalDay(IntervalValue); bool allNull = true; if (intervalYear != 0) { signYear = "Y"; } if (intervalMonth != 0) { signMonth = "M"; } if (intervalDay != 0) { signDay = "D"; } if (IntervalValue.Hours != 0) { signHour = "H"; allNull = false; } if (IntervalValue.Minutes != 0) { signMinute = "M"; allNull = false; } if (IntervalValue.Seconds != 0) { signSecond = "S"; allNull = false; } if (false == allNull) { signTime = "T"; } switch (Format) { case ISO8601TimeIntervalFormats.BasicDateByStartAndEndInSameYear: return(String.Format(MyRegularExpressions[no][2], StartValue.Year, StartValue.Month, StartValue.Day, EndValue.Month, EndValue.Day)); case ISO8601TimeIntervalFormats.ExtendedDateByStartAndEndInSameYear: return(String.Format(MyRegularExpressions[no][2], StartValue.Year, StartValue.Month, StartValue.Day, EndValue.Month, EndValue.Day)); case ISO8601TimeIntervalFormats.BasicDateTimeByStartAndEnd: return(String.Format(MyRegularExpressions[no][2], StartValue.Year, StartValue.Month, StartValue.Day, StartValue.Hour, StartValue.Minute, StartValue.Second, EndValue.Year, EndValue.Month, EndValue.Day, EndValue.Hour, EndValue.Minute, EndValue.Second)); case ISO8601TimeIntervalFormats.ExtendedDateTimeByStartAndEnd: return(String.Format(MyRegularExpressions[no][2], StartValue.Year, StartValue.Month, StartValue.Day, StartValue.Hour, StartValue.Minute, StartValue.Second, EndValue.Year, EndValue.Month, EndValue.Day, EndValue.Hour, EndValue.Minute, EndValue.Second)); case ISO8601TimeIntervalFormats.SymbolicDateTimeByDuration: return(String.Format(MyRegularExpressions[no][2], ISO8601TimeIntervalHelper.ZeroToNull(intervalYear), signYear, ISO8601TimeIntervalHelper.ZeroToNull(intervalMonth), signMonth, ISO8601TimeIntervalHelper.ZeroToNull(intervalDay), signDay, signTime, ISO8601TimeIntervalHelper.ZeroToNull(IntervalValue.Hours), signHour, ISO8601TimeIntervalHelper.ZeroToNull(IntervalValue.Minutes), signMinute, ISO8601TimeIntervalHelper.ZeroToNull(IntervalValue.Seconds), signSecond)); case ISO8601TimeIntervalFormats.BasicDateTimeByDuration: return(String.Format(MyRegularExpressions[no][2], intervalYear, intervalMonth, intervalDay, IntervalValue.Hours, IntervalValue.Minutes, IntervalValue.Seconds)); case ISO8601TimeIntervalFormats.ExtendedDateTimeByDuration: return(String.Format(MyRegularExpressions[no][2], intervalYear, intervalMonth, intervalDay, IntervalValue.Hours, IntervalValue.Minutes, IntervalValue.Seconds)); case ISO8601TimeIntervalFormats.BasicDateTimeByStartAndDuration: return(String.Format(MyRegularExpressions[no][2], StartValue.Year, StartValue.Month, StartValue.Day, StartValue.Hour, StartValue.Minute, StartValue.Second, ISO8601TimeIntervalHelper.ZeroToNull(intervalYear), signYear, ISO8601TimeIntervalHelper.ZeroToNull(intervalMonth), signMonth, ISO8601TimeIntervalHelper.ZeroToNull(intervalDay), signDay, signTime, ISO8601TimeIntervalHelper.ZeroToNull(IntervalValue.Hours), signHour, ISO8601TimeIntervalHelper.ZeroToNull(IntervalValue.Minutes), signMinute, ISO8601TimeIntervalHelper.ZeroToNull(IntervalValue.Seconds), signSecond)); case ISO8601TimeIntervalFormats.ExtendedDateTimeByStartAndDuration: return(String.Format(MyRegularExpressions[no][2], StartValue.Year, StartValue.Month, StartValue.Day, StartValue.Hour, StartValue.Minute, StartValue.Second, ISO8601TimeIntervalHelper.ZeroToNull(intervalYear), signYear, ISO8601TimeIntervalHelper.ZeroToNull(intervalMonth), signMonth, ISO8601TimeIntervalHelper.ZeroToNull(intervalDay), signDay, signTime, ISO8601TimeIntervalHelper.ZeroToNull(IntervalValue.Hours), signHour, ISO8601TimeIntervalHelper.ZeroToNull(IntervalValue.Minutes), signMinute, ISO8601TimeIntervalHelper.ZeroToNull(IntervalValue.Seconds), signSecond)); case ISO8601TimeIntervalFormats.BasicDateTimeByDurationAndEnd: return(String.Format(MyRegularExpressions[no][2], ISO8601TimeIntervalHelper.ZeroToNull(intervalYear), signYear, ISO8601TimeIntervalHelper.ZeroToNull(intervalMonth), signMonth, ISO8601TimeIntervalHelper.ZeroToNull(intervalDay), signDay, signTime, ISO8601TimeIntervalHelper.ZeroToNull(IntervalValue.Hours), signHour, ISO8601TimeIntervalHelper.ZeroToNull(IntervalValue.Minutes), signMinute, ISO8601TimeIntervalHelper.ZeroToNull(IntervalValue.Seconds), signSecond, EndValue.Year, EndValue.Month, EndValue.Day, EndValue.Hour, EndValue.Minute, EndValue.Second)); case ISO8601TimeIntervalFormats.ExtendedDateTimeByDurationAndEnd: return(String.Format(MyRegularExpressions[no][2], ISO8601TimeIntervalHelper.ZeroToNull(intervalYear), signYear, ISO8601TimeIntervalHelper.ZeroToNull(intervalMonth), signMonth, ISO8601TimeIntervalHelper.ZeroToNull(intervalDay), signDay, signTime, ISO8601TimeIntervalHelper.ZeroToNull(IntervalValue.Hours), signHour, ISO8601TimeIntervalHelper.ZeroToNull(IntervalValue.Minutes), signMinute, ISO8601TimeIntervalHelper.ZeroToNull(IntervalValue.Seconds), signSecond, EndValue.Year, EndValue.Month, EndValue.Day, EndValue.Hour, EndValue.Minute, EndValue.Second)); case ISO8601TimeIntervalFormats.ExtendedDateByDuration: return(String.Format(MyRegularExpressions[no][2], intervalYear, intervalMonth)); case ISO8601TimeIntervalFormats.SymbolicHourByDuration: string strHour = ""; int hour = IntervalValue.Hours; if (IntervalValue.Days > 0) { hour = IntervalValue.Days * 24 + IntervalValue.Hours; } if (hour > 0) { strHour = String.Format("{0}", hour); } return(String.Format(MyRegularExpressions[no][2], strHour)); default: return(""); } }
/// <summary> /// 生成由正则表达式解析的数据 /// </summary> /// <param name="m">正则表达式解析类</param> /// <param name="format">ISO8601TimeInterval格式</param> /// <param name="start">所需要时间段的开始时间</param> /// <param name="end">所需要时间段的结束时间</param> /// <returns>时间段</returns> internal static TimeSpan Match2DateTime(Match m, ISO8601TimeIntervalFormats format, out DateTime start, out DateTime end) { TimeSpan interval = default(TimeSpan); switch (format) { case ISO8601TimeIntervalFormats.BasicDateByStartAndEndInSameYear: case ISO8601TimeIntervalFormats.ExtendedDateByStartAndEndInSameYear: start = new DateTime(Convert.ToInt32(m.Groups["year"].Value), Convert.ToInt32(m.Groups["startmonth"].Value), Convert.ToInt32(m.Groups["startday"].Value)); end = new DateTime(Convert.ToInt32(m.Groups["year"].Value), Convert.ToInt32(m.Groups["endmonth"].Value), Convert.ToInt32(m.Groups["endday"].Value)); interval = ISO8601TimeIntervalHelper.GetInterval(start, end); break; case ISO8601TimeIntervalFormats.BasicDateTimeByStartAndEnd: case ISO8601TimeIntervalFormats.ExtendedDateTimeByStartAndEnd: start = new DateTime ( Convert.ToInt32(m.Groups["startyear"].Value), Convert.ToInt32(m.Groups["startmonth"].Value), Convert.ToInt32(m.Groups["startday"].Value), Convert.ToInt32(m.Groups["starthour"].Value), Convert.ToInt32(m.Groups["startminute"].Value), Convert.ToInt32(m.Groups["startsecond"].Value) ); end = new DateTime ( Convert.ToInt32(m.Groups["endyear"].Value), Convert.ToInt32(m.Groups["endmonth"].Value), Convert.ToInt32(m.Groups["endday"].Value), Convert.ToInt32(m.Groups["endhour"].Value), Convert.ToInt32(m.Groups["endminute"].Value), Convert.ToInt32(m.Groups["endsecond"].Value) ); interval = ISO8601TimeIntervalHelper.GetInterval(start, end); break; case ISO8601TimeIntervalFormats.SymbolicDateTimeByDuration: case ISO8601TimeIntervalFormats.BasicDateTimeByDuration: case ISO8601TimeIntervalFormats.ExtendedDateTimeByDuration: interval = ISO8601TimeIntervalHelper.GetInterval ( m.Groups["year"].Value, m.Groups["month"].Value, m.Groups["day"].Value, m.Groups["hour"].Value, m.Groups["minute"].Value, m.Groups["second"].Value ); start = default(DateTime); end = default(DateTime); break; case ISO8601TimeIntervalFormats.BasicDateTimeByStartAndDuration: case ISO8601TimeIntervalFormats.ExtendedDateTimeByStartAndDuration: start = new DateTime ( Convert.ToInt32(m.Groups["startyear"].Value), Convert.ToInt32(m.Groups["startmonth"].Value), Convert.ToInt32(m.Groups["startday"].Value), Convert.ToInt32(m.Groups["starthour"].Value), Convert.ToInt32(m.Groups["startminute"].Value), Convert.ToInt32(m.Groups["startsecond"].Value) ); interval = ISO8601TimeIntervalHelper.GetInterval ( m.Groups["year"].Value, m.Groups["month"].Value, m.Groups["day"].Value, m.Groups["hour"].Value, m.Groups["minute"].Value, m.Groups["second"].Value ); end = ISO8601TimeIntervalHelper.GetEndDate(start, interval); break; case ISO8601TimeIntervalFormats.BasicDateTimeByDurationAndEnd: case ISO8601TimeIntervalFormats.ExtendedDateTimeByDurationAndEnd: interval = ISO8601TimeIntervalHelper.GetInterval ( m.Groups["year"].Value, m.Groups["month"].Value, m.Groups["day"].Value, m.Groups["hour"].Value, m.Groups["minute"].Value, m.Groups["second"].Value ); end = new DateTime ( Convert.ToInt32(m.Groups["endyear"].Value), Convert.ToInt32(m.Groups["endmonth"].Value), Convert.ToInt32(m.Groups["endday"].Value), Convert.ToInt32(m.Groups["endhour"].Value), Convert.ToInt32(m.Groups["endminute"].Value), Convert.ToInt32(m.Groups["endsecond"].Value) ); start = ISO8601TimeIntervalHelper.GetStartDate(interval, end); break; case ISO8601TimeIntervalFormats.ExtendedDateByDuration: interval = ISO8601TimeIntervalHelper.GetInterval ( m.Groups["year"].Value, m.Groups["month"].Value, m.Groups["day"].Value ); start = default(DateTime); end = default(DateTime); break; case ISO8601TimeIntervalFormats.SymbolicHourByDuration: interval = ISO8601TimeIntervalHelper.GetNewInterval ( m.Groups["hour"].Value, m.Groups["minute"].Value, m.Groups["second"].Value ); start = default(DateTime); end = default(DateTime); break; default: start = default(DateTime); end = default(DateTime); break; } return(interval); }