using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace GpsCardGatewayPosition.Common
{
public class SafeType
{
///
/// Safes the int.
///
/// The o.
///
public static int SafeInt(object o)
{
var returnValue = 0;
try
{
returnValue = Convert.ToInt32(o);
}
catch
{
returnValue = 0;
}
return returnValue;
}
///
/// Safes the int.
///
/// The o.
///
public static long SafeInt64(object o)
{
var returnValue = 0L;
try
{
returnValue = Convert.ToInt64(o);
}
catch
{
returnValue = 0;
}
return returnValue;
}
///
/// Safes the string.
///
/// The o.
///
public static string SafeString(object o)
{
var returnValue = "";
try
{
returnValue = Convert.ToString(o);
}
catch
{
returnValue = "";
}
return returnValue;
}
///
/// Safes the bool.
///
/// The o.
///
public static bool SafeBool(object o)
{
var returnValue = false;
try
{
returnValue = Convert.ToBoolean(o);
}
catch
{
returnValue = false;
}
return returnValue;
}
public static double SafeDouble(object o)
{
double returnValue = 0;
try
{
returnValue = Convert.ToDouble(o);
}
catch
{
returnValue = 0;
}
return returnValue;
}
///
/// Safes the decimal.
///
/// The o.
///
public static decimal SafeDecimal(object o)
{
var returnValue = 0.0M;
try
{
returnValue = Convert.ToDecimal(o);
}
catch
{
returnValue = 0.0M;
}
return returnValue;
}
///
/// Safes the time.
///
/// The o.
///
public static DateTime SafeTime(object o)
{
var returnValue = DateTime.Parse("1900-01-01");
try
{
returnValue = Convert.ToDateTime(o);
}
catch
{
returnValue = DateTime.Parse("1900-01-01");
}
return returnValue;
}
///
/// OBD时间转换
///
///
///
public static DateTime OBDSafeTime(object o)
{
var returnValue = DateTime.Parse("1900-01-01");
try
{
returnValue = Convert.ToDateTime(o);
}
catch
{
returnValue = DateTime.UtcNow;
}
return returnValue;
}
///
/// 是否为数字
///
///
///
public static bool IsNumber(string str)
{
int n;
if (int.TryParse(str, out n))
{
return true;
}
else
{
return false;
}
}
}
}