TimePeriodConverter.cs 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. namespace IndexFormula.Finance
  2. {
  3. using System;
  4. using System.ComponentModel;
  5. using System.Globalization;
  6. public class TimePeriodConverter : TypeConverter
  7. {
  8. public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
  9. {
  10. return ((sourceType == typeof(string)) || base.CanConvertFrom(context, sourceType));
  11. }
  12. public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
  13. {
  14. return ((destinationType == typeof(string)) || base.CanConvertTo(context, destinationType));
  15. }
  16. public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
  17. {
  18. if (value is string)
  19. {
  20. string str = value as string;
  21. int index = str.IndexOf('-');
  22. if (index >= 0)
  23. {
  24. string s = str.Substring(0, index) + ":00";
  25. string str3 = str.Substring(index + 1) + ":00";
  26. return new TimePeriod(DateTime.Parse(s), DateTime.Parse(str3));
  27. }
  28. return new TimePeriod(0.0, 0.0);
  29. }
  30. return base.ConvertFrom(context, culture, value);
  31. }
  32. public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
  33. {
  34. if ((destinationType == typeof(string)) && (value is TimePeriod))
  35. {
  36. TimePeriod period = (TimePeriod) value;
  37. return period.ToString();
  38. }
  39. return base.ConvertTo(context, culture, value, destinationType);
  40. }
  41. }
  42. }