Пример #1
0
 /// <summary>
 /// Simply calls a "Time Elapsed" method and returns a default value on failure.
 /// </summary>
 /// <param name="timeElapsedMethod">The method to call.</param>
 /// <param name="defaultValue">The default value on failure.</param>
 /// <param name="dateValue1">The first date of the difference calculation.</param>
 /// <param name="dateValue2">The second date of the difference calculation.</param>
 /// <param name="absolute">Whether or not to return an absolute value.</param>
 /// <returns>The requested time laps.</returns>
 public static object TimeElapsedOrDefault(TimeElapsedMethod timeElapsedMethod, object defaultValue, object dateValue1, object dateValue2, bool absolute)
 {
     try
     {
         return(timeElapsedMethod(dateValue1, dateValue2, absolute));
     }
     catch
     {
         return(defaultValue);
     }
 }
Пример #2
0
        // --------------------------------------------------------------------------------------------------------------------

        /// <summary>
        /// Returns the difference between the two values, or "default" on error.
        /// An effort is made to convert the given objects to numerical representations, then a difference is determined.
        /// <para>Note: The difference is inclusive. (example: difference between Jan 1 and Jan 2 is 2 days).</para>
        /// </summary>
        /// <param name="val1">First value.</param>
        /// <param name="val2">Second value.</param>
        /// <param name="defaultValue">The default value.</param>
        /// <param name="absolute">For values that can be negative, this insures a positive difference.</param>
        /// <returns></returns>
        public static object DiffOrDefault(object val1, Type val1Type, object val2, Type val2Type, object defaultValue, bool absolute, bool inclusive, TimeElapsedMethod timeElapsedMethod)
        {
            if (val1Type == null)
            {
                val1Type = val1.GetType();
            }
            if (val2Type == null)
            {
                val2Type = val2.GetType();
            }
            try
            {
                if (IsFloat(val1Type))
                {
                    double v1   = Convert.ToDouble(val1);
                    object diff = null;
                    if (IsFloat(val2Type))
                    {
                        diff = Convert.ToDouble(val2) - v1;
                    }
                    else if (IsNumeric(val2Type))
                    {
                        diff = Convert.ToDouble(val2) - v1;
                    }
                    else if (IsDateTime(val2Type))
                    {
                        diff = Convert.ToDouble(Convert.ToDateTime(val2).Ticks) - v1;
                    }
                    if (absolute && diff != null)
                    {
                        diff = System.Math.Abs((double)diff);
                    }
                    return((double)diff + ((inclusive) ? (System.Math.Sign((double)diff)) : (0.0)));
                }
                else if (IsNumeric(val1Type))
                {
                    Int64  v1   = Convert.ToInt64(val1);
                    object diff = null;
                    if (IsFloat(val2Type))
                    {
                        diff = Convert.ToInt64(val2) - v1;
                    }
                    else if (IsNumeric(val2Type))
                    {
                        diff = Convert.ToInt64(val2) - v1;
                    }
                    else if (IsDateTime(val2Type))
                    {
                        diff = Convert.ToInt64(Convert.ToDateTime(val2).Ticks) - v1;
                    }
                    if (absolute && diff != null)
                    {
                        diff = System.Math.Abs((Int64)diff);
                    }
                    return((Int64)diff + ((inclusive) ? (System.Math.Sign((Int64)diff)) : (0)));
                }
                else if (IsDateTime(val1Type))
                {
                    DateTime v1   = Convert.ToDateTime(val1);
                    object   diff = null;
                    if (IsFloat(val2Type))
                    {
                        diff = Convert.ToInt64(val2) - v1.Ticks;
                    }
                    else if (IsNumeric(val2Type))
                    {
                        diff = Convert.ToInt64(val2) - v1.Ticks;
                    }
                    else if (IsDateTime(val2Type))
                    {
                        if (timeElapsedMethod != null)
                        {
                            diff = TimeElapsedOrDefault(timeElapsedMethod, defaultValue, v1, val2, absolute);
                            return((double)diff + ((inclusive) ? (System.Math.Sign((double)diff)) : (0.0)));
                        }
                        else
                        {
                            diff = Convert.ToInt64(Convert.ToDateTime(val2).Ticks) - v1.Ticks;
                        }
                    }
                    if (absolute && diff != null)
                    {
                        diff = System.Math.Abs((Int64)diff);
                    }
                    return((Int64)diff + ((inclusive) ? (System.Math.Sign((Int64)diff)) : (0)));
                }
            }
            catch
            {
                return(defaultValue);
            }
            return(null);
        }