namespace IndexFormula.Finance { using System; using System.ComponentModel; using System.Globalization; public class TimePeriodConverter : TypeConverter { public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType) { return ((sourceType == typeof(string)) || base.CanConvertFrom(context, sourceType)); } public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType) { return ((destinationType == typeof(string)) || base.CanConvertTo(context, destinationType)); } public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) { if (value is string) { string str = value as string; int index = str.IndexOf('-'); if (index >= 0) { string s = str.Substring(0, index) + ":00"; string str3 = str.Substring(index + 1) + ":00"; return new TimePeriod(DateTime.Parse(s), DateTime.Parse(str3)); } return new TimePeriod(0.0, 0.0); } return base.ConvertFrom(context, culture, value); } public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType) { if ((destinationType == typeof(string)) && (value is TimePeriod)) { TimePeriod period = (TimePeriod) value; return period.ToString(); } return base.ConvertTo(context, culture, value, destinationType); } } }