public static DbTime?SafeParseNullableTime(object value, object fieldInfo = null,
                                                   DateTimeKind kind = DateTimeKind.Utc, DbTimeStyle style = DbTimeStyle.Full)
        {
            if (value != null && !(value is DBNull))
            {
                try
                {
                    string timeString;

                    switch (Type.GetTypeCode(value.GetType()))
                    {
                    case TypeCode.Empty:
                        return(null);

                    case TypeCode.String:
                        timeString = (string)value;
                        break;

                    case TypeCode.Int32:
                        return(new DbTime(Convert.ToInt32(value), kind));

                    case TypeCode.Int64:
                        return(new DbTime(Convert.ToInt32(value), kind));

                    case TypeCode.UInt32:
                        return(new DbTime(Convert.ToInt32(value), kind));

                    case TypeCode.UInt64:
                        return(new DbTime(Convert.ToInt32(value), kind));

                    case TypeCode.DateTime:
                        var dt = (DateTime)value;
                        return((DbTime)dt);

                    default:
                        timeString = value.ToString();
                        break;
                    }

                    timeString = timeString.Trim();
                    if (String.IsNullOrEmpty(timeString))
                    {
                        return(null);
                    }

                    if (timeString == "0")
                    {
                        return(DbTime.MinValue);
                    }

                    return(DbDateTime.ParseTime(timeString, kind, style));
                }
                catch (Exception ex)
                {
                    throw FormattedException(ex, value, value.GetType(), typeof(DbDate), fieldInfo);
                }
            }

            return(null);
        }
        public static DbDate?SafeParseNullableDate(object value, object fieldInfo = null)
        {
            if (value != null && !(value is DBNull))
            {
                try
                {
                    string dateString;

                    switch (Type.GetTypeCode(value.GetType()))
                    {
                    case TypeCode.Empty:
                        return(null);

                    case TypeCode.String:
                        dateString = (string)value;
                        break;

                    case TypeCode.Int32:
                        return(new DbDate(Convert.ToInt32(value)));

                    case TypeCode.Int64:
                        return(new DbDate(Convert.ToInt32(value)));

                    case TypeCode.UInt32:
                        return(new DbDate(Convert.ToInt32(value)));

                    case TypeCode.UInt64:
                        return(new DbDate(Convert.ToInt32(value)));

                    case TypeCode.DateTime:
                        var date = (DateTime)value;
                        return((DbDate)date);

                    default:
                        dateString = (string)value;
                        break;
                    }

                    dateString = dateString.Trim();
                    if (String.IsNullOrEmpty(dateString))
                    {
                        return(null);
                    }

                    if (dateString == "0")
                    {
                        return(DbDate.MinValue);
                    }

                    return(DbDateTime.ParseDate(dateString.Trim()));
                }
                catch (Exception ex)
                {
                    throw FormattedException(ex, value, value.GetType(), typeof(DbDate), fieldInfo);
                }
            }

            return(null);
        }
        public static DateTime?SafeParseNullableDateTime(object value, object fieldInfo = null,
                                                         DateTimeKind kind = DateTimeKind.Utc)
        {
            if (value != null && !(value is DBNull))
            {
                try
                {
                    string dateTimeString;

                    switch (Type.GetTypeCode(value.GetType()))
                    {
                    case TypeCode.Empty:
                        return(null);

                    case TypeCode.String:
                        dateTimeString = (string)value;
                        break;

                    case TypeCode.Int32:
                        dateTimeString = Convert.ToInt32(value).ToString(Invariant);
                        break;

                    case TypeCode.UInt32:
                        dateTimeString = Convert.ToUInt32(value).ToString(Invariant);
                        break;

                    case TypeCode.Int64:
                        return(new DateTime(Convert.ToInt64(value), kind));

                    case TypeCode.UInt64:
                        return(new DateTime(Convert.ToInt64(value), kind));

                    case TypeCode.DateTime:
                        //TODO: review if we have to use DateTimeKind to transform - this info is DB provider specific stored or not...
                        return((DateTime)value);

                    default:
                        dateTimeString = value.ToString();
                        break;
                    }

                    dateTimeString = dateTimeString.Trim();
                    if (String.IsNullOrEmpty(dateTimeString))
                    {
                        return(null);
                    }

                    if (dateTimeString == "0")
                    {
                        return(DateTime.MinValue);
                    }

                    return(DbDateTime.ParseDateTime(dateTimeString, kind));
                }
                catch (Exception ex)
                {
                    throw FormattedException(ex, value, value.GetType(), typeof(DateTime), fieldInfo);
                }
            }

            return(null);
        }