Example #1
0
 public static bool Equals(NullableDateTime x, NullableDateTime y)
 {
     if (x.HasValue != y.HasValue) //one is null
     {
         return(false);
     }
     else if (x.HasValue) //therefor y also HasValue
     {
         return(x.Value == y.Value);
     }
     else //both are null
     {
         return(true);
     }
 }
Example #2
0
        public int CompareTo(object obj)
        {
            if (obj is NullableDateTime) //chack and unbox
            {
                NullableDateTime value = (NullableDateTime)obj;

                if (value.HasValue == this.HasValue) //both null or not null
                {
                    if (this.HasValue)               //this has a value, so they both do
                    {
                        return(Value.CompareTo(value.Value));
                    }
                    else
                    {
                        return(0); //both null, so they are equal;
                    }
                }
                else //one is null
                {
                    if (HasValue) //he have a value, so we are greater.
                    {
                        return(1);
                    }
                    else
                    {
                        return(-1);
                    }
                }
            }
            else if (obj is DateTime)
            {
                DateTime value = (DateTime)obj;

                if (HasValue) //not null, so compare the real values.
                {
                    return(Value.CompareTo(value));
                }
                else
                {
                    return(-1); //this is null, so less that the real value;
                }
            }

            throw new ArgumentException("NullableDateTime can only compare to another NullableDateTime or a System.DateTime");
        }
Example #3
0
        public static NullableDateTime Parse(string s)
        {
            NullableDateTime time;

            if ((s == null) || (s.Trim().Length == 0))
            {
                return(new NullableDateTime());
            }
            try
            {
                time = new NullableDateTime(DateTime.Parse(s));
            }
            catch (Exception exception)
            {
                throw new FormatException("Error parsing '" + s + "' to NullableDateTime.", exception);
            }
            return(time);
        }
 public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
 {
     if ((destinationType == typeof(InstanceDescriptor)) && (value is NullableDateTime))
     {
         NullableDateTime time        = (NullableDateTime)value;
         Type[]           types       = new Type[] { typeof(DateTime) };
         ConstructorInfo  constructor = typeof(NullableDateTime).GetConstructor(types);
         if (constructor != null)
         {
             return(new InstanceDescriptor(constructor, new object[] { time.Value }));
         }
     }
     else if (destinationType == typeof(DateTime))
     {
         NullableDateTime time2 = (NullableDateTime)value;
         if (time2.HasValue)
         {
             return(time2.Value);
         }
         return(DBNull.Value);
     }
     return(base.ConvertTo(context, culture, value, destinationType));
 }
Example #5
0
        public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value,
                                         Type destinationType)
        {
            if (destinationType == typeof(InstanceDescriptor) && value is NullableDateTime)
            {
                NullableDateTime nullable = (NullableDateTime)value;

                Type[] constructorArgTypes = new Type[1] {
                    typeof(DateTime)
                };
                ConstructorInfo constructor = typeof(NullableDateTime).GetConstructor(constructorArgTypes);

                if (constructor != null)
                {
                    object[] constructorArgValues = new object[1] {
                        nullable.Value
                    };
                    return(new InstanceDescriptor(constructor, constructorArgValues));
                }
            }
            else if (destinationType == typeof(DateTime))
            {
                NullableDateTime ndt = (NullableDateTime)value;

                if (ndt.HasValue)
                {
                    return(ndt.Value);
                }
                else
                {
                    return(DBNull.Value);
                }
            }

            return(base.ConvertTo(context, culture, value, destinationType));
        }
Example #6
0
 public bool Equals(NullableDateTime x)
 {
     return(Equals(this, x));
 }
Example #7
0
 static NullableDateTime()
 {
     Default = new NullableDateTime();
 }